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.aiplatform.v1beta1; 18 19 import static com.google.cloud.aiplatform.v1beta1.PredictionServiceClient.ListLocationsPagedResponse; 20 21 import com.google.api.HttpBody; 22 import com.google.api.gax.core.NoCredentialsProvider; 23 import com.google.api.gax.grpc.GaxGrpcProperties; 24 import com.google.api.gax.grpc.testing.LocalChannelProvider; 25 import com.google.api.gax.grpc.testing.MockGrpcService; 26 import com.google.api.gax.grpc.testing.MockServiceHelper; 27 import com.google.api.gax.rpc.ApiClientHeaderProvider; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.cloud.location.GetLocationRequest; 30 import com.google.cloud.location.ListLocationsRequest; 31 import com.google.cloud.location.ListLocationsResponse; 32 import com.google.cloud.location.Location; 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.AbstractMessage; 43 import com.google.protobuf.Any; 44 import com.google.protobuf.ByteString; 45 import com.google.protobuf.FieldMask; 46 import com.google.protobuf.Value; 47 import io.grpc.StatusRuntimeException; 48 import java.io.IOException; 49 import java.util.ArrayList; 50 import java.util.Arrays; 51 import java.util.HashMap; 52 import java.util.List; 53 import java.util.UUID; 54 import javax.annotation.Generated; 55 import org.junit.After; 56 import org.junit.AfterClass; 57 import org.junit.Assert; 58 import org.junit.Before; 59 import org.junit.BeforeClass; 60 import org.junit.Test; 61 62 @Generated("by gapic-generator-java") 63 public class PredictionServiceClientTest { 64 private static MockIAMPolicy mockIAMPolicy; 65 private static MockLocations mockLocations; 66 private static MockPredictionService mockPredictionService; 67 private static MockServiceHelper mockServiceHelper; 68 private LocalChannelProvider channelProvider; 69 private PredictionServiceClient client; 70 71 @BeforeClass startStaticServer()72 public static void startStaticServer() { 73 mockPredictionService = new MockPredictionService(); 74 mockLocations = new MockLocations(); 75 mockIAMPolicy = new MockIAMPolicy(); 76 mockServiceHelper = 77 new MockServiceHelper( 78 UUID.randomUUID().toString(), 79 Arrays.<MockGrpcService>asList(mockPredictionService, mockLocations, mockIAMPolicy)); 80 mockServiceHelper.start(); 81 } 82 83 @AfterClass stopServer()84 public static void stopServer() { 85 mockServiceHelper.stop(); 86 } 87 88 @Before setUp()89 public void setUp() throws IOException { 90 mockServiceHelper.reset(); 91 channelProvider = mockServiceHelper.createChannelProvider(); 92 PredictionServiceSettings settings = 93 PredictionServiceSettings.newBuilder() 94 .setTransportChannelProvider(channelProvider) 95 .setCredentialsProvider(NoCredentialsProvider.create()) 96 .build(); 97 client = PredictionServiceClient.create(settings); 98 } 99 100 @After tearDown()101 public void tearDown() throws Exception { 102 client.close(); 103 } 104 105 @Test predictTest()106 public void predictTest() throws Exception { 107 PredictResponse expectedResponse = 108 PredictResponse.newBuilder() 109 .addAllPredictions(new ArrayList<Value>()) 110 .setDeployedModelId("deployedModelId-1817547906") 111 .setModel(ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]").toString()) 112 .setModelVersionId("modelVersionId-2006125846") 113 .setModelDisplayName("modelDisplayName1578770308") 114 .build(); 115 mockPredictionService.addResponse(expectedResponse); 116 117 EndpointName endpoint = 118 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]"); 119 List<Value> instances = new ArrayList<>(); 120 Value parameters = Value.newBuilder().setBoolValue(true).build(); 121 122 PredictResponse actualResponse = client.predict(endpoint, instances, parameters); 123 Assert.assertEquals(expectedResponse, actualResponse); 124 125 List<AbstractMessage> actualRequests = mockPredictionService.getRequests(); 126 Assert.assertEquals(1, actualRequests.size()); 127 PredictRequest actualRequest = ((PredictRequest) actualRequests.get(0)); 128 129 Assert.assertEquals(endpoint.toString(), actualRequest.getEndpoint()); 130 Assert.assertEquals(instances, actualRequest.getInstancesList()); 131 Assert.assertEquals(parameters, actualRequest.getParameters()); 132 Assert.assertTrue( 133 channelProvider.isHeaderSent( 134 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 135 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 136 } 137 138 @Test predictExceptionTest()139 public void predictExceptionTest() throws Exception { 140 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 141 mockPredictionService.addException(exception); 142 143 try { 144 EndpointName endpoint = 145 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]"); 146 List<Value> instances = new ArrayList<>(); 147 Value parameters = Value.newBuilder().setBoolValue(true).build(); 148 client.predict(endpoint, instances, parameters); 149 Assert.fail("No exception raised"); 150 } catch (InvalidArgumentException e) { 151 // Expected exception. 152 } 153 } 154 155 @Test predictTest2()156 public void predictTest2() throws Exception { 157 PredictResponse expectedResponse = 158 PredictResponse.newBuilder() 159 .addAllPredictions(new ArrayList<Value>()) 160 .setDeployedModelId("deployedModelId-1817547906") 161 .setModel(ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]").toString()) 162 .setModelVersionId("modelVersionId-2006125846") 163 .setModelDisplayName("modelDisplayName1578770308") 164 .build(); 165 mockPredictionService.addResponse(expectedResponse); 166 167 String endpoint = "endpoint1741102485"; 168 List<Value> instances = new ArrayList<>(); 169 Value parameters = Value.newBuilder().setBoolValue(true).build(); 170 171 PredictResponse actualResponse = client.predict(endpoint, instances, parameters); 172 Assert.assertEquals(expectedResponse, actualResponse); 173 174 List<AbstractMessage> actualRequests = mockPredictionService.getRequests(); 175 Assert.assertEquals(1, actualRequests.size()); 176 PredictRequest actualRequest = ((PredictRequest) actualRequests.get(0)); 177 178 Assert.assertEquals(endpoint, actualRequest.getEndpoint()); 179 Assert.assertEquals(instances, actualRequest.getInstancesList()); 180 Assert.assertEquals(parameters, actualRequest.getParameters()); 181 Assert.assertTrue( 182 channelProvider.isHeaderSent( 183 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 184 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 185 } 186 187 @Test predictExceptionTest2()188 public void predictExceptionTest2() throws Exception { 189 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 190 mockPredictionService.addException(exception); 191 192 try { 193 String endpoint = "endpoint1741102485"; 194 List<Value> instances = new ArrayList<>(); 195 Value parameters = Value.newBuilder().setBoolValue(true).build(); 196 client.predict(endpoint, instances, parameters); 197 Assert.fail("No exception raised"); 198 } catch (InvalidArgumentException e) { 199 // Expected exception. 200 } 201 } 202 203 @Test rawPredictTest()204 public void rawPredictTest() throws Exception { 205 HttpBody expectedResponse = 206 HttpBody.newBuilder() 207 .setContentType("contentType-389131437") 208 .setData(ByteString.EMPTY) 209 .addAllExtensions(new ArrayList<Any>()) 210 .build(); 211 mockPredictionService.addResponse(expectedResponse); 212 213 EndpointName endpoint = 214 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]"); 215 HttpBody httpBody = HttpBody.newBuilder().build(); 216 217 HttpBody actualResponse = client.rawPredict(endpoint, httpBody); 218 Assert.assertEquals(expectedResponse, actualResponse); 219 220 List<AbstractMessage> actualRequests = mockPredictionService.getRequests(); 221 Assert.assertEquals(1, actualRequests.size()); 222 RawPredictRequest actualRequest = ((RawPredictRequest) actualRequests.get(0)); 223 224 Assert.assertEquals(endpoint.toString(), actualRequest.getEndpoint()); 225 Assert.assertEquals(httpBody, actualRequest.getHttpBody()); 226 Assert.assertTrue( 227 channelProvider.isHeaderSent( 228 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 229 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 230 } 231 232 @Test rawPredictExceptionTest()233 public void rawPredictExceptionTest() throws Exception { 234 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 235 mockPredictionService.addException(exception); 236 237 try { 238 EndpointName endpoint = 239 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]"); 240 HttpBody httpBody = HttpBody.newBuilder().build(); 241 client.rawPredict(endpoint, httpBody); 242 Assert.fail("No exception raised"); 243 } catch (InvalidArgumentException e) { 244 // Expected exception. 245 } 246 } 247 248 @Test rawPredictTest2()249 public void rawPredictTest2() throws Exception { 250 HttpBody expectedResponse = 251 HttpBody.newBuilder() 252 .setContentType("contentType-389131437") 253 .setData(ByteString.EMPTY) 254 .addAllExtensions(new ArrayList<Any>()) 255 .build(); 256 mockPredictionService.addResponse(expectedResponse); 257 258 String endpoint = "endpoint1741102485"; 259 HttpBody httpBody = HttpBody.newBuilder().build(); 260 261 HttpBody actualResponse = client.rawPredict(endpoint, httpBody); 262 Assert.assertEquals(expectedResponse, actualResponse); 263 264 List<AbstractMessage> actualRequests = mockPredictionService.getRequests(); 265 Assert.assertEquals(1, actualRequests.size()); 266 RawPredictRequest actualRequest = ((RawPredictRequest) actualRequests.get(0)); 267 268 Assert.assertEquals(endpoint, actualRequest.getEndpoint()); 269 Assert.assertEquals(httpBody, actualRequest.getHttpBody()); 270 Assert.assertTrue( 271 channelProvider.isHeaderSent( 272 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 273 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 274 } 275 276 @Test rawPredictExceptionTest2()277 public void rawPredictExceptionTest2() throws Exception { 278 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 279 mockPredictionService.addException(exception); 280 281 try { 282 String endpoint = "endpoint1741102485"; 283 HttpBody httpBody = HttpBody.newBuilder().build(); 284 client.rawPredict(endpoint, httpBody); 285 Assert.fail("No exception raised"); 286 } catch (InvalidArgumentException e) { 287 // Expected exception. 288 } 289 } 290 291 @Test explainTest()292 public void explainTest() throws Exception { 293 ExplainResponse expectedResponse = 294 ExplainResponse.newBuilder() 295 .addAllExplanations(new ArrayList<Explanation>()) 296 .setDeployedModelId("deployedModelId-1817547906") 297 .addAllPredictions(new ArrayList<Value>()) 298 .build(); 299 mockPredictionService.addResponse(expectedResponse); 300 301 EndpointName endpoint = 302 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]"); 303 List<Value> instances = new ArrayList<>(); 304 Value parameters = Value.newBuilder().setBoolValue(true).build(); 305 String deployedModelId = "deployedModelId-1817547906"; 306 307 ExplainResponse actualResponse = 308 client.explain(endpoint, instances, parameters, deployedModelId); 309 Assert.assertEquals(expectedResponse, actualResponse); 310 311 List<AbstractMessage> actualRequests = mockPredictionService.getRequests(); 312 Assert.assertEquals(1, actualRequests.size()); 313 ExplainRequest actualRequest = ((ExplainRequest) actualRequests.get(0)); 314 315 Assert.assertEquals(endpoint.toString(), actualRequest.getEndpoint()); 316 Assert.assertEquals(instances, actualRequest.getInstancesList()); 317 Assert.assertEquals(parameters, actualRequest.getParameters()); 318 Assert.assertEquals(deployedModelId, actualRequest.getDeployedModelId()); 319 Assert.assertTrue( 320 channelProvider.isHeaderSent( 321 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 322 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 323 } 324 325 @Test explainExceptionTest()326 public void explainExceptionTest() throws Exception { 327 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 328 mockPredictionService.addException(exception); 329 330 try { 331 EndpointName endpoint = 332 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]"); 333 List<Value> instances = new ArrayList<>(); 334 Value parameters = Value.newBuilder().setBoolValue(true).build(); 335 String deployedModelId = "deployedModelId-1817547906"; 336 client.explain(endpoint, instances, parameters, deployedModelId); 337 Assert.fail("No exception raised"); 338 } catch (InvalidArgumentException e) { 339 // Expected exception. 340 } 341 } 342 343 @Test explainTest2()344 public void explainTest2() throws Exception { 345 ExplainResponse expectedResponse = 346 ExplainResponse.newBuilder() 347 .addAllExplanations(new ArrayList<Explanation>()) 348 .setDeployedModelId("deployedModelId-1817547906") 349 .addAllPredictions(new ArrayList<Value>()) 350 .build(); 351 mockPredictionService.addResponse(expectedResponse); 352 353 String endpoint = "endpoint1741102485"; 354 List<Value> instances = new ArrayList<>(); 355 Value parameters = Value.newBuilder().setBoolValue(true).build(); 356 String deployedModelId = "deployedModelId-1817547906"; 357 358 ExplainResponse actualResponse = 359 client.explain(endpoint, instances, parameters, deployedModelId); 360 Assert.assertEquals(expectedResponse, actualResponse); 361 362 List<AbstractMessage> actualRequests = mockPredictionService.getRequests(); 363 Assert.assertEquals(1, actualRequests.size()); 364 ExplainRequest actualRequest = ((ExplainRequest) actualRequests.get(0)); 365 366 Assert.assertEquals(endpoint, actualRequest.getEndpoint()); 367 Assert.assertEquals(instances, actualRequest.getInstancesList()); 368 Assert.assertEquals(parameters, actualRequest.getParameters()); 369 Assert.assertEquals(deployedModelId, actualRequest.getDeployedModelId()); 370 Assert.assertTrue( 371 channelProvider.isHeaderSent( 372 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 373 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 374 } 375 376 @Test explainExceptionTest2()377 public void explainExceptionTest2() throws Exception { 378 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 379 mockPredictionService.addException(exception); 380 381 try { 382 String endpoint = "endpoint1741102485"; 383 List<Value> instances = new ArrayList<>(); 384 Value parameters = Value.newBuilder().setBoolValue(true).build(); 385 String deployedModelId = "deployedModelId-1817547906"; 386 client.explain(endpoint, instances, parameters, deployedModelId); 387 Assert.fail("No exception raised"); 388 } catch (InvalidArgumentException e) { 389 // Expected exception. 390 } 391 } 392 393 @Test listLocationsTest()394 public void listLocationsTest() throws Exception { 395 Location responsesElement = Location.newBuilder().build(); 396 ListLocationsResponse expectedResponse = 397 ListLocationsResponse.newBuilder() 398 .setNextPageToken("") 399 .addAllLocations(Arrays.asList(responsesElement)) 400 .build(); 401 mockLocations.addResponse(expectedResponse); 402 403 ListLocationsRequest request = 404 ListLocationsRequest.newBuilder() 405 .setName("name3373707") 406 .setFilter("filter-1274492040") 407 .setPageSize(883849137) 408 .setPageToken("pageToken873572522") 409 .build(); 410 411 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 412 413 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 414 415 Assert.assertEquals(1, resources.size()); 416 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 417 418 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 419 Assert.assertEquals(1, actualRequests.size()); 420 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 421 422 Assert.assertEquals(request.getName(), actualRequest.getName()); 423 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 424 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 425 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 426 Assert.assertTrue( 427 channelProvider.isHeaderSent( 428 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 429 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 430 } 431 432 @Test listLocationsExceptionTest()433 public void listLocationsExceptionTest() throws Exception { 434 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 435 mockLocations.addException(exception); 436 437 try { 438 ListLocationsRequest request = 439 ListLocationsRequest.newBuilder() 440 .setName("name3373707") 441 .setFilter("filter-1274492040") 442 .setPageSize(883849137) 443 .setPageToken("pageToken873572522") 444 .build(); 445 client.listLocations(request); 446 Assert.fail("No exception raised"); 447 } catch (InvalidArgumentException e) { 448 // Expected exception. 449 } 450 } 451 452 @Test getLocationTest()453 public void getLocationTest() throws Exception { 454 Location expectedResponse = 455 Location.newBuilder() 456 .setName("name3373707") 457 .setLocationId("locationId1541836720") 458 .setDisplayName("displayName1714148973") 459 .putAllLabels(new HashMap<String, String>()) 460 .setMetadata(Any.newBuilder().build()) 461 .build(); 462 mockLocations.addResponse(expectedResponse); 463 464 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 465 466 Location actualResponse = client.getLocation(request); 467 Assert.assertEquals(expectedResponse, actualResponse); 468 469 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 470 Assert.assertEquals(1, actualRequests.size()); 471 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 472 473 Assert.assertEquals(request.getName(), actualRequest.getName()); 474 Assert.assertTrue( 475 channelProvider.isHeaderSent( 476 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 477 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 478 } 479 480 @Test getLocationExceptionTest()481 public void getLocationExceptionTest() throws Exception { 482 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 483 mockLocations.addException(exception); 484 485 try { 486 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 487 client.getLocation(request); 488 Assert.fail("No exception raised"); 489 } catch (InvalidArgumentException e) { 490 // Expected exception. 491 } 492 } 493 494 @Test setIamPolicyTest()495 public void setIamPolicyTest() throws Exception { 496 Policy expectedResponse = 497 Policy.newBuilder() 498 .setVersion(351608024) 499 .addAllBindings(new ArrayList<Binding>()) 500 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 501 .setEtag(ByteString.EMPTY) 502 .build(); 503 mockIAMPolicy.addResponse(expectedResponse); 504 505 SetIamPolicyRequest request = 506 SetIamPolicyRequest.newBuilder() 507 .setResource( 508 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]") 509 .toString()) 510 .setPolicy(Policy.newBuilder().build()) 511 .setUpdateMask(FieldMask.newBuilder().build()) 512 .build(); 513 514 Policy actualResponse = client.setIamPolicy(request); 515 Assert.assertEquals(expectedResponse, actualResponse); 516 517 List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests(); 518 Assert.assertEquals(1, actualRequests.size()); 519 SetIamPolicyRequest actualRequest = ((SetIamPolicyRequest) actualRequests.get(0)); 520 521 Assert.assertEquals(request.getResource(), actualRequest.getResource()); 522 Assert.assertEquals(request.getPolicy(), actualRequest.getPolicy()); 523 Assert.assertEquals(request.getUpdateMask(), actualRequest.getUpdateMask()); 524 Assert.assertTrue( 525 channelProvider.isHeaderSent( 526 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 527 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 528 } 529 530 @Test setIamPolicyExceptionTest()531 public void setIamPolicyExceptionTest() throws Exception { 532 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 533 mockIAMPolicy.addException(exception); 534 535 try { 536 SetIamPolicyRequest request = 537 SetIamPolicyRequest.newBuilder() 538 .setResource( 539 EndpointName.ofProjectLocationEndpointName( 540 "[PROJECT]", "[LOCATION]", "[ENDPOINT]") 541 .toString()) 542 .setPolicy(Policy.newBuilder().build()) 543 .setUpdateMask(FieldMask.newBuilder().build()) 544 .build(); 545 client.setIamPolicy(request); 546 Assert.fail("No exception raised"); 547 } catch (InvalidArgumentException e) { 548 // Expected exception. 549 } 550 } 551 552 @Test getIamPolicyTest()553 public void getIamPolicyTest() throws Exception { 554 Policy expectedResponse = 555 Policy.newBuilder() 556 .setVersion(351608024) 557 .addAllBindings(new ArrayList<Binding>()) 558 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 559 .setEtag(ByteString.EMPTY) 560 .build(); 561 mockIAMPolicy.addResponse(expectedResponse); 562 563 GetIamPolicyRequest request = 564 GetIamPolicyRequest.newBuilder() 565 .setResource( 566 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]") 567 .toString()) 568 .setOptions(GetPolicyOptions.newBuilder().build()) 569 .build(); 570 571 Policy actualResponse = client.getIamPolicy(request); 572 Assert.assertEquals(expectedResponse, actualResponse); 573 574 List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests(); 575 Assert.assertEquals(1, actualRequests.size()); 576 GetIamPolicyRequest actualRequest = ((GetIamPolicyRequest) actualRequests.get(0)); 577 578 Assert.assertEquals(request.getResource(), actualRequest.getResource()); 579 Assert.assertEquals(request.getOptions(), actualRequest.getOptions()); 580 Assert.assertTrue( 581 channelProvider.isHeaderSent( 582 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 583 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 584 } 585 586 @Test getIamPolicyExceptionTest()587 public void getIamPolicyExceptionTest() throws Exception { 588 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 589 mockIAMPolicy.addException(exception); 590 591 try { 592 GetIamPolicyRequest request = 593 GetIamPolicyRequest.newBuilder() 594 .setResource( 595 EndpointName.ofProjectLocationEndpointName( 596 "[PROJECT]", "[LOCATION]", "[ENDPOINT]") 597 .toString()) 598 .setOptions(GetPolicyOptions.newBuilder().build()) 599 .build(); 600 client.getIamPolicy(request); 601 Assert.fail("No exception raised"); 602 } catch (InvalidArgumentException e) { 603 // Expected exception. 604 } 605 } 606 607 @Test testIamPermissionsTest()608 public void testIamPermissionsTest() throws Exception { 609 TestIamPermissionsResponse expectedResponse = 610 TestIamPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build(); 611 mockIAMPolicy.addResponse(expectedResponse); 612 613 TestIamPermissionsRequest request = 614 TestIamPermissionsRequest.newBuilder() 615 .setResource( 616 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]") 617 .toString()) 618 .addAllPermissions(new ArrayList<String>()) 619 .build(); 620 621 TestIamPermissionsResponse actualResponse = client.testIamPermissions(request); 622 Assert.assertEquals(expectedResponse, actualResponse); 623 624 List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests(); 625 Assert.assertEquals(1, actualRequests.size()); 626 TestIamPermissionsRequest actualRequest = ((TestIamPermissionsRequest) actualRequests.get(0)); 627 628 Assert.assertEquals(request.getResource(), actualRequest.getResource()); 629 Assert.assertEquals(request.getPermissionsList(), actualRequest.getPermissionsList()); 630 Assert.assertTrue( 631 channelProvider.isHeaderSent( 632 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 633 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 634 } 635 636 @Test testIamPermissionsExceptionTest()637 public void testIamPermissionsExceptionTest() throws Exception { 638 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 639 mockIAMPolicy.addException(exception); 640 641 try { 642 TestIamPermissionsRequest request = 643 TestIamPermissionsRequest.newBuilder() 644 .setResource( 645 EndpointName.ofProjectLocationEndpointName( 646 "[PROJECT]", "[LOCATION]", "[ENDPOINT]") 647 .toString()) 648 .addAllPermissions(new ArrayList<String>()) 649 .build(); 650 client.testIamPermissions(request); 651 Assert.fail("No exception raised"); 652 } catch (InvalidArgumentException e) { 653 // Expected exception. 654 } 655 } 656 } 657