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.v1; 18 19 import static com.google.cloud.aiplatform.v1.FeaturestoreOnlineServingServiceClient.ListLocationsPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.grpc.GaxGrpcProperties; 23 import com.google.api.gax.grpc.testing.LocalChannelProvider; 24 import com.google.api.gax.grpc.testing.MockGrpcService; 25 import com.google.api.gax.grpc.testing.MockServiceHelper; 26 import com.google.api.gax.grpc.testing.MockStreamObserver; 27 import com.google.api.gax.rpc.ApiClientHeaderProvider; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.api.gax.rpc.ServerStreamingCallable; 30 import com.google.api.gax.rpc.StatusCode; 31 import com.google.cloud.location.GetLocationRequest; 32 import com.google.cloud.location.ListLocationsRequest; 33 import com.google.cloud.location.ListLocationsResponse; 34 import com.google.cloud.location.Location; 35 import com.google.common.collect.Lists; 36 import com.google.iam.v1.AuditConfig; 37 import com.google.iam.v1.Binding; 38 import com.google.iam.v1.GetIamPolicyRequest; 39 import com.google.iam.v1.GetPolicyOptions; 40 import com.google.iam.v1.Policy; 41 import com.google.iam.v1.SetIamPolicyRequest; 42 import com.google.iam.v1.TestIamPermissionsRequest; 43 import com.google.iam.v1.TestIamPermissionsResponse; 44 import com.google.protobuf.AbstractMessage; 45 import com.google.protobuf.Any; 46 import com.google.protobuf.ByteString; 47 import com.google.protobuf.FieldMask; 48 import io.grpc.StatusRuntimeException; 49 import java.io.IOException; 50 import java.util.ArrayList; 51 import java.util.Arrays; 52 import java.util.HashMap; 53 import java.util.List; 54 import java.util.UUID; 55 import java.util.concurrent.ExecutionException; 56 import javax.annotation.Generated; 57 import org.junit.After; 58 import org.junit.AfterClass; 59 import org.junit.Assert; 60 import org.junit.Before; 61 import org.junit.BeforeClass; 62 import org.junit.Test; 63 64 @Generated("by gapic-generator-java") 65 public class FeaturestoreOnlineServingServiceClientTest { 66 private static MockFeaturestoreOnlineServingService mockFeaturestoreOnlineServingService; 67 private static MockIAMPolicy mockIAMPolicy; 68 private static MockLocations mockLocations; 69 private static MockServiceHelper mockServiceHelper; 70 private LocalChannelProvider channelProvider; 71 private FeaturestoreOnlineServingServiceClient client; 72 73 @BeforeClass startStaticServer()74 public static void startStaticServer() { 75 mockFeaturestoreOnlineServingService = new MockFeaturestoreOnlineServingService(); 76 mockLocations = new MockLocations(); 77 mockIAMPolicy = new MockIAMPolicy(); 78 mockServiceHelper = 79 new MockServiceHelper( 80 UUID.randomUUID().toString(), 81 Arrays.<MockGrpcService>asList( 82 mockFeaturestoreOnlineServingService, mockLocations, mockIAMPolicy)); 83 mockServiceHelper.start(); 84 } 85 86 @AfterClass stopServer()87 public static void stopServer() { 88 mockServiceHelper.stop(); 89 } 90 91 @Before setUp()92 public void setUp() throws IOException { 93 mockServiceHelper.reset(); 94 channelProvider = mockServiceHelper.createChannelProvider(); 95 FeaturestoreOnlineServingServiceSettings settings = 96 FeaturestoreOnlineServingServiceSettings.newBuilder() 97 .setTransportChannelProvider(channelProvider) 98 .setCredentialsProvider(NoCredentialsProvider.create()) 99 .build(); 100 client = FeaturestoreOnlineServingServiceClient.create(settings); 101 } 102 103 @After tearDown()104 public void tearDown() throws Exception { 105 client.close(); 106 } 107 108 @Test readFeatureValuesTest()109 public void readFeatureValuesTest() throws Exception { 110 ReadFeatureValuesResponse expectedResponse = 111 ReadFeatureValuesResponse.newBuilder() 112 .setHeader(ReadFeatureValuesResponse.Header.newBuilder().build()) 113 .setEntityView(ReadFeatureValuesResponse.EntityView.newBuilder().build()) 114 .build(); 115 mockFeaturestoreOnlineServingService.addResponse(expectedResponse); 116 117 EntityTypeName entityType = 118 EntityTypeName.of("[PROJECT]", "[LOCATION]", "[FEATURESTORE]", "[ENTITY_TYPE]"); 119 120 ReadFeatureValuesResponse actualResponse = client.readFeatureValues(entityType); 121 Assert.assertEquals(expectedResponse, actualResponse); 122 123 List<AbstractMessage> actualRequests = mockFeaturestoreOnlineServingService.getRequests(); 124 Assert.assertEquals(1, actualRequests.size()); 125 ReadFeatureValuesRequest actualRequest = ((ReadFeatureValuesRequest) actualRequests.get(0)); 126 127 Assert.assertEquals(entityType.toString(), actualRequest.getEntityType()); 128 Assert.assertTrue( 129 channelProvider.isHeaderSent( 130 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 131 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 132 } 133 134 @Test readFeatureValuesExceptionTest()135 public void readFeatureValuesExceptionTest() throws Exception { 136 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 137 mockFeaturestoreOnlineServingService.addException(exception); 138 139 try { 140 EntityTypeName entityType = 141 EntityTypeName.of("[PROJECT]", "[LOCATION]", "[FEATURESTORE]", "[ENTITY_TYPE]"); 142 client.readFeatureValues(entityType); 143 Assert.fail("No exception raised"); 144 } catch (InvalidArgumentException e) { 145 // Expected exception. 146 } 147 } 148 149 @Test readFeatureValuesTest2()150 public void readFeatureValuesTest2() throws Exception { 151 ReadFeatureValuesResponse expectedResponse = 152 ReadFeatureValuesResponse.newBuilder() 153 .setHeader(ReadFeatureValuesResponse.Header.newBuilder().build()) 154 .setEntityView(ReadFeatureValuesResponse.EntityView.newBuilder().build()) 155 .build(); 156 mockFeaturestoreOnlineServingService.addResponse(expectedResponse); 157 158 String entityType = "entityType-1482998339"; 159 160 ReadFeatureValuesResponse actualResponse = client.readFeatureValues(entityType); 161 Assert.assertEquals(expectedResponse, actualResponse); 162 163 List<AbstractMessage> actualRequests = mockFeaturestoreOnlineServingService.getRequests(); 164 Assert.assertEquals(1, actualRequests.size()); 165 ReadFeatureValuesRequest actualRequest = ((ReadFeatureValuesRequest) actualRequests.get(0)); 166 167 Assert.assertEquals(entityType, actualRequest.getEntityType()); 168 Assert.assertTrue( 169 channelProvider.isHeaderSent( 170 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 171 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 172 } 173 174 @Test readFeatureValuesExceptionTest2()175 public void readFeatureValuesExceptionTest2() throws Exception { 176 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 177 mockFeaturestoreOnlineServingService.addException(exception); 178 179 try { 180 String entityType = "entityType-1482998339"; 181 client.readFeatureValues(entityType); 182 Assert.fail("No exception raised"); 183 } catch (InvalidArgumentException e) { 184 // Expected exception. 185 } 186 } 187 188 @Test streamingReadFeatureValuesTest()189 public void streamingReadFeatureValuesTest() throws Exception { 190 ReadFeatureValuesResponse expectedResponse = 191 ReadFeatureValuesResponse.newBuilder() 192 .setHeader(ReadFeatureValuesResponse.Header.newBuilder().build()) 193 .setEntityView(ReadFeatureValuesResponse.EntityView.newBuilder().build()) 194 .build(); 195 mockFeaturestoreOnlineServingService.addResponse(expectedResponse); 196 StreamingReadFeatureValuesRequest request = 197 StreamingReadFeatureValuesRequest.newBuilder() 198 .setEntityType( 199 EntityTypeName.of("[PROJECT]", "[LOCATION]", "[FEATURESTORE]", "[ENTITY_TYPE]") 200 .toString()) 201 .addAllEntityIds(new ArrayList<String>()) 202 .setFeatureSelector(FeatureSelector.newBuilder().build()) 203 .build(); 204 205 MockStreamObserver<ReadFeatureValuesResponse> responseObserver = new MockStreamObserver<>(); 206 207 ServerStreamingCallable<StreamingReadFeatureValuesRequest, ReadFeatureValuesResponse> callable = 208 client.streamingReadFeatureValuesCallable(); 209 callable.serverStreamingCall(request, responseObserver); 210 211 List<ReadFeatureValuesResponse> actualResponses = responseObserver.future().get(); 212 Assert.assertEquals(1, actualResponses.size()); 213 Assert.assertEquals(expectedResponse, actualResponses.get(0)); 214 } 215 216 @Test streamingReadFeatureValuesExceptionTest()217 public void streamingReadFeatureValuesExceptionTest() throws Exception { 218 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 219 mockFeaturestoreOnlineServingService.addException(exception); 220 StreamingReadFeatureValuesRequest request = 221 StreamingReadFeatureValuesRequest.newBuilder() 222 .setEntityType( 223 EntityTypeName.of("[PROJECT]", "[LOCATION]", "[FEATURESTORE]", "[ENTITY_TYPE]") 224 .toString()) 225 .addAllEntityIds(new ArrayList<String>()) 226 .setFeatureSelector(FeatureSelector.newBuilder().build()) 227 .build(); 228 229 MockStreamObserver<ReadFeatureValuesResponse> responseObserver = new MockStreamObserver<>(); 230 231 ServerStreamingCallable<StreamingReadFeatureValuesRequest, ReadFeatureValuesResponse> callable = 232 client.streamingReadFeatureValuesCallable(); 233 callable.serverStreamingCall(request, responseObserver); 234 235 try { 236 List<ReadFeatureValuesResponse> actualResponses = responseObserver.future().get(); 237 Assert.fail("No exception thrown"); 238 } catch (ExecutionException e) { 239 Assert.assertTrue(e.getCause() instanceof InvalidArgumentException); 240 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 241 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 242 } 243 } 244 245 @Test writeFeatureValuesTest()246 public void writeFeatureValuesTest() throws Exception { 247 WriteFeatureValuesResponse expectedResponse = WriteFeatureValuesResponse.newBuilder().build(); 248 mockFeaturestoreOnlineServingService.addResponse(expectedResponse); 249 250 EntityTypeName entityType = 251 EntityTypeName.of("[PROJECT]", "[LOCATION]", "[FEATURESTORE]", "[ENTITY_TYPE]"); 252 List<WriteFeatureValuesPayload> payloads = new ArrayList<>(); 253 254 WriteFeatureValuesResponse actualResponse = client.writeFeatureValues(entityType, payloads); 255 Assert.assertEquals(expectedResponse, actualResponse); 256 257 List<AbstractMessage> actualRequests = mockFeaturestoreOnlineServingService.getRequests(); 258 Assert.assertEquals(1, actualRequests.size()); 259 WriteFeatureValuesRequest actualRequest = ((WriteFeatureValuesRequest) actualRequests.get(0)); 260 261 Assert.assertEquals(entityType.toString(), actualRequest.getEntityType()); 262 Assert.assertEquals(payloads, actualRequest.getPayloadsList()); 263 Assert.assertTrue( 264 channelProvider.isHeaderSent( 265 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 266 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 267 } 268 269 @Test writeFeatureValuesExceptionTest()270 public void writeFeatureValuesExceptionTest() throws Exception { 271 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 272 mockFeaturestoreOnlineServingService.addException(exception); 273 274 try { 275 EntityTypeName entityType = 276 EntityTypeName.of("[PROJECT]", "[LOCATION]", "[FEATURESTORE]", "[ENTITY_TYPE]"); 277 List<WriteFeatureValuesPayload> payloads = new ArrayList<>(); 278 client.writeFeatureValues(entityType, payloads); 279 Assert.fail("No exception raised"); 280 } catch (InvalidArgumentException e) { 281 // Expected exception. 282 } 283 } 284 285 @Test writeFeatureValuesTest2()286 public void writeFeatureValuesTest2() throws Exception { 287 WriteFeatureValuesResponse expectedResponse = WriteFeatureValuesResponse.newBuilder().build(); 288 mockFeaturestoreOnlineServingService.addResponse(expectedResponse); 289 290 String entityType = "entityType-1482998339"; 291 List<WriteFeatureValuesPayload> payloads = new ArrayList<>(); 292 293 WriteFeatureValuesResponse actualResponse = client.writeFeatureValues(entityType, payloads); 294 Assert.assertEquals(expectedResponse, actualResponse); 295 296 List<AbstractMessage> actualRequests = mockFeaturestoreOnlineServingService.getRequests(); 297 Assert.assertEquals(1, actualRequests.size()); 298 WriteFeatureValuesRequest actualRequest = ((WriteFeatureValuesRequest) actualRequests.get(0)); 299 300 Assert.assertEquals(entityType, actualRequest.getEntityType()); 301 Assert.assertEquals(payloads, actualRequest.getPayloadsList()); 302 Assert.assertTrue( 303 channelProvider.isHeaderSent( 304 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 305 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 306 } 307 308 @Test writeFeatureValuesExceptionTest2()309 public void writeFeatureValuesExceptionTest2() throws Exception { 310 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 311 mockFeaturestoreOnlineServingService.addException(exception); 312 313 try { 314 String entityType = "entityType-1482998339"; 315 List<WriteFeatureValuesPayload> payloads = new ArrayList<>(); 316 client.writeFeatureValues(entityType, payloads); 317 Assert.fail("No exception raised"); 318 } catch (InvalidArgumentException e) { 319 // Expected exception. 320 } 321 } 322 323 @Test listLocationsTest()324 public void listLocationsTest() throws Exception { 325 Location responsesElement = Location.newBuilder().build(); 326 ListLocationsResponse expectedResponse = 327 ListLocationsResponse.newBuilder() 328 .setNextPageToken("") 329 .addAllLocations(Arrays.asList(responsesElement)) 330 .build(); 331 mockLocations.addResponse(expectedResponse); 332 333 ListLocationsRequest request = 334 ListLocationsRequest.newBuilder() 335 .setName("name3373707") 336 .setFilter("filter-1274492040") 337 .setPageSize(883849137) 338 .setPageToken("pageToken873572522") 339 .build(); 340 341 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 342 343 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 344 345 Assert.assertEquals(1, resources.size()); 346 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 347 348 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 349 Assert.assertEquals(1, actualRequests.size()); 350 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 351 352 Assert.assertEquals(request.getName(), actualRequest.getName()); 353 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 354 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 355 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 356 Assert.assertTrue( 357 channelProvider.isHeaderSent( 358 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 359 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 360 } 361 362 @Test listLocationsExceptionTest()363 public void listLocationsExceptionTest() throws Exception { 364 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 365 mockLocations.addException(exception); 366 367 try { 368 ListLocationsRequest request = 369 ListLocationsRequest.newBuilder() 370 .setName("name3373707") 371 .setFilter("filter-1274492040") 372 .setPageSize(883849137) 373 .setPageToken("pageToken873572522") 374 .build(); 375 client.listLocations(request); 376 Assert.fail("No exception raised"); 377 } catch (InvalidArgumentException e) { 378 // Expected exception. 379 } 380 } 381 382 @Test getLocationTest()383 public void getLocationTest() throws Exception { 384 Location expectedResponse = 385 Location.newBuilder() 386 .setName("name3373707") 387 .setLocationId("locationId1541836720") 388 .setDisplayName("displayName1714148973") 389 .putAllLabels(new HashMap<String, String>()) 390 .setMetadata(Any.newBuilder().build()) 391 .build(); 392 mockLocations.addResponse(expectedResponse); 393 394 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 395 396 Location actualResponse = client.getLocation(request); 397 Assert.assertEquals(expectedResponse, actualResponse); 398 399 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 400 Assert.assertEquals(1, actualRequests.size()); 401 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 402 403 Assert.assertEquals(request.getName(), actualRequest.getName()); 404 Assert.assertTrue( 405 channelProvider.isHeaderSent( 406 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 407 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 408 } 409 410 @Test getLocationExceptionTest()411 public void getLocationExceptionTest() throws Exception { 412 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 413 mockLocations.addException(exception); 414 415 try { 416 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 417 client.getLocation(request); 418 Assert.fail("No exception raised"); 419 } catch (InvalidArgumentException e) { 420 // Expected exception. 421 } 422 } 423 424 @Test setIamPolicyTest()425 public void setIamPolicyTest() throws Exception { 426 Policy expectedResponse = 427 Policy.newBuilder() 428 .setVersion(351608024) 429 .addAllBindings(new ArrayList<Binding>()) 430 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 431 .setEtag(ByteString.EMPTY) 432 .build(); 433 mockIAMPolicy.addResponse(expectedResponse); 434 435 SetIamPolicyRequest request = 436 SetIamPolicyRequest.newBuilder() 437 .setResource( 438 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]") 439 .toString()) 440 .setPolicy(Policy.newBuilder().build()) 441 .setUpdateMask(FieldMask.newBuilder().build()) 442 .build(); 443 444 Policy actualResponse = client.setIamPolicy(request); 445 Assert.assertEquals(expectedResponse, actualResponse); 446 447 List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests(); 448 Assert.assertEquals(1, actualRequests.size()); 449 SetIamPolicyRequest actualRequest = ((SetIamPolicyRequest) actualRequests.get(0)); 450 451 Assert.assertEquals(request.getResource(), actualRequest.getResource()); 452 Assert.assertEquals(request.getPolicy(), actualRequest.getPolicy()); 453 Assert.assertEquals(request.getUpdateMask(), actualRequest.getUpdateMask()); 454 Assert.assertTrue( 455 channelProvider.isHeaderSent( 456 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 457 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 458 } 459 460 @Test setIamPolicyExceptionTest()461 public void setIamPolicyExceptionTest() throws Exception { 462 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 463 mockIAMPolicy.addException(exception); 464 465 try { 466 SetIamPolicyRequest request = 467 SetIamPolicyRequest.newBuilder() 468 .setResource( 469 EndpointName.ofProjectLocationEndpointName( 470 "[PROJECT]", "[LOCATION]", "[ENDPOINT]") 471 .toString()) 472 .setPolicy(Policy.newBuilder().build()) 473 .setUpdateMask(FieldMask.newBuilder().build()) 474 .build(); 475 client.setIamPolicy(request); 476 Assert.fail("No exception raised"); 477 } catch (InvalidArgumentException e) { 478 // Expected exception. 479 } 480 } 481 482 @Test getIamPolicyTest()483 public void getIamPolicyTest() throws Exception { 484 Policy expectedResponse = 485 Policy.newBuilder() 486 .setVersion(351608024) 487 .addAllBindings(new ArrayList<Binding>()) 488 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 489 .setEtag(ByteString.EMPTY) 490 .build(); 491 mockIAMPolicy.addResponse(expectedResponse); 492 493 GetIamPolicyRequest request = 494 GetIamPolicyRequest.newBuilder() 495 .setResource( 496 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]") 497 .toString()) 498 .setOptions(GetPolicyOptions.newBuilder().build()) 499 .build(); 500 501 Policy actualResponse = client.getIamPolicy(request); 502 Assert.assertEquals(expectedResponse, actualResponse); 503 504 List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests(); 505 Assert.assertEquals(1, actualRequests.size()); 506 GetIamPolicyRequest actualRequest = ((GetIamPolicyRequest) actualRequests.get(0)); 507 508 Assert.assertEquals(request.getResource(), actualRequest.getResource()); 509 Assert.assertEquals(request.getOptions(), actualRequest.getOptions()); 510 Assert.assertTrue( 511 channelProvider.isHeaderSent( 512 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 513 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 514 } 515 516 @Test getIamPolicyExceptionTest()517 public void getIamPolicyExceptionTest() throws Exception { 518 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 519 mockIAMPolicy.addException(exception); 520 521 try { 522 GetIamPolicyRequest request = 523 GetIamPolicyRequest.newBuilder() 524 .setResource( 525 EndpointName.ofProjectLocationEndpointName( 526 "[PROJECT]", "[LOCATION]", "[ENDPOINT]") 527 .toString()) 528 .setOptions(GetPolicyOptions.newBuilder().build()) 529 .build(); 530 client.getIamPolicy(request); 531 Assert.fail("No exception raised"); 532 } catch (InvalidArgumentException e) { 533 // Expected exception. 534 } 535 } 536 537 @Test testIamPermissionsTest()538 public void testIamPermissionsTest() throws Exception { 539 TestIamPermissionsResponse expectedResponse = 540 TestIamPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build(); 541 mockIAMPolicy.addResponse(expectedResponse); 542 543 TestIamPermissionsRequest request = 544 TestIamPermissionsRequest.newBuilder() 545 .setResource( 546 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]") 547 .toString()) 548 .addAllPermissions(new ArrayList<String>()) 549 .build(); 550 551 TestIamPermissionsResponse actualResponse = client.testIamPermissions(request); 552 Assert.assertEquals(expectedResponse, actualResponse); 553 554 List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests(); 555 Assert.assertEquals(1, actualRequests.size()); 556 TestIamPermissionsRequest actualRequest = ((TestIamPermissionsRequest) actualRequests.get(0)); 557 558 Assert.assertEquals(request.getResource(), actualRequest.getResource()); 559 Assert.assertEquals(request.getPermissionsList(), actualRequest.getPermissionsList()); 560 Assert.assertTrue( 561 channelProvider.isHeaderSent( 562 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 563 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 564 } 565 566 @Test testIamPermissionsExceptionTest()567 public void testIamPermissionsExceptionTest() throws Exception { 568 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 569 mockIAMPolicy.addException(exception); 570 571 try { 572 TestIamPermissionsRequest request = 573 TestIamPermissionsRequest.newBuilder() 574 .setResource( 575 EndpointName.ofProjectLocationEndpointName( 576 "[PROJECT]", "[LOCATION]", "[ENDPOINT]") 577 .toString()) 578 .addAllPermissions(new ArrayList<String>()) 579 .build(); 580 client.testIamPermissions(request); 581 Assert.fail("No exception raised"); 582 } catch (InvalidArgumentException e) { 583 // Expected exception. 584 } 585 } 586 } 587