• 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.VpnGatewaysClient.AggregatedListPagedResponse;
20 import static com.google.cloud.compute.v1.VpnGatewaysClient.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.HttpJsonVpnGatewaysStub;
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.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.concurrent.ExecutionException;
42 import javax.annotation.Generated;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 
50 @Generated("by gapic-generator-java")
51 public class VpnGatewaysClientTest {
52   private static MockHttpService mockService;
53   private static VpnGatewaysClient client;
54 
55   @BeforeClass
startStaticServer()56   public static void startStaticServer() throws IOException {
57     mockService =
58         new MockHttpService(
59             HttpJsonVpnGatewaysStub.getMethodDescriptors(),
60             VpnGatewaysSettings.getDefaultEndpoint());
61     VpnGatewaysSettings settings =
62         VpnGatewaysSettings.newBuilder()
63             .setTransportChannelProvider(
64                 VpnGatewaysSettings.defaultHttpJsonTransportProviderBuilder()
65                     .setHttpTransport(mockService)
66                     .build())
67             .setCredentialsProvider(NoCredentialsProvider.create())
68             .build();
69     client = VpnGatewaysClient.create(settings);
70   }
71 
72   @AfterClass
stopServer()73   public static void stopServer() {
74     client.close();
75   }
76 
77   @Before
setUp()78   public void setUp() {}
79 
80   @After
tearDown()81   public void tearDown() throws Exception {
82     mockService.reset();
83   }
84 
85   @Test
aggregatedListTest()86   public void aggregatedListTest() throws Exception {
87     VpnGatewaysScopedList responsesElement = VpnGatewaysScopedList.newBuilder().build();
88     VpnGatewayAggregatedList expectedResponse =
89         VpnGatewayAggregatedList.newBuilder()
90             .setNextPageToken("")
91             .putAllItems(Collections.singletonMap("items", responsesElement))
92             .build();
93     mockService.addResponse(expectedResponse);
94 
95     String project = "project-6911";
96 
97     AggregatedListPagedResponse pagedListResponse = client.aggregatedList(project);
98 
99     List<Map.Entry<String, VpnGatewaysScopedList>> resources =
100         Lists.newArrayList(pagedListResponse.iterateAll());
101 
102     Assert.assertEquals(1, resources.size());
103     Assert.assertEquals(
104         expectedResponse.getItemsMap().entrySet().iterator().next(), resources.get(0));
105 
106     List<String> actualRequests = mockService.getRequestPaths();
107     Assert.assertEquals(1, actualRequests.size());
108 
109     String apiClientHeaderKey =
110         mockService
111             .getRequestHeaders()
112             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
113             .iterator()
114             .next();
115     Assert.assertTrue(
116         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
117             .matcher(apiClientHeaderKey)
118             .matches());
119   }
120 
121   @Test
aggregatedListExceptionTest()122   public void aggregatedListExceptionTest() throws Exception {
123     ApiException exception =
124         ApiExceptionFactory.createException(
125             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
126     mockService.addException(exception);
127 
128     try {
129       String project = "project-6911";
130       client.aggregatedList(project);
131       Assert.fail("No exception raised");
132     } catch (InvalidArgumentException e) {
133       // Expected exception.
134     }
135   }
136 
137   @Test
deleteTest()138   public void deleteTest() throws Exception {
139     Operation expectedResponse =
140         Operation.newBuilder()
141             .setClientOperationId("clientOperationId-1230366697")
142             .setCreationTimestamp("creationTimestamp-370203401")
143             .setDescription("description-1724546052")
144             .setEndTime("endTime-1607243192")
145             .setError(Error.newBuilder().build())
146             .setHttpErrorMessage("httpErrorMessage1577303431")
147             .setHttpErrorStatusCode(0)
148             .setId(3355)
149             .setInsertTime("insertTime966165798")
150             .setKind("kind3292052")
151             .setName("name3373707")
152             .setOperationGroupId("operationGroupId1716161683")
153             .setOperationType("operationType91999553")
154             .setProgress(-1001078227)
155             .setRegion("region-934795532")
156             .setSelfLink("selfLink1191800166")
157             .setStartTime("startTime-2129294769")
158             .setStatus(Status.DONE)
159             .setStatusMessage("statusMessage-958704715")
160             .setTargetId(-815576439)
161             .setTargetLink("targetLink486368555")
162             .setUser("user3599307")
163             .addAllWarnings(new ArrayList<Warnings>())
164             .setZone("zone3744684")
165             .build();
166     mockService.addResponse(expectedResponse);
167 
168     String project = "project-6911";
169     String region = "region-9622";
170     String vpnGateway = "vpnGateway-1638";
171 
172     Operation actualResponse = client.deleteAsync(project, region, vpnGateway).get();
173     Assert.assertEquals(expectedResponse, actualResponse);
174 
175     List<String> actualRequests = mockService.getRequestPaths();
176     Assert.assertEquals(1, actualRequests.size());
177 
178     String apiClientHeaderKey =
179         mockService
180             .getRequestHeaders()
181             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
182             .iterator()
183             .next();
184     Assert.assertTrue(
185         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
186             .matcher(apiClientHeaderKey)
187             .matches());
188   }
189 
190   @Test
deleteExceptionTest()191   public void deleteExceptionTest() throws Exception {
192     ApiException exception =
193         ApiExceptionFactory.createException(
194             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
195     mockService.addException(exception);
196 
197     try {
198       String project = "project-6911";
199       String region = "region-9622";
200       String vpnGateway = "vpnGateway-1638";
201       client.deleteAsync(project, region, vpnGateway).get();
202       Assert.fail("No exception raised");
203     } catch (ExecutionException e) {
204     }
205   }
206 
207   @Test
getTest()208   public void getTest() throws Exception {
209     VpnGateway expectedResponse =
210         VpnGateway.newBuilder()
211             .setCreationTimestamp("creationTimestamp-370203401")
212             .setDescription("description-1724546052")
213             .setId(3355)
214             .setKind("kind3292052")
215             .setLabelFingerprint("labelFingerprint379449680")
216             .putAllLabels(new HashMap<String, String>())
217             .setName("name3373707")
218             .setNetwork("network1843485230")
219             .setRegion("region-934795532")
220             .setSelfLink("selfLink1191800166")
221             .setStackType("stackType1727939042")
222             .addAllVpnInterfaces(new ArrayList<VpnGatewayVpnGatewayInterface>())
223             .build();
224     mockService.addResponse(expectedResponse);
225 
226     String project = "project-6911";
227     String region = "region-9622";
228     String vpnGateway = "vpnGateway-1638";
229 
230     VpnGateway actualResponse = client.get(project, region, vpnGateway);
231     Assert.assertEquals(expectedResponse, actualResponse);
232 
233     List<String> actualRequests = mockService.getRequestPaths();
234     Assert.assertEquals(1, actualRequests.size());
235 
236     String apiClientHeaderKey =
237         mockService
238             .getRequestHeaders()
239             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
240             .iterator()
241             .next();
242     Assert.assertTrue(
243         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
244             .matcher(apiClientHeaderKey)
245             .matches());
246   }
247 
248   @Test
getExceptionTest()249   public void getExceptionTest() throws Exception {
250     ApiException exception =
251         ApiExceptionFactory.createException(
252             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
253     mockService.addException(exception);
254 
255     try {
256       String project = "project-6911";
257       String region = "region-9622";
258       String vpnGateway = "vpnGateway-1638";
259       client.get(project, region, vpnGateway);
260       Assert.fail("No exception raised");
261     } catch (InvalidArgumentException e) {
262       // Expected exception.
263     }
264   }
265 
266   @Test
getStatusTest()267   public void getStatusTest() throws Exception {
268     VpnGatewaysGetStatusResponse expectedResponse =
269         VpnGatewaysGetStatusResponse.newBuilder()
270             .setResult(VpnGatewayStatus.newBuilder().build())
271             .build();
272     mockService.addResponse(expectedResponse);
273 
274     String project = "project-6911";
275     String region = "region-9622";
276     String vpnGateway = "vpnGateway-1638";
277 
278     VpnGatewaysGetStatusResponse actualResponse = client.getStatus(project, region, vpnGateway);
279     Assert.assertEquals(expectedResponse, actualResponse);
280 
281     List<String> actualRequests = mockService.getRequestPaths();
282     Assert.assertEquals(1, actualRequests.size());
283 
284     String apiClientHeaderKey =
285         mockService
286             .getRequestHeaders()
287             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
288             .iterator()
289             .next();
290     Assert.assertTrue(
291         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
292             .matcher(apiClientHeaderKey)
293             .matches());
294   }
295 
296   @Test
getStatusExceptionTest()297   public void getStatusExceptionTest() throws Exception {
298     ApiException exception =
299         ApiExceptionFactory.createException(
300             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
301     mockService.addException(exception);
302 
303     try {
304       String project = "project-6911";
305       String region = "region-9622";
306       String vpnGateway = "vpnGateway-1638";
307       client.getStatus(project, region, vpnGateway);
308       Assert.fail("No exception raised");
309     } catch (InvalidArgumentException e) {
310       // Expected exception.
311     }
312   }
313 
314   @Test
insertTest()315   public void insertTest() throws Exception {
316     Operation expectedResponse =
317         Operation.newBuilder()
318             .setClientOperationId("clientOperationId-1230366697")
319             .setCreationTimestamp("creationTimestamp-370203401")
320             .setDescription("description-1724546052")
321             .setEndTime("endTime-1607243192")
322             .setError(Error.newBuilder().build())
323             .setHttpErrorMessage("httpErrorMessage1577303431")
324             .setHttpErrorStatusCode(0)
325             .setId(3355)
326             .setInsertTime("insertTime966165798")
327             .setKind("kind3292052")
328             .setName("name3373707")
329             .setOperationGroupId("operationGroupId1716161683")
330             .setOperationType("operationType91999553")
331             .setProgress(-1001078227)
332             .setRegion("region-934795532")
333             .setSelfLink("selfLink1191800166")
334             .setStartTime("startTime-2129294769")
335             .setStatus(Status.DONE)
336             .setStatusMessage("statusMessage-958704715")
337             .setTargetId(-815576439)
338             .setTargetLink("targetLink486368555")
339             .setUser("user3599307")
340             .addAllWarnings(new ArrayList<Warnings>())
341             .setZone("zone3744684")
342             .build();
343     mockService.addResponse(expectedResponse);
344 
345     String project = "project-6911";
346     String region = "region-9622";
347     VpnGateway vpnGatewayResource = VpnGateway.newBuilder().build();
348 
349     Operation actualResponse = client.insertAsync(project, region, vpnGatewayResource).get();
350     Assert.assertEquals(expectedResponse, actualResponse);
351 
352     List<String> actualRequests = mockService.getRequestPaths();
353     Assert.assertEquals(1, actualRequests.size());
354 
355     String apiClientHeaderKey =
356         mockService
357             .getRequestHeaders()
358             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
359             .iterator()
360             .next();
361     Assert.assertTrue(
362         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
363             .matcher(apiClientHeaderKey)
364             .matches());
365   }
366 
367   @Test
insertExceptionTest()368   public void insertExceptionTest() throws Exception {
369     ApiException exception =
370         ApiExceptionFactory.createException(
371             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
372     mockService.addException(exception);
373 
374     try {
375       String project = "project-6911";
376       String region = "region-9622";
377       VpnGateway vpnGatewayResource = VpnGateway.newBuilder().build();
378       client.insertAsync(project, region, vpnGatewayResource).get();
379       Assert.fail("No exception raised");
380     } catch (ExecutionException e) {
381     }
382   }
383 
384   @Test
listTest()385   public void listTest() throws Exception {
386     VpnGateway responsesElement = VpnGateway.newBuilder().build();
387     VpnGatewayList expectedResponse =
388         VpnGatewayList.newBuilder()
389             .setNextPageToken("")
390             .addAllItems(Arrays.asList(responsesElement))
391             .build();
392     mockService.addResponse(expectedResponse);
393 
394     String project = "project-6911";
395     String region = "region-9622";
396 
397     ListPagedResponse pagedListResponse = client.list(project, region);
398 
399     List<VpnGateway> resources = Lists.newArrayList(pagedListResponse.iterateAll());
400 
401     Assert.assertEquals(1, resources.size());
402     Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0));
403 
404     List<String> actualRequests = mockService.getRequestPaths();
405     Assert.assertEquals(1, actualRequests.size());
406 
407     String apiClientHeaderKey =
408         mockService
409             .getRequestHeaders()
410             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
411             .iterator()
412             .next();
413     Assert.assertTrue(
414         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
415             .matcher(apiClientHeaderKey)
416             .matches());
417   }
418 
419   @Test
listExceptionTest()420   public void listExceptionTest() throws Exception {
421     ApiException exception =
422         ApiExceptionFactory.createException(
423             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
424     mockService.addException(exception);
425 
426     try {
427       String project = "project-6911";
428       String region = "region-9622";
429       client.list(project, region);
430       Assert.fail("No exception raised");
431     } catch (InvalidArgumentException e) {
432       // Expected exception.
433     }
434   }
435 
436   @Test
setLabelsTest()437   public void setLabelsTest() throws Exception {
438     Operation expectedResponse =
439         Operation.newBuilder()
440             .setClientOperationId("clientOperationId-1230366697")
441             .setCreationTimestamp("creationTimestamp-370203401")
442             .setDescription("description-1724546052")
443             .setEndTime("endTime-1607243192")
444             .setError(Error.newBuilder().build())
445             .setHttpErrorMessage("httpErrorMessage1577303431")
446             .setHttpErrorStatusCode(0)
447             .setId(3355)
448             .setInsertTime("insertTime966165798")
449             .setKind("kind3292052")
450             .setName("name3373707")
451             .setOperationGroupId("operationGroupId1716161683")
452             .setOperationType("operationType91999553")
453             .setProgress(-1001078227)
454             .setRegion("region-934795532")
455             .setSelfLink("selfLink1191800166")
456             .setStartTime("startTime-2129294769")
457             .setStatus(Status.DONE)
458             .setStatusMessage("statusMessage-958704715")
459             .setTargetId(-815576439)
460             .setTargetLink("targetLink486368555")
461             .setUser("user3599307")
462             .addAllWarnings(new ArrayList<Warnings>())
463             .setZone("zone3744684")
464             .build();
465     mockService.addResponse(expectedResponse);
466 
467     String project = "project-6911";
468     String region = "region-9622";
469     String resource = "resource-756";
470     RegionSetLabelsRequest regionSetLabelsRequestResource =
471         RegionSetLabelsRequest.newBuilder().build();
472 
473     Operation actualResponse =
474         client.setLabelsAsync(project, region, resource, regionSetLabelsRequestResource).get();
475     Assert.assertEquals(expectedResponse, actualResponse);
476 
477     List<String> actualRequests = mockService.getRequestPaths();
478     Assert.assertEquals(1, actualRequests.size());
479 
480     String apiClientHeaderKey =
481         mockService
482             .getRequestHeaders()
483             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
484             .iterator()
485             .next();
486     Assert.assertTrue(
487         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
488             .matcher(apiClientHeaderKey)
489             .matches());
490   }
491 
492   @Test
setLabelsExceptionTest()493   public void setLabelsExceptionTest() throws Exception {
494     ApiException exception =
495         ApiExceptionFactory.createException(
496             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
497     mockService.addException(exception);
498 
499     try {
500       String project = "project-6911";
501       String region = "region-9622";
502       String resource = "resource-756";
503       RegionSetLabelsRequest regionSetLabelsRequestResource =
504           RegionSetLabelsRequest.newBuilder().build();
505       client.setLabelsAsync(project, region, resource, regionSetLabelsRequestResource).get();
506       Assert.fail("No exception raised");
507     } catch (ExecutionException e) {
508     }
509   }
510 
511   @Test
testIamPermissionsTest()512   public void testIamPermissionsTest() throws Exception {
513     TestPermissionsResponse expectedResponse =
514         TestPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build();
515     mockService.addResponse(expectedResponse);
516 
517     String project = "project-6911";
518     String region = "region-9622";
519     String resource = "resource-756";
520     TestPermissionsRequest testPermissionsRequestResource =
521         TestPermissionsRequest.newBuilder().build();
522 
523     TestPermissionsResponse actualResponse =
524         client.testIamPermissions(project, region, resource, testPermissionsRequestResource);
525     Assert.assertEquals(expectedResponse, actualResponse);
526 
527     List<String> actualRequests = mockService.getRequestPaths();
528     Assert.assertEquals(1, actualRequests.size());
529 
530     String apiClientHeaderKey =
531         mockService
532             .getRequestHeaders()
533             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
534             .iterator()
535             .next();
536     Assert.assertTrue(
537         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
538             .matcher(apiClientHeaderKey)
539             .matches());
540   }
541 
542   @Test
testIamPermissionsExceptionTest()543   public void testIamPermissionsExceptionTest() throws Exception {
544     ApiException exception =
545         ApiExceptionFactory.createException(
546             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
547     mockService.addException(exception);
548 
549     try {
550       String project = "project-6911";
551       String region = "region-9622";
552       String resource = "resource-756";
553       TestPermissionsRequest testPermissionsRequestResource =
554           TestPermissionsRequest.newBuilder().build();
555       client.testIamPermissions(project, region, resource, testPermissionsRequestResource);
556       Assert.fail("No exception raised");
557     } catch (InvalidArgumentException e) {
558       // Expected exception.
559     }
560   }
561 }
562