• 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.billing.v1;
18 
19 import static com.google.cloud.billing.v1.CloudBillingClient.ListBillingAccountsPagedResponse;
20 import static com.google.cloud.billing.v1.CloudBillingClient.ListProjectBillingInfoPagedResponse;
21 
22 import com.google.api.gax.core.NoCredentialsProvider;
23 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
24 import com.google.api.gax.httpjson.testing.MockHttpService;
25 import com.google.api.gax.rpc.ApiClientHeaderProvider;
26 import com.google.api.gax.rpc.ApiException;
27 import com.google.api.gax.rpc.ApiExceptionFactory;
28 import com.google.api.gax.rpc.InvalidArgumentException;
29 import com.google.api.gax.rpc.StatusCode;
30 import com.google.api.gax.rpc.testing.FakeStatusCode;
31 import com.google.api.resourcenames.ResourceName;
32 import com.google.cloud.billing.v1.stub.HttpJsonCloudBillingStub;
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.Policy;
37 import com.google.iam.v1.TestIamPermissionsResponse;
38 import com.google.protobuf.ByteString;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
43 import javax.annotation.Generated;
44 import org.junit.After;
45 import org.junit.AfterClass;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 
51 @Generated("by gapic-generator-java")
52 public class CloudBillingClientHttpJsonTest {
53   private static MockHttpService mockService;
54   private static CloudBillingClient client;
55 
56   @BeforeClass
startStaticServer()57   public static void startStaticServer() throws IOException {
58     mockService =
59         new MockHttpService(
60             HttpJsonCloudBillingStub.getMethodDescriptors(),
61             CloudBillingSettings.getDefaultEndpoint());
62     CloudBillingSettings settings =
63         CloudBillingSettings.newHttpJsonBuilder()
64             .setTransportChannelProvider(
65                 CloudBillingSettings.defaultHttpJsonTransportProviderBuilder()
66                     .setHttpTransport(mockService)
67                     .build())
68             .setCredentialsProvider(NoCredentialsProvider.create())
69             .build();
70     client = CloudBillingClient.create(settings);
71   }
72 
73   @AfterClass
stopServer()74   public static void stopServer() {
75     client.close();
76   }
77 
78   @Before
setUp()79   public void setUp() {}
80 
81   @After
tearDown()82   public void tearDown() throws Exception {
83     mockService.reset();
84   }
85 
86   @Test
getBillingAccountTest()87   public void getBillingAccountTest() throws Exception {
88     BillingAccount expectedResponse =
89         BillingAccount.newBuilder()
90             .setName(BillingAccountName.of("[BILLING_ACCOUNT]").toString())
91             .setOpen(true)
92             .setDisplayName("displayName1714148973")
93             .setMasterBillingAccount("masterBillingAccount1488941620")
94             .build();
95     mockService.addResponse(expectedResponse);
96 
97     BillingAccountName name = BillingAccountName.of("[BILLING_ACCOUNT]");
98 
99     BillingAccount actualResponse = client.getBillingAccount(name);
100     Assert.assertEquals(expectedResponse, actualResponse);
101 
102     List<String> actualRequests = mockService.getRequestPaths();
103     Assert.assertEquals(1, actualRequests.size());
104 
105     String apiClientHeaderKey =
106         mockService
107             .getRequestHeaders()
108             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
109             .iterator()
110             .next();
111     Assert.assertTrue(
112         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
113             .matcher(apiClientHeaderKey)
114             .matches());
115   }
116 
117   @Test
getBillingAccountExceptionTest()118   public void getBillingAccountExceptionTest() throws Exception {
119     ApiException exception =
120         ApiExceptionFactory.createException(
121             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
122     mockService.addException(exception);
123 
124     try {
125       BillingAccountName name = BillingAccountName.of("[BILLING_ACCOUNT]");
126       client.getBillingAccount(name);
127       Assert.fail("No exception raised");
128     } catch (InvalidArgumentException e) {
129       // Expected exception.
130     }
131   }
132 
133   @Test
getBillingAccountTest2()134   public void getBillingAccountTest2() throws Exception {
135     BillingAccount expectedResponse =
136         BillingAccount.newBuilder()
137             .setName(BillingAccountName.of("[BILLING_ACCOUNT]").toString())
138             .setOpen(true)
139             .setDisplayName("displayName1714148973")
140             .setMasterBillingAccount("masterBillingAccount1488941620")
141             .build();
142     mockService.addResponse(expectedResponse);
143 
144     String name = "billingAccounts/billingAccount-6593";
145 
146     BillingAccount actualResponse = client.getBillingAccount(name);
147     Assert.assertEquals(expectedResponse, actualResponse);
148 
149     List<String> actualRequests = mockService.getRequestPaths();
150     Assert.assertEquals(1, actualRequests.size());
151 
152     String apiClientHeaderKey =
153         mockService
154             .getRequestHeaders()
155             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
156             .iterator()
157             .next();
158     Assert.assertTrue(
159         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
160             .matcher(apiClientHeaderKey)
161             .matches());
162   }
163 
164   @Test
getBillingAccountExceptionTest2()165   public void getBillingAccountExceptionTest2() throws Exception {
166     ApiException exception =
167         ApiExceptionFactory.createException(
168             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
169     mockService.addException(exception);
170 
171     try {
172       String name = "billingAccounts/billingAccount-6593";
173       client.getBillingAccount(name);
174       Assert.fail("No exception raised");
175     } catch (InvalidArgumentException e) {
176       // Expected exception.
177     }
178   }
179 
180   @Test
listBillingAccountsTest()181   public void listBillingAccountsTest() throws Exception {
182     BillingAccount responsesElement = BillingAccount.newBuilder().build();
183     ListBillingAccountsResponse expectedResponse =
184         ListBillingAccountsResponse.newBuilder()
185             .setNextPageToken("")
186             .addAllBillingAccounts(Arrays.asList(responsesElement))
187             .build();
188     mockService.addResponse(expectedResponse);
189 
190     ListBillingAccountsPagedResponse pagedListResponse = client.listBillingAccounts();
191 
192     List<BillingAccount> resources = Lists.newArrayList(pagedListResponse.iterateAll());
193 
194     Assert.assertEquals(1, resources.size());
195     Assert.assertEquals(expectedResponse.getBillingAccountsList().get(0), resources.get(0));
196 
197     List<String> actualRequests = mockService.getRequestPaths();
198     Assert.assertEquals(1, actualRequests.size());
199 
200     String apiClientHeaderKey =
201         mockService
202             .getRequestHeaders()
203             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
204             .iterator()
205             .next();
206     Assert.assertTrue(
207         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
208             .matcher(apiClientHeaderKey)
209             .matches());
210   }
211 
212   @Test
listBillingAccountsExceptionTest()213   public void listBillingAccountsExceptionTest() throws Exception {
214     ApiException exception =
215         ApiExceptionFactory.createException(
216             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
217     mockService.addException(exception);
218 
219     try {
220       ListBillingAccountsRequest request =
221           ListBillingAccountsRequest.newBuilder()
222               .setPageSize(883849137)
223               .setPageToken("pageToken873572522")
224               .setFilter("filter-1274492040")
225               .build();
226       client.listBillingAccounts(request);
227       Assert.fail("No exception raised");
228     } catch (InvalidArgumentException e) {
229       // Expected exception.
230     }
231   }
232 
233   @Test
updateBillingAccountTest()234   public void updateBillingAccountTest() throws Exception {
235     BillingAccount expectedResponse =
236         BillingAccount.newBuilder()
237             .setName(BillingAccountName.of("[BILLING_ACCOUNT]").toString())
238             .setOpen(true)
239             .setDisplayName("displayName1714148973")
240             .setMasterBillingAccount("masterBillingAccount1488941620")
241             .build();
242     mockService.addResponse(expectedResponse);
243 
244     BillingAccountName name = BillingAccountName.of("[BILLING_ACCOUNT]");
245     BillingAccount account = BillingAccount.newBuilder().build();
246 
247     BillingAccount actualResponse = client.updateBillingAccount(name, account);
248     Assert.assertEquals(expectedResponse, actualResponse);
249 
250     List<String> actualRequests = mockService.getRequestPaths();
251     Assert.assertEquals(1, actualRequests.size());
252 
253     String apiClientHeaderKey =
254         mockService
255             .getRequestHeaders()
256             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
257             .iterator()
258             .next();
259     Assert.assertTrue(
260         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
261             .matcher(apiClientHeaderKey)
262             .matches());
263   }
264 
265   @Test
updateBillingAccountExceptionTest()266   public void updateBillingAccountExceptionTest() throws Exception {
267     ApiException exception =
268         ApiExceptionFactory.createException(
269             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
270     mockService.addException(exception);
271 
272     try {
273       BillingAccountName name = BillingAccountName.of("[BILLING_ACCOUNT]");
274       BillingAccount account = BillingAccount.newBuilder().build();
275       client.updateBillingAccount(name, account);
276       Assert.fail("No exception raised");
277     } catch (InvalidArgumentException e) {
278       // Expected exception.
279     }
280   }
281 
282   @Test
updateBillingAccountTest2()283   public void updateBillingAccountTest2() throws Exception {
284     BillingAccount expectedResponse =
285         BillingAccount.newBuilder()
286             .setName(BillingAccountName.of("[BILLING_ACCOUNT]").toString())
287             .setOpen(true)
288             .setDisplayName("displayName1714148973")
289             .setMasterBillingAccount("masterBillingAccount1488941620")
290             .build();
291     mockService.addResponse(expectedResponse);
292 
293     String name = "billingAccounts/billingAccount-6593";
294     BillingAccount account = BillingAccount.newBuilder().build();
295 
296     BillingAccount actualResponse = client.updateBillingAccount(name, account);
297     Assert.assertEquals(expectedResponse, actualResponse);
298 
299     List<String> actualRequests = mockService.getRequestPaths();
300     Assert.assertEquals(1, actualRequests.size());
301 
302     String apiClientHeaderKey =
303         mockService
304             .getRequestHeaders()
305             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
306             .iterator()
307             .next();
308     Assert.assertTrue(
309         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
310             .matcher(apiClientHeaderKey)
311             .matches());
312   }
313 
314   @Test
updateBillingAccountExceptionTest2()315   public void updateBillingAccountExceptionTest2() throws Exception {
316     ApiException exception =
317         ApiExceptionFactory.createException(
318             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
319     mockService.addException(exception);
320 
321     try {
322       String name = "billingAccounts/billingAccount-6593";
323       BillingAccount account = BillingAccount.newBuilder().build();
324       client.updateBillingAccount(name, account);
325       Assert.fail("No exception raised");
326     } catch (InvalidArgumentException e) {
327       // Expected exception.
328     }
329   }
330 
331   @Test
createBillingAccountTest()332   public void createBillingAccountTest() throws Exception {
333     BillingAccount expectedResponse =
334         BillingAccount.newBuilder()
335             .setName(BillingAccountName.of("[BILLING_ACCOUNT]").toString())
336             .setOpen(true)
337             .setDisplayName("displayName1714148973")
338             .setMasterBillingAccount("masterBillingAccount1488941620")
339             .build();
340     mockService.addResponse(expectedResponse);
341 
342     BillingAccount billingAccount = BillingAccount.newBuilder().build();
343 
344     BillingAccount actualResponse = client.createBillingAccount(billingAccount);
345     Assert.assertEquals(expectedResponse, actualResponse);
346 
347     List<String> actualRequests = mockService.getRequestPaths();
348     Assert.assertEquals(1, actualRequests.size());
349 
350     String apiClientHeaderKey =
351         mockService
352             .getRequestHeaders()
353             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
354             .iterator()
355             .next();
356     Assert.assertTrue(
357         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
358             .matcher(apiClientHeaderKey)
359             .matches());
360   }
361 
362   @Test
createBillingAccountExceptionTest()363   public void createBillingAccountExceptionTest() throws Exception {
364     ApiException exception =
365         ApiExceptionFactory.createException(
366             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
367     mockService.addException(exception);
368 
369     try {
370       BillingAccount billingAccount = BillingAccount.newBuilder().build();
371       client.createBillingAccount(billingAccount);
372       Assert.fail("No exception raised");
373     } catch (InvalidArgumentException e) {
374       // Expected exception.
375     }
376   }
377 
378   @Test
listProjectBillingInfoTest()379   public void listProjectBillingInfoTest() throws Exception {
380     ProjectBillingInfo responsesElement = ProjectBillingInfo.newBuilder().build();
381     ListProjectBillingInfoResponse expectedResponse =
382         ListProjectBillingInfoResponse.newBuilder()
383             .setNextPageToken("")
384             .addAllProjectBillingInfo(Arrays.asList(responsesElement))
385             .build();
386     mockService.addResponse(expectedResponse);
387 
388     BillingAccountName name = BillingAccountName.of("[BILLING_ACCOUNT]");
389 
390     ListProjectBillingInfoPagedResponse pagedListResponse = client.listProjectBillingInfo(name);
391 
392     List<ProjectBillingInfo> resources = Lists.newArrayList(pagedListResponse.iterateAll());
393 
394     Assert.assertEquals(1, resources.size());
395     Assert.assertEquals(expectedResponse.getProjectBillingInfoList().get(0), resources.get(0));
396 
397     List<String> actualRequests = mockService.getRequestPaths();
398     Assert.assertEquals(1, actualRequests.size());
399 
400     String apiClientHeaderKey =
401         mockService
402             .getRequestHeaders()
403             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
404             .iterator()
405             .next();
406     Assert.assertTrue(
407         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
408             .matcher(apiClientHeaderKey)
409             .matches());
410   }
411 
412   @Test
listProjectBillingInfoExceptionTest()413   public void listProjectBillingInfoExceptionTest() throws Exception {
414     ApiException exception =
415         ApiExceptionFactory.createException(
416             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
417     mockService.addException(exception);
418 
419     try {
420       BillingAccountName name = BillingAccountName.of("[BILLING_ACCOUNT]");
421       client.listProjectBillingInfo(name);
422       Assert.fail("No exception raised");
423     } catch (InvalidArgumentException e) {
424       // Expected exception.
425     }
426   }
427 
428   @Test
listProjectBillingInfoTest2()429   public void listProjectBillingInfoTest2() throws Exception {
430     ProjectBillingInfo responsesElement = ProjectBillingInfo.newBuilder().build();
431     ListProjectBillingInfoResponse expectedResponse =
432         ListProjectBillingInfoResponse.newBuilder()
433             .setNextPageToken("")
434             .addAllProjectBillingInfo(Arrays.asList(responsesElement))
435             .build();
436     mockService.addResponse(expectedResponse);
437 
438     String name = "billingAccounts/billingAccount-6593";
439 
440     ListProjectBillingInfoPagedResponse pagedListResponse = client.listProjectBillingInfo(name);
441 
442     List<ProjectBillingInfo> resources = Lists.newArrayList(pagedListResponse.iterateAll());
443 
444     Assert.assertEquals(1, resources.size());
445     Assert.assertEquals(expectedResponse.getProjectBillingInfoList().get(0), resources.get(0));
446 
447     List<String> actualRequests = mockService.getRequestPaths();
448     Assert.assertEquals(1, actualRequests.size());
449 
450     String apiClientHeaderKey =
451         mockService
452             .getRequestHeaders()
453             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
454             .iterator()
455             .next();
456     Assert.assertTrue(
457         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
458             .matcher(apiClientHeaderKey)
459             .matches());
460   }
461 
462   @Test
listProjectBillingInfoExceptionTest2()463   public void listProjectBillingInfoExceptionTest2() throws Exception {
464     ApiException exception =
465         ApiExceptionFactory.createException(
466             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
467     mockService.addException(exception);
468 
469     try {
470       String name = "billingAccounts/billingAccount-6593";
471       client.listProjectBillingInfo(name);
472       Assert.fail("No exception raised");
473     } catch (InvalidArgumentException e) {
474       // Expected exception.
475     }
476   }
477 
478   @Test
getProjectBillingInfoTest()479   public void getProjectBillingInfoTest() throws Exception {
480     ProjectBillingInfo expectedResponse =
481         ProjectBillingInfo.newBuilder()
482             .setName("name3373707")
483             .setProjectId("projectId-894832108")
484             .setBillingAccountName("billingAccountName929322205")
485             .setBillingEnabled(true)
486             .build();
487     mockService.addResponse(expectedResponse);
488 
489     String name = "projects/project-3664";
490 
491     ProjectBillingInfo actualResponse = client.getProjectBillingInfo(name);
492     Assert.assertEquals(expectedResponse, actualResponse);
493 
494     List<String> actualRequests = mockService.getRequestPaths();
495     Assert.assertEquals(1, actualRequests.size());
496 
497     String apiClientHeaderKey =
498         mockService
499             .getRequestHeaders()
500             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
501             .iterator()
502             .next();
503     Assert.assertTrue(
504         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
505             .matcher(apiClientHeaderKey)
506             .matches());
507   }
508 
509   @Test
getProjectBillingInfoExceptionTest()510   public void getProjectBillingInfoExceptionTest() throws Exception {
511     ApiException exception =
512         ApiExceptionFactory.createException(
513             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
514     mockService.addException(exception);
515 
516     try {
517       String name = "projects/project-3664";
518       client.getProjectBillingInfo(name);
519       Assert.fail("No exception raised");
520     } catch (InvalidArgumentException e) {
521       // Expected exception.
522     }
523   }
524 
525   @Test
updateProjectBillingInfoTest()526   public void updateProjectBillingInfoTest() throws Exception {
527     ProjectBillingInfo expectedResponse =
528         ProjectBillingInfo.newBuilder()
529             .setName("name3373707")
530             .setProjectId("projectId-894832108")
531             .setBillingAccountName("billingAccountName929322205")
532             .setBillingEnabled(true)
533             .build();
534     mockService.addResponse(expectedResponse);
535 
536     String name = "projects/project-3664";
537     ProjectBillingInfo projectBillingInfo = ProjectBillingInfo.newBuilder().build();
538 
539     ProjectBillingInfo actualResponse = client.updateProjectBillingInfo(name, projectBillingInfo);
540     Assert.assertEquals(expectedResponse, actualResponse);
541 
542     List<String> actualRequests = mockService.getRequestPaths();
543     Assert.assertEquals(1, actualRequests.size());
544 
545     String apiClientHeaderKey =
546         mockService
547             .getRequestHeaders()
548             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
549             .iterator()
550             .next();
551     Assert.assertTrue(
552         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
553             .matcher(apiClientHeaderKey)
554             .matches());
555   }
556 
557   @Test
updateProjectBillingInfoExceptionTest()558   public void updateProjectBillingInfoExceptionTest() throws Exception {
559     ApiException exception =
560         ApiExceptionFactory.createException(
561             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
562     mockService.addException(exception);
563 
564     try {
565       String name = "projects/project-3664";
566       ProjectBillingInfo projectBillingInfo = ProjectBillingInfo.newBuilder().build();
567       client.updateProjectBillingInfo(name, projectBillingInfo);
568       Assert.fail("No exception raised");
569     } catch (InvalidArgumentException e) {
570       // Expected exception.
571     }
572   }
573 
574   @Test
getIamPolicyTest()575   public void getIamPolicyTest() throws Exception {
576     Policy expectedResponse =
577         Policy.newBuilder()
578             .setVersion(351608024)
579             .addAllBindings(new ArrayList<Binding>())
580             .addAllAuditConfigs(new ArrayList<AuditConfig>())
581             .setEtag(ByteString.EMPTY)
582             .build();
583     mockService.addResponse(expectedResponse);
584 
585     ResourceName resource = BillingAccountName.of("[BILLING_ACCOUNT]");
586 
587     Policy actualResponse = client.getIamPolicy(resource);
588     Assert.assertEquals(expectedResponse, actualResponse);
589 
590     List<String> actualRequests = mockService.getRequestPaths();
591     Assert.assertEquals(1, actualRequests.size());
592 
593     String apiClientHeaderKey =
594         mockService
595             .getRequestHeaders()
596             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
597             .iterator()
598             .next();
599     Assert.assertTrue(
600         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
601             .matcher(apiClientHeaderKey)
602             .matches());
603   }
604 
605   @Test
getIamPolicyExceptionTest()606   public void getIamPolicyExceptionTest() throws Exception {
607     ApiException exception =
608         ApiExceptionFactory.createException(
609             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
610     mockService.addException(exception);
611 
612     try {
613       ResourceName resource = BillingAccountName.of("[BILLING_ACCOUNT]");
614       client.getIamPolicy(resource);
615       Assert.fail("No exception raised");
616     } catch (InvalidArgumentException e) {
617       // Expected exception.
618     }
619   }
620 
621   @Test
getIamPolicyTest2()622   public void getIamPolicyTest2() throws Exception {
623     Policy expectedResponse =
624         Policy.newBuilder()
625             .setVersion(351608024)
626             .addAllBindings(new ArrayList<Binding>())
627             .addAllAuditConfigs(new ArrayList<AuditConfig>())
628             .setEtag(ByteString.EMPTY)
629             .build();
630     mockService.addResponse(expectedResponse);
631 
632     String resource = "billingAccounts/billingAccount-2850";
633 
634     Policy actualResponse = client.getIamPolicy(resource);
635     Assert.assertEquals(expectedResponse, actualResponse);
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
getIamPolicyExceptionTest2()653   public void getIamPolicyExceptionTest2() 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       String resource = "billingAccounts/billingAccount-2850";
661       client.getIamPolicy(resource);
662       Assert.fail("No exception raised");
663     } catch (InvalidArgumentException e) {
664       // Expected exception.
665     }
666   }
667 
668   @Test
setIamPolicyTest()669   public void setIamPolicyTest() throws Exception {
670     Policy expectedResponse =
671         Policy.newBuilder()
672             .setVersion(351608024)
673             .addAllBindings(new ArrayList<Binding>())
674             .addAllAuditConfigs(new ArrayList<AuditConfig>())
675             .setEtag(ByteString.EMPTY)
676             .build();
677     mockService.addResponse(expectedResponse);
678 
679     ResourceName resource = BillingAccountName.of("[BILLING_ACCOUNT]");
680     Policy policy = Policy.newBuilder().build();
681 
682     Policy actualResponse = client.setIamPolicy(resource, policy);
683     Assert.assertEquals(expectedResponse, actualResponse);
684 
685     List<String> actualRequests = mockService.getRequestPaths();
686     Assert.assertEquals(1, actualRequests.size());
687 
688     String apiClientHeaderKey =
689         mockService
690             .getRequestHeaders()
691             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
692             .iterator()
693             .next();
694     Assert.assertTrue(
695         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
696             .matcher(apiClientHeaderKey)
697             .matches());
698   }
699 
700   @Test
setIamPolicyExceptionTest()701   public void setIamPolicyExceptionTest() throws Exception {
702     ApiException exception =
703         ApiExceptionFactory.createException(
704             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
705     mockService.addException(exception);
706 
707     try {
708       ResourceName resource = BillingAccountName.of("[BILLING_ACCOUNT]");
709       Policy policy = Policy.newBuilder().build();
710       client.setIamPolicy(resource, policy);
711       Assert.fail("No exception raised");
712     } catch (InvalidArgumentException e) {
713       // Expected exception.
714     }
715   }
716 
717   @Test
setIamPolicyTest2()718   public void setIamPolicyTest2() throws Exception {
719     Policy expectedResponse =
720         Policy.newBuilder()
721             .setVersion(351608024)
722             .addAllBindings(new ArrayList<Binding>())
723             .addAllAuditConfigs(new ArrayList<AuditConfig>())
724             .setEtag(ByteString.EMPTY)
725             .build();
726     mockService.addResponse(expectedResponse);
727 
728     String resource = "billingAccounts/billingAccount-2850";
729     Policy policy = Policy.newBuilder().build();
730 
731     Policy actualResponse = client.setIamPolicy(resource, policy);
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
setIamPolicyExceptionTest2()750   public void setIamPolicyExceptionTest2() 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       String resource = "billingAccounts/billingAccount-2850";
758       Policy policy = Policy.newBuilder().build();
759       client.setIamPolicy(resource, policy);
760       Assert.fail("No exception raised");
761     } catch (InvalidArgumentException e) {
762       // Expected exception.
763     }
764   }
765 
766   @Test
testIamPermissionsTest()767   public void testIamPermissionsTest() throws Exception {
768     TestIamPermissionsResponse expectedResponse =
769         TestIamPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build();
770     mockService.addResponse(expectedResponse);
771 
772     ResourceName resource = BillingAccountName.of("[BILLING_ACCOUNT]");
773     List<String> permissions = new ArrayList<>();
774 
775     TestIamPermissionsResponse actualResponse = client.testIamPermissions(resource, permissions);
776     Assert.assertEquals(expectedResponse, actualResponse);
777 
778     List<String> actualRequests = mockService.getRequestPaths();
779     Assert.assertEquals(1, actualRequests.size());
780 
781     String apiClientHeaderKey =
782         mockService
783             .getRequestHeaders()
784             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
785             .iterator()
786             .next();
787     Assert.assertTrue(
788         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
789             .matcher(apiClientHeaderKey)
790             .matches());
791   }
792 
793   @Test
testIamPermissionsExceptionTest()794   public void testIamPermissionsExceptionTest() throws Exception {
795     ApiException exception =
796         ApiExceptionFactory.createException(
797             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
798     mockService.addException(exception);
799 
800     try {
801       ResourceName resource = BillingAccountName.of("[BILLING_ACCOUNT]");
802       List<String> permissions = new ArrayList<>();
803       client.testIamPermissions(resource, permissions);
804       Assert.fail("No exception raised");
805     } catch (InvalidArgumentException e) {
806       // Expected exception.
807     }
808   }
809 
810   @Test
testIamPermissionsTest2()811   public void testIamPermissionsTest2() throws Exception {
812     TestIamPermissionsResponse expectedResponse =
813         TestIamPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build();
814     mockService.addResponse(expectedResponse);
815 
816     String resource = "billingAccounts/billingAccount-2850";
817     List<String> permissions = new ArrayList<>();
818 
819     TestIamPermissionsResponse actualResponse = client.testIamPermissions(resource, permissions);
820     Assert.assertEquals(expectedResponse, actualResponse);
821 
822     List<String> actualRequests = mockService.getRequestPaths();
823     Assert.assertEquals(1, actualRequests.size());
824 
825     String apiClientHeaderKey =
826         mockService
827             .getRequestHeaders()
828             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
829             .iterator()
830             .next();
831     Assert.assertTrue(
832         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
833             .matcher(apiClientHeaderKey)
834             .matches());
835   }
836 
837   @Test
testIamPermissionsExceptionTest2()838   public void testIamPermissionsExceptionTest2() throws Exception {
839     ApiException exception =
840         ApiExceptionFactory.createException(
841             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
842     mockService.addException(exception);
843 
844     try {
845       String resource = "billingAccounts/billingAccount-2850";
846       List<String> permissions = new ArrayList<>();
847       client.testIamPermissions(resource, permissions);
848       Assert.fail("No exception raised");
849     } catch (InvalidArgumentException e) {
850       // Expected exception.
851     }
852   }
853 }
854