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