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.api.apikeys.v2; 18 19 import static com.google.api.apikeys.v2.ApiKeysClient.ListKeysPagedResponse; 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.rpc.ApiClientHeaderProvider; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.common.collect.Lists; 30 import com.google.longrunning.Operation; 31 import com.google.protobuf.AbstractMessage; 32 import com.google.protobuf.Any; 33 import com.google.protobuf.FieldMask; 34 import com.google.protobuf.Timestamp; 35 import io.grpc.StatusRuntimeException; 36 import java.io.IOException; 37 import java.util.Arrays; 38 import java.util.HashMap; 39 import java.util.List; 40 import java.util.UUID; 41 import java.util.concurrent.ExecutionException; 42 import javax.annotation.Generated; 43 import org.junit.After; 44 import org.junit.AfterClass; 45 import org.junit.Assert; 46 import org.junit.Before; 47 import org.junit.BeforeClass; 48 import org.junit.Test; 49 50 @Generated("by gapic-generator-java") 51 public class ApiKeysClientTest { 52 private static MockApiKeys mockApiKeys; 53 private static MockServiceHelper mockServiceHelper; 54 private LocalChannelProvider channelProvider; 55 private ApiKeysClient client; 56 57 @BeforeClass startStaticServer()58 public static void startStaticServer() { 59 mockApiKeys = new MockApiKeys(); 60 mockServiceHelper = 61 new MockServiceHelper( 62 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockApiKeys)); 63 mockServiceHelper.start(); 64 } 65 66 @AfterClass stopServer()67 public static void stopServer() { 68 mockServiceHelper.stop(); 69 } 70 71 @Before setUp()72 public void setUp() throws IOException { 73 mockServiceHelper.reset(); 74 channelProvider = mockServiceHelper.createChannelProvider(); 75 ApiKeysSettings settings = 76 ApiKeysSettings.newBuilder() 77 .setTransportChannelProvider(channelProvider) 78 .setCredentialsProvider(NoCredentialsProvider.create()) 79 .build(); 80 client = ApiKeysClient.create(settings); 81 } 82 83 @After tearDown()84 public void tearDown() throws Exception { 85 client.close(); 86 } 87 88 @Test createKeyTest()89 public void createKeyTest() throws Exception { 90 Key expectedResponse = 91 Key.newBuilder() 92 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 93 .setUid("uid115792") 94 .setDisplayName("displayName1714148973") 95 .setKeyString("keyString-1988270256") 96 .setCreateTime(Timestamp.newBuilder().build()) 97 .setUpdateTime(Timestamp.newBuilder().build()) 98 .setDeleteTime(Timestamp.newBuilder().build()) 99 .putAllAnnotations(new HashMap<String, String>()) 100 .setRestrictions(Restrictions.newBuilder().build()) 101 .setEtag("etag3123477") 102 .build(); 103 Operation resultOperation = 104 Operation.newBuilder() 105 .setName("createKeyTest") 106 .setDone(true) 107 .setResponse(Any.pack(expectedResponse)) 108 .build(); 109 mockApiKeys.addResponse(resultOperation); 110 111 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 112 Key key = Key.newBuilder().build(); 113 String keyId = "keyId101944282"; 114 115 Key actualResponse = client.createKeyAsync(parent, key, keyId).get(); 116 Assert.assertEquals(expectedResponse, actualResponse); 117 118 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 119 Assert.assertEquals(1, actualRequests.size()); 120 CreateKeyRequest actualRequest = ((CreateKeyRequest) actualRequests.get(0)); 121 122 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 123 Assert.assertEquals(key, actualRequest.getKey()); 124 Assert.assertEquals(keyId, actualRequest.getKeyId()); 125 Assert.assertTrue( 126 channelProvider.isHeaderSent( 127 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 128 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 129 } 130 131 @Test createKeyExceptionTest()132 public void createKeyExceptionTest() throws Exception { 133 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 134 mockApiKeys.addException(exception); 135 136 try { 137 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 138 Key key = Key.newBuilder().build(); 139 String keyId = "keyId101944282"; 140 client.createKeyAsync(parent, key, keyId).get(); 141 Assert.fail("No exception raised"); 142 } catch (ExecutionException e) { 143 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 144 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 145 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 146 } 147 } 148 149 @Test createKeyTest2()150 public void createKeyTest2() throws Exception { 151 Key expectedResponse = 152 Key.newBuilder() 153 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 154 .setUid("uid115792") 155 .setDisplayName("displayName1714148973") 156 .setKeyString("keyString-1988270256") 157 .setCreateTime(Timestamp.newBuilder().build()) 158 .setUpdateTime(Timestamp.newBuilder().build()) 159 .setDeleteTime(Timestamp.newBuilder().build()) 160 .putAllAnnotations(new HashMap<String, String>()) 161 .setRestrictions(Restrictions.newBuilder().build()) 162 .setEtag("etag3123477") 163 .build(); 164 Operation resultOperation = 165 Operation.newBuilder() 166 .setName("createKeyTest") 167 .setDone(true) 168 .setResponse(Any.pack(expectedResponse)) 169 .build(); 170 mockApiKeys.addResponse(resultOperation); 171 172 String parent = "parent-995424086"; 173 Key key = Key.newBuilder().build(); 174 String keyId = "keyId101944282"; 175 176 Key actualResponse = client.createKeyAsync(parent, key, keyId).get(); 177 Assert.assertEquals(expectedResponse, actualResponse); 178 179 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 180 Assert.assertEquals(1, actualRequests.size()); 181 CreateKeyRequest actualRequest = ((CreateKeyRequest) actualRequests.get(0)); 182 183 Assert.assertEquals(parent, actualRequest.getParent()); 184 Assert.assertEquals(key, actualRequest.getKey()); 185 Assert.assertEquals(keyId, actualRequest.getKeyId()); 186 Assert.assertTrue( 187 channelProvider.isHeaderSent( 188 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 189 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 190 } 191 192 @Test createKeyExceptionTest2()193 public void createKeyExceptionTest2() throws Exception { 194 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 195 mockApiKeys.addException(exception); 196 197 try { 198 String parent = "parent-995424086"; 199 Key key = Key.newBuilder().build(); 200 String keyId = "keyId101944282"; 201 client.createKeyAsync(parent, key, keyId).get(); 202 Assert.fail("No exception raised"); 203 } catch (ExecutionException e) { 204 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 205 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 206 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 207 } 208 } 209 210 @Test listKeysTest()211 public void listKeysTest() throws Exception { 212 Key responsesElement = Key.newBuilder().build(); 213 ListKeysResponse expectedResponse = 214 ListKeysResponse.newBuilder() 215 .setNextPageToken("") 216 .addAllKeys(Arrays.asList(responsesElement)) 217 .build(); 218 mockApiKeys.addResponse(expectedResponse); 219 220 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 221 222 ListKeysPagedResponse pagedListResponse = client.listKeys(parent); 223 224 List<Key> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 225 226 Assert.assertEquals(1, resources.size()); 227 Assert.assertEquals(expectedResponse.getKeysList().get(0), resources.get(0)); 228 229 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 230 Assert.assertEquals(1, actualRequests.size()); 231 ListKeysRequest actualRequest = ((ListKeysRequest) actualRequests.get(0)); 232 233 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 234 Assert.assertTrue( 235 channelProvider.isHeaderSent( 236 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 237 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 238 } 239 240 @Test listKeysExceptionTest()241 public void listKeysExceptionTest() throws Exception { 242 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 243 mockApiKeys.addException(exception); 244 245 try { 246 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 247 client.listKeys(parent); 248 Assert.fail("No exception raised"); 249 } catch (InvalidArgumentException e) { 250 // Expected exception. 251 } 252 } 253 254 @Test listKeysTest2()255 public void listKeysTest2() throws Exception { 256 Key responsesElement = Key.newBuilder().build(); 257 ListKeysResponse expectedResponse = 258 ListKeysResponse.newBuilder() 259 .setNextPageToken("") 260 .addAllKeys(Arrays.asList(responsesElement)) 261 .build(); 262 mockApiKeys.addResponse(expectedResponse); 263 264 String parent = "parent-995424086"; 265 266 ListKeysPagedResponse pagedListResponse = client.listKeys(parent); 267 268 List<Key> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 269 270 Assert.assertEquals(1, resources.size()); 271 Assert.assertEquals(expectedResponse.getKeysList().get(0), resources.get(0)); 272 273 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 274 Assert.assertEquals(1, actualRequests.size()); 275 ListKeysRequest actualRequest = ((ListKeysRequest) actualRequests.get(0)); 276 277 Assert.assertEquals(parent, actualRequest.getParent()); 278 Assert.assertTrue( 279 channelProvider.isHeaderSent( 280 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 281 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 282 } 283 284 @Test listKeysExceptionTest2()285 public void listKeysExceptionTest2() throws Exception { 286 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 287 mockApiKeys.addException(exception); 288 289 try { 290 String parent = "parent-995424086"; 291 client.listKeys(parent); 292 Assert.fail("No exception raised"); 293 } catch (InvalidArgumentException e) { 294 // Expected exception. 295 } 296 } 297 298 @Test getKeyTest()299 public void getKeyTest() throws Exception { 300 Key expectedResponse = 301 Key.newBuilder() 302 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 303 .setUid("uid115792") 304 .setDisplayName("displayName1714148973") 305 .setKeyString("keyString-1988270256") 306 .setCreateTime(Timestamp.newBuilder().build()) 307 .setUpdateTime(Timestamp.newBuilder().build()) 308 .setDeleteTime(Timestamp.newBuilder().build()) 309 .putAllAnnotations(new HashMap<String, String>()) 310 .setRestrictions(Restrictions.newBuilder().build()) 311 .setEtag("etag3123477") 312 .build(); 313 mockApiKeys.addResponse(expectedResponse); 314 315 KeyName name = KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]"); 316 317 Key actualResponse = client.getKey(name); 318 Assert.assertEquals(expectedResponse, actualResponse); 319 320 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 321 Assert.assertEquals(1, actualRequests.size()); 322 GetKeyRequest actualRequest = ((GetKeyRequest) actualRequests.get(0)); 323 324 Assert.assertEquals(name.toString(), actualRequest.getName()); 325 Assert.assertTrue( 326 channelProvider.isHeaderSent( 327 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 328 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 329 } 330 331 @Test getKeyExceptionTest()332 public void getKeyExceptionTest() throws Exception { 333 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 334 mockApiKeys.addException(exception); 335 336 try { 337 KeyName name = KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]"); 338 client.getKey(name); 339 Assert.fail("No exception raised"); 340 } catch (InvalidArgumentException e) { 341 // Expected exception. 342 } 343 } 344 345 @Test getKeyTest2()346 public void getKeyTest2() throws Exception { 347 Key expectedResponse = 348 Key.newBuilder() 349 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 350 .setUid("uid115792") 351 .setDisplayName("displayName1714148973") 352 .setKeyString("keyString-1988270256") 353 .setCreateTime(Timestamp.newBuilder().build()) 354 .setUpdateTime(Timestamp.newBuilder().build()) 355 .setDeleteTime(Timestamp.newBuilder().build()) 356 .putAllAnnotations(new HashMap<String, String>()) 357 .setRestrictions(Restrictions.newBuilder().build()) 358 .setEtag("etag3123477") 359 .build(); 360 mockApiKeys.addResponse(expectedResponse); 361 362 String name = "name3373707"; 363 364 Key actualResponse = client.getKey(name); 365 Assert.assertEquals(expectedResponse, actualResponse); 366 367 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 368 Assert.assertEquals(1, actualRequests.size()); 369 GetKeyRequest actualRequest = ((GetKeyRequest) actualRequests.get(0)); 370 371 Assert.assertEquals(name, actualRequest.getName()); 372 Assert.assertTrue( 373 channelProvider.isHeaderSent( 374 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 375 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 376 } 377 378 @Test getKeyExceptionTest2()379 public void getKeyExceptionTest2() throws Exception { 380 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 381 mockApiKeys.addException(exception); 382 383 try { 384 String name = "name3373707"; 385 client.getKey(name); 386 Assert.fail("No exception raised"); 387 } catch (InvalidArgumentException e) { 388 // Expected exception. 389 } 390 } 391 392 @Test getKeyStringTest()393 public void getKeyStringTest() throws Exception { 394 GetKeyStringResponse expectedResponse = 395 GetKeyStringResponse.newBuilder().setKeyString("keyString-1988270256").build(); 396 mockApiKeys.addResponse(expectedResponse); 397 398 KeyName name = KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]"); 399 400 GetKeyStringResponse actualResponse = client.getKeyString(name); 401 Assert.assertEquals(expectedResponse, actualResponse); 402 403 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 404 Assert.assertEquals(1, actualRequests.size()); 405 GetKeyStringRequest actualRequest = ((GetKeyStringRequest) actualRequests.get(0)); 406 407 Assert.assertEquals(name.toString(), actualRequest.getName()); 408 Assert.assertTrue( 409 channelProvider.isHeaderSent( 410 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 411 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 412 } 413 414 @Test getKeyStringExceptionTest()415 public void getKeyStringExceptionTest() throws Exception { 416 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 417 mockApiKeys.addException(exception); 418 419 try { 420 KeyName name = KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]"); 421 client.getKeyString(name); 422 Assert.fail("No exception raised"); 423 } catch (InvalidArgumentException e) { 424 // Expected exception. 425 } 426 } 427 428 @Test getKeyStringTest2()429 public void getKeyStringTest2() throws Exception { 430 GetKeyStringResponse expectedResponse = 431 GetKeyStringResponse.newBuilder().setKeyString("keyString-1988270256").build(); 432 mockApiKeys.addResponse(expectedResponse); 433 434 String name = "name3373707"; 435 436 GetKeyStringResponse actualResponse = client.getKeyString(name); 437 Assert.assertEquals(expectedResponse, actualResponse); 438 439 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 440 Assert.assertEquals(1, actualRequests.size()); 441 GetKeyStringRequest actualRequest = ((GetKeyStringRequest) actualRequests.get(0)); 442 443 Assert.assertEquals(name, actualRequest.getName()); 444 Assert.assertTrue( 445 channelProvider.isHeaderSent( 446 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 447 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 448 } 449 450 @Test getKeyStringExceptionTest2()451 public void getKeyStringExceptionTest2() throws Exception { 452 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 453 mockApiKeys.addException(exception); 454 455 try { 456 String name = "name3373707"; 457 client.getKeyString(name); 458 Assert.fail("No exception raised"); 459 } catch (InvalidArgumentException e) { 460 // Expected exception. 461 } 462 } 463 464 @Test updateKeyTest()465 public void updateKeyTest() throws Exception { 466 Key expectedResponse = 467 Key.newBuilder() 468 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 469 .setUid("uid115792") 470 .setDisplayName("displayName1714148973") 471 .setKeyString("keyString-1988270256") 472 .setCreateTime(Timestamp.newBuilder().build()) 473 .setUpdateTime(Timestamp.newBuilder().build()) 474 .setDeleteTime(Timestamp.newBuilder().build()) 475 .putAllAnnotations(new HashMap<String, String>()) 476 .setRestrictions(Restrictions.newBuilder().build()) 477 .setEtag("etag3123477") 478 .build(); 479 Operation resultOperation = 480 Operation.newBuilder() 481 .setName("updateKeyTest") 482 .setDone(true) 483 .setResponse(Any.pack(expectedResponse)) 484 .build(); 485 mockApiKeys.addResponse(resultOperation); 486 487 Key key = Key.newBuilder().build(); 488 FieldMask updateMask = FieldMask.newBuilder().build(); 489 490 Key actualResponse = client.updateKeyAsync(key, updateMask).get(); 491 Assert.assertEquals(expectedResponse, actualResponse); 492 493 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 494 Assert.assertEquals(1, actualRequests.size()); 495 UpdateKeyRequest actualRequest = ((UpdateKeyRequest) actualRequests.get(0)); 496 497 Assert.assertEquals(key, actualRequest.getKey()); 498 Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); 499 Assert.assertTrue( 500 channelProvider.isHeaderSent( 501 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 502 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 503 } 504 505 @Test updateKeyExceptionTest()506 public void updateKeyExceptionTest() throws Exception { 507 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 508 mockApiKeys.addException(exception); 509 510 try { 511 Key key = Key.newBuilder().build(); 512 FieldMask updateMask = FieldMask.newBuilder().build(); 513 client.updateKeyAsync(key, updateMask).get(); 514 Assert.fail("No exception raised"); 515 } catch (ExecutionException e) { 516 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 517 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 518 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 519 } 520 } 521 522 @Test deleteKeyTest()523 public void deleteKeyTest() throws Exception { 524 Key expectedResponse = 525 Key.newBuilder() 526 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 527 .setUid("uid115792") 528 .setDisplayName("displayName1714148973") 529 .setKeyString("keyString-1988270256") 530 .setCreateTime(Timestamp.newBuilder().build()) 531 .setUpdateTime(Timestamp.newBuilder().build()) 532 .setDeleteTime(Timestamp.newBuilder().build()) 533 .putAllAnnotations(new HashMap<String, String>()) 534 .setRestrictions(Restrictions.newBuilder().build()) 535 .setEtag("etag3123477") 536 .build(); 537 Operation resultOperation = 538 Operation.newBuilder() 539 .setName("deleteKeyTest") 540 .setDone(true) 541 .setResponse(Any.pack(expectedResponse)) 542 .build(); 543 mockApiKeys.addResponse(resultOperation); 544 545 KeyName name = KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]"); 546 547 Key actualResponse = client.deleteKeyAsync(name).get(); 548 Assert.assertEquals(expectedResponse, actualResponse); 549 550 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 551 Assert.assertEquals(1, actualRequests.size()); 552 DeleteKeyRequest actualRequest = ((DeleteKeyRequest) actualRequests.get(0)); 553 554 Assert.assertEquals(name.toString(), actualRequest.getName()); 555 Assert.assertTrue( 556 channelProvider.isHeaderSent( 557 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 558 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 559 } 560 561 @Test deleteKeyExceptionTest()562 public void deleteKeyExceptionTest() throws Exception { 563 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 564 mockApiKeys.addException(exception); 565 566 try { 567 KeyName name = KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]"); 568 client.deleteKeyAsync(name).get(); 569 Assert.fail("No exception raised"); 570 } catch (ExecutionException e) { 571 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 572 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 573 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 574 } 575 } 576 577 @Test deleteKeyTest2()578 public void deleteKeyTest2() throws Exception { 579 Key expectedResponse = 580 Key.newBuilder() 581 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 582 .setUid("uid115792") 583 .setDisplayName("displayName1714148973") 584 .setKeyString("keyString-1988270256") 585 .setCreateTime(Timestamp.newBuilder().build()) 586 .setUpdateTime(Timestamp.newBuilder().build()) 587 .setDeleteTime(Timestamp.newBuilder().build()) 588 .putAllAnnotations(new HashMap<String, String>()) 589 .setRestrictions(Restrictions.newBuilder().build()) 590 .setEtag("etag3123477") 591 .build(); 592 Operation resultOperation = 593 Operation.newBuilder() 594 .setName("deleteKeyTest") 595 .setDone(true) 596 .setResponse(Any.pack(expectedResponse)) 597 .build(); 598 mockApiKeys.addResponse(resultOperation); 599 600 String name = "name3373707"; 601 602 Key actualResponse = client.deleteKeyAsync(name).get(); 603 Assert.assertEquals(expectedResponse, actualResponse); 604 605 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 606 Assert.assertEquals(1, actualRequests.size()); 607 DeleteKeyRequest actualRequest = ((DeleteKeyRequest) actualRequests.get(0)); 608 609 Assert.assertEquals(name, actualRequest.getName()); 610 Assert.assertTrue( 611 channelProvider.isHeaderSent( 612 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 613 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 614 } 615 616 @Test deleteKeyExceptionTest2()617 public void deleteKeyExceptionTest2() throws Exception { 618 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 619 mockApiKeys.addException(exception); 620 621 try { 622 String name = "name3373707"; 623 client.deleteKeyAsync(name).get(); 624 Assert.fail("No exception raised"); 625 } catch (ExecutionException e) { 626 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 627 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 628 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 629 } 630 } 631 632 @Test undeleteKeyTest()633 public void undeleteKeyTest() throws Exception { 634 Key expectedResponse = 635 Key.newBuilder() 636 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 637 .setUid("uid115792") 638 .setDisplayName("displayName1714148973") 639 .setKeyString("keyString-1988270256") 640 .setCreateTime(Timestamp.newBuilder().build()) 641 .setUpdateTime(Timestamp.newBuilder().build()) 642 .setDeleteTime(Timestamp.newBuilder().build()) 643 .putAllAnnotations(new HashMap<String, String>()) 644 .setRestrictions(Restrictions.newBuilder().build()) 645 .setEtag("etag3123477") 646 .build(); 647 Operation resultOperation = 648 Operation.newBuilder() 649 .setName("undeleteKeyTest") 650 .setDone(true) 651 .setResponse(Any.pack(expectedResponse)) 652 .build(); 653 mockApiKeys.addResponse(resultOperation); 654 655 UndeleteKeyRequest request = 656 UndeleteKeyRequest.newBuilder() 657 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 658 .build(); 659 660 Key actualResponse = client.undeleteKeyAsync(request).get(); 661 Assert.assertEquals(expectedResponse, actualResponse); 662 663 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 664 Assert.assertEquals(1, actualRequests.size()); 665 UndeleteKeyRequest actualRequest = ((UndeleteKeyRequest) actualRequests.get(0)); 666 667 Assert.assertEquals(request.getName(), actualRequest.getName()); 668 Assert.assertTrue( 669 channelProvider.isHeaderSent( 670 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 671 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 672 } 673 674 @Test undeleteKeyExceptionTest()675 public void undeleteKeyExceptionTest() throws Exception { 676 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 677 mockApiKeys.addException(exception); 678 679 try { 680 UndeleteKeyRequest request = 681 UndeleteKeyRequest.newBuilder() 682 .setName(KeyName.of("[PROJECT]", "[LOCATION]", "[KEY]").toString()) 683 .build(); 684 client.undeleteKeyAsync(request).get(); 685 Assert.fail("No exception raised"); 686 } catch (ExecutionException e) { 687 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 688 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 689 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 690 } 691 } 692 693 @Test lookupKeyTest()694 public void lookupKeyTest() throws Exception { 695 LookupKeyResponse expectedResponse = 696 LookupKeyResponse.newBuilder().setParent("parent-995424086").setName("name3373707").build(); 697 mockApiKeys.addResponse(expectedResponse); 698 699 LookupKeyRequest request = 700 LookupKeyRequest.newBuilder().setKeyString("keyString-1988270256").build(); 701 702 LookupKeyResponse actualResponse = client.lookupKey(request); 703 Assert.assertEquals(expectedResponse, actualResponse); 704 705 List<AbstractMessage> actualRequests = mockApiKeys.getRequests(); 706 Assert.assertEquals(1, actualRequests.size()); 707 LookupKeyRequest actualRequest = ((LookupKeyRequest) actualRequests.get(0)); 708 709 Assert.assertEquals(request.getKeyString(), actualRequest.getKeyString()); 710 Assert.assertTrue( 711 channelProvider.isHeaderSent( 712 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 713 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 714 } 715 716 @Test lookupKeyExceptionTest()717 public void lookupKeyExceptionTest() throws Exception { 718 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 719 mockApiKeys.addException(exception); 720 721 try { 722 LookupKeyRequest request = 723 LookupKeyRequest.newBuilder().setKeyString("keyString-1988270256").build(); 724 client.lookupKey(request); 725 Assert.fail("No exception raised"); 726 } catch (InvalidArgumentException e) { 727 // Expected exception. 728 } 729 } 730 } 731