• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.cloud.kms.inventory.v1;
18 
19 import static com.google.cloud.kms.inventory.v1.KeyTrackingServiceClient.SearchProtectedResourcesPagedResponse;
20 
21 import com.google.api.gax.core.NoCredentialsProvider;
22 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
23 import com.google.api.gax.httpjson.testing.MockHttpService;
24 import com.google.api.gax.rpc.ApiClientHeaderProvider;
25 import com.google.api.gax.rpc.ApiException;
26 import com.google.api.gax.rpc.ApiExceptionFactory;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.api.gax.rpc.StatusCode;
29 import com.google.api.gax.rpc.testing.FakeStatusCode;
30 import com.google.api.resourcenames.ResourceName;
31 import com.google.cloud.kms.inventory.v1.stub.HttpJsonKeyTrackingServiceStub;
32 import com.google.common.collect.Lists;
33 import java.io.IOException;
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.List;
37 import javax.annotation.Generated;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Assert;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 
45 @Generated("by gapic-generator-java")
46 public class KeyTrackingServiceClientHttpJsonTest {
47   private static MockHttpService mockService;
48   private static KeyTrackingServiceClient client;
49 
50   @BeforeClass
startStaticServer()51   public static void startStaticServer() throws IOException {
52     mockService =
53         new MockHttpService(
54             HttpJsonKeyTrackingServiceStub.getMethodDescriptors(),
55             KeyTrackingServiceSettings.getDefaultEndpoint());
56     KeyTrackingServiceSettings settings =
57         KeyTrackingServiceSettings.newHttpJsonBuilder()
58             .setTransportChannelProvider(
59                 KeyTrackingServiceSettings.defaultHttpJsonTransportProviderBuilder()
60                     .setHttpTransport(mockService)
61                     .build())
62             .setCredentialsProvider(NoCredentialsProvider.create())
63             .build();
64     client = KeyTrackingServiceClient.create(settings);
65   }
66 
67   @AfterClass
stopServer()68   public static void stopServer() {
69     client.close();
70   }
71 
72   @Before
setUp()73   public void setUp() {}
74 
75   @After
tearDown()76   public void tearDown() throws Exception {
77     mockService.reset();
78   }
79 
80   @Test
getProtectedResourcesSummaryTest()81   public void getProtectedResourcesSummaryTest() throws Exception {
82     ProtectedResourcesSummary expectedResponse =
83         ProtectedResourcesSummary.newBuilder()
84             .setName(
85                 ProtectedResourcesSummaryName.ofProjectLocationKeyRingCryptoKeyName(
86                         "[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]")
87                     .toString())
88             .setResourceCount(287552926)
89             .setProjectCount(-953448343)
90             .putAllResourceTypes(new HashMap<String, Long>())
91             .putAllCloudProducts(new HashMap<String, Long>())
92             .putAllLocations(new HashMap<String, Long>())
93             .build();
94     mockService.addResponse(expectedResponse);
95 
96     ProtectedResourcesSummaryName name =
97         ProtectedResourcesSummaryName.ofProjectLocationKeyRingCryptoKeyName(
98             "[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]");
99 
100     ProtectedResourcesSummary actualResponse = client.getProtectedResourcesSummary(name);
101     Assert.assertEquals(expectedResponse, actualResponse);
102 
103     List<String> actualRequests = mockService.getRequestPaths();
104     Assert.assertEquals(1, actualRequests.size());
105 
106     String apiClientHeaderKey =
107         mockService
108             .getRequestHeaders()
109             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
110             .iterator()
111             .next();
112     Assert.assertTrue(
113         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
114             .matcher(apiClientHeaderKey)
115             .matches());
116   }
117 
118   @Test
getProtectedResourcesSummaryExceptionTest()119   public void getProtectedResourcesSummaryExceptionTest() throws Exception {
120     ApiException exception =
121         ApiExceptionFactory.createException(
122             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
123     mockService.addException(exception);
124 
125     try {
126       ProtectedResourcesSummaryName name =
127           ProtectedResourcesSummaryName.ofProjectLocationKeyRingCryptoKeyName(
128               "[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]");
129       client.getProtectedResourcesSummary(name);
130       Assert.fail("No exception raised");
131     } catch (InvalidArgumentException e) {
132       // Expected exception.
133     }
134   }
135 
136   @Test
getProtectedResourcesSummaryTest2()137   public void getProtectedResourcesSummaryTest2() throws Exception {
138     ProtectedResourcesSummary expectedResponse =
139         ProtectedResourcesSummary.newBuilder()
140             .setName(
141                 ProtectedResourcesSummaryName.ofProjectLocationKeyRingCryptoKeyName(
142                         "[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]")
143                     .toString())
144             .setResourceCount(287552926)
145             .setProjectCount(-953448343)
146             .putAllResourceTypes(new HashMap<String, Long>())
147             .putAllCloudProducts(new HashMap<String, Long>())
148             .putAllLocations(new HashMap<String, Long>())
149             .build();
150     mockService.addResponse(expectedResponse);
151 
152     String name =
153         "projects/project-4975/locations/location-4975/keyRings/keyRing-4975/cryptoKeys/cryptoKey-4975";
154 
155     ProtectedResourcesSummary actualResponse = client.getProtectedResourcesSummary(name);
156     Assert.assertEquals(expectedResponse, actualResponse);
157 
158     List<String> actualRequests = mockService.getRequestPaths();
159     Assert.assertEquals(1, actualRequests.size());
160 
161     String apiClientHeaderKey =
162         mockService
163             .getRequestHeaders()
164             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
165             .iterator()
166             .next();
167     Assert.assertTrue(
168         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
169             .matcher(apiClientHeaderKey)
170             .matches());
171   }
172 
173   @Test
getProtectedResourcesSummaryExceptionTest2()174   public void getProtectedResourcesSummaryExceptionTest2() throws Exception {
175     ApiException exception =
176         ApiExceptionFactory.createException(
177             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
178     mockService.addException(exception);
179 
180     try {
181       String name =
182           "projects/project-4975/locations/location-4975/keyRings/keyRing-4975/cryptoKeys/cryptoKey-4975";
183       client.getProtectedResourcesSummary(name);
184       Assert.fail("No exception raised");
185     } catch (InvalidArgumentException e) {
186       // Expected exception.
187     }
188   }
189 
190   @Test
searchProtectedResourcesTest()191   public void searchProtectedResourcesTest() throws Exception {
192     ProtectedResource responsesElement = ProtectedResource.newBuilder().build();
193     SearchProtectedResourcesResponse expectedResponse =
194         SearchProtectedResourcesResponse.newBuilder()
195             .setNextPageToken("")
196             .addAllProtectedResources(Arrays.asList(responsesElement))
197             .build();
198     mockService.addResponse(expectedResponse);
199 
200     OrganizationName scope = OrganizationName.of("[ORGANIZATION]");
201     ResourceName cryptoKey = OrganizationName.of("[ORGANIZATION]");
202 
203     SearchProtectedResourcesPagedResponse pagedListResponse =
204         client.searchProtectedResources(scope, cryptoKey);
205 
206     List<ProtectedResource> resources = Lists.newArrayList(pagedListResponse.iterateAll());
207 
208     Assert.assertEquals(1, resources.size());
209     Assert.assertEquals(expectedResponse.getProtectedResourcesList().get(0), resources.get(0));
210 
211     List<String> actualRequests = mockService.getRequestPaths();
212     Assert.assertEquals(1, actualRequests.size());
213 
214     String apiClientHeaderKey =
215         mockService
216             .getRequestHeaders()
217             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
218             .iterator()
219             .next();
220     Assert.assertTrue(
221         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
222             .matcher(apiClientHeaderKey)
223             .matches());
224   }
225 
226   @Test
searchProtectedResourcesExceptionTest()227   public void searchProtectedResourcesExceptionTest() throws Exception {
228     ApiException exception =
229         ApiExceptionFactory.createException(
230             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
231     mockService.addException(exception);
232 
233     try {
234       OrganizationName scope = OrganizationName.of("[ORGANIZATION]");
235       ResourceName cryptoKey = OrganizationName.of("[ORGANIZATION]");
236       client.searchProtectedResources(scope, cryptoKey);
237       Assert.fail("No exception raised");
238     } catch (InvalidArgumentException e) {
239       // Expected exception.
240     }
241   }
242 
243   @Test
searchProtectedResourcesTest2()244   public void searchProtectedResourcesTest2() throws Exception {
245     ProtectedResource responsesElement = ProtectedResource.newBuilder().build();
246     SearchProtectedResourcesResponse expectedResponse =
247         SearchProtectedResourcesResponse.newBuilder()
248             .setNextPageToken("")
249             .addAllProtectedResources(Arrays.asList(responsesElement))
250             .build();
251     mockService.addResponse(expectedResponse);
252 
253     OrganizationName scope = OrganizationName.of("[ORGANIZATION]");
254     String cryptoKey = "cryptoKey1598288350";
255 
256     SearchProtectedResourcesPagedResponse pagedListResponse =
257         client.searchProtectedResources(scope, cryptoKey);
258 
259     List<ProtectedResource> resources = Lists.newArrayList(pagedListResponse.iterateAll());
260 
261     Assert.assertEquals(1, resources.size());
262     Assert.assertEquals(expectedResponse.getProtectedResourcesList().get(0), resources.get(0));
263 
264     List<String> actualRequests = mockService.getRequestPaths();
265     Assert.assertEquals(1, actualRequests.size());
266 
267     String apiClientHeaderKey =
268         mockService
269             .getRequestHeaders()
270             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
271             .iterator()
272             .next();
273     Assert.assertTrue(
274         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
275             .matcher(apiClientHeaderKey)
276             .matches());
277   }
278 
279   @Test
searchProtectedResourcesExceptionTest2()280   public void searchProtectedResourcesExceptionTest2() throws Exception {
281     ApiException exception =
282         ApiExceptionFactory.createException(
283             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
284     mockService.addException(exception);
285 
286     try {
287       OrganizationName scope = OrganizationName.of("[ORGANIZATION]");
288       String cryptoKey = "cryptoKey1598288350";
289       client.searchProtectedResources(scope, cryptoKey);
290       Assert.fail("No exception raised");
291     } catch (InvalidArgumentException e) {
292       // Expected exception.
293     }
294   }
295 
296   @Test
searchProtectedResourcesTest3()297   public void searchProtectedResourcesTest3() throws Exception {
298     ProtectedResource responsesElement = ProtectedResource.newBuilder().build();
299     SearchProtectedResourcesResponse expectedResponse =
300         SearchProtectedResourcesResponse.newBuilder()
301             .setNextPageToken("")
302             .addAllProtectedResources(Arrays.asList(responsesElement))
303             .build();
304     mockService.addResponse(expectedResponse);
305 
306     String scope = "organizations/organization-6471";
307     ResourceName cryptoKey = OrganizationName.of("[ORGANIZATION]");
308 
309     SearchProtectedResourcesPagedResponse pagedListResponse =
310         client.searchProtectedResources(scope, cryptoKey);
311 
312     List<ProtectedResource> resources = Lists.newArrayList(pagedListResponse.iterateAll());
313 
314     Assert.assertEquals(1, resources.size());
315     Assert.assertEquals(expectedResponse.getProtectedResourcesList().get(0), resources.get(0));
316 
317     List<String> actualRequests = mockService.getRequestPaths();
318     Assert.assertEquals(1, actualRequests.size());
319 
320     String apiClientHeaderKey =
321         mockService
322             .getRequestHeaders()
323             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
324             .iterator()
325             .next();
326     Assert.assertTrue(
327         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
328             .matcher(apiClientHeaderKey)
329             .matches());
330   }
331 
332   @Test
searchProtectedResourcesExceptionTest3()333   public void searchProtectedResourcesExceptionTest3() throws Exception {
334     ApiException exception =
335         ApiExceptionFactory.createException(
336             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
337     mockService.addException(exception);
338 
339     try {
340       String scope = "organizations/organization-6471";
341       ResourceName cryptoKey = OrganizationName.of("[ORGANIZATION]");
342       client.searchProtectedResources(scope, cryptoKey);
343       Assert.fail("No exception raised");
344     } catch (InvalidArgumentException e) {
345       // Expected exception.
346     }
347   }
348 
349   @Test
searchProtectedResourcesTest4()350   public void searchProtectedResourcesTest4() throws Exception {
351     ProtectedResource responsesElement = ProtectedResource.newBuilder().build();
352     SearchProtectedResourcesResponse expectedResponse =
353         SearchProtectedResourcesResponse.newBuilder()
354             .setNextPageToken("")
355             .addAllProtectedResources(Arrays.asList(responsesElement))
356             .build();
357     mockService.addResponse(expectedResponse);
358 
359     String scope = "organizations/organization-6471";
360     String cryptoKey = "cryptoKey1598288350";
361 
362     SearchProtectedResourcesPagedResponse pagedListResponse =
363         client.searchProtectedResources(scope, cryptoKey);
364 
365     List<ProtectedResource> resources = Lists.newArrayList(pagedListResponse.iterateAll());
366 
367     Assert.assertEquals(1, resources.size());
368     Assert.assertEquals(expectedResponse.getProtectedResourcesList().get(0), resources.get(0));
369 
370     List<String> actualRequests = mockService.getRequestPaths();
371     Assert.assertEquals(1, actualRequests.size());
372 
373     String apiClientHeaderKey =
374         mockService
375             .getRequestHeaders()
376             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
377             .iterator()
378             .next();
379     Assert.assertTrue(
380         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
381             .matcher(apiClientHeaderKey)
382             .matches());
383   }
384 
385   @Test
searchProtectedResourcesExceptionTest4()386   public void searchProtectedResourcesExceptionTest4() throws Exception {
387     ApiException exception =
388         ApiExceptionFactory.createException(
389             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
390     mockService.addException(exception);
391 
392     try {
393       String scope = "organizations/organization-6471";
394       String cryptoKey = "cryptoKey1598288350";
395       client.searchProtectedResources(scope, cryptoKey);
396       Assert.fail("No exception raised");
397     } catch (InvalidArgumentException e) {
398       // Expected exception.
399     }
400   }
401 }
402