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.v1beta1; 18 19 import static com.google.cloud.bigquery.datapolicies.v1beta1.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.v1beta1.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 deleteDataPolicyTest()240 public void deleteDataPolicyTest() throws Exception { 241 Empty expectedResponse = Empty.newBuilder().build(); 242 mockService.addResponse(expectedResponse); 243 244 DataPolicyName name = DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]"); 245 246 client.deleteDataPolicy(name); 247 248 List<String> actualRequests = mockService.getRequestPaths(); 249 Assert.assertEquals(1, actualRequests.size()); 250 251 String apiClientHeaderKey = 252 mockService 253 .getRequestHeaders() 254 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 255 .iterator() 256 .next(); 257 Assert.assertTrue( 258 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 259 .matcher(apiClientHeaderKey) 260 .matches()); 261 } 262 263 @Test deleteDataPolicyExceptionTest()264 public void deleteDataPolicyExceptionTest() throws Exception { 265 ApiException exception = 266 ApiExceptionFactory.createException( 267 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 268 mockService.addException(exception); 269 270 try { 271 DataPolicyName name = DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]"); 272 client.deleteDataPolicy(name); 273 Assert.fail("No exception raised"); 274 } catch (InvalidArgumentException e) { 275 // Expected exception. 276 } 277 } 278 279 @Test deleteDataPolicyTest2()280 public void deleteDataPolicyTest2() throws Exception { 281 Empty expectedResponse = Empty.newBuilder().build(); 282 mockService.addResponse(expectedResponse); 283 284 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 285 286 client.deleteDataPolicy(name); 287 288 List<String> actualRequests = mockService.getRequestPaths(); 289 Assert.assertEquals(1, actualRequests.size()); 290 291 String apiClientHeaderKey = 292 mockService 293 .getRequestHeaders() 294 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 295 .iterator() 296 .next(); 297 Assert.assertTrue( 298 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 299 .matcher(apiClientHeaderKey) 300 .matches()); 301 } 302 303 @Test deleteDataPolicyExceptionTest2()304 public void deleteDataPolicyExceptionTest2() throws Exception { 305 ApiException exception = 306 ApiExceptionFactory.createException( 307 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 308 mockService.addException(exception); 309 310 try { 311 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 312 client.deleteDataPolicy(name); 313 Assert.fail("No exception raised"); 314 } catch (InvalidArgumentException e) { 315 // Expected exception. 316 } 317 } 318 319 @Test getDataPolicyTest()320 public void getDataPolicyTest() throws Exception { 321 DataPolicy expectedResponse = 322 DataPolicy.newBuilder() 323 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 324 .setDataPolicyId("dataPolicyId1874146231") 325 .build(); 326 mockService.addResponse(expectedResponse); 327 328 DataPolicyName name = DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]"); 329 330 DataPolicy actualResponse = client.getDataPolicy(name); 331 Assert.assertEquals(expectedResponse, actualResponse); 332 333 List<String> actualRequests = mockService.getRequestPaths(); 334 Assert.assertEquals(1, actualRequests.size()); 335 336 String apiClientHeaderKey = 337 mockService 338 .getRequestHeaders() 339 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 340 .iterator() 341 .next(); 342 Assert.assertTrue( 343 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 344 .matcher(apiClientHeaderKey) 345 .matches()); 346 } 347 348 @Test getDataPolicyExceptionTest()349 public void getDataPolicyExceptionTest() throws Exception { 350 ApiException exception = 351 ApiExceptionFactory.createException( 352 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 353 mockService.addException(exception); 354 355 try { 356 DataPolicyName name = DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]"); 357 client.getDataPolicy(name); 358 Assert.fail("No exception raised"); 359 } catch (InvalidArgumentException e) { 360 // Expected exception. 361 } 362 } 363 364 @Test getDataPolicyTest2()365 public void getDataPolicyTest2() throws Exception { 366 DataPolicy expectedResponse = 367 DataPolicy.newBuilder() 368 .setName(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 369 .setDataPolicyId("dataPolicyId1874146231") 370 .build(); 371 mockService.addResponse(expectedResponse); 372 373 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 374 375 DataPolicy actualResponse = client.getDataPolicy(name); 376 Assert.assertEquals(expectedResponse, actualResponse); 377 378 List<String> actualRequests = mockService.getRequestPaths(); 379 Assert.assertEquals(1, actualRequests.size()); 380 381 String apiClientHeaderKey = 382 mockService 383 .getRequestHeaders() 384 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 385 .iterator() 386 .next(); 387 Assert.assertTrue( 388 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 389 .matcher(apiClientHeaderKey) 390 .matches()); 391 } 392 393 @Test getDataPolicyExceptionTest2()394 public void getDataPolicyExceptionTest2() throws Exception { 395 ApiException exception = 396 ApiExceptionFactory.createException( 397 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 398 mockService.addException(exception); 399 400 try { 401 String name = "projects/project-2886/locations/location-2886/dataPolicies/dataPolicie-2886"; 402 client.getDataPolicy(name); 403 Assert.fail("No exception raised"); 404 } catch (InvalidArgumentException e) { 405 // Expected exception. 406 } 407 } 408 409 @Test listDataPoliciesTest()410 public void listDataPoliciesTest() throws Exception { 411 DataPolicy responsesElement = DataPolicy.newBuilder().build(); 412 ListDataPoliciesResponse expectedResponse = 413 ListDataPoliciesResponse.newBuilder() 414 .setNextPageToken("") 415 .addAllDataPolicies(Arrays.asList(responsesElement)) 416 .build(); 417 mockService.addResponse(expectedResponse); 418 419 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 420 421 ListDataPoliciesPagedResponse pagedListResponse = client.listDataPolicies(parent); 422 423 List<DataPolicy> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 424 425 Assert.assertEquals(1, resources.size()); 426 Assert.assertEquals(expectedResponse.getDataPoliciesList().get(0), resources.get(0)); 427 428 List<String> actualRequests = mockService.getRequestPaths(); 429 Assert.assertEquals(1, actualRequests.size()); 430 431 String apiClientHeaderKey = 432 mockService 433 .getRequestHeaders() 434 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 435 .iterator() 436 .next(); 437 Assert.assertTrue( 438 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 439 .matcher(apiClientHeaderKey) 440 .matches()); 441 } 442 443 @Test listDataPoliciesExceptionTest()444 public void listDataPoliciesExceptionTest() throws Exception { 445 ApiException exception = 446 ApiExceptionFactory.createException( 447 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 448 mockService.addException(exception); 449 450 try { 451 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 452 client.listDataPolicies(parent); 453 Assert.fail("No exception raised"); 454 } catch (InvalidArgumentException e) { 455 // Expected exception. 456 } 457 } 458 459 @Test listDataPoliciesTest2()460 public void listDataPoliciesTest2() throws Exception { 461 DataPolicy responsesElement = DataPolicy.newBuilder().build(); 462 ListDataPoliciesResponse expectedResponse = 463 ListDataPoliciesResponse.newBuilder() 464 .setNextPageToken("") 465 .addAllDataPolicies(Arrays.asList(responsesElement)) 466 .build(); 467 mockService.addResponse(expectedResponse); 468 469 String parent = "projects/project-5833/locations/location-5833"; 470 471 ListDataPoliciesPagedResponse pagedListResponse = client.listDataPolicies(parent); 472 473 List<DataPolicy> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 474 475 Assert.assertEquals(1, resources.size()); 476 Assert.assertEquals(expectedResponse.getDataPoliciesList().get(0), resources.get(0)); 477 478 List<String> actualRequests = mockService.getRequestPaths(); 479 Assert.assertEquals(1, actualRequests.size()); 480 481 String apiClientHeaderKey = 482 mockService 483 .getRequestHeaders() 484 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 485 .iterator() 486 .next(); 487 Assert.assertTrue( 488 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 489 .matcher(apiClientHeaderKey) 490 .matches()); 491 } 492 493 @Test listDataPoliciesExceptionTest2()494 public void listDataPoliciesExceptionTest2() throws Exception { 495 ApiException exception = 496 ApiExceptionFactory.createException( 497 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 498 mockService.addException(exception); 499 500 try { 501 String parent = "projects/project-5833/locations/location-5833"; 502 client.listDataPolicies(parent); 503 Assert.fail("No exception raised"); 504 } catch (InvalidArgumentException e) { 505 // Expected exception. 506 } 507 } 508 509 @Test getIamPolicyTest()510 public void getIamPolicyTest() throws Exception { 511 Policy expectedResponse = 512 Policy.newBuilder() 513 .setVersion(351608024) 514 .addAllBindings(new ArrayList<Binding>()) 515 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 516 .setEtag(ByteString.EMPTY) 517 .build(); 518 mockService.addResponse(expectedResponse); 519 520 GetIamPolicyRequest request = 521 GetIamPolicyRequest.newBuilder() 522 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 523 .setOptions(GetPolicyOptions.newBuilder().build()) 524 .build(); 525 526 Policy actualResponse = client.getIamPolicy(request); 527 Assert.assertEquals(expectedResponse, actualResponse); 528 529 List<String> actualRequests = mockService.getRequestPaths(); 530 Assert.assertEquals(1, actualRequests.size()); 531 532 String apiClientHeaderKey = 533 mockService 534 .getRequestHeaders() 535 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 536 .iterator() 537 .next(); 538 Assert.assertTrue( 539 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 540 .matcher(apiClientHeaderKey) 541 .matches()); 542 } 543 544 @Test getIamPolicyExceptionTest()545 public void getIamPolicyExceptionTest() throws Exception { 546 ApiException exception = 547 ApiExceptionFactory.createException( 548 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 549 mockService.addException(exception); 550 551 try { 552 GetIamPolicyRequest request = 553 GetIamPolicyRequest.newBuilder() 554 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 555 .setOptions(GetPolicyOptions.newBuilder().build()) 556 .build(); 557 client.getIamPolicy(request); 558 Assert.fail("No exception raised"); 559 } catch (InvalidArgumentException e) { 560 // Expected exception. 561 } 562 } 563 564 @Test setIamPolicyTest()565 public void setIamPolicyTest() throws Exception { 566 Policy expectedResponse = 567 Policy.newBuilder() 568 .setVersion(351608024) 569 .addAllBindings(new ArrayList<Binding>()) 570 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 571 .setEtag(ByteString.EMPTY) 572 .build(); 573 mockService.addResponse(expectedResponse); 574 575 SetIamPolicyRequest request = 576 SetIamPolicyRequest.newBuilder() 577 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 578 .setPolicy(Policy.newBuilder().build()) 579 .setUpdateMask(FieldMask.newBuilder().build()) 580 .build(); 581 582 Policy actualResponse = client.setIamPolicy(request); 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 setIamPolicyExceptionTest()601 public void setIamPolicyExceptionTest() 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 SetIamPolicyRequest request = 609 SetIamPolicyRequest.newBuilder() 610 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 611 .setPolicy(Policy.newBuilder().build()) 612 .setUpdateMask(FieldMask.newBuilder().build()) 613 .build(); 614 client.setIamPolicy(request); 615 Assert.fail("No exception raised"); 616 } catch (InvalidArgumentException e) { 617 // Expected exception. 618 } 619 } 620 621 @Test testIamPermissionsTest()622 public void testIamPermissionsTest() throws Exception { 623 TestIamPermissionsResponse expectedResponse = 624 TestIamPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build(); 625 mockService.addResponse(expectedResponse); 626 627 TestIamPermissionsRequest request = 628 TestIamPermissionsRequest.newBuilder() 629 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 630 .addAllPermissions(new ArrayList<String>()) 631 .build(); 632 633 TestIamPermissionsResponse actualResponse = client.testIamPermissions(request); 634 Assert.assertEquals(expectedResponse, actualResponse); 635 636 List<String> actualRequests = mockService.getRequestPaths(); 637 Assert.assertEquals(1, actualRequests.size()); 638 639 String apiClientHeaderKey = 640 mockService 641 .getRequestHeaders() 642 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 643 .iterator() 644 .next(); 645 Assert.assertTrue( 646 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 647 .matcher(apiClientHeaderKey) 648 .matches()); 649 } 650 651 @Test testIamPermissionsExceptionTest()652 public void testIamPermissionsExceptionTest() throws Exception { 653 ApiException exception = 654 ApiExceptionFactory.createException( 655 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 656 mockService.addException(exception); 657 658 try { 659 TestIamPermissionsRequest request = 660 TestIamPermissionsRequest.newBuilder() 661 .setResource(DataPolicyName.of("[PROJECT]", "[LOCATION]", "[DATA_POLICY]").toString()) 662 .addAllPermissions(new ArrayList<String>()) 663 .build(); 664 client.testIamPermissions(request); 665 Assert.fail("No exception raised"); 666 } catch (InvalidArgumentException e) { 667 // Expected exception. 668 } 669 } 670 } 671