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.bigquery.datapolicies.v1; 18 19 import static com.google.cloud.bigquery.datapolicies.v1.DataPolicyServiceClient.ListDataPoliciesPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 23 import com.google.api.gax.httpjson.testing.MockHttpService; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.ApiException; 26 import com.google.api.gax.rpc.ApiExceptionFactory; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.api.gax.rpc.testing.FakeStatusCode; 30 import com.google.cloud.bigquery.datapolicies.v1.stub.HttpJsonDataPolicyServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.iam.v1.AuditConfig; 33 import com.google.iam.v1.Binding; 34 import com.google.iam.v1.GetIamPolicyRequest; 35 import com.google.iam.v1.GetPolicyOptions; 36 import com.google.iam.v1.Policy; 37 import com.google.iam.v1.SetIamPolicyRequest; 38 import com.google.iam.v1.TestIamPermissionsRequest; 39 import com.google.iam.v1.TestIamPermissionsResponse; 40 import com.google.protobuf.ByteString; 41 import com.google.protobuf.Empty; 42 import com.google.protobuf.FieldMask; 43 import java.io.IOException; 44 import java.util.ArrayList; 45 import java.util.Arrays; 46 import java.util.List; 47 import javax.annotation.Generated; 48 import org.junit.After; 49 import org.junit.AfterClass; 50 import org.junit.Assert; 51 import org.junit.Before; 52 import org.junit.BeforeClass; 53 import org.junit.Test; 54 55 @Generated("by gapic-generator-java") 56 public class DataPolicyServiceClientHttpJsonTest { 57 private static MockHttpService mockService; 58 private static DataPolicyServiceClient client; 59 60 @BeforeClass startStaticServer()61 public static void startStaticServer() throws IOException { 62 mockService = 63 new MockHttpService( 64 HttpJsonDataPolicyServiceStub.getMethodDescriptors(), 65 DataPolicyServiceSettings.getDefaultEndpoint()); 66 DataPolicyServiceSettings settings = 67 DataPolicyServiceSettings.newHttpJsonBuilder() 68 .setTransportChannelProvider( 69 DataPolicyServiceSettings.defaultHttpJsonTransportProviderBuilder() 70 .setHttpTransport(mockService) 71 .build()) 72 .setCredentialsProvider(NoCredentialsProvider.create()) 73 .build(); 74 client = DataPolicyServiceClient.create(settings); 75 } 76 77 @AfterClass stopServer()78 public static void stopServer() { 79 client.close(); 80 } 81 82 @Before setUp()83 public void setUp() {} 84 85 @After tearDown()86 public void tearDown() throws Exception { 87 mockService.reset(); 88 } 89 90 @Test createDataPolicyTest()91 public void createDataPolicyTest() throws Exception { 92 DataPolicy expectedResponse = 93 DataPolicy.newBuilder() 94 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 95 .setDataPolicyId("dataPolicyId1874146231") 96 .build(); 97 mockService.addResponse(expectedResponse); 98 99 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 100 DataPolicy dataPolicy = DataPolicy.newBuilder().build(); 101 102 DataPolicy actualResponse = client.createDataPolicy(parent, dataPolicy); 103 Assert.assertEquals(expectedResponse, actualResponse); 104 105 List<String> actualRequests = mockService.getRequestPaths(); 106 Assert.assertEquals(1, actualRequests.size()); 107 108 String apiClientHeaderKey = 109 mockService 110 .getRequestHeaders() 111 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 112 .iterator() 113 .next(); 114 Assert.assertTrue( 115 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 116 .matcher(apiClientHeaderKey) 117 .matches()); 118 } 119 120 @Test createDataPolicyExceptionTest()121 public void createDataPolicyExceptionTest() throws Exception { 122 ApiException exception = 123 ApiExceptionFactory.createException( 124 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 125 mockService.addException(exception); 126 127 try { 128 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 129 DataPolicy dataPolicy = DataPolicy.newBuilder().build(); 130 client.createDataPolicy(parent, dataPolicy); 131 Assert.fail("No exception raised"); 132 } catch (InvalidArgumentException e) { 133 // Expected exception. 134 } 135 } 136 137 @Test createDataPolicyTest2()138 public void createDataPolicyTest2() throws Exception { 139 DataPolicy expectedResponse = 140 DataPolicy.newBuilder() 141 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 142 .setDataPolicyId("dataPolicyId1874146231") 143 .build(); 144 mockService.addResponse(expectedResponse); 145 146 String parent = "projects/project-5833/locations/location-5833"; 147 DataPolicy dataPolicy = DataPolicy.newBuilder().build(); 148 149 DataPolicy actualResponse = client.createDataPolicy(parent, dataPolicy); 150 Assert.assertEquals(expectedResponse, actualResponse); 151 152 List<String> actualRequests = mockService.getRequestPaths(); 153 Assert.assertEquals(1, actualRequests.size()); 154 155 String apiClientHeaderKey = 156 mockService 157 .getRequestHeaders() 158 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 159 .iterator() 160 .next(); 161 Assert.assertTrue( 162 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 163 .matcher(apiClientHeaderKey) 164 .matches()); 165 } 166 167 @Test createDataPolicyExceptionTest2()168 public void createDataPolicyExceptionTest2() throws Exception { 169 ApiException exception = 170 ApiExceptionFactory.createException( 171 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 172 mockService.addException(exception); 173 174 try { 175 String parent = "projects/project-5833/locations/location-5833"; 176 DataPolicy dataPolicy = DataPolicy.newBuilder().build(); 177 client.createDataPolicy(parent, dataPolicy); 178 Assert.fail("No exception raised"); 179 } catch (InvalidArgumentException e) { 180 // Expected exception. 181 } 182 } 183 184 @Test updateDataPolicyTest()185 public void updateDataPolicyTest() throws Exception { 186 DataPolicy expectedResponse = 187 DataPolicy.newBuilder() 188 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 189 .setDataPolicyId("dataPolicyId1874146231") 190 .build(); 191 mockService.addResponse(expectedResponse); 192 193 DataPolicy dataPolicy = 194 DataPolicy.newBuilder() 195 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 196 .setDataPolicyId("dataPolicyId1874146231") 197 .build(); 198 FieldMask updateMask = FieldMask.newBuilder().build(); 199 200 DataPolicy actualResponse = client.updateDataPolicy(dataPolicy, updateMask); 201 Assert.assertEquals(expectedResponse, actualResponse); 202 203 List<String> actualRequests = mockService.getRequestPaths(); 204 Assert.assertEquals(1, actualRequests.size()); 205 206 String apiClientHeaderKey = 207 mockService 208 .getRequestHeaders() 209 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 210 .iterator() 211 .next(); 212 Assert.assertTrue( 213 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 214 .matcher(apiClientHeaderKey) 215 .matches()); 216 } 217 218 @Test updateDataPolicyExceptionTest()219 public void updateDataPolicyExceptionTest() throws Exception { 220 ApiException exception = 221 ApiExceptionFactory.createException( 222 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 223 mockService.addException(exception); 224 225 try { 226 DataPolicy dataPolicy = 227 DataPolicy.newBuilder() 228 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 229 .setDataPolicyId("dataPolicyId1874146231") 230 .build(); 231 FieldMask updateMask = FieldMask.newBuilder().build(); 232 client.updateDataPolicy(dataPolicy, updateMask); 233 Assert.fail("No exception raised"); 234 } catch (InvalidArgumentException e) { 235 // Expected exception. 236 } 237 } 238 239 @Test renameDataPolicyTest()240 public void renameDataPolicyTest() throws Exception { 241 DataPolicy expectedResponse = 242 DataPolicy.newBuilder() 243 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 244 .setDataPolicyId("dataPolicyId1874146231") 245 .build(); 246 mockService.addResponse(expectedResponse); 247 248 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 249 String newDataPolicyId = "newDataPolicyId288407927"; 250 251 DataPolicy actualResponse = client.renameDataPolicy(name, newDataPolicyId); 252 Assert.assertEquals(expectedResponse, actualResponse); 253 254 List<String> actualRequests = mockService.getRequestPaths(); 255 Assert.assertEquals(1, actualRequests.size()); 256 257 String apiClientHeaderKey = 258 mockService 259 .getRequestHeaders() 260 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 261 .iterator() 262 .next(); 263 Assert.assertTrue( 264 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 265 .matcher(apiClientHeaderKey) 266 .matches()); 267 } 268 269 @Test renameDataPolicyExceptionTest()270 public void renameDataPolicyExceptionTest() throws Exception { 271 ApiException exception = 272 ApiExceptionFactory.createException( 273 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 274 mockService.addException(exception); 275 276 try { 277 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 278 String newDataPolicyId = "newDataPolicyId288407927"; 279 client.renameDataPolicy(name, newDataPolicyId); 280 Assert.fail("No exception raised"); 281 } catch (InvalidArgumentException e) { 282 // Expected exception. 283 } 284 } 285 286 @Test deleteDataPolicyTest()287 public void deleteDataPolicyTest() throws Exception { 288 Empty expectedResponse = Empty.newBuilder().build(); 289 mockService.addResponse(expectedResponse); 290 291 DataPolicyName name = DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]"); 292 293 client.deleteDataPolicy(name); 294 295 List<String> actualRequests = mockService.getRequestPaths(); 296 Assert.assertEquals(1, actualRequests.size()); 297 298 String apiClientHeaderKey = 299 mockService 300 .getRequestHeaders() 301 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 302 .iterator() 303 .next(); 304 Assert.assertTrue( 305 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 306 .matcher(apiClientHeaderKey) 307 .matches()); 308 } 309 310 @Test deleteDataPolicyExceptionTest()311 public void deleteDataPolicyExceptionTest() throws Exception { 312 ApiException exception = 313 ApiExceptionFactory.createException( 314 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 315 mockService.addException(exception); 316 317 try { 318 DataPolicyName name = DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]"); 319 client.deleteDataPolicy(name); 320 Assert.fail("No exception raised"); 321 } catch (InvalidArgumentException e) { 322 // Expected exception. 323 } 324 } 325 326 @Test deleteDataPolicyTest2()327 public void deleteDataPolicyTest2() throws Exception { 328 Empty expectedResponse = Empty.newBuilder().build(); 329 mockService.addResponse(expectedResponse); 330 331 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 332 333 client.deleteDataPolicy(name); 334 335 List<String> actualRequests = mockService.getRequestPaths(); 336 Assert.assertEquals(1, actualRequests.size()); 337 338 String apiClientHeaderKey = 339 mockService 340 .getRequestHeaders() 341 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 342 .iterator() 343 .next(); 344 Assert.assertTrue( 345 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 346 .matcher(apiClientHeaderKey) 347 .matches()); 348 } 349 350 @Test deleteDataPolicyExceptionTest2()351 public void deleteDataPolicyExceptionTest2() throws Exception { 352 ApiException exception = 353 ApiExceptionFactory.createException( 354 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 355 mockService.addException(exception); 356 357 try { 358 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 359 client.deleteDataPolicy(name); 360 Assert.fail("No exception raised"); 361 } catch (InvalidArgumentException e) { 362 // Expected exception. 363 } 364 } 365 366 @Test getDataPolicyTest()367 public void getDataPolicyTest() throws Exception { 368 DataPolicy expectedResponse = 369 DataPolicy.newBuilder() 370 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 371 .setDataPolicyId("dataPolicyId1874146231") 372 .build(); 373 mockService.addResponse(expectedResponse); 374 375 DataPolicyName name = DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]"); 376 377 DataPolicy actualResponse = client.getDataPolicy(name); 378 Assert.assertEquals(expectedResponse, actualResponse); 379 380 List<String> actualRequests = mockService.getRequestPaths(); 381 Assert.assertEquals(1, actualRequests.size()); 382 383 String apiClientHeaderKey = 384 mockService 385 .getRequestHeaders() 386 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 387 .iterator() 388 .next(); 389 Assert.assertTrue( 390 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 391 .matcher(apiClientHeaderKey) 392 .matches()); 393 } 394 395 @Test getDataPolicyExceptionTest()396 public void getDataPolicyExceptionTest() throws Exception { 397 ApiException exception = 398 ApiExceptionFactory.createException( 399 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 400 mockService.addException(exception); 401 402 try { 403 DataPolicyName name = DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]"); 404 client.getDataPolicy(name); 405 Assert.fail("No exception raised"); 406 } catch (InvalidArgumentException e) { 407 // Expected exception. 408 } 409 } 410 411 @Test getDataPolicyTest2()412 public void getDataPolicyTest2() throws Exception { 413 DataPolicy expectedResponse = 414 DataPolicy.newBuilder() 415 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 416 .setDataPolicyId("dataPolicyId1874146231") 417 .build(); 418 mockService.addResponse(expectedResponse); 419 420 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 421 422 DataPolicy actualResponse = client.getDataPolicy(name); 423 Assert.assertEquals(expectedResponse, actualResponse); 424 425 List<String> actualRequests = mockService.getRequestPaths(); 426 Assert.assertEquals(1, actualRequests.size()); 427 428 String apiClientHeaderKey = 429 mockService 430 .getRequestHeaders() 431 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 432 .iterator() 433 .next(); 434 Assert.assertTrue( 435 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 436 .matcher(apiClientHeaderKey) 437 .matches()); 438 } 439 440 @Test getDataPolicyExceptionTest2()441 public void getDataPolicyExceptionTest2() throws Exception { 442 ApiException exception = 443 ApiExceptionFactory.createException( 444 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 445 mockService.addException(exception); 446 447 try { 448 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 449 client.getDataPolicy(name); 450 Assert.fail("No exception raised"); 451 } catch (InvalidArgumentException e) { 452 // Expected exception. 453 } 454 } 455 456 @Test listDataPoliciesTest()457 public void listDataPoliciesTest() throws Exception { 458 DataPolicy responsesElement = DataPolicy.newBuilder().build(); 459 ListDataPoliciesResponse expectedResponse = 460 ListDataPoliciesResponse.newBuilder() 461 .setNextPageToken("") 462 .addAllDataPolicies(Arrays.asList(responsesElement)) 463 .build(); 464 mockService.addResponse(expectedResponse); 465 466 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 467 468 ListDataPoliciesPagedResponse pagedListResponse = client.listDataPolicies(parent); 469 470 List<DataPolicy> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 471 472 Assert.assertEquals(1, resources.size()); 473 Assert.assertEquals(expectedResponse.getDataPoliciesList().get(0), resources.get(0)); 474 475 List<String> actualRequests = mockService.getRequestPaths(); 476 Assert.assertEquals(1, actualRequests.size()); 477 478 String apiClientHeaderKey = 479 mockService 480 .getRequestHeaders() 481 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 482 .iterator() 483 .next(); 484 Assert.assertTrue( 485 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 486 .matcher(apiClientHeaderKey) 487 .matches()); 488 } 489 490 @Test listDataPoliciesExceptionTest()491 public void listDataPoliciesExceptionTest() throws Exception { 492 ApiException exception = 493 ApiExceptionFactory.createException( 494 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 495 mockService.addException(exception); 496 497 try { 498 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 499 client.listDataPolicies(parent); 500 Assert.fail("No exception raised"); 501 } catch (InvalidArgumentException e) { 502 // Expected exception. 503 } 504 } 505 506 @Test listDataPoliciesTest2()507 public void listDataPoliciesTest2() throws Exception { 508 DataPolicy responsesElement = DataPolicy.newBuilder().build(); 509 ListDataPoliciesResponse expectedResponse = 510 ListDataPoliciesResponse.newBuilder() 511 .setNextPageToken("") 512 .addAllDataPolicies(Arrays.asList(responsesElement)) 513 .build(); 514 mockService.addResponse(expectedResponse); 515 516 String parent = "projects/project-5833/locations/location-5833"; 517 518 ListDataPoliciesPagedResponse pagedListResponse = client.listDataPolicies(parent); 519 520 List<DataPolicy> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 521 522 Assert.assertEquals(1, resources.size()); 523 Assert.assertEquals(expectedResponse.getDataPoliciesList().get(0), resources.get(0)); 524 525 List<String> actualRequests = mockService.getRequestPaths(); 526 Assert.assertEquals(1, actualRequests.size()); 527 528 String apiClientHeaderKey = 529 mockService 530 .getRequestHeaders() 531 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 532 .iterator() 533 .next(); 534 Assert.assertTrue( 535 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 536 .matcher(apiClientHeaderKey) 537 .matches()); 538 } 539 540 @Test listDataPoliciesExceptionTest2()541 public void listDataPoliciesExceptionTest2() throws Exception { 542 ApiException exception = 543 ApiExceptionFactory.createException( 544 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 545 mockService.addException(exception); 546 547 try { 548 String parent = "projects/project-5833/locations/location-5833"; 549 client.listDataPolicies(parent); 550 Assert.fail("No exception raised"); 551 } catch (InvalidArgumentException e) { 552 // Expected exception. 553 } 554 } 555 556 @Test getIamPolicyTest()557 public void getIamPolicyTest() throws Exception { 558 Policy expectedResponse = 559 Policy.newBuilder() 560 .setVersion(351608024) 561 .addAllBindings(new ArrayList<Binding>()) 562 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 563 .setEtag(ByteString.EMPTY) 564 .build(); 565 mockService.addResponse(expectedResponse); 566 567 GetIamPolicyRequest request = 568 GetIamPolicyRequest.newBuilder() 569 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 570 .setOptions(GetPolicyOptions.newBuilder().build()) 571 .build(); 572 573 Policy actualResponse = client.getIamPolicy(request); 574 Assert.assertEquals(expectedResponse, actualResponse); 575 576 List<String> actualRequests = mockService.getRequestPaths(); 577 Assert.assertEquals(1, actualRequests.size()); 578 579 String apiClientHeaderKey = 580 mockService 581 .getRequestHeaders() 582 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 583 .iterator() 584 .next(); 585 Assert.assertTrue( 586 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 587 .matcher(apiClientHeaderKey) 588 .matches()); 589 } 590 591 @Test getIamPolicyExceptionTest()592 public void getIamPolicyExceptionTest() throws Exception { 593 ApiException exception = 594 ApiExceptionFactory.createException( 595 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 596 mockService.addException(exception); 597 598 try { 599 GetIamPolicyRequest request = 600 GetIamPolicyRequest.newBuilder() 601 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 602 .setOptions(GetPolicyOptions.newBuilder().build()) 603 .build(); 604 client.getIamPolicy(request); 605 Assert.fail("No exception raised"); 606 } catch (InvalidArgumentException e) { 607 // Expected exception. 608 } 609 } 610 611 @Test setIamPolicyTest()612 public void setIamPolicyTest() throws Exception { 613 Policy expectedResponse = 614 Policy.newBuilder() 615 .setVersion(351608024) 616 .addAllBindings(new ArrayList<Binding>()) 617 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 618 .setEtag(ByteString.EMPTY) 619 .build(); 620 mockService.addResponse(expectedResponse); 621 622 SetIamPolicyRequest request = 623 SetIamPolicyRequest.newBuilder() 624 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 625 .setPolicy(Policy.newBuilder().build()) 626 .setUpdateMask(FieldMask.newBuilder().build()) 627 .build(); 628 629 Policy actualResponse = client.setIamPolicy(request); 630 Assert.assertEquals(expectedResponse, actualResponse); 631 632 List<String> actualRequests = mockService.getRequestPaths(); 633 Assert.assertEquals(1, actualRequests.size()); 634 635 String apiClientHeaderKey = 636 mockService 637 .getRequestHeaders() 638 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 639 .iterator() 640 .next(); 641 Assert.assertTrue( 642 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 643 .matcher(apiClientHeaderKey) 644 .matches()); 645 } 646 647 @Test setIamPolicyExceptionTest()648 public void setIamPolicyExceptionTest() throws Exception { 649 ApiException exception = 650 ApiExceptionFactory.createException( 651 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 652 mockService.addException(exception); 653 654 try { 655 SetIamPolicyRequest request = 656 SetIamPolicyRequest.newBuilder() 657 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 658 .setPolicy(Policy.newBuilder().build()) 659 .setUpdateMask(FieldMask.newBuilder().build()) 660 .build(); 661 client.setIamPolicy(request); 662 Assert.fail("No exception raised"); 663 } catch (InvalidArgumentException e) { 664 // Expected exception. 665 } 666 } 667 668 @Test testIamPermissionsTest()669 public void testIamPermissionsTest() throws Exception { 670 TestIamPermissionsResponse expectedResponse = 671 TestIamPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build(); 672 mockService.addResponse(expectedResponse); 673 674 TestIamPermissionsRequest request = 675 TestIamPermissionsRequest.newBuilder() 676 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 677 .addAllPermissions(new ArrayList<String>()) 678 .build(); 679 680 TestIamPermissionsResponse actualResponse = client.testIamPermissions(request); 681 Assert.assertEquals(expectedResponse, actualResponse); 682 683 List<String> actualRequests = mockService.getRequestPaths(); 684 Assert.assertEquals(1, actualRequests.size()); 685 686 String apiClientHeaderKey = 687 mockService 688 .getRequestHeaders() 689 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 690 .iterator() 691 .next(); 692 Assert.assertTrue( 693 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 694 .matcher(apiClientHeaderKey) 695 .matches()); 696 } 697 698 @Test testIamPermissionsExceptionTest()699 public void testIamPermissionsExceptionTest() throws Exception { 700 ApiException exception = 701 ApiExceptionFactory.createException( 702 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 703 mockService.addException(exception); 704 705 try { 706 TestIamPermissionsRequest request = 707 TestIamPermissionsRequest.newBuilder() 708 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 709 .addAllPermissions(new ArrayList<String>()) 710 .build(); 711 client.testIamPermissions(request); 712 Assert.fail("No exception raised"); 713 } catch (InvalidArgumentException e) { 714 // Expected exception. 715 } 716 } 717 } 718