• 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.ServiceAttachmentsClient.AggregatedListPagedResponse;
20 import static com.google.cloud.compute.v1.ServiceAttachmentsClient.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.HttpJsonServiceAttachmentsStub;
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 ServiceAttachmentsClientTest {
51   private static MockHttpService mockService;
52   private static ServiceAttachmentsClient client;
53 
54   @BeforeClass
startStaticServer()55   public static void startStaticServer() throws IOException {
56     mockService =
57         new MockHttpService(
58             HttpJsonServiceAttachmentsStub.getMethodDescriptors(),
59             ServiceAttachmentsSettings.getDefaultEndpoint());
60     ServiceAttachmentsSettings settings =
61         ServiceAttachmentsSettings.newBuilder()
62             .setTransportChannelProvider(
63                 ServiceAttachmentsSettings.defaultHttpJsonTransportProviderBuilder()
64                     .setHttpTransport(mockService)
65                     .build())
66             .setCredentialsProvider(NoCredentialsProvider.create())
67             .build();
68     client = ServiceAttachmentsClient.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     ServiceAttachmentsScopedList responsesElement =
87         ServiceAttachmentsScopedList.newBuilder().build();
88     ServiceAttachmentAggregatedList expectedResponse =
89         ServiceAttachmentAggregatedList.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, ServiceAttachmentsScopedList>> 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 serviceAttachment = "serviceAttachment-4594";
171 
172     Operation actualResponse = client.deleteAsync(project, region, serviceAttachment).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 serviceAttachment = "serviceAttachment-4594";
201       client.deleteAsync(project, region, serviceAttachment).get();
202       Assert.fail("No exception raised");
203     } catch (ExecutionException e) {
204     }
205   }
206 
207   @Test
getTest()208   public void getTest() throws Exception {
209     ServiceAttachment expectedResponse =
210         ServiceAttachment.newBuilder()
211             .addAllConnectedEndpoints(new ArrayList<ServiceAttachmentConnectedEndpoint>())
212             .setConnectionPreference("connectionPreference987798105")
213             .addAllConsumerAcceptLists(new ArrayList<ServiceAttachmentConsumerProjectLimit>())
214             .addAllConsumerRejectLists(new ArrayList<String>())
215             .setCreationTimestamp("creationTimestamp-370203401")
216             .setDescription("description-1724546052")
217             .addAllDomainNames(new ArrayList<String>())
218             .setEnableProxyProtocol(true)
219             .setFingerprint("fingerprint-1375934236")
220             .setId(3355)
221             .setKind("kind3292052")
222             .setName("name3373707")
223             .addAllNatSubnets(new ArrayList<String>())
224             .setProducerForwardingRule("producerForwardingRule-1240429621")
225             .setPscServiceAttachmentId(Uint128.newBuilder().build())
226             .setRegion("region-934795532")
227             .setSelfLink("selfLink1191800166")
228             .setTargetService("targetService-6866972")
229             .build();
230     mockService.addResponse(expectedResponse);
231 
232     String project = "project-6911";
233     String region = "region-9622";
234     String serviceAttachment = "serviceAttachment-4594";
235 
236     ServiceAttachment actualResponse = client.get(project, region, serviceAttachment);
237     Assert.assertEquals(expectedResponse, actualResponse);
238 
239     List<String> actualRequests = mockService.getRequestPaths();
240     Assert.assertEquals(1, actualRequests.size());
241 
242     String apiClientHeaderKey =
243         mockService
244             .getRequestHeaders()
245             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
246             .iterator()
247             .next();
248     Assert.assertTrue(
249         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
250             .matcher(apiClientHeaderKey)
251             .matches());
252   }
253 
254   @Test
getExceptionTest()255   public void getExceptionTest() throws Exception {
256     ApiException exception =
257         ApiExceptionFactory.createException(
258             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
259     mockService.addException(exception);
260 
261     try {
262       String project = "project-6911";
263       String region = "region-9622";
264       String serviceAttachment = "serviceAttachment-4594";
265       client.get(project, region, serviceAttachment);
266       Assert.fail("No exception raised");
267     } catch (InvalidArgumentException e) {
268       // Expected exception.
269     }
270   }
271 
272   @Test
getIamPolicyTest()273   public void getIamPolicyTest() throws Exception {
274     Policy expectedResponse =
275         Policy.newBuilder()
276             .addAllAuditConfigs(new ArrayList<AuditConfig>())
277             .addAllBindings(new ArrayList<Binding>())
278             .setEtag("etag3123477")
279             .setIamOwned(true)
280             .addAllRules(new ArrayList<Rule>())
281             .setVersion(351608024)
282             .build();
283     mockService.addResponse(expectedResponse);
284 
285     String project = "project-6911";
286     String region = "region-9622";
287     String resource = "resource-756";
288 
289     Policy actualResponse = client.getIamPolicy(project, region, resource);
290     Assert.assertEquals(expectedResponse, actualResponse);
291 
292     List<String> actualRequests = mockService.getRequestPaths();
293     Assert.assertEquals(1, actualRequests.size());
294 
295     String apiClientHeaderKey =
296         mockService
297             .getRequestHeaders()
298             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
299             .iterator()
300             .next();
301     Assert.assertTrue(
302         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
303             .matcher(apiClientHeaderKey)
304             .matches());
305   }
306 
307   @Test
getIamPolicyExceptionTest()308   public void getIamPolicyExceptionTest() throws Exception {
309     ApiException exception =
310         ApiExceptionFactory.createException(
311             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
312     mockService.addException(exception);
313 
314     try {
315       String project = "project-6911";
316       String region = "region-9622";
317       String resource = "resource-756";
318       client.getIamPolicy(project, region, resource);
319       Assert.fail("No exception raised");
320     } catch (InvalidArgumentException e) {
321       // Expected exception.
322     }
323   }
324 
325   @Test
insertTest()326   public void insertTest() throws Exception {
327     Operation expectedResponse =
328         Operation.newBuilder()
329             .setClientOperationId("clientOperationId-1230366697")
330             .setCreationTimestamp("creationTimestamp-370203401")
331             .setDescription("description-1724546052")
332             .setEndTime("endTime-1607243192")
333             .setError(Error.newBuilder().build())
334             .setHttpErrorMessage("httpErrorMessage1577303431")
335             .setHttpErrorStatusCode(0)
336             .setId(3355)
337             .setInsertTime("insertTime966165798")
338             .setKind("kind3292052")
339             .setName("name3373707")
340             .setOperationGroupId("operationGroupId1716161683")
341             .setOperationType("operationType91999553")
342             .setProgress(-1001078227)
343             .setRegion("region-934795532")
344             .setSelfLink("selfLink1191800166")
345             .setStartTime("startTime-2129294769")
346             .setStatus(Status.DONE)
347             .setStatusMessage("statusMessage-958704715")
348             .setTargetId(-815576439)
349             .setTargetLink("targetLink486368555")
350             .setUser("user3599307")
351             .addAllWarnings(new ArrayList<Warnings>())
352             .setZone("zone3744684")
353             .build();
354     mockService.addResponse(expectedResponse);
355 
356     String project = "project-6911";
357     String region = "region-9622";
358     ServiceAttachment serviceAttachmentResource = ServiceAttachment.newBuilder().build();
359 
360     Operation actualResponse = client.insertAsync(project, region, serviceAttachmentResource).get();
361     Assert.assertEquals(expectedResponse, actualResponse);
362 
363     List<String> actualRequests = mockService.getRequestPaths();
364     Assert.assertEquals(1, actualRequests.size());
365 
366     String apiClientHeaderKey =
367         mockService
368             .getRequestHeaders()
369             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
370             .iterator()
371             .next();
372     Assert.assertTrue(
373         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
374             .matcher(apiClientHeaderKey)
375             .matches());
376   }
377 
378   @Test
insertExceptionTest()379   public void insertExceptionTest() throws Exception {
380     ApiException exception =
381         ApiExceptionFactory.createException(
382             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
383     mockService.addException(exception);
384 
385     try {
386       String project = "project-6911";
387       String region = "region-9622";
388       ServiceAttachment serviceAttachmentResource = ServiceAttachment.newBuilder().build();
389       client.insertAsync(project, region, serviceAttachmentResource).get();
390       Assert.fail("No exception raised");
391     } catch (ExecutionException e) {
392     }
393   }
394 
395   @Test
listTest()396   public void listTest() throws Exception {
397     ServiceAttachment responsesElement = ServiceAttachment.newBuilder().build();
398     ServiceAttachmentList expectedResponse =
399         ServiceAttachmentList.newBuilder()
400             .setNextPageToken("")
401             .addAllItems(Arrays.asList(responsesElement))
402             .build();
403     mockService.addResponse(expectedResponse);
404 
405     String project = "project-6911";
406     String region = "region-9622";
407 
408     ListPagedResponse pagedListResponse = client.list(project, region);
409 
410     List<ServiceAttachment> resources = Lists.newArrayList(pagedListResponse.iterateAll());
411 
412     Assert.assertEquals(1, resources.size());
413     Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0));
414 
415     List<String> actualRequests = mockService.getRequestPaths();
416     Assert.assertEquals(1, actualRequests.size());
417 
418     String apiClientHeaderKey =
419         mockService
420             .getRequestHeaders()
421             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
422             .iterator()
423             .next();
424     Assert.assertTrue(
425         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
426             .matcher(apiClientHeaderKey)
427             .matches());
428   }
429 
430   @Test
listExceptionTest()431   public void listExceptionTest() throws Exception {
432     ApiException exception =
433         ApiExceptionFactory.createException(
434             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
435     mockService.addException(exception);
436 
437     try {
438       String project = "project-6911";
439       String region = "region-9622";
440       client.list(project, region);
441       Assert.fail("No exception raised");
442     } catch (InvalidArgumentException e) {
443       // Expected exception.
444     }
445   }
446 
447   @Test
patchTest()448   public void patchTest() throws Exception {
449     Operation expectedResponse =
450         Operation.newBuilder()
451             .setClientOperationId("clientOperationId-1230366697")
452             .setCreationTimestamp("creationTimestamp-370203401")
453             .setDescription("description-1724546052")
454             .setEndTime("endTime-1607243192")
455             .setError(Error.newBuilder().build())
456             .setHttpErrorMessage("httpErrorMessage1577303431")
457             .setHttpErrorStatusCode(0)
458             .setId(3355)
459             .setInsertTime("insertTime966165798")
460             .setKind("kind3292052")
461             .setName("name3373707")
462             .setOperationGroupId("operationGroupId1716161683")
463             .setOperationType("operationType91999553")
464             .setProgress(-1001078227)
465             .setRegion("region-934795532")
466             .setSelfLink("selfLink1191800166")
467             .setStartTime("startTime-2129294769")
468             .setStatus(Status.DONE)
469             .setStatusMessage("statusMessage-958704715")
470             .setTargetId(-815576439)
471             .setTargetLink("targetLink486368555")
472             .setUser("user3599307")
473             .addAllWarnings(new ArrayList<Warnings>())
474             .setZone("zone3744684")
475             .build();
476     mockService.addResponse(expectedResponse);
477 
478     String project = "project-6911";
479     String region = "region-9622";
480     String serviceAttachment = "serviceAttachment-4594";
481     ServiceAttachment serviceAttachmentResource = ServiceAttachment.newBuilder().build();
482 
483     Operation actualResponse =
484         client.patchAsync(project, region, serviceAttachment, serviceAttachmentResource).get();
485     Assert.assertEquals(expectedResponse, actualResponse);
486 
487     List<String> actualRequests = mockService.getRequestPaths();
488     Assert.assertEquals(1, actualRequests.size());
489 
490     String apiClientHeaderKey =
491         mockService
492             .getRequestHeaders()
493             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
494             .iterator()
495             .next();
496     Assert.assertTrue(
497         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
498             .matcher(apiClientHeaderKey)
499             .matches());
500   }
501 
502   @Test
patchExceptionTest()503   public void patchExceptionTest() throws Exception {
504     ApiException exception =
505         ApiExceptionFactory.createException(
506             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
507     mockService.addException(exception);
508 
509     try {
510       String project = "project-6911";
511       String region = "region-9622";
512       String serviceAttachment = "serviceAttachment-4594";
513       ServiceAttachment serviceAttachmentResource = ServiceAttachment.newBuilder().build();
514       client.patchAsync(project, region, serviceAttachment, serviceAttachmentResource).get();
515       Assert.fail("No exception raised");
516     } catch (ExecutionException e) {
517     }
518   }
519 
520   @Test
setIamPolicyTest()521   public void setIamPolicyTest() throws Exception {
522     Policy expectedResponse =
523         Policy.newBuilder()
524             .addAllAuditConfigs(new ArrayList<AuditConfig>())
525             .addAllBindings(new ArrayList<Binding>())
526             .setEtag("etag3123477")
527             .setIamOwned(true)
528             .addAllRules(new ArrayList<Rule>())
529             .setVersion(351608024)
530             .build();
531     mockService.addResponse(expectedResponse);
532 
533     String project = "project-6911";
534     String region = "region-9622";
535     String resource = "resource-756";
536     RegionSetPolicyRequest regionSetPolicyRequestResource =
537         RegionSetPolicyRequest.newBuilder().build();
538 
539     Policy actualResponse =
540         client.setIamPolicy(project, region, resource, regionSetPolicyRequestResource);
541     Assert.assertEquals(expectedResponse, actualResponse);
542 
543     List<String> actualRequests = mockService.getRequestPaths();
544     Assert.assertEquals(1, actualRequests.size());
545 
546     String apiClientHeaderKey =
547         mockService
548             .getRequestHeaders()
549             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
550             .iterator()
551             .next();
552     Assert.assertTrue(
553         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
554             .matcher(apiClientHeaderKey)
555             .matches());
556   }
557 
558   @Test
setIamPolicyExceptionTest()559   public void setIamPolicyExceptionTest() throws Exception {
560     ApiException exception =
561         ApiExceptionFactory.createException(
562             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
563     mockService.addException(exception);
564 
565     try {
566       String project = "project-6911";
567       String region = "region-9622";
568       String resource = "resource-756";
569       RegionSetPolicyRequest regionSetPolicyRequestResource =
570           RegionSetPolicyRequest.newBuilder().build();
571       client.setIamPolicy(project, region, resource, regionSetPolicyRequestResource);
572       Assert.fail("No exception raised");
573     } catch (InvalidArgumentException e) {
574       // Expected exception.
575     }
576   }
577 
578   @Test
testIamPermissionsTest()579   public void testIamPermissionsTest() throws Exception {
580     TestPermissionsResponse expectedResponse =
581         TestPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build();
582     mockService.addResponse(expectedResponse);
583 
584     String project = "project-6911";
585     String region = "region-9622";
586     String resource = "resource-756";
587     TestPermissionsRequest testPermissionsRequestResource =
588         TestPermissionsRequest.newBuilder().build();
589 
590     TestPermissionsResponse actualResponse =
591         client.testIamPermissions(project, region, resource, testPermissionsRequestResource);
592     Assert.assertEquals(expectedResponse, actualResponse);
593 
594     List<String> actualRequests = mockService.getRequestPaths();
595     Assert.assertEquals(1, actualRequests.size());
596 
597     String apiClientHeaderKey =
598         mockService
599             .getRequestHeaders()
600             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
601             .iterator()
602             .next();
603     Assert.assertTrue(
604         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
605             .matcher(apiClientHeaderKey)
606             .matches());
607   }
608 
609   @Test
testIamPermissionsExceptionTest()610   public void testIamPermissionsExceptionTest() throws Exception {
611     ApiException exception =
612         ApiExceptionFactory.createException(
613             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
614     mockService.addException(exception);
615 
616     try {
617       String project = "project-6911";
618       String region = "region-9622";
619       String resource = "resource-756";
620       TestPermissionsRequest testPermissionsRequestResource =
621           TestPermissionsRequest.newBuilder().build();
622       client.testIamPermissions(project, region, resource, testPermissionsRequestResource);
623       Assert.fail("No exception raised");
624     } catch (InvalidArgumentException e) {
625       // Expected exception.
626     }
627   }
628 }
629