• 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.compute.v1;
18 
19 import static com.google.cloud.compute.v1.AddressesClient.AggregatedListPagedResponse;
20 import static com.google.cloud.compute.v1.AddressesClient.ListPagedResponse;
21 
22 import com.google.api.gax.core.NoCredentialsProvider;
23 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
24 import com.google.api.gax.httpjson.testing.MockHttpService;
25 import com.google.api.gax.rpc.ApiClientHeaderProvider;
26 import com.google.api.gax.rpc.ApiException;
27 import com.google.api.gax.rpc.ApiExceptionFactory;
28 import com.google.api.gax.rpc.InvalidArgumentException;
29 import com.google.api.gax.rpc.StatusCode;
30 import com.google.api.gax.rpc.testing.FakeStatusCode;
31 import com.google.cloud.compute.v1.Operation.Status;
32 import com.google.cloud.compute.v1.stub.HttpJsonAddressesStub;
33 import com.google.common.collect.Lists;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.concurrent.ExecutionException;
41 import javax.annotation.Generated;
42 import org.junit.After;
43 import org.junit.AfterClass;
44 import org.junit.Assert;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 
49 @Generated("by gapic-generator-java")
50 public class AddressesClientTest {
51   private static MockHttpService mockService;
52   private static AddressesClient client;
53 
54   @BeforeClass
startStaticServer()55   public static void startStaticServer() throws IOException {
56     mockService =
57         new MockHttpService(
58             HttpJsonAddressesStub.getMethodDescriptors(), AddressesSettings.getDefaultEndpoint());
59     AddressesSettings settings =
60         AddressesSettings.newBuilder()
61             .setTransportChannelProvider(
62                 AddressesSettings.defaultHttpJsonTransportProviderBuilder()
63                     .setHttpTransport(mockService)
64                     .build())
65             .setCredentialsProvider(NoCredentialsProvider.create())
66             .build();
67     client = AddressesClient.create(settings);
68   }
69 
70   @AfterClass
stopServer()71   public static void stopServer() {
72     client.close();
73   }
74 
75   @Before
setUp()76   public void setUp() {}
77 
78   @After
tearDown()79   public void tearDown() throws Exception {
80     mockService.reset();
81   }
82 
83   @Test
aggregatedListTest()84   public void aggregatedListTest() throws Exception {
85     AddressesScopedList responsesElement = AddressesScopedList.newBuilder().build();
86     AddressAggregatedList expectedResponse =
87         AddressAggregatedList.newBuilder()
88             .setNextPageToken("")
89             .putAllItems(Collections.singletonMap("items", responsesElement))
90             .build();
91     mockService.addResponse(expectedResponse);
92 
93     String project = "project-6911";
94 
95     AggregatedListPagedResponse pagedListResponse = client.aggregatedList(project);
96 
97     List<Map.Entry<String, AddressesScopedList>> resources =
98         Lists.newArrayList(pagedListResponse.iterateAll());
99 
100     Assert.assertEquals(1, resources.size());
101     Assert.assertEquals(
102         expectedResponse.getItemsMap().entrySet().iterator().next(), resources.get(0));
103 
104     List<String> actualRequests = mockService.getRequestPaths();
105     Assert.assertEquals(1, actualRequests.size());
106 
107     String apiClientHeaderKey =
108         mockService
109             .getRequestHeaders()
110             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
111             .iterator()
112             .next();
113     Assert.assertTrue(
114         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
115             .matcher(apiClientHeaderKey)
116             .matches());
117   }
118 
119   @Test
aggregatedListExceptionTest()120   public void aggregatedListExceptionTest() throws Exception {
121     ApiException exception =
122         ApiExceptionFactory.createException(
123             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
124     mockService.addException(exception);
125 
126     try {
127       String project = "project-6911";
128       client.aggregatedList(project);
129       Assert.fail("No exception raised");
130     } catch (InvalidArgumentException e) {
131       // Expected exception.
132     }
133   }
134 
135   @Test
deleteTest()136   public void deleteTest() throws Exception {
137     Operation expectedResponse =
138         Operation.newBuilder()
139             .setClientOperationId("clientOperationId-1230366697")
140             .setCreationTimestamp("creationTimestamp-370203401")
141             .setDescription("description-1724546052")
142             .setEndTime("endTime-1607243192")
143             .setError(Error.newBuilder().build())
144             .setHttpErrorMessage("httpErrorMessage1577303431")
145             .setHttpErrorStatusCode(0)
146             .setId(3355)
147             .setInsertTime("insertTime966165798")
148             .setKind("kind3292052")
149             .setName("name3373707")
150             .setOperationGroupId("operationGroupId1716161683")
151             .setOperationType("operationType91999553")
152             .setProgress(-1001078227)
153             .setRegion("region-934795532")
154             .setSelfLink("selfLink1191800166")
155             .setStartTime("startTime-2129294769")
156             .setStatus(Status.DONE)
157             .setStatusMessage("statusMessage-958704715")
158             .setTargetId(-815576439)
159             .setTargetLink("targetLink486368555")
160             .setUser("user3599307")
161             .addAllWarnings(new ArrayList<Warnings>())
162             .setZone("zone3744684")
163             .build();
164     mockService.addResponse(expectedResponse);
165 
166     String project = "project-6911";
167     String region = "region-9622";
168     String address = "address-4954";
169 
170     Operation actualResponse = client.deleteAsync(project, region, address).get();
171     Assert.assertEquals(expectedResponse, actualResponse);
172 
173     List<String> actualRequests = mockService.getRequestPaths();
174     Assert.assertEquals(1, actualRequests.size());
175 
176     String apiClientHeaderKey =
177         mockService
178             .getRequestHeaders()
179             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
180             .iterator()
181             .next();
182     Assert.assertTrue(
183         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
184             .matcher(apiClientHeaderKey)
185             .matches());
186   }
187 
188   @Test
deleteExceptionTest()189   public void deleteExceptionTest() throws Exception {
190     ApiException exception =
191         ApiExceptionFactory.createException(
192             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
193     mockService.addException(exception);
194 
195     try {
196       String project = "project-6911";
197       String region = "region-9622";
198       String address = "address-4954";
199       client.deleteAsync(project, region, address).get();
200       Assert.fail("No exception raised");
201     } catch (ExecutionException e) {
202     }
203   }
204 
205   @Test
getTest()206   public void getTest() throws Exception {
207     Address expectedResponse =
208         Address.newBuilder()
209             .setAddress("address-1147692044")
210             .setAddressType("addressType-1377270450")
211             .setCreationTimestamp("creationTimestamp-370203401")
212             .setDescription("description-1724546052")
213             .setId(3355)
214             .setIpVersion("ipVersion-1161634383")
215             .setIpv6EndpointType("ipv6EndpointType399011318")
216             .setKind("kind3292052")
217             .setName("name3373707")
218             .setNetwork("network1843485230")
219             .setNetworkTier("networkTier-1940629200")
220             .setPrefixLength(-1157046989)
221             .setPurpose("purpose-220463842")
222             .setRegion("region-934795532")
223             .setSelfLink("selfLink1191800166")
224             .setStatus("status-892481550")
225             .setSubnetwork("subnetwork-1302785042")
226             .addAllUsers(new ArrayList<String>())
227             .build();
228     mockService.addResponse(expectedResponse);
229 
230     String project = "project-6911";
231     String region = "region-9622";
232     String address = "address-4954";
233 
234     Address actualResponse = client.get(project, region, address);
235     Assert.assertEquals(expectedResponse, actualResponse);
236 
237     List<String> actualRequests = mockService.getRequestPaths();
238     Assert.assertEquals(1, actualRequests.size());
239 
240     String apiClientHeaderKey =
241         mockService
242             .getRequestHeaders()
243             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
244             .iterator()
245             .next();
246     Assert.assertTrue(
247         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
248             .matcher(apiClientHeaderKey)
249             .matches());
250   }
251 
252   @Test
getExceptionTest()253   public void getExceptionTest() throws Exception {
254     ApiException exception =
255         ApiExceptionFactory.createException(
256             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
257     mockService.addException(exception);
258 
259     try {
260       String project = "project-6911";
261       String region = "region-9622";
262       String address = "address-4954";
263       client.get(project, region, address);
264       Assert.fail("No exception raised");
265     } catch (InvalidArgumentException e) {
266       // Expected exception.
267     }
268   }
269 
270   @Test
insertTest()271   public void insertTest() throws Exception {
272     Operation expectedResponse =
273         Operation.newBuilder()
274             .setClientOperationId("clientOperationId-1230366697")
275             .setCreationTimestamp("creationTimestamp-370203401")
276             .setDescription("description-1724546052")
277             .setEndTime("endTime-1607243192")
278             .setError(Error.newBuilder().build())
279             .setHttpErrorMessage("httpErrorMessage1577303431")
280             .setHttpErrorStatusCode(0)
281             .setId(3355)
282             .setInsertTime("insertTime966165798")
283             .setKind("kind3292052")
284             .setName("name3373707")
285             .setOperationGroupId("operationGroupId1716161683")
286             .setOperationType("operationType91999553")
287             .setProgress(-1001078227)
288             .setRegion("region-934795532")
289             .setSelfLink("selfLink1191800166")
290             .setStartTime("startTime-2129294769")
291             .setStatus(Status.DONE)
292             .setStatusMessage("statusMessage-958704715")
293             .setTargetId(-815576439)
294             .setTargetLink("targetLink486368555")
295             .setUser("user3599307")
296             .addAllWarnings(new ArrayList<Warnings>())
297             .setZone("zone3744684")
298             .build();
299     mockService.addResponse(expectedResponse);
300 
301     String project = "project-6911";
302     String region = "region-9622";
303     Address addressResource = Address.newBuilder().build();
304 
305     Operation actualResponse = client.insertAsync(project, region, addressResource).get();
306     Assert.assertEquals(expectedResponse, actualResponse);
307 
308     List<String> actualRequests = mockService.getRequestPaths();
309     Assert.assertEquals(1, actualRequests.size());
310 
311     String apiClientHeaderKey =
312         mockService
313             .getRequestHeaders()
314             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
315             .iterator()
316             .next();
317     Assert.assertTrue(
318         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
319             .matcher(apiClientHeaderKey)
320             .matches());
321   }
322 
323   @Test
insertExceptionTest()324   public void insertExceptionTest() throws Exception {
325     ApiException exception =
326         ApiExceptionFactory.createException(
327             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
328     mockService.addException(exception);
329 
330     try {
331       String project = "project-6911";
332       String region = "region-9622";
333       Address addressResource = Address.newBuilder().build();
334       client.insertAsync(project, region, addressResource).get();
335       Assert.fail("No exception raised");
336     } catch (ExecutionException e) {
337     }
338   }
339 
340   @Test
listTest()341   public void listTest() throws Exception {
342     Address responsesElement = Address.newBuilder().build();
343     AddressList expectedResponse =
344         AddressList.newBuilder()
345             .setNextPageToken("")
346             .addAllItems(Arrays.asList(responsesElement))
347             .build();
348     mockService.addResponse(expectedResponse);
349 
350     String project = "project-6911";
351     String region = "region-9622";
352 
353     ListPagedResponse pagedListResponse = client.list(project, region);
354 
355     List<Address> resources = Lists.newArrayList(pagedListResponse.iterateAll());
356 
357     Assert.assertEquals(1, resources.size());
358     Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0));
359 
360     List<String> actualRequests = mockService.getRequestPaths();
361     Assert.assertEquals(1, actualRequests.size());
362 
363     String apiClientHeaderKey =
364         mockService
365             .getRequestHeaders()
366             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
367             .iterator()
368             .next();
369     Assert.assertTrue(
370         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
371             .matcher(apiClientHeaderKey)
372             .matches());
373   }
374 
375   @Test
listExceptionTest()376   public void listExceptionTest() throws Exception {
377     ApiException exception =
378         ApiExceptionFactory.createException(
379             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
380     mockService.addException(exception);
381 
382     try {
383       String project = "project-6911";
384       String region = "region-9622";
385       client.list(project, region);
386       Assert.fail("No exception raised");
387     } catch (InvalidArgumentException e) {
388       // Expected exception.
389     }
390   }
391 
392   @Test
setLabelsTest()393   public void setLabelsTest() throws Exception {
394     Operation expectedResponse =
395         Operation.newBuilder()
396             .setClientOperationId("clientOperationId-1230366697")
397             .setCreationTimestamp("creationTimestamp-370203401")
398             .setDescription("description-1724546052")
399             .setEndTime("endTime-1607243192")
400             .setError(Error.newBuilder().build())
401             .setHttpErrorMessage("httpErrorMessage1577303431")
402             .setHttpErrorStatusCode(0)
403             .setId(3355)
404             .setInsertTime("insertTime966165798")
405             .setKind("kind3292052")
406             .setName("name3373707")
407             .setOperationGroupId("operationGroupId1716161683")
408             .setOperationType("operationType91999553")
409             .setProgress(-1001078227)
410             .setRegion("region-934795532")
411             .setSelfLink("selfLink1191800166")
412             .setStartTime("startTime-2129294769")
413             .setStatus(Status.DONE)
414             .setStatusMessage("statusMessage-958704715")
415             .setTargetId(-815576439)
416             .setTargetLink("targetLink486368555")
417             .setUser("user3599307")
418             .addAllWarnings(new ArrayList<Warnings>())
419             .setZone("zone3744684")
420             .build();
421     mockService.addResponse(expectedResponse);
422 
423     String project = "project-6911";
424     String region = "region-9622";
425     String resource = "resource-756";
426     RegionSetLabelsRequest regionSetLabelsRequestResource =
427         RegionSetLabelsRequest.newBuilder().build();
428 
429     Operation actualResponse =
430         client.setLabelsAsync(project, region, resource, regionSetLabelsRequestResource).get();
431     Assert.assertEquals(expectedResponse, actualResponse);
432 
433     List<String> actualRequests = mockService.getRequestPaths();
434     Assert.assertEquals(1, actualRequests.size());
435 
436     String apiClientHeaderKey =
437         mockService
438             .getRequestHeaders()
439             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
440             .iterator()
441             .next();
442     Assert.assertTrue(
443         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
444             .matcher(apiClientHeaderKey)
445             .matches());
446   }
447 
448   @Test
setLabelsExceptionTest()449   public void setLabelsExceptionTest() throws Exception {
450     ApiException exception =
451         ApiExceptionFactory.createException(
452             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
453     mockService.addException(exception);
454 
455     try {
456       String project = "project-6911";
457       String region = "region-9622";
458       String resource = "resource-756";
459       RegionSetLabelsRequest regionSetLabelsRequestResource =
460           RegionSetLabelsRequest.newBuilder().build();
461       client.setLabelsAsync(project, region, resource, regionSetLabelsRequestResource).get();
462       Assert.fail("No exception raised");
463     } catch (ExecutionException e) {
464     }
465   }
466 }
467