• 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.servicedirectory.v1;
18 
19 import static com.google.cloud.servicedirectory.v1.RegistrationServiceClient.ListEndpointsPagedResponse;
20 import static com.google.cloud.servicedirectory.v1.RegistrationServiceClient.ListNamespacesPagedResponse;
21 import static com.google.cloud.servicedirectory.v1.RegistrationServiceClient.ListServicesPagedResponse;
22 
23 import com.google.api.gax.core.NoCredentialsProvider;
24 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
25 import com.google.api.gax.httpjson.testing.MockHttpService;
26 import com.google.api.gax.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.ApiException;
28 import com.google.api.gax.rpc.ApiExceptionFactory;
29 import com.google.api.gax.rpc.InvalidArgumentException;
30 import com.google.api.gax.rpc.StatusCode;
31 import com.google.api.gax.rpc.testing.FakeStatusCode;
32 import com.google.cloud.servicedirectory.v1.stub.HttpJsonRegistrationServiceStub;
33 import com.google.common.collect.Lists;
34 import com.google.iam.v1.AuditConfig;
35 import com.google.iam.v1.Binding;
36 import com.google.iam.v1.GetIamPolicyRequest;
37 import com.google.iam.v1.GetPolicyOptions;
38 import com.google.iam.v1.Policy;
39 import com.google.iam.v1.SetIamPolicyRequest;
40 import com.google.iam.v1.TestIamPermissionsRequest;
41 import com.google.iam.v1.TestIamPermissionsResponse;
42 import com.google.protobuf.ByteString;
43 import com.google.protobuf.Empty;
44 import com.google.protobuf.FieldMask;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.HashMap;
49 import java.util.List;
50 import javax.annotation.Generated;
51 import org.junit.After;
52 import org.junit.AfterClass;
53 import org.junit.Assert;
54 import org.junit.Before;
55 import org.junit.BeforeClass;
56 import org.junit.Test;
57 
58 @Generated("by gapic-generator-java")
59 public class RegistrationServiceClientHttpJsonTest {
60   private static MockHttpService mockService;
61   private static RegistrationServiceClient client;
62 
63   @BeforeClass
startStaticServer()64   public static void startStaticServer() throws IOException {
65     mockService =
66         new MockHttpService(
67             HttpJsonRegistrationServiceStub.getMethodDescriptors(),
68             RegistrationServiceSettings.getDefaultEndpoint());
69     RegistrationServiceSettings settings =
70         RegistrationServiceSettings.newHttpJsonBuilder()
71             .setTransportChannelProvider(
72                 RegistrationServiceSettings.defaultHttpJsonTransportProviderBuilder()
73                     .setHttpTransport(mockService)
74                     .build())
75             .setCredentialsProvider(NoCredentialsProvider.create())
76             .build();
77     client = RegistrationServiceClient.create(settings);
78   }
79 
80   @AfterClass
stopServer()81   public static void stopServer() {
82     client.close();
83   }
84 
85   @Before
setUp()86   public void setUp() {}
87 
88   @After
tearDown()89   public void tearDown() throws Exception {
90     mockService.reset();
91   }
92 
93   @Test
createNamespaceTest()94   public void createNamespaceTest() throws Exception {
95     Namespace expectedResponse =
96         Namespace.newBuilder()
97             .setName(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
98             .putAllLabels(new HashMap<String, String>())
99             .build();
100     mockService.addResponse(expectedResponse);
101 
102     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
103     Namespace namespace = Namespace.newBuilder().build();
104     String namespaceId = "namespaceId790852566";
105 
106     Namespace actualResponse = client.createNamespace(parent, namespace, namespaceId);
107     Assert.assertEquals(expectedResponse, actualResponse);
108 
109     List<String> actualRequests = mockService.getRequestPaths();
110     Assert.assertEquals(1, actualRequests.size());
111 
112     String apiClientHeaderKey =
113         mockService
114             .getRequestHeaders()
115             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
116             .iterator()
117             .next();
118     Assert.assertTrue(
119         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
120             .matcher(apiClientHeaderKey)
121             .matches());
122   }
123 
124   @Test
createNamespaceExceptionTest()125   public void createNamespaceExceptionTest() throws Exception {
126     ApiException exception =
127         ApiExceptionFactory.createException(
128             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
129     mockService.addException(exception);
130 
131     try {
132       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
133       Namespace namespace = Namespace.newBuilder().build();
134       String namespaceId = "namespaceId790852566";
135       client.createNamespace(parent, namespace, namespaceId);
136       Assert.fail("No exception raised");
137     } catch (InvalidArgumentException e) {
138       // Expected exception.
139     }
140   }
141 
142   @Test
createNamespaceTest2()143   public void createNamespaceTest2() throws Exception {
144     Namespace expectedResponse =
145         Namespace.newBuilder()
146             .setName(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
147             .putAllLabels(new HashMap<String, String>())
148             .build();
149     mockService.addResponse(expectedResponse);
150 
151     String parent = "projects/project-5833/locations/location-5833";
152     Namespace namespace = Namespace.newBuilder().build();
153     String namespaceId = "namespaceId790852566";
154 
155     Namespace actualResponse = client.createNamespace(parent, namespace, namespaceId);
156     Assert.assertEquals(expectedResponse, actualResponse);
157 
158     List<String> actualRequests = mockService.getRequestPaths();
159     Assert.assertEquals(1, actualRequests.size());
160 
161     String apiClientHeaderKey =
162         mockService
163             .getRequestHeaders()
164             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
165             .iterator()
166             .next();
167     Assert.assertTrue(
168         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
169             .matcher(apiClientHeaderKey)
170             .matches());
171   }
172 
173   @Test
createNamespaceExceptionTest2()174   public void createNamespaceExceptionTest2() throws Exception {
175     ApiException exception =
176         ApiExceptionFactory.createException(
177             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
178     mockService.addException(exception);
179 
180     try {
181       String parent = "projects/project-5833/locations/location-5833";
182       Namespace namespace = Namespace.newBuilder().build();
183       String namespaceId = "namespaceId790852566";
184       client.createNamespace(parent, namespace, namespaceId);
185       Assert.fail("No exception raised");
186     } catch (InvalidArgumentException e) {
187       // Expected exception.
188     }
189   }
190 
191   @Test
listNamespacesTest()192   public void listNamespacesTest() throws Exception {
193     Namespace responsesElement = Namespace.newBuilder().build();
194     ListNamespacesResponse expectedResponse =
195         ListNamespacesResponse.newBuilder()
196             .setNextPageToken("")
197             .addAllNamespaces(Arrays.asList(responsesElement))
198             .build();
199     mockService.addResponse(expectedResponse);
200 
201     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
202 
203     ListNamespacesPagedResponse pagedListResponse = client.listNamespaces(parent);
204 
205     List<Namespace> resources = Lists.newArrayList(pagedListResponse.iterateAll());
206 
207     Assert.assertEquals(1, resources.size());
208     Assert.assertEquals(expectedResponse.getNamespacesList().get(0), resources.get(0));
209 
210     List<String> actualRequests = mockService.getRequestPaths();
211     Assert.assertEquals(1, actualRequests.size());
212 
213     String apiClientHeaderKey =
214         mockService
215             .getRequestHeaders()
216             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
217             .iterator()
218             .next();
219     Assert.assertTrue(
220         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
221             .matcher(apiClientHeaderKey)
222             .matches());
223   }
224 
225   @Test
listNamespacesExceptionTest()226   public void listNamespacesExceptionTest() throws Exception {
227     ApiException exception =
228         ApiExceptionFactory.createException(
229             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
230     mockService.addException(exception);
231 
232     try {
233       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
234       client.listNamespaces(parent);
235       Assert.fail("No exception raised");
236     } catch (InvalidArgumentException e) {
237       // Expected exception.
238     }
239   }
240 
241   @Test
listNamespacesTest2()242   public void listNamespacesTest2() throws Exception {
243     Namespace responsesElement = Namespace.newBuilder().build();
244     ListNamespacesResponse expectedResponse =
245         ListNamespacesResponse.newBuilder()
246             .setNextPageToken("")
247             .addAllNamespaces(Arrays.asList(responsesElement))
248             .build();
249     mockService.addResponse(expectedResponse);
250 
251     String parent = "projects/project-5833/locations/location-5833";
252 
253     ListNamespacesPagedResponse pagedListResponse = client.listNamespaces(parent);
254 
255     List<Namespace> resources = Lists.newArrayList(pagedListResponse.iterateAll());
256 
257     Assert.assertEquals(1, resources.size());
258     Assert.assertEquals(expectedResponse.getNamespacesList().get(0), resources.get(0));
259 
260     List<String> actualRequests = mockService.getRequestPaths();
261     Assert.assertEquals(1, actualRequests.size());
262 
263     String apiClientHeaderKey =
264         mockService
265             .getRequestHeaders()
266             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
267             .iterator()
268             .next();
269     Assert.assertTrue(
270         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
271             .matcher(apiClientHeaderKey)
272             .matches());
273   }
274 
275   @Test
listNamespacesExceptionTest2()276   public void listNamespacesExceptionTest2() throws Exception {
277     ApiException exception =
278         ApiExceptionFactory.createException(
279             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
280     mockService.addException(exception);
281 
282     try {
283       String parent = "projects/project-5833/locations/location-5833";
284       client.listNamespaces(parent);
285       Assert.fail("No exception raised");
286     } catch (InvalidArgumentException e) {
287       // Expected exception.
288     }
289   }
290 
291   @Test
getNamespaceTest()292   public void getNamespaceTest() throws Exception {
293     Namespace expectedResponse =
294         Namespace.newBuilder()
295             .setName(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
296             .putAllLabels(new HashMap<String, String>())
297             .build();
298     mockService.addResponse(expectedResponse);
299 
300     NamespaceName name = NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]");
301 
302     Namespace actualResponse = client.getNamespace(name);
303     Assert.assertEquals(expectedResponse, actualResponse);
304 
305     List<String> actualRequests = mockService.getRequestPaths();
306     Assert.assertEquals(1, actualRequests.size());
307 
308     String apiClientHeaderKey =
309         mockService
310             .getRequestHeaders()
311             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
312             .iterator()
313             .next();
314     Assert.assertTrue(
315         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
316             .matcher(apiClientHeaderKey)
317             .matches());
318   }
319 
320   @Test
getNamespaceExceptionTest()321   public void getNamespaceExceptionTest() throws Exception {
322     ApiException exception =
323         ApiExceptionFactory.createException(
324             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
325     mockService.addException(exception);
326 
327     try {
328       NamespaceName name = NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]");
329       client.getNamespace(name);
330       Assert.fail("No exception raised");
331     } catch (InvalidArgumentException e) {
332       // Expected exception.
333     }
334   }
335 
336   @Test
getNamespaceTest2()337   public void getNamespaceTest2() throws Exception {
338     Namespace expectedResponse =
339         Namespace.newBuilder()
340             .setName(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
341             .putAllLabels(new HashMap<String, String>())
342             .build();
343     mockService.addResponse(expectedResponse);
344 
345     String name = "projects/project-9720/locations/location-9720/namespaces/namespace-9720";
346 
347     Namespace actualResponse = client.getNamespace(name);
348     Assert.assertEquals(expectedResponse, actualResponse);
349 
350     List<String> actualRequests = mockService.getRequestPaths();
351     Assert.assertEquals(1, actualRequests.size());
352 
353     String apiClientHeaderKey =
354         mockService
355             .getRequestHeaders()
356             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
357             .iterator()
358             .next();
359     Assert.assertTrue(
360         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
361             .matcher(apiClientHeaderKey)
362             .matches());
363   }
364 
365   @Test
getNamespaceExceptionTest2()366   public void getNamespaceExceptionTest2() throws Exception {
367     ApiException exception =
368         ApiExceptionFactory.createException(
369             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
370     mockService.addException(exception);
371 
372     try {
373       String name = "projects/project-9720/locations/location-9720/namespaces/namespace-9720";
374       client.getNamespace(name);
375       Assert.fail("No exception raised");
376     } catch (InvalidArgumentException e) {
377       // Expected exception.
378     }
379   }
380 
381   @Test
updateNamespaceTest()382   public void updateNamespaceTest() throws Exception {
383     Namespace expectedResponse =
384         Namespace.newBuilder()
385             .setName(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
386             .putAllLabels(new HashMap<String, String>())
387             .build();
388     mockService.addResponse(expectedResponse);
389 
390     Namespace namespace =
391         Namespace.newBuilder()
392             .setName(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
393             .putAllLabels(new HashMap<String, String>())
394             .build();
395     FieldMask updateMask = FieldMask.newBuilder().build();
396 
397     Namespace actualResponse = client.updateNamespace(namespace, updateMask);
398     Assert.assertEquals(expectedResponse, actualResponse);
399 
400     List<String> actualRequests = mockService.getRequestPaths();
401     Assert.assertEquals(1, actualRequests.size());
402 
403     String apiClientHeaderKey =
404         mockService
405             .getRequestHeaders()
406             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
407             .iterator()
408             .next();
409     Assert.assertTrue(
410         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
411             .matcher(apiClientHeaderKey)
412             .matches());
413   }
414 
415   @Test
updateNamespaceExceptionTest()416   public void updateNamespaceExceptionTest() throws Exception {
417     ApiException exception =
418         ApiExceptionFactory.createException(
419             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
420     mockService.addException(exception);
421 
422     try {
423       Namespace namespace =
424           Namespace.newBuilder()
425               .setName(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
426               .putAllLabels(new HashMap<String, String>())
427               .build();
428       FieldMask updateMask = FieldMask.newBuilder().build();
429       client.updateNamespace(namespace, updateMask);
430       Assert.fail("No exception raised");
431     } catch (InvalidArgumentException e) {
432       // Expected exception.
433     }
434   }
435 
436   @Test
deleteNamespaceTest()437   public void deleteNamespaceTest() throws Exception {
438     Empty expectedResponse = Empty.newBuilder().build();
439     mockService.addResponse(expectedResponse);
440 
441     NamespaceName name = NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]");
442 
443     client.deleteNamespace(name);
444 
445     List<String> actualRequests = mockService.getRequestPaths();
446     Assert.assertEquals(1, actualRequests.size());
447 
448     String apiClientHeaderKey =
449         mockService
450             .getRequestHeaders()
451             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
452             .iterator()
453             .next();
454     Assert.assertTrue(
455         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
456             .matcher(apiClientHeaderKey)
457             .matches());
458   }
459 
460   @Test
deleteNamespaceExceptionTest()461   public void deleteNamespaceExceptionTest() throws Exception {
462     ApiException exception =
463         ApiExceptionFactory.createException(
464             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
465     mockService.addException(exception);
466 
467     try {
468       NamespaceName name = NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]");
469       client.deleteNamespace(name);
470       Assert.fail("No exception raised");
471     } catch (InvalidArgumentException e) {
472       // Expected exception.
473     }
474   }
475 
476   @Test
deleteNamespaceTest2()477   public void deleteNamespaceTest2() throws Exception {
478     Empty expectedResponse = Empty.newBuilder().build();
479     mockService.addResponse(expectedResponse);
480 
481     String name = "projects/project-9720/locations/location-9720/namespaces/namespace-9720";
482 
483     client.deleteNamespace(name);
484 
485     List<String> actualRequests = mockService.getRequestPaths();
486     Assert.assertEquals(1, actualRequests.size());
487 
488     String apiClientHeaderKey =
489         mockService
490             .getRequestHeaders()
491             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
492             .iterator()
493             .next();
494     Assert.assertTrue(
495         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
496             .matcher(apiClientHeaderKey)
497             .matches());
498   }
499 
500   @Test
deleteNamespaceExceptionTest2()501   public void deleteNamespaceExceptionTest2() throws Exception {
502     ApiException exception =
503         ApiExceptionFactory.createException(
504             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
505     mockService.addException(exception);
506 
507     try {
508       String name = "projects/project-9720/locations/location-9720/namespaces/namespace-9720";
509       client.deleteNamespace(name);
510       Assert.fail("No exception raised");
511     } catch (InvalidArgumentException e) {
512       // Expected exception.
513     }
514   }
515 
516   @Test
createServiceTest()517   public void createServiceTest() throws Exception {
518     Service expectedResponse =
519         Service.newBuilder()
520             .setName(
521                 ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString())
522             .putAllAnnotations(new HashMap<String, String>())
523             .addAllEndpoints(new ArrayList<Endpoint>())
524             .build();
525     mockService.addResponse(expectedResponse);
526 
527     NamespaceName parent = NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]");
528     Service service = Service.newBuilder().build();
529     String serviceId = "serviceId-194185552";
530 
531     Service actualResponse = client.createService(parent, service, serviceId);
532     Assert.assertEquals(expectedResponse, actualResponse);
533 
534     List<String> actualRequests = mockService.getRequestPaths();
535     Assert.assertEquals(1, actualRequests.size());
536 
537     String apiClientHeaderKey =
538         mockService
539             .getRequestHeaders()
540             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
541             .iterator()
542             .next();
543     Assert.assertTrue(
544         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
545             .matcher(apiClientHeaderKey)
546             .matches());
547   }
548 
549   @Test
createServiceExceptionTest()550   public void createServiceExceptionTest() throws Exception {
551     ApiException exception =
552         ApiExceptionFactory.createException(
553             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
554     mockService.addException(exception);
555 
556     try {
557       NamespaceName parent = NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]");
558       Service service = Service.newBuilder().build();
559       String serviceId = "serviceId-194185552";
560       client.createService(parent, service, serviceId);
561       Assert.fail("No exception raised");
562     } catch (InvalidArgumentException e) {
563       // Expected exception.
564     }
565   }
566 
567   @Test
createServiceTest2()568   public void createServiceTest2() throws Exception {
569     Service expectedResponse =
570         Service.newBuilder()
571             .setName(
572                 ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString())
573             .putAllAnnotations(new HashMap<String, String>())
574             .addAllEndpoints(new ArrayList<Endpoint>())
575             .build();
576     mockService.addResponse(expectedResponse);
577 
578     String parent = "projects/project-1689/locations/location-1689/namespaces/namespace-1689";
579     Service service = Service.newBuilder().build();
580     String serviceId = "serviceId-194185552";
581 
582     Service actualResponse = client.createService(parent, service, serviceId);
583     Assert.assertEquals(expectedResponse, actualResponse);
584 
585     List<String> actualRequests = mockService.getRequestPaths();
586     Assert.assertEquals(1, actualRequests.size());
587 
588     String apiClientHeaderKey =
589         mockService
590             .getRequestHeaders()
591             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
592             .iterator()
593             .next();
594     Assert.assertTrue(
595         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
596             .matcher(apiClientHeaderKey)
597             .matches());
598   }
599 
600   @Test
createServiceExceptionTest2()601   public void createServiceExceptionTest2() throws Exception {
602     ApiException exception =
603         ApiExceptionFactory.createException(
604             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
605     mockService.addException(exception);
606 
607     try {
608       String parent = "projects/project-1689/locations/location-1689/namespaces/namespace-1689";
609       Service service = Service.newBuilder().build();
610       String serviceId = "serviceId-194185552";
611       client.createService(parent, service, serviceId);
612       Assert.fail("No exception raised");
613     } catch (InvalidArgumentException e) {
614       // Expected exception.
615     }
616   }
617 
618   @Test
listServicesTest()619   public void listServicesTest() throws Exception {
620     Service responsesElement = Service.newBuilder().build();
621     ListServicesResponse expectedResponse =
622         ListServicesResponse.newBuilder()
623             .setNextPageToken("")
624             .addAllServices(Arrays.asList(responsesElement))
625             .build();
626     mockService.addResponse(expectedResponse);
627 
628     NamespaceName parent = NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]");
629 
630     ListServicesPagedResponse pagedListResponse = client.listServices(parent);
631 
632     List<Service> resources = Lists.newArrayList(pagedListResponse.iterateAll());
633 
634     Assert.assertEquals(1, resources.size());
635     Assert.assertEquals(expectedResponse.getServicesList().get(0), resources.get(0));
636 
637     List<String> actualRequests = mockService.getRequestPaths();
638     Assert.assertEquals(1, actualRequests.size());
639 
640     String apiClientHeaderKey =
641         mockService
642             .getRequestHeaders()
643             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
644             .iterator()
645             .next();
646     Assert.assertTrue(
647         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
648             .matcher(apiClientHeaderKey)
649             .matches());
650   }
651 
652   @Test
listServicesExceptionTest()653   public void listServicesExceptionTest() throws Exception {
654     ApiException exception =
655         ApiExceptionFactory.createException(
656             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
657     mockService.addException(exception);
658 
659     try {
660       NamespaceName parent = NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]");
661       client.listServices(parent);
662       Assert.fail("No exception raised");
663     } catch (InvalidArgumentException e) {
664       // Expected exception.
665     }
666   }
667 
668   @Test
listServicesTest2()669   public void listServicesTest2() throws Exception {
670     Service responsesElement = Service.newBuilder().build();
671     ListServicesResponse expectedResponse =
672         ListServicesResponse.newBuilder()
673             .setNextPageToken("")
674             .addAllServices(Arrays.asList(responsesElement))
675             .build();
676     mockService.addResponse(expectedResponse);
677 
678     String parent = "projects/project-1689/locations/location-1689/namespaces/namespace-1689";
679 
680     ListServicesPagedResponse pagedListResponse = client.listServices(parent);
681 
682     List<Service> resources = Lists.newArrayList(pagedListResponse.iterateAll());
683 
684     Assert.assertEquals(1, resources.size());
685     Assert.assertEquals(expectedResponse.getServicesList().get(0), resources.get(0));
686 
687     List<String> actualRequests = mockService.getRequestPaths();
688     Assert.assertEquals(1, actualRequests.size());
689 
690     String apiClientHeaderKey =
691         mockService
692             .getRequestHeaders()
693             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
694             .iterator()
695             .next();
696     Assert.assertTrue(
697         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
698             .matcher(apiClientHeaderKey)
699             .matches());
700   }
701 
702   @Test
listServicesExceptionTest2()703   public void listServicesExceptionTest2() throws Exception {
704     ApiException exception =
705         ApiExceptionFactory.createException(
706             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
707     mockService.addException(exception);
708 
709     try {
710       String parent = "projects/project-1689/locations/location-1689/namespaces/namespace-1689";
711       client.listServices(parent);
712       Assert.fail("No exception raised");
713     } catch (InvalidArgumentException e) {
714       // Expected exception.
715     }
716   }
717 
718   @Test
getServiceTest()719   public void getServiceTest() throws Exception {
720     Service expectedResponse =
721         Service.newBuilder()
722             .setName(
723                 ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString())
724             .putAllAnnotations(new HashMap<String, String>())
725             .addAllEndpoints(new ArrayList<Endpoint>())
726             .build();
727     mockService.addResponse(expectedResponse);
728 
729     ServiceName name = ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]");
730 
731     Service actualResponse = client.getService(name);
732     Assert.assertEquals(expectedResponse, actualResponse);
733 
734     List<String> actualRequests = mockService.getRequestPaths();
735     Assert.assertEquals(1, actualRequests.size());
736 
737     String apiClientHeaderKey =
738         mockService
739             .getRequestHeaders()
740             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
741             .iterator()
742             .next();
743     Assert.assertTrue(
744         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
745             .matcher(apiClientHeaderKey)
746             .matches());
747   }
748 
749   @Test
getServiceExceptionTest()750   public void getServiceExceptionTest() throws Exception {
751     ApiException exception =
752         ApiExceptionFactory.createException(
753             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
754     mockService.addException(exception);
755 
756     try {
757       ServiceName name = ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]");
758       client.getService(name);
759       Assert.fail("No exception raised");
760     } catch (InvalidArgumentException e) {
761       // Expected exception.
762     }
763   }
764 
765   @Test
getServiceTest2()766   public void getServiceTest2() throws Exception {
767     Service expectedResponse =
768         Service.newBuilder()
769             .setName(
770                 ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString())
771             .putAllAnnotations(new HashMap<String, String>())
772             .addAllEndpoints(new ArrayList<Endpoint>())
773             .build();
774     mockService.addResponse(expectedResponse);
775 
776     String name =
777         "projects/project-160/locations/location-160/namespaces/namespace-160/services/service-160";
778 
779     Service actualResponse = client.getService(name);
780     Assert.assertEquals(expectedResponse, actualResponse);
781 
782     List<String> actualRequests = mockService.getRequestPaths();
783     Assert.assertEquals(1, actualRequests.size());
784 
785     String apiClientHeaderKey =
786         mockService
787             .getRequestHeaders()
788             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
789             .iterator()
790             .next();
791     Assert.assertTrue(
792         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
793             .matcher(apiClientHeaderKey)
794             .matches());
795   }
796 
797   @Test
getServiceExceptionTest2()798   public void getServiceExceptionTest2() throws Exception {
799     ApiException exception =
800         ApiExceptionFactory.createException(
801             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
802     mockService.addException(exception);
803 
804     try {
805       String name =
806           "projects/project-160/locations/location-160/namespaces/namespace-160/services/service-160";
807       client.getService(name);
808       Assert.fail("No exception raised");
809     } catch (InvalidArgumentException e) {
810       // Expected exception.
811     }
812   }
813 
814   @Test
updateServiceTest()815   public void updateServiceTest() throws Exception {
816     Service expectedResponse =
817         Service.newBuilder()
818             .setName(
819                 ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString())
820             .putAllAnnotations(new HashMap<String, String>())
821             .addAllEndpoints(new ArrayList<Endpoint>())
822             .build();
823     mockService.addResponse(expectedResponse);
824 
825     Service service =
826         Service.newBuilder()
827             .setName(
828                 ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString())
829             .putAllAnnotations(new HashMap<String, String>())
830             .addAllEndpoints(new ArrayList<Endpoint>())
831             .build();
832     FieldMask updateMask = FieldMask.newBuilder().build();
833 
834     Service actualResponse = client.updateService(service, updateMask);
835     Assert.assertEquals(expectedResponse, actualResponse);
836 
837     List<String> actualRequests = mockService.getRequestPaths();
838     Assert.assertEquals(1, actualRequests.size());
839 
840     String apiClientHeaderKey =
841         mockService
842             .getRequestHeaders()
843             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
844             .iterator()
845             .next();
846     Assert.assertTrue(
847         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
848             .matcher(apiClientHeaderKey)
849             .matches());
850   }
851 
852   @Test
updateServiceExceptionTest()853   public void updateServiceExceptionTest() throws Exception {
854     ApiException exception =
855         ApiExceptionFactory.createException(
856             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
857     mockService.addException(exception);
858 
859     try {
860       Service service =
861           Service.newBuilder()
862               .setName(
863                   ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString())
864               .putAllAnnotations(new HashMap<String, String>())
865               .addAllEndpoints(new ArrayList<Endpoint>())
866               .build();
867       FieldMask updateMask = FieldMask.newBuilder().build();
868       client.updateService(service, updateMask);
869       Assert.fail("No exception raised");
870     } catch (InvalidArgumentException e) {
871       // Expected exception.
872     }
873   }
874 
875   @Test
deleteServiceTest()876   public void deleteServiceTest() throws Exception {
877     Empty expectedResponse = Empty.newBuilder().build();
878     mockService.addResponse(expectedResponse);
879 
880     ServiceName name = ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]");
881 
882     client.deleteService(name);
883 
884     List<String> actualRequests = mockService.getRequestPaths();
885     Assert.assertEquals(1, actualRequests.size());
886 
887     String apiClientHeaderKey =
888         mockService
889             .getRequestHeaders()
890             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
891             .iterator()
892             .next();
893     Assert.assertTrue(
894         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
895             .matcher(apiClientHeaderKey)
896             .matches());
897   }
898 
899   @Test
deleteServiceExceptionTest()900   public void deleteServiceExceptionTest() throws Exception {
901     ApiException exception =
902         ApiExceptionFactory.createException(
903             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
904     mockService.addException(exception);
905 
906     try {
907       ServiceName name = ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]");
908       client.deleteService(name);
909       Assert.fail("No exception raised");
910     } catch (InvalidArgumentException e) {
911       // Expected exception.
912     }
913   }
914 
915   @Test
deleteServiceTest2()916   public void deleteServiceTest2() throws Exception {
917     Empty expectedResponse = Empty.newBuilder().build();
918     mockService.addResponse(expectedResponse);
919 
920     String name =
921         "projects/project-160/locations/location-160/namespaces/namespace-160/services/service-160";
922 
923     client.deleteService(name);
924 
925     List<String> actualRequests = mockService.getRequestPaths();
926     Assert.assertEquals(1, actualRequests.size());
927 
928     String apiClientHeaderKey =
929         mockService
930             .getRequestHeaders()
931             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
932             .iterator()
933             .next();
934     Assert.assertTrue(
935         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
936             .matcher(apiClientHeaderKey)
937             .matches());
938   }
939 
940   @Test
deleteServiceExceptionTest2()941   public void deleteServiceExceptionTest2() throws Exception {
942     ApiException exception =
943         ApiExceptionFactory.createException(
944             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
945     mockService.addException(exception);
946 
947     try {
948       String name =
949           "projects/project-160/locations/location-160/namespaces/namespace-160/services/service-160";
950       client.deleteService(name);
951       Assert.fail("No exception raised");
952     } catch (InvalidArgumentException e) {
953       // Expected exception.
954     }
955   }
956 
957   @Test
createEndpointTest()958   public void createEndpointTest() throws Exception {
959     Endpoint expectedResponse =
960         Endpoint.newBuilder()
961             .setName(
962                 EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]")
963                     .toString())
964             .setAddress("address-1147692044")
965             .setPort(3446913)
966             .putAllAnnotations(new HashMap<String, String>())
967             .build();
968     mockService.addResponse(expectedResponse);
969 
970     ServiceName parent = ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]");
971     Endpoint endpoint = Endpoint.newBuilder().build();
972     String endpointId = "endpointId-1837754992";
973 
974     Endpoint actualResponse = client.createEndpoint(parent, endpoint, endpointId);
975     Assert.assertEquals(expectedResponse, actualResponse);
976 
977     List<String> actualRequests = mockService.getRequestPaths();
978     Assert.assertEquals(1, actualRequests.size());
979 
980     String apiClientHeaderKey =
981         mockService
982             .getRequestHeaders()
983             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
984             .iterator()
985             .next();
986     Assert.assertTrue(
987         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
988             .matcher(apiClientHeaderKey)
989             .matches());
990   }
991 
992   @Test
createEndpointExceptionTest()993   public void createEndpointExceptionTest() throws Exception {
994     ApiException exception =
995         ApiExceptionFactory.createException(
996             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
997     mockService.addException(exception);
998 
999     try {
1000       ServiceName parent = ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]");
1001       Endpoint endpoint = Endpoint.newBuilder().build();
1002       String endpointId = "endpointId-1837754992";
1003       client.createEndpoint(parent, endpoint, endpointId);
1004       Assert.fail("No exception raised");
1005     } catch (InvalidArgumentException e) {
1006       // Expected exception.
1007     }
1008   }
1009 
1010   @Test
createEndpointTest2()1011   public void createEndpointTest2() throws Exception {
1012     Endpoint expectedResponse =
1013         Endpoint.newBuilder()
1014             .setName(
1015                 EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]")
1016                     .toString())
1017             .setAddress("address-1147692044")
1018             .setPort(3446913)
1019             .putAllAnnotations(new HashMap<String, String>())
1020             .build();
1021     mockService.addResponse(expectedResponse);
1022 
1023     String parent =
1024         "projects/project-4575/locations/location-4575/namespaces/namespace-4575/services/service-4575";
1025     Endpoint endpoint = Endpoint.newBuilder().build();
1026     String endpointId = "endpointId-1837754992";
1027 
1028     Endpoint actualResponse = client.createEndpoint(parent, endpoint, endpointId);
1029     Assert.assertEquals(expectedResponse, actualResponse);
1030 
1031     List<String> actualRequests = mockService.getRequestPaths();
1032     Assert.assertEquals(1, actualRequests.size());
1033 
1034     String apiClientHeaderKey =
1035         mockService
1036             .getRequestHeaders()
1037             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1038             .iterator()
1039             .next();
1040     Assert.assertTrue(
1041         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1042             .matcher(apiClientHeaderKey)
1043             .matches());
1044   }
1045 
1046   @Test
createEndpointExceptionTest2()1047   public void createEndpointExceptionTest2() throws Exception {
1048     ApiException exception =
1049         ApiExceptionFactory.createException(
1050             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1051     mockService.addException(exception);
1052 
1053     try {
1054       String parent =
1055           "projects/project-4575/locations/location-4575/namespaces/namespace-4575/services/service-4575";
1056       Endpoint endpoint = Endpoint.newBuilder().build();
1057       String endpointId = "endpointId-1837754992";
1058       client.createEndpoint(parent, endpoint, endpointId);
1059       Assert.fail("No exception raised");
1060     } catch (InvalidArgumentException e) {
1061       // Expected exception.
1062     }
1063   }
1064 
1065   @Test
listEndpointsTest()1066   public void listEndpointsTest() throws Exception {
1067     Endpoint responsesElement = Endpoint.newBuilder().build();
1068     ListEndpointsResponse expectedResponse =
1069         ListEndpointsResponse.newBuilder()
1070             .setNextPageToken("")
1071             .addAllEndpoints(Arrays.asList(responsesElement))
1072             .build();
1073     mockService.addResponse(expectedResponse);
1074 
1075     ServiceName parent = ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]");
1076 
1077     ListEndpointsPagedResponse pagedListResponse = client.listEndpoints(parent);
1078 
1079     List<Endpoint> resources = Lists.newArrayList(pagedListResponse.iterateAll());
1080 
1081     Assert.assertEquals(1, resources.size());
1082     Assert.assertEquals(expectedResponse.getEndpointsList().get(0), resources.get(0));
1083 
1084     List<String> actualRequests = mockService.getRequestPaths();
1085     Assert.assertEquals(1, actualRequests.size());
1086 
1087     String apiClientHeaderKey =
1088         mockService
1089             .getRequestHeaders()
1090             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1091             .iterator()
1092             .next();
1093     Assert.assertTrue(
1094         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1095             .matcher(apiClientHeaderKey)
1096             .matches());
1097   }
1098 
1099   @Test
listEndpointsExceptionTest()1100   public void listEndpointsExceptionTest() throws Exception {
1101     ApiException exception =
1102         ApiExceptionFactory.createException(
1103             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1104     mockService.addException(exception);
1105 
1106     try {
1107       ServiceName parent = ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]");
1108       client.listEndpoints(parent);
1109       Assert.fail("No exception raised");
1110     } catch (InvalidArgumentException e) {
1111       // Expected exception.
1112     }
1113   }
1114 
1115   @Test
listEndpointsTest2()1116   public void listEndpointsTest2() throws Exception {
1117     Endpoint responsesElement = Endpoint.newBuilder().build();
1118     ListEndpointsResponse expectedResponse =
1119         ListEndpointsResponse.newBuilder()
1120             .setNextPageToken("")
1121             .addAllEndpoints(Arrays.asList(responsesElement))
1122             .build();
1123     mockService.addResponse(expectedResponse);
1124 
1125     String parent =
1126         "projects/project-4575/locations/location-4575/namespaces/namespace-4575/services/service-4575";
1127 
1128     ListEndpointsPagedResponse pagedListResponse = client.listEndpoints(parent);
1129 
1130     List<Endpoint> resources = Lists.newArrayList(pagedListResponse.iterateAll());
1131 
1132     Assert.assertEquals(1, resources.size());
1133     Assert.assertEquals(expectedResponse.getEndpointsList().get(0), resources.get(0));
1134 
1135     List<String> actualRequests = mockService.getRequestPaths();
1136     Assert.assertEquals(1, actualRequests.size());
1137 
1138     String apiClientHeaderKey =
1139         mockService
1140             .getRequestHeaders()
1141             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1142             .iterator()
1143             .next();
1144     Assert.assertTrue(
1145         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1146             .matcher(apiClientHeaderKey)
1147             .matches());
1148   }
1149 
1150   @Test
listEndpointsExceptionTest2()1151   public void listEndpointsExceptionTest2() throws Exception {
1152     ApiException exception =
1153         ApiExceptionFactory.createException(
1154             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1155     mockService.addException(exception);
1156 
1157     try {
1158       String parent =
1159           "projects/project-4575/locations/location-4575/namespaces/namespace-4575/services/service-4575";
1160       client.listEndpoints(parent);
1161       Assert.fail("No exception raised");
1162     } catch (InvalidArgumentException e) {
1163       // Expected exception.
1164     }
1165   }
1166 
1167   @Test
getEndpointTest()1168   public void getEndpointTest() throws Exception {
1169     Endpoint expectedResponse =
1170         Endpoint.newBuilder()
1171             .setName(
1172                 EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]")
1173                     .toString())
1174             .setAddress("address-1147692044")
1175             .setPort(3446913)
1176             .putAllAnnotations(new HashMap<String, String>())
1177             .build();
1178     mockService.addResponse(expectedResponse);
1179 
1180     EndpointName name =
1181         EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]");
1182 
1183     Endpoint actualResponse = client.getEndpoint(name);
1184     Assert.assertEquals(expectedResponse, actualResponse);
1185 
1186     List<String> actualRequests = mockService.getRequestPaths();
1187     Assert.assertEquals(1, actualRequests.size());
1188 
1189     String apiClientHeaderKey =
1190         mockService
1191             .getRequestHeaders()
1192             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1193             .iterator()
1194             .next();
1195     Assert.assertTrue(
1196         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1197             .matcher(apiClientHeaderKey)
1198             .matches());
1199   }
1200 
1201   @Test
getEndpointExceptionTest()1202   public void getEndpointExceptionTest() throws Exception {
1203     ApiException exception =
1204         ApiExceptionFactory.createException(
1205             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1206     mockService.addException(exception);
1207 
1208     try {
1209       EndpointName name =
1210           EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]");
1211       client.getEndpoint(name);
1212       Assert.fail("No exception raised");
1213     } catch (InvalidArgumentException e) {
1214       // Expected exception.
1215     }
1216   }
1217 
1218   @Test
getEndpointTest2()1219   public void getEndpointTest2() throws Exception {
1220     Endpoint expectedResponse =
1221         Endpoint.newBuilder()
1222             .setName(
1223                 EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]")
1224                     .toString())
1225             .setAddress("address-1147692044")
1226             .setPort(3446913)
1227             .putAllAnnotations(new HashMap<String, String>())
1228             .build();
1229     mockService.addResponse(expectedResponse);
1230 
1231     String name =
1232         "projects/project-9594/locations/location-9594/namespaces/namespace-9594/services/service-9594/endpoints/endpoint-9594";
1233 
1234     Endpoint actualResponse = client.getEndpoint(name);
1235     Assert.assertEquals(expectedResponse, actualResponse);
1236 
1237     List<String> actualRequests = mockService.getRequestPaths();
1238     Assert.assertEquals(1, actualRequests.size());
1239 
1240     String apiClientHeaderKey =
1241         mockService
1242             .getRequestHeaders()
1243             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1244             .iterator()
1245             .next();
1246     Assert.assertTrue(
1247         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1248             .matcher(apiClientHeaderKey)
1249             .matches());
1250   }
1251 
1252   @Test
getEndpointExceptionTest2()1253   public void getEndpointExceptionTest2() throws Exception {
1254     ApiException exception =
1255         ApiExceptionFactory.createException(
1256             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1257     mockService.addException(exception);
1258 
1259     try {
1260       String name =
1261           "projects/project-9594/locations/location-9594/namespaces/namespace-9594/services/service-9594/endpoints/endpoint-9594";
1262       client.getEndpoint(name);
1263       Assert.fail("No exception raised");
1264     } catch (InvalidArgumentException e) {
1265       // Expected exception.
1266     }
1267   }
1268 
1269   @Test
updateEndpointTest()1270   public void updateEndpointTest() throws Exception {
1271     Endpoint expectedResponse =
1272         Endpoint.newBuilder()
1273             .setName(
1274                 EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]")
1275                     .toString())
1276             .setAddress("address-1147692044")
1277             .setPort(3446913)
1278             .putAllAnnotations(new HashMap<String, String>())
1279             .build();
1280     mockService.addResponse(expectedResponse);
1281 
1282     Endpoint endpoint =
1283         Endpoint.newBuilder()
1284             .setName(
1285                 EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]")
1286                     .toString())
1287             .setAddress("address-1147692044")
1288             .setPort(3446913)
1289             .putAllAnnotations(new HashMap<String, String>())
1290             .build();
1291     FieldMask updateMask = FieldMask.newBuilder().build();
1292 
1293     Endpoint actualResponse = client.updateEndpoint(endpoint, updateMask);
1294     Assert.assertEquals(expectedResponse, actualResponse);
1295 
1296     List<String> actualRequests = mockService.getRequestPaths();
1297     Assert.assertEquals(1, actualRequests.size());
1298 
1299     String apiClientHeaderKey =
1300         mockService
1301             .getRequestHeaders()
1302             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1303             .iterator()
1304             .next();
1305     Assert.assertTrue(
1306         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1307             .matcher(apiClientHeaderKey)
1308             .matches());
1309   }
1310 
1311   @Test
updateEndpointExceptionTest()1312   public void updateEndpointExceptionTest() throws Exception {
1313     ApiException exception =
1314         ApiExceptionFactory.createException(
1315             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1316     mockService.addException(exception);
1317 
1318     try {
1319       Endpoint endpoint =
1320           Endpoint.newBuilder()
1321               .setName(
1322                   EndpointName.of(
1323                           "[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]")
1324                       .toString())
1325               .setAddress("address-1147692044")
1326               .setPort(3446913)
1327               .putAllAnnotations(new HashMap<String, String>())
1328               .build();
1329       FieldMask updateMask = FieldMask.newBuilder().build();
1330       client.updateEndpoint(endpoint, updateMask);
1331       Assert.fail("No exception raised");
1332     } catch (InvalidArgumentException e) {
1333       // Expected exception.
1334     }
1335   }
1336 
1337   @Test
deleteEndpointTest()1338   public void deleteEndpointTest() throws Exception {
1339     Empty expectedResponse = Empty.newBuilder().build();
1340     mockService.addResponse(expectedResponse);
1341 
1342     EndpointName name =
1343         EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]");
1344 
1345     client.deleteEndpoint(name);
1346 
1347     List<String> actualRequests = mockService.getRequestPaths();
1348     Assert.assertEquals(1, actualRequests.size());
1349 
1350     String apiClientHeaderKey =
1351         mockService
1352             .getRequestHeaders()
1353             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1354             .iterator()
1355             .next();
1356     Assert.assertTrue(
1357         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1358             .matcher(apiClientHeaderKey)
1359             .matches());
1360   }
1361 
1362   @Test
deleteEndpointExceptionTest()1363   public void deleteEndpointExceptionTest() throws Exception {
1364     ApiException exception =
1365         ApiExceptionFactory.createException(
1366             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1367     mockService.addException(exception);
1368 
1369     try {
1370       EndpointName name =
1371           EndpointName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]", "[ENDPOINT]");
1372       client.deleteEndpoint(name);
1373       Assert.fail("No exception raised");
1374     } catch (InvalidArgumentException e) {
1375       // Expected exception.
1376     }
1377   }
1378 
1379   @Test
deleteEndpointTest2()1380   public void deleteEndpointTest2() throws Exception {
1381     Empty expectedResponse = Empty.newBuilder().build();
1382     mockService.addResponse(expectedResponse);
1383 
1384     String name =
1385         "projects/project-9594/locations/location-9594/namespaces/namespace-9594/services/service-9594/endpoints/endpoint-9594";
1386 
1387     client.deleteEndpoint(name);
1388 
1389     List<String> actualRequests = mockService.getRequestPaths();
1390     Assert.assertEquals(1, actualRequests.size());
1391 
1392     String apiClientHeaderKey =
1393         mockService
1394             .getRequestHeaders()
1395             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1396             .iterator()
1397             .next();
1398     Assert.assertTrue(
1399         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1400             .matcher(apiClientHeaderKey)
1401             .matches());
1402   }
1403 
1404   @Test
deleteEndpointExceptionTest2()1405   public void deleteEndpointExceptionTest2() throws Exception {
1406     ApiException exception =
1407         ApiExceptionFactory.createException(
1408             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1409     mockService.addException(exception);
1410 
1411     try {
1412       String name =
1413           "projects/project-9594/locations/location-9594/namespaces/namespace-9594/services/service-9594/endpoints/endpoint-9594";
1414       client.deleteEndpoint(name);
1415       Assert.fail("No exception raised");
1416     } catch (InvalidArgumentException e) {
1417       // Expected exception.
1418     }
1419   }
1420 
1421   @Test
getIamPolicyTest()1422   public void getIamPolicyTest() throws Exception {
1423     Policy expectedResponse =
1424         Policy.newBuilder()
1425             .setVersion(351608024)
1426             .addAllBindings(new ArrayList<Binding>())
1427             .addAllAuditConfigs(new ArrayList<AuditConfig>())
1428             .setEtag(ByteString.EMPTY)
1429             .build();
1430     mockService.addResponse(expectedResponse);
1431 
1432     GetIamPolicyRequest request =
1433         GetIamPolicyRequest.newBuilder()
1434             .setResource(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
1435             .setOptions(GetPolicyOptions.newBuilder().build())
1436             .build();
1437 
1438     Policy actualResponse = client.getIamPolicy(request);
1439     Assert.assertEquals(expectedResponse, actualResponse);
1440 
1441     List<String> actualRequests = mockService.getRequestPaths();
1442     Assert.assertEquals(1, actualRequests.size());
1443 
1444     String apiClientHeaderKey =
1445         mockService
1446             .getRequestHeaders()
1447             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1448             .iterator()
1449             .next();
1450     Assert.assertTrue(
1451         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1452             .matcher(apiClientHeaderKey)
1453             .matches());
1454   }
1455 
1456   @Test
getIamPolicyExceptionTest()1457   public void getIamPolicyExceptionTest() throws Exception {
1458     ApiException exception =
1459         ApiExceptionFactory.createException(
1460             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1461     mockService.addException(exception);
1462 
1463     try {
1464       GetIamPolicyRequest request =
1465           GetIamPolicyRequest.newBuilder()
1466               .setResource(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
1467               .setOptions(GetPolicyOptions.newBuilder().build())
1468               .build();
1469       client.getIamPolicy(request);
1470       Assert.fail("No exception raised");
1471     } catch (InvalidArgumentException e) {
1472       // Expected exception.
1473     }
1474   }
1475 
1476   @Test
setIamPolicyTest()1477   public void setIamPolicyTest() throws Exception {
1478     Policy expectedResponse =
1479         Policy.newBuilder()
1480             .setVersion(351608024)
1481             .addAllBindings(new ArrayList<Binding>())
1482             .addAllAuditConfigs(new ArrayList<AuditConfig>())
1483             .setEtag(ByteString.EMPTY)
1484             .build();
1485     mockService.addResponse(expectedResponse);
1486 
1487     SetIamPolicyRequest request =
1488         SetIamPolicyRequest.newBuilder()
1489             .setResource(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
1490             .setPolicy(Policy.newBuilder().build())
1491             .setUpdateMask(FieldMask.newBuilder().build())
1492             .build();
1493 
1494     Policy actualResponse = client.setIamPolicy(request);
1495     Assert.assertEquals(expectedResponse, actualResponse);
1496 
1497     List<String> actualRequests = mockService.getRequestPaths();
1498     Assert.assertEquals(1, actualRequests.size());
1499 
1500     String apiClientHeaderKey =
1501         mockService
1502             .getRequestHeaders()
1503             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1504             .iterator()
1505             .next();
1506     Assert.assertTrue(
1507         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1508             .matcher(apiClientHeaderKey)
1509             .matches());
1510   }
1511 
1512   @Test
setIamPolicyExceptionTest()1513   public void setIamPolicyExceptionTest() throws Exception {
1514     ApiException exception =
1515         ApiExceptionFactory.createException(
1516             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1517     mockService.addException(exception);
1518 
1519     try {
1520       SetIamPolicyRequest request =
1521           SetIamPolicyRequest.newBuilder()
1522               .setResource(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
1523               .setPolicy(Policy.newBuilder().build())
1524               .setUpdateMask(FieldMask.newBuilder().build())
1525               .build();
1526       client.setIamPolicy(request);
1527       Assert.fail("No exception raised");
1528     } catch (InvalidArgumentException e) {
1529       // Expected exception.
1530     }
1531   }
1532 
1533   @Test
testIamPermissionsTest()1534   public void testIamPermissionsTest() throws Exception {
1535     TestIamPermissionsResponse expectedResponse =
1536         TestIamPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build();
1537     mockService.addResponse(expectedResponse);
1538 
1539     TestIamPermissionsRequest request =
1540         TestIamPermissionsRequest.newBuilder()
1541             .setResource(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
1542             .addAllPermissions(new ArrayList<String>())
1543             .build();
1544 
1545     TestIamPermissionsResponse actualResponse = client.testIamPermissions(request);
1546     Assert.assertEquals(expectedResponse, actualResponse);
1547 
1548     List<String> actualRequests = mockService.getRequestPaths();
1549     Assert.assertEquals(1, actualRequests.size());
1550 
1551     String apiClientHeaderKey =
1552         mockService
1553             .getRequestHeaders()
1554             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1555             .iterator()
1556             .next();
1557     Assert.assertTrue(
1558         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1559             .matcher(apiClientHeaderKey)
1560             .matches());
1561   }
1562 
1563   @Test
testIamPermissionsExceptionTest()1564   public void testIamPermissionsExceptionTest() throws Exception {
1565     ApiException exception =
1566         ApiExceptionFactory.createException(
1567             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1568     mockService.addException(exception);
1569 
1570     try {
1571       TestIamPermissionsRequest request =
1572           TestIamPermissionsRequest.newBuilder()
1573               .setResource(NamespaceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]").toString())
1574               .addAllPermissions(new ArrayList<String>())
1575               .build();
1576       client.testIamPermissions(request);
1577       Assert.fail("No exception raised");
1578     } catch (InvalidArgumentException e) {
1579       // Expected exception.
1580     }
1581   }
1582 }
1583