• 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.beyondcorp.clientgateways.v1;
18 
19 import static com.google.cloud.beyondcorp.clientgateways.v1.ClientGatewaysServiceClient.ListClientGatewaysPagedResponse;
20 import static com.google.cloud.beyondcorp.clientgateways.v1.ClientGatewaysServiceClient.ListLocationsPagedResponse;
21 
22 import com.google.api.gax.core.NoCredentialsProvider;
23 import com.google.api.gax.grpc.GaxGrpcProperties;
24 import com.google.api.gax.grpc.testing.LocalChannelProvider;
25 import com.google.api.gax.grpc.testing.MockGrpcService;
26 import com.google.api.gax.grpc.testing.MockServiceHelper;
27 import com.google.api.gax.rpc.ApiClientHeaderProvider;
28 import com.google.api.gax.rpc.InvalidArgumentException;
29 import com.google.api.gax.rpc.StatusCode;
30 import com.google.cloud.location.GetLocationRequest;
31 import com.google.cloud.location.ListLocationsRequest;
32 import com.google.cloud.location.ListLocationsResponse;
33 import com.google.cloud.location.Location;
34 import com.google.common.collect.Lists;
35 import com.google.iam.v1.AuditConfig;
36 import com.google.iam.v1.Binding;
37 import com.google.iam.v1.GetIamPolicyRequest;
38 import com.google.iam.v1.GetPolicyOptions;
39 import com.google.iam.v1.Policy;
40 import com.google.iam.v1.SetIamPolicyRequest;
41 import com.google.iam.v1.TestIamPermissionsRequest;
42 import com.google.iam.v1.TestIamPermissionsResponse;
43 import com.google.longrunning.Operation;
44 import com.google.protobuf.AbstractMessage;
45 import com.google.protobuf.Any;
46 import com.google.protobuf.ByteString;
47 import com.google.protobuf.Empty;
48 import com.google.protobuf.FieldMask;
49 import com.google.protobuf.Timestamp;
50 import io.grpc.StatusRuntimeException;
51 import java.io.IOException;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.UUID;
57 import java.util.concurrent.ExecutionException;
58 import javax.annotation.Generated;
59 import org.junit.After;
60 import org.junit.AfterClass;
61 import org.junit.Assert;
62 import org.junit.Before;
63 import org.junit.BeforeClass;
64 import org.junit.Test;
65 
66 @Generated("by gapic-generator-java")
67 public class ClientGatewaysServiceClientTest {
68   private static MockClientGatewaysService mockClientGatewaysService;
69   private static MockIAMPolicy mockIAMPolicy;
70   private static MockLocations mockLocations;
71   private static MockServiceHelper mockServiceHelper;
72   private LocalChannelProvider channelProvider;
73   private ClientGatewaysServiceClient client;
74 
75   @BeforeClass
startStaticServer()76   public static void startStaticServer() {
77     mockClientGatewaysService = new MockClientGatewaysService();
78     mockLocations = new MockLocations();
79     mockIAMPolicy = new MockIAMPolicy();
80     mockServiceHelper =
81         new MockServiceHelper(
82             UUID.randomUUID().toString(),
83             Arrays.<MockGrpcService>asList(
84                 mockClientGatewaysService, mockLocations, mockIAMPolicy));
85     mockServiceHelper.start();
86   }
87 
88   @AfterClass
stopServer()89   public static void stopServer() {
90     mockServiceHelper.stop();
91   }
92 
93   @Before
setUp()94   public void setUp() throws IOException {
95     mockServiceHelper.reset();
96     channelProvider = mockServiceHelper.createChannelProvider();
97     ClientGatewaysServiceSettings settings =
98         ClientGatewaysServiceSettings.newBuilder()
99             .setTransportChannelProvider(channelProvider)
100             .setCredentialsProvider(NoCredentialsProvider.create())
101             .build();
102     client = ClientGatewaysServiceClient.create(settings);
103   }
104 
105   @After
tearDown()106   public void tearDown() throws Exception {
107     client.close();
108   }
109 
110   @Test
listClientGatewaysTest()111   public void listClientGatewaysTest() throws Exception {
112     ClientGateway responsesElement = ClientGateway.newBuilder().build();
113     ListClientGatewaysResponse expectedResponse =
114         ListClientGatewaysResponse.newBuilder()
115             .setNextPageToken("")
116             .addAllClientGateways(Arrays.asList(responsesElement))
117             .build();
118     mockClientGatewaysService.addResponse(expectedResponse);
119 
120     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
121 
122     ListClientGatewaysPagedResponse pagedListResponse = client.listClientGateways(parent);
123 
124     List<ClientGateway> resources = Lists.newArrayList(pagedListResponse.iterateAll());
125 
126     Assert.assertEquals(1, resources.size());
127     Assert.assertEquals(expectedResponse.getClientGatewaysList().get(0), resources.get(0));
128 
129     List<AbstractMessage> actualRequests = mockClientGatewaysService.getRequests();
130     Assert.assertEquals(1, actualRequests.size());
131     ListClientGatewaysRequest actualRequest = ((ListClientGatewaysRequest) actualRequests.get(0));
132 
133     Assert.assertEquals(parent.toString(), actualRequest.getParent());
134     Assert.assertTrue(
135         channelProvider.isHeaderSent(
136             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
137             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
138   }
139 
140   @Test
listClientGatewaysExceptionTest()141   public void listClientGatewaysExceptionTest() throws Exception {
142     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
143     mockClientGatewaysService.addException(exception);
144 
145     try {
146       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
147       client.listClientGateways(parent);
148       Assert.fail("No exception raised");
149     } catch (InvalidArgumentException e) {
150       // Expected exception.
151     }
152   }
153 
154   @Test
listClientGatewaysTest2()155   public void listClientGatewaysTest2() throws Exception {
156     ClientGateway responsesElement = ClientGateway.newBuilder().build();
157     ListClientGatewaysResponse expectedResponse =
158         ListClientGatewaysResponse.newBuilder()
159             .setNextPageToken("")
160             .addAllClientGateways(Arrays.asList(responsesElement))
161             .build();
162     mockClientGatewaysService.addResponse(expectedResponse);
163 
164     String parent = "parent-995424086";
165 
166     ListClientGatewaysPagedResponse pagedListResponse = client.listClientGateways(parent);
167 
168     List<ClientGateway> resources = Lists.newArrayList(pagedListResponse.iterateAll());
169 
170     Assert.assertEquals(1, resources.size());
171     Assert.assertEquals(expectedResponse.getClientGatewaysList().get(0), resources.get(0));
172 
173     List<AbstractMessage> actualRequests = mockClientGatewaysService.getRequests();
174     Assert.assertEquals(1, actualRequests.size());
175     ListClientGatewaysRequest actualRequest = ((ListClientGatewaysRequest) actualRequests.get(0));
176 
177     Assert.assertEquals(parent, actualRequest.getParent());
178     Assert.assertTrue(
179         channelProvider.isHeaderSent(
180             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
181             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
182   }
183 
184   @Test
listClientGatewaysExceptionTest2()185   public void listClientGatewaysExceptionTest2() throws Exception {
186     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
187     mockClientGatewaysService.addException(exception);
188 
189     try {
190       String parent = "parent-995424086";
191       client.listClientGateways(parent);
192       Assert.fail("No exception raised");
193     } catch (InvalidArgumentException e) {
194       // Expected exception.
195     }
196   }
197 
198   @Test
getClientGatewayTest()199   public void getClientGatewayTest() throws Exception {
200     ClientGateway expectedResponse =
201         ClientGateway.newBuilder()
202             .setName(ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
203             .setCreateTime(Timestamp.newBuilder().build())
204             .setUpdateTime(Timestamp.newBuilder().build())
205             .setId("id3355")
206             .setClientConnectorService("clientConnectorService-1129927789")
207             .build();
208     mockClientGatewaysService.addResponse(expectedResponse);
209 
210     ClientGatewayName name = ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]");
211 
212     ClientGateway actualResponse = client.getClientGateway(name);
213     Assert.assertEquals(expectedResponse, actualResponse);
214 
215     List<AbstractMessage> actualRequests = mockClientGatewaysService.getRequests();
216     Assert.assertEquals(1, actualRequests.size());
217     GetClientGatewayRequest actualRequest = ((GetClientGatewayRequest) actualRequests.get(0));
218 
219     Assert.assertEquals(name.toString(), actualRequest.getName());
220     Assert.assertTrue(
221         channelProvider.isHeaderSent(
222             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
223             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
224   }
225 
226   @Test
getClientGatewayExceptionTest()227   public void getClientGatewayExceptionTest() throws Exception {
228     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
229     mockClientGatewaysService.addException(exception);
230 
231     try {
232       ClientGatewayName name = ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]");
233       client.getClientGateway(name);
234       Assert.fail("No exception raised");
235     } catch (InvalidArgumentException e) {
236       // Expected exception.
237     }
238   }
239 
240   @Test
getClientGatewayTest2()241   public void getClientGatewayTest2() throws Exception {
242     ClientGateway expectedResponse =
243         ClientGateway.newBuilder()
244             .setName(ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
245             .setCreateTime(Timestamp.newBuilder().build())
246             .setUpdateTime(Timestamp.newBuilder().build())
247             .setId("id3355")
248             .setClientConnectorService("clientConnectorService-1129927789")
249             .build();
250     mockClientGatewaysService.addResponse(expectedResponse);
251 
252     String name = "name3373707";
253 
254     ClientGateway actualResponse = client.getClientGateway(name);
255     Assert.assertEquals(expectedResponse, actualResponse);
256 
257     List<AbstractMessage> actualRequests = mockClientGatewaysService.getRequests();
258     Assert.assertEquals(1, actualRequests.size());
259     GetClientGatewayRequest actualRequest = ((GetClientGatewayRequest) actualRequests.get(0));
260 
261     Assert.assertEquals(name, actualRequest.getName());
262     Assert.assertTrue(
263         channelProvider.isHeaderSent(
264             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
265             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
266   }
267 
268   @Test
getClientGatewayExceptionTest2()269   public void getClientGatewayExceptionTest2() throws Exception {
270     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
271     mockClientGatewaysService.addException(exception);
272 
273     try {
274       String name = "name3373707";
275       client.getClientGateway(name);
276       Assert.fail("No exception raised");
277     } catch (InvalidArgumentException e) {
278       // Expected exception.
279     }
280   }
281 
282   @Test
createClientGatewayTest()283   public void createClientGatewayTest() throws Exception {
284     ClientGateway expectedResponse =
285         ClientGateway.newBuilder()
286             .setName(ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
287             .setCreateTime(Timestamp.newBuilder().build())
288             .setUpdateTime(Timestamp.newBuilder().build())
289             .setId("id3355")
290             .setClientConnectorService("clientConnectorService-1129927789")
291             .build();
292     Operation resultOperation =
293         Operation.newBuilder()
294             .setName("createClientGatewayTest")
295             .setDone(true)
296             .setResponse(Any.pack(expectedResponse))
297             .build();
298     mockClientGatewaysService.addResponse(resultOperation);
299 
300     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
301     ClientGateway clientGateway = ClientGateway.newBuilder().build();
302     String clientGatewayId = "clientGatewayId-1199632652";
303 
304     ClientGateway actualResponse =
305         client.createClientGatewayAsync(parent, clientGateway, clientGatewayId).get();
306     Assert.assertEquals(expectedResponse, actualResponse);
307 
308     List<AbstractMessage> actualRequests = mockClientGatewaysService.getRequests();
309     Assert.assertEquals(1, actualRequests.size());
310     CreateClientGatewayRequest actualRequest = ((CreateClientGatewayRequest) actualRequests.get(0));
311 
312     Assert.assertEquals(parent.toString(), actualRequest.getParent());
313     Assert.assertEquals(clientGateway, actualRequest.getClientGateway());
314     Assert.assertEquals(clientGatewayId, actualRequest.getClientGatewayId());
315     Assert.assertTrue(
316         channelProvider.isHeaderSent(
317             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
318             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
319   }
320 
321   @Test
createClientGatewayExceptionTest()322   public void createClientGatewayExceptionTest() throws Exception {
323     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
324     mockClientGatewaysService.addException(exception);
325 
326     try {
327       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
328       ClientGateway clientGateway = ClientGateway.newBuilder().build();
329       String clientGatewayId = "clientGatewayId-1199632652";
330       client.createClientGatewayAsync(parent, clientGateway, clientGatewayId).get();
331       Assert.fail("No exception raised");
332     } catch (ExecutionException e) {
333       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
334       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
335       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
336     }
337   }
338 
339   @Test
createClientGatewayTest2()340   public void createClientGatewayTest2() throws Exception {
341     ClientGateway expectedResponse =
342         ClientGateway.newBuilder()
343             .setName(ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
344             .setCreateTime(Timestamp.newBuilder().build())
345             .setUpdateTime(Timestamp.newBuilder().build())
346             .setId("id3355")
347             .setClientConnectorService("clientConnectorService-1129927789")
348             .build();
349     Operation resultOperation =
350         Operation.newBuilder()
351             .setName("createClientGatewayTest")
352             .setDone(true)
353             .setResponse(Any.pack(expectedResponse))
354             .build();
355     mockClientGatewaysService.addResponse(resultOperation);
356 
357     String parent = "parent-995424086";
358     ClientGateway clientGateway = ClientGateway.newBuilder().build();
359     String clientGatewayId = "clientGatewayId-1199632652";
360 
361     ClientGateway actualResponse =
362         client.createClientGatewayAsync(parent, clientGateway, clientGatewayId).get();
363     Assert.assertEquals(expectedResponse, actualResponse);
364 
365     List<AbstractMessage> actualRequests = mockClientGatewaysService.getRequests();
366     Assert.assertEquals(1, actualRequests.size());
367     CreateClientGatewayRequest actualRequest = ((CreateClientGatewayRequest) actualRequests.get(0));
368 
369     Assert.assertEquals(parent, actualRequest.getParent());
370     Assert.assertEquals(clientGateway, actualRequest.getClientGateway());
371     Assert.assertEquals(clientGatewayId, actualRequest.getClientGatewayId());
372     Assert.assertTrue(
373         channelProvider.isHeaderSent(
374             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
375             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
376   }
377 
378   @Test
createClientGatewayExceptionTest2()379   public void createClientGatewayExceptionTest2() throws Exception {
380     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
381     mockClientGatewaysService.addException(exception);
382 
383     try {
384       String parent = "parent-995424086";
385       ClientGateway clientGateway = ClientGateway.newBuilder().build();
386       String clientGatewayId = "clientGatewayId-1199632652";
387       client.createClientGatewayAsync(parent, clientGateway, clientGatewayId).get();
388       Assert.fail("No exception raised");
389     } catch (ExecutionException e) {
390       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
391       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
392       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
393     }
394   }
395 
396   @Test
deleteClientGatewayTest()397   public void deleteClientGatewayTest() throws Exception {
398     Empty expectedResponse = Empty.newBuilder().build();
399     Operation resultOperation =
400         Operation.newBuilder()
401             .setName("deleteClientGatewayTest")
402             .setDone(true)
403             .setResponse(Any.pack(expectedResponse))
404             .build();
405     mockClientGatewaysService.addResponse(resultOperation);
406 
407     ClientGatewayName name = ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]");
408 
409     client.deleteClientGatewayAsync(name).get();
410 
411     List<AbstractMessage> actualRequests = mockClientGatewaysService.getRequests();
412     Assert.assertEquals(1, actualRequests.size());
413     DeleteClientGatewayRequest actualRequest = ((DeleteClientGatewayRequest) actualRequests.get(0));
414 
415     Assert.assertEquals(name.toString(), actualRequest.getName());
416     Assert.assertTrue(
417         channelProvider.isHeaderSent(
418             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
419             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
420   }
421 
422   @Test
deleteClientGatewayExceptionTest()423   public void deleteClientGatewayExceptionTest() throws Exception {
424     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
425     mockClientGatewaysService.addException(exception);
426 
427     try {
428       ClientGatewayName name = ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]");
429       client.deleteClientGatewayAsync(name).get();
430       Assert.fail("No exception raised");
431     } catch (ExecutionException e) {
432       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
433       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
434       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
435     }
436   }
437 
438   @Test
deleteClientGatewayTest2()439   public void deleteClientGatewayTest2() throws Exception {
440     Empty expectedResponse = Empty.newBuilder().build();
441     Operation resultOperation =
442         Operation.newBuilder()
443             .setName("deleteClientGatewayTest")
444             .setDone(true)
445             .setResponse(Any.pack(expectedResponse))
446             .build();
447     mockClientGatewaysService.addResponse(resultOperation);
448 
449     String name = "name3373707";
450 
451     client.deleteClientGatewayAsync(name).get();
452 
453     List<AbstractMessage> actualRequests = mockClientGatewaysService.getRequests();
454     Assert.assertEquals(1, actualRequests.size());
455     DeleteClientGatewayRequest actualRequest = ((DeleteClientGatewayRequest) actualRequests.get(0));
456 
457     Assert.assertEquals(name, actualRequest.getName());
458     Assert.assertTrue(
459         channelProvider.isHeaderSent(
460             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
461             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
462   }
463 
464   @Test
deleteClientGatewayExceptionTest2()465   public void deleteClientGatewayExceptionTest2() throws Exception {
466     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
467     mockClientGatewaysService.addException(exception);
468 
469     try {
470       String name = "name3373707";
471       client.deleteClientGatewayAsync(name).get();
472       Assert.fail("No exception raised");
473     } catch (ExecutionException e) {
474       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
475       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
476       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
477     }
478   }
479 
480   @Test
listLocationsTest()481   public void listLocationsTest() throws Exception {
482     Location responsesElement = Location.newBuilder().build();
483     ListLocationsResponse expectedResponse =
484         ListLocationsResponse.newBuilder()
485             .setNextPageToken("")
486             .addAllLocations(Arrays.asList(responsesElement))
487             .build();
488     mockLocations.addResponse(expectedResponse);
489 
490     ListLocationsRequest request =
491         ListLocationsRequest.newBuilder()
492             .setName("name3373707")
493             .setFilter("filter-1274492040")
494             .setPageSize(883849137)
495             .setPageToken("pageToken873572522")
496             .build();
497 
498     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
499 
500     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
501 
502     Assert.assertEquals(1, resources.size());
503     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
504 
505     List<AbstractMessage> actualRequests = mockLocations.getRequests();
506     Assert.assertEquals(1, actualRequests.size());
507     ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0));
508 
509     Assert.assertEquals(request.getName(), actualRequest.getName());
510     Assert.assertEquals(request.getFilter(), actualRequest.getFilter());
511     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
512     Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
513     Assert.assertTrue(
514         channelProvider.isHeaderSent(
515             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
516             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
517   }
518 
519   @Test
listLocationsExceptionTest()520   public void listLocationsExceptionTest() throws Exception {
521     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
522     mockLocations.addException(exception);
523 
524     try {
525       ListLocationsRequest request =
526           ListLocationsRequest.newBuilder()
527               .setName("name3373707")
528               .setFilter("filter-1274492040")
529               .setPageSize(883849137)
530               .setPageToken("pageToken873572522")
531               .build();
532       client.listLocations(request);
533       Assert.fail("No exception raised");
534     } catch (InvalidArgumentException e) {
535       // Expected exception.
536     }
537   }
538 
539   @Test
getLocationTest()540   public void getLocationTest() throws Exception {
541     Location expectedResponse =
542         Location.newBuilder()
543             .setName("name3373707")
544             .setLocationId("locationId1541836720")
545             .setDisplayName("displayName1714148973")
546             .putAllLabels(new HashMap<String, String>())
547             .setMetadata(Any.newBuilder().build())
548             .build();
549     mockLocations.addResponse(expectedResponse);
550 
551     GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
552 
553     Location actualResponse = client.getLocation(request);
554     Assert.assertEquals(expectedResponse, actualResponse);
555 
556     List<AbstractMessage> actualRequests = mockLocations.getRequests();
557     Assert.assertEquals(1, actualRequests.size());
558     GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0));
559 
560     Assert.assertEquals(request.getName(), actualRequest.getName());
561     Assert.assertTrue(
562         channelProvider.isHeaderSent(
563             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
564             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
565   }
566 
567   @Test
getLocationExceptionTest()568   public void getLocationExceptionTest() throws Exception {
569     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
570     mockLocations.addException(exception);
571 
572     try {
573       GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
574       client.getLocation(request);
575       Assert.fail("No exception raised");
576     } catch (InvalidArgumentException e) {
577       // Expected exception.
578     }
579   }
580 
581   @Test
setIamPolicyTest()582   public void setIamPolicyTest() throws Exception {
583     Policy expectedResponse =
584         Policy.newBuilder()
585             .setVersion(351608024)
586             .addAllBindings(new ArrayList<Binding>())
587             .addAllAuditConfigs(new ArrayList<AuditConfig>())
588             .setEtag(ByteString.EMPTY)
589             .build();
590     mockIAMPolicy.addResponse(expectedResponse);
591 
592     SetIamPolicyRequest request =
593         SetIamPolicyRequest.newBuilder()
594             .setResource(
595                 ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
596             .setPolicy(Policy.newBuilder().build())
597             .setUpdateMask(FieldMask.newBuilder().build())
598             .build();
599 
600     Policy actualResponse = client.setIamPolicy(request);
601     Assert.assertEquals(expectedResponse, actualResponse);
602 
603     List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests();
604     Assert.assertEquals(1, actualRequests.size());
605     SetIamPolicyRequest actualRequest = ((SetIamPolicyRequest) actualRequests.get(0));
606 
607     Assert.assertEquals(request.getResource(), actualRequest.getResource());
608     Assert.assertEquals(request.getPolicy(), actualRequest.getPolicy());
609     Assert.assertEquals(request.getUpdateMask(), actualRequest.getUpdateMask());
610     Assert.assertTrue(
611         channelProvider.isHeaderSent(
612             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
613             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
614   }
615 
616   @Test
setIamPolicyExceptionTest()617   public void setIamPolicyExceptionTest() throws Exception {
618     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
619     mockIAMPolicy.addException(exception);
620 
621     try {
622       SetIamPolicyRequest request =
623           SetIamPolicyRequest.newBuilder()
624               .setResource(
625                   ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
626               .setPolicy(Policy.newBuilder().build())
627               .setUpdateMask(FieldMask.newBuilder().build())
628               .build();
629       client.setIamPolicy(request);
630       Assert.fail("No exception raised");
631     } catch (InvalidArgumentException e) {
632       // Expected exception.
633     }
634   }
635 
636   @Test
getIamPolicyTest()637   public void getIamPolicyTest() throws Exception {
638     Policy expectedResponse =
639         Policy.newBuilder()
640             .setVersion(351608024)
641             .addAllBindings(new ArrayList<Binding>())
642             .addAllAuditConfigs(new ArrayList<AuditConfig>())
643             .setEtag(ByteString.EMPTY)
644             .build();
645     mockIAMPolicy.addResponse(expectedResponse);
646 
647     GetIamPolicyRequest request =
648         GetIamPolicyRequest.newBuilder()
649             .setResource(
650                 ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
651             .setOptions(GetPolicyOptions.newBuilder().build())
652             .build();
653 
654     Policy actualResponse = client.getIamPolicy(request);
655     Assert.assertEquals(expectedResponse, actualResponse);
656 
657     List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests();
658     Assert.assertEquals(1, actualRequests.size());
659     GetIamPolicyRequest actualRequest = ((GetIamPolicyRequest) actualRequests.get(0));
660 
661     Assert.assertEquals(request.getResource(), actualRequest.getResource());
662     Assert.assertEquals(request.getOptions(), actualRequest.getOptions());
663     Assert.assertTrue(
664         channelProvider.isHeaderSent(
665             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
666             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
667   }
668 
669   @Test
getIamPolicyExceptionTest()670   public void getIamPolicyExceptionTest() throws Exception {
671     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
672     mockIAMPolicy.addException(exception);
673 
674     try {
675       GetIamPolicyRequest request =
676           GetIamPolicyRequest.newBuilder()
677               .setResource(
678                   ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
679               .setOptions(GetPolicyOptions.newBuilder().build())
680               .build();
681       client.getIamPolicy(request);
682       Assert.fail("No exception raised");
683     } catch (InvalidArgumentException e) {
684       // Expected exception.
685     }
686   }
687 
688   @Test
testIamPermissionsTest()689   public void testIamPermissionsTest() throws Exception {
690     TestIamPermissionsResponse expectedResponse =
691         TestIamPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build();
692     mockIAMPolicy.addResponse(expectedResponse);
693 
694     TestIamPermissionsRequest request =
695         TestIamPermissionsRequest.newBuilder()
696             .setResource(
697                 ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
698             .addAllPermissions(new ArrayList<String>())
699             .build();
700 
701     TestIamPermissionsResponse actualResponse = client.testIamPermissions(request);
702     Assert.assertEquals(expectedResponse, actualResponse);
703 
704     List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests();
705     Assert.assertEquals(1, actualRequests.size());
706     TestIamPermissionsRequest actualRequest = ((TestIamPermissionsRequest) actualRequests.get(0));
707 
708     Assert.assertEquals(request.getResource(), actualRequest.getResource());
709     Assert.assertEquals(request.getPermissionsList(), actualRequest.getPermissionsList());
710     Assert.assertTrue(
711         channelProvider.isHeaderSent(
712             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
713             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
714   }
715 
716   @Test
testIamPermissionsExceptionTest()717   public void testIamPermissionsExceptionTest() throws Exception {
718     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
719     mockIAMPolicy.addException(exception);
720 
721     try {
722       TestIamPermissionsRequest request =
723           TestIamPermissionsRequest.newBuilder()
724               .setResource(
725                   ClientGatewayName.of("[PROJECT]", "[LOCATION]", "[CLIENT_GATEWAY]").toString())
726               .addAllPermissions(new ArrayList<String>())
727               .build();
728       client.testIamPermissions(request);
729       Assert.fail("No exception raised");
730     } catch (InvalidArgumentException e) {
731       // Expected exception.
732     }
733   }
734 }
735