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.tpu.v2; 18 19 import static com.google.cloud.tpu.v2.TpuClient.ListAcceleratorTypesPagedResponse; 20 import static com.google.cloud.tpu.v2.TpuClient.ListLocationsPagedResponse; 21 import static com.google.cloud.tpu.v2.TpuClient.ListNodesPagedResponse; 22 import static com.google.cloud.tpu.v2.TpuClient.ListRuntimeVersionsPagedResponse; 23 24 import com.google.api.gax.core.NoCredentialsProvider; 25 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 26 import com.google.api.gax.httpjson.testing.MockHttpService; 27 import com.google.api.gax.rpc.ApiClientHeaderProvider; 28 import com.google.api.gax.rpc.ApiException; 29 import com.google.api.gax.rpc.ApiExceptionFactory; 30 import com.google.api.gax.rpc.InvalidArgumentException; 31 import com.google.api.gax.rpc.StatusCode; 32 import com.google.api.gax.rpc.testing.FakeStatusCode; 33 import com.google.cloud.location.GetLocationRequest; 34 import com.google.cloud.location.ListLocationsRequest; 35 import com.google.cloud.location.ListLocationsResponse; 36 import com.google.cloud.location.Location; 37 import com.google.cloud.tpu.v2.stub.HttpJsonTpuStub; 38 import com.google.common.collect.Lists; 39 import com.google.longrunning.Operation; 40 import com.google.protobuf.Any; 41 import com.google.protobuf.Empty; 42 import com.google.protobuf.FieldMask; 43 import com.google.protobuf.Timestamp; 44 import java.io.IOException; 45 import java.util.ArrayList; 46 import java.util.Arrays; 47 import java.util.HashMap; 48 import java.util.List; 49 import java.util.concurrent.ExecutionException; 50 import javax.annotation.Generated; 51 import org.junit.After; 52 import org.junit.AfterClass; 53 import org.junit.Assert; 54 import org.junit.Before; 55 import org.junit.BeforeClass; 56 import org.junit.Test; 57 58 @Generated("by gapic-generator-java") 59 public class TpuClientHttpJsonTest { 60 private static MockHttpService mockService; 61 private static TpuClient client; 62 63 @BeforeClass startStaticServer()64 public static void startStaticServer() throws IOException { 65 mockService = 66 new MockHttpService( 67 HttpJsonTpuStub.getMethodDescriptors(), TpuSettings.getDefaultEndpoint()); 68 TpuSettings settings = 69 TpuSettings.newHttpJsonBuilder() 70 .setTransportChannelProvider( 71 TpuSettings.defaultHttpJsonTransportProviderBuilder() 72 .setHttpTransport(mockService) 73 .build()) 74 .setCredentialsProvider(NoCredentialsProvider.create()) 75 .build(); 76 client = TpuClient.create(settings); 77 } 78 79 @AfterClass stopServer()80 public static void stopServer() { 81 client.close(); 82 } 83 84 @Before setUp()85 public void setUp() {} 86 87 @After tearDown()88 public void tearDown() throws Exception { 89 mockService.reset(); 90 } 91 92 @Test listNodesTest()93 public void listNodesTest() throws Exception { 94 Node responsesElement = Node.newBuilder().build(); 95 ListNodesResponse expectedResponse = 96 ListNodesResponse.newBuilder() 97 .setNextPageToken("") 98 .addAllNodes(Arrays.asList(responsesElement)) 99 .build(); 100 mockService.addResponse(expectedResponse); 101 102 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 103 104 ListNodesPagedResponse pagedListResponse = client.listNodes(parent); 105 106 List<Node> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 107 108 Assert.assertEquals(1, resources.size()); 109 Assert.assertEquals(expectedResponse.getNodesList().get(0), resources.get(0)); 110 111 List<String> actualRequests = mockService.getRequestPaths(); 112 Assert.assertEquals(1, actualRequests.size()); 113 114 String apiClientHeaderKey = 115 mockService 116 .getRequestHeaders() 117 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 118 .iterator() 119 .next(); 120 Assert.assertTrue( 121 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 122 .matcher(apiClientHeaderKey) 123 .matches()); 124 } 125 126 @Test listNodesExceptionTest()127 public void listNodesExceptionTest() throws Exception { 128 ApiException exception = 129 ApiExceptionFactory.createException( 130 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 131 mockService.addException(exception); 132 133 try { 134 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 135 client.listNodes(parent); 136 Assert.fail("No exception raised"); 137 } catch (InvalidArgumentException e) { 138 // Expected exception. 139 } 140 } 141 142 @Test listNodesTest2()143 public void listNodesTest2() throws Exception { 144 Node responsesElement = Node.newBuilder().build(); 145 ListNodesResponse expectedResponse = 146 ListNodesResponse.newBuilder() 147 .setNextPageToken("") 148 .addAllNodes(Arrays.asList(responsesElement)) 149 .build(); 150 mockService.addResponse(expectedResponse); 151 152 String parent = "projects/project-5833/locations/location-5833"; 153 154 ListNodesPagedResponse pagedListResponse = client.listNodes(parent); 155 156 List<Node> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 157 158 Assert.assertEquals(1, resources.size()); 159 Assert.assertEquals(expectedResponse.getNodesList().get(0), resources.get(0)); 160 161 List<String> actualRequests = mockService.getRequestPaths(); 162 Assert.assertEquals(1, actualRequests.size()); 163 164 String apiClientHeaderKey = 165 mockService 166 .getRequestHeaders() 167 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 168 .iterator() 169 .next(); 170 Assert.assertTrue( 171 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 172 .matcher(apiClientHeaderKey) 173 .matches()); 174 } 175 176 @Test listNodesExceptionTest2()177 public void listNodesExceptionTest2() throws Exception { 178 ApiException exception = 179 ApiExceptionFactory.createException( 180 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 181 mockService.addException(exception); 182 183 try { 184 String parent = "projects/project-5833/locations/location-5833"; 185 client.listNodes(parent); 186 Assert.fail("No exception raised"); 187 } catch (InvalidArgumentException e) { 188 // Expected exception. 189 } 190 } 191 192 @Test getNodeTest()193 public void getNodeTest() throws Exception { 194 Node expectedResponse = 195 Node.newBuilder() 196 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 197 .setDescription("description-1724546052") 198 .setAcceleratorType("acceleratorType-82462651") 199 .setHealthDescription("healthDescription1231837184") 200 .setRuntimeVersion("runtimeVersion602071520") 201 .setNetworkConfig(NetworkConfig.newBuilder().build()) 202 .setCidrBlock("cidrBlock1646183801") 203 .setServiceAccount(ServiceAccount.newBuilder().build()) 204 .setCreateTime(Timestamp.newBuilder().build()) 205 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 206 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 207 .putAllLabels(new HashMap<String, String>()) 208 .putAllMetadata(new HashMap<String, String>()) 209 .addAllTags(new ArrayList<String>()) 210 .setId(3355) 211 .addAllDataDisks(new ArrayList<AttachedDisk>()) 212 .addAllSymptoms(new ArrayList<Symptom>()) 213 .setShieldedInstanceConfig(ShieldedInstanceConfig.newBuilder().build()) 214 .setAcceleratorConfig(AcceleratorConfig.newBuilder().build()) 215 .build(); 216 mockService.addResponse(expectedResponse); 217 218 NodeName name = NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]"); 219 220 Node actualResponse = client.getNode(name); 221 Assert.assertEquals(expectedResponse, actualResponse); 222 223 List<String> actualRequests = mockService.getRequestPaths(); 224 Assert.assertEquals(1, actualRequests.size()); 225 226 String apiClientHeaderKey = 227 mockService 228 .getRequestHeaders() 229 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 230 .iterator() 231 .next(); 232 Assert.assertTrue( 233 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 234 .matcher(apiClientHeaderKey) 235 .matches()); 236 } 237 238 @Test getNodeExceptionTest()239 public void getNodeExceptionTest() throws Exception { 240 ApiException exception = 241 ApiExceptionFactory.createException( 242 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 243 mockService.addException(exception); 244 245 try { 246 NodeName name = NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]"); 247 client.getNode(name); 248 Assert.fail("No exception raised"); 249 } catch (InvalidArgumentException e) { 250 // Expected exception. 251 } 252 } 253 254 @Test getNodeTest2()255 public void getNodeTest2() throws Exception { 256 Node expectedResponse = 257 Node.newBuilder() 258 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 259 .setDescription("description-1724546052") 260 .setAcceleratorType("acceleratorType-82462651") 261 .setHealthDescription("healthDescription1231837184") 262 .setRuntimeVersion("runtimeVersion602071520") 263 .setNetworkConfig(NetworkConfig.newBuilder().build()) 264 .setCidrBlock("cidrBlock1646183801") 265 .setServiceAccount(ServiceAccount.newBuilder().build()) 266 .setCreateTime(Timestamp.newBuilder().build()) 267 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 268 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 269 .putAllLabels(new HashMap<String, String>()) 270 .putAllMetadata(new HashMap<String, String>()) 271 .addAllTags(new ArrayList<String>()) 272 .setId(3355) 273 .addAllDataDisks(new ArrayList<AttachedDisk>()) 274 .addAllSymptoms(new ArrayList<Symptom>()) 275 .setShieldedInstanceConfig(ShieldedInstanceConfig.newBuilder().build()) 276 .setAcceleratorConfig(AcceleratorConfig.newBuilder().build()) 277 .build(); 278 mockService.addResponse(expectedResponse); 279 280 String name = "projects/project-8007/locations/location-8007/nodes/node-8007"; 281 282 Node actualResponse = client.getNode(name); 283 Assert.assertEquals(expectedResponse, actualResponse); 284 285 List<String> actualRequests = mockService.getRequestPaths(); 286 Assert.assertEquals(1, actualRequests.size()); 287 288 String apiClientHeaderKey = 289 mockService 290 .getRequestHeaders() 291 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 292 .iterator() 293 .next(); 294 Assert.assertTrue( 295 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 296 .matcher(apiClientHeaderKey) 297 .matches()); 298 } 299 300 @Test getNodeExceptionTest2()301 public void getNodeExceptionTest2() throws Exception { 302 ApiException exception = 303 ApiExceptionFactory.createException( 304 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 305 mockService.addException(exception); 306 307 try { 308 String name = "projects/project-8007/locations/location-8007/nodes/node-8007"; 309 client.getNode(name); 310 Assert.fail("No exception raised"); 311 } catch (InvalidArgumentException e) { 312 // Expected exception. 313 } 314 } 315 316 @Test createNodeTest()317 public void createNodeTest() throws Exception { 318 Node expectedResponse = 319 Node.newBuilder() 320 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 321 .setDescription("description-1724546052") 322 .setAcceleratorType("acceleratorType-82462651") 323 .setHealthDescription("healthDescription1231837184") 324 .setRuntimeVersion("runtimeVersion602071520") 325 .setNetworkConfig(NetworkConfig.newBuilder().build()) 326 .setCidrBlock("cidrBlock1646183801") 327 .setServiceAccount(ServiceAccount.newBuilder().build()) 328 .setCreateTime(Timestamp.newBuilder().build()) 329 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 330 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 331 .putAllLabels(new HashMap<String, String>()) 332 .putAllMetadata(new HashMap<String, String>()) 333 .addAllTags(new ArrayList<String>()) 334 .setId(3355) 335 .addAllDataDisks(new ArrayList<AttachedDisk>()) 336 .addAllSymptoms(new ArrayList<Symptom>()) 337 .setShieldedInstanceConfig(ShieldedInstanceConfig.newBuilder().build()) 338 .setAcceleratorConfig(AcceleratorConfig.newBuilder().build()) 339 .build(); 340 Operation resultOperation = 341 Operation.newBuilder() 342 .setName("createNodeTest") 343 .setDone(true) 344 .setResponse(Any.pack(expectedResponse)) 345 .build(); 346 mockService.addResponse(resultOperation); 347 348 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 349 Node node = Node.newBuilder().build(); 350 String nodeId = "nodeId-1040171331"; 351 352 Node actualResponse = client.createNodeAsync(parent, node, nodeId).get(); 353 Assert.assertEquals(expectedResponse, actualResponse); 354 355 List<String> actualRequests = mockService.getRequestPaths(); 356 Assert.assertEquals(1, actualRequests.size()); 357 358 String apiClientHeaderKey = 359 mockService 360 .getRequestHeaders() 361 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 362 .iterator() 363 .next(); 364 Assert.assertTrue( 365 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 366 .matcher(apiClientHeaderKey) 367 .matches()); 368 } 369 370 @Test createNodeExceptionTest()371 public void createNodeExceptionTest() throws Exception { 372 ApiException exception = 373 ApiExceptionFactory.createException( 374 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 375 mockService.addException(exception); 376 377 try { 378 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 379 Node node = Node.newBuilder().build(); 380 String nodeId = "nodeId-1040171331"; 381 client.createNodeAsync(parent, node, nodeId).get(); 382 Assert.fail("No exception raised"); 383 } catch (ExecutionException e) { 384 } 385 } 386 387 @Test createNodeTest2()388 public void createNodeTest2() throws Exception { 389 Node expectedResponse = 390 Node.newBuilder() 391 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 392 .setDescription("description-1724546052") 393 .setAcceleratorType("acceleratorType-82462651") 394 .setHealthDescription("healthDescription1231837184") 395 .setRuntimeVersion("runtimeVersion602071520") 396 .setNetworkConfig(NetworkConfig.newBuilder().build()) 397 .setCidrBlock("cidrBlock1646183801") 398 .setServiceAccount(ServiceAccount.newBuilder().build()) 399 .setCreateTime(Timestamp.newBuilder().build()) 400 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 401 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 402 .putAllLabels(new HashMap<String, String>()) 403 .putAllMetadata(new HashMap<String, String>()) 404 .addAllTags(new ArrayList<String>()) 405 .setId(3355) 406 .addAllDataDisks(new ArrayList<AttachedDisk>()) 407 .addAllSymptoms(new ArrayList<Symptom>()) 408 .setShieldedInstanceConfig(ShieldedInstanceConfig.newBuilder().build()) 409 .setAcceleratorConfig(AcceleratorConfig.newBuilder().build()) 410 .build(); 411 Operation resultOperation = 412 Operation.newBuilder() 413 .setName("createNodeTest") 414 .setDone(true) 415 .setResponse(Any.pack(expectedResponse)) 416 .build(); 417 mockService.addResponse(resultOperation); 418 419 String parent = "projects/project-5833/locations/location-5833"; 420 Node node = Node.newBuilder().build(); 421 String nodeId = "nodeId-1040171331"; 422 423 Node actualResponse = client.createNodeAsync(parent, node, nodeId).get(); 424 Assert.assertEquals(expectedResponse, actualResponse); 425 426 List<String> actualRequests = mockService.getRequestPaths(); 427 Assert.assertEquals(1, actualRequests.size()); 428 429 String apiClientHeaderKey = 430 mockService 431 .getRequestHeaders() 432 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 433 .iterator() 434 .next(); 435 Assert.assertTrue( 436 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 437 .matcher(apiClientHeaderKey) 438 .matches()); 439 } 440 441 @Test createNodeExceptionTest2()442 public void createNodeExceptionTest2() throws Exception { 443 ApiException exception = 444 ApiExceptionFactory.createException( 445 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 446 mockService.addException(exception); 447 448 try { 449 String parent = "projects/project-5833/locations/location-5833"; 450 Node node = Node.newBuilder().build(); 451 String nodeId = "nodeId-1040171331"; 452 client.createNodeAsync(parent, node, nodeId).get(); 453 Assert.fail("No exception raised"); 454 } catch (ExecutionException e) { 455 } 456 } 457 458 @Test deleteNodeTest()459 public void deleteNodeTest() throws Exception { 460 Empty expectedResponse = Empty.newBuilder().build(); 461 Operation resultOperation = 462 Operation.newBuilder() 463 .setName("deleteNodeTest") 464 .setDone(true) 465 .setResponse(Any.pack(expectedResponse)) 466 .build(); 467 mockService.addResponse(resultOperation); 468 469 NodeName name = NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]"); 470 471 client.deleteNodeAsync(name).get(); 472 473 List<String> actualRequests = mockService.getRequestPaths(); 474 Assert.assertEquals(1, actualRequests.size()); 475 476 String apiClientHeaderKey = 477 mockService 478 .getRequestHeaders() 479 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 480 .iterator() 481 .next(); 482 Assert.assertTrue( 483 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 484 .matcher(apiClientHeaderKey) 485 .matches()); 486 } 487 488 @Test deleteNodeExceptionTest()489 public void deleteNodeExceptionTest() throws Exception { 490 ApiException exception = 491 ApiExceptionFactory.createException( 492 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 493 mockService.addException(exception); 494 495 try { 496 NodeName name = NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]"); 497 client.deleteNodeAsync(name).get(); 498 Assert.fail("No exception raised"); 499 } catch (ExecutionException e) { 500 } 501 } 502 503 @Test deleteNodeTest2()504 public void deleteNodeTest2() throws Exception { 505 Empty expectedResponse = Empty.newBuilder().build(); 506 Operation resultOperation = 507 Operation.newBuilder() 508 .setName("deleteNodeTest") 509 .setDone(true) 510 .setResponse(Any.pack(expectedResponse)) 511 .build(); 512 mockService.addResponse(resultOperation); 513 514 String name = "projects/project-8007/locations/location-8007/nodes/node-8007"; 515 516 client.deleteNodeAsync(name).get(); 517 518 List<String> actualRequests = mockService.getRequestPaths(); 519 Assert.assertEquals(1, actualRequests.size()); 520 521 String apiClientHeaderKey = 522 mockService 523 .getRequestHeaders() 524 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 525 .iterator() 526 .next(); 527 Assert.assertTrue( 528 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 529 .matcher(apiClientHeaderKey) 530 .matches()); 531 } 532 533 @Test deleteNodeExceptionTest2()534 public void deleteNodeExceptionTest2() throws Exception { 535 ApiException exception = 536 ApiExceptionFactory.createException( 537 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 538 mockService.addException(exception); 539 540 try { 541 String name = "projects/project-8007/locations/location-8007/nodes/node-8007"; 542 client.deleteNodeAsync(name).get(); 543 Assert.fail("No exception raised"); 544 } catch (ExecutionException e) { 545 } 546 } 547 548 @Test stopNodeTest()549 public void stopNodeTest() throws Exception { 550 Node expectedResponse = 551 Node.newBuilder() 552 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 553 .setDescription("description-1724546052") 554 .setAcceleratorType("acceleratorType-82462651") 555 .setHealthDescription("healthDescription1231837184") 556 .setRuntimeVersion("runtimeVersion602071520") 557 .setNetworkConfig(NetworkConfig.newBuilder().build()) 558 .setCidrBlock("cidrBlock1646183801") 559 .setServiceAccount(ServiceAccount.newBuilder().build()) 560 .setCreateTime(Timestamp.newBuilder().build()) 561 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 562 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 563 .putAllLabels(new HashMap<String, String>()) 564 .putAllMetadata(new HashMap<String, String>()) 565 .addAllTags(new ArrayList<String>()) 566 .setId(3355) 567 .addAllDataDisks(new ArrayList<AttachedDisk>()) 568 .addAllSymptoms(new ArrayList<Symptom>()) 569 .setShieldedInstanceConfig(ShieldedInstanceConfig.newBuilder().build()) 570 .setAcceleratorConfig(AcceleratorConfig.newBuilder().build()) 571 .build(); 572 Operation resultOperation = 573 Operation.newBuilder() 574 .setName("stopNodeTest") 575 .setDone(true) 576 .setResponse(Any.pack(expectedResponse)) 577 .build(); 578 mockService.addResponse(resultOperation); 579 580 StopNodeRequest request = 581 StopNodeRequest.newBuilder() 582 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 583 .build(); 584 585 Node actualResponse = client.stopNodeAsync(request).get(); 586 Assert.assertEquals(expectedResponse, actualResponse); 587 588 List<String> actualRequests = mockService.getRequestPaths(); 589 Assert.assertEquals(1, actualRequests.size()); 590 591 String apiClientHeaderKey = 592 mockService 593 .getRequestHeaders() 594 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 595 .iterator() 596 .next(); 597 Assert.assertTrue( 598 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 599 .matcher(apiClientHeaderKey) 600 .matches()); 601 } 602 603 @Test stopNodeExceptionTest()604 public void stopNodeExceptionTest() throws Exception { 605 ApiException exception = 606 ApiExceptionFactory.createException( 607 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 608 mockService.addException(exception); 609 610 try { 611 StopNodeRequest request = 612 StopNodeRequest.newBuilder() 613 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 614 .build(); 615 client.stopNodeAsync(request).get(); 616 Assert.fail("No exception raised"); 617 } catch (ExecutionException e) { 618 } 619 } 620 621 @Test startNodeTest()622 public void startNodeTest() throws Exception { 623 Node expectedResponse = 624 Node.newBuilder() 625 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 626 .setDescription("description-1724546052") 627 .setAcceleratorType("acceleratorType-82462651") 628 .setHealthDescription("healthDescription1231837184") 629 .setRuntimeVersion("runtimeVersion602071520") 630 .setNetworkConfig(NetworkConfig.newBuilder().build()) 631 .setCidrBlock("cidrBlock1646183801") 632 .setServiceAccount(ServiceAccount.newBuilder().build()) 633 .setCreateTime(Timestamp.newBuilder().build()) 634 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 635 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 636 .putAllLabels(new HashMap<String, String>()) 637 .putAllMetadata(new HashMap<String, String>()) 638 .addAllTags(new ArrayList<String>()) 639 .setId(3355) 640 .addAllDataDisks(new ArrayList<AttachedDisk>()) 641 .addAllSymptoms(new ArrayList<Symptom>()) 642 .setShieldedInstanceConfig(ShieldedInstanceConfig.newBuilder().build()) 643 .setAcceleratorConfig(AcceleratorConfig.newBuilder().build()) 644 .build(); 645 Operation resultOperation = 646 Operation.newBuilder() 647 .setName("startNodeTest") 648 .setDone(true) 649 .setResponse(Any.pack(expectedResponse)) 650 .build(); 651 mockService.addResponse(resultOperation); 652 653 StartNodeRequest request = 654 StartNodeRequest.newBuilder() 655 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 656 .build(); 657 658 Node actualResponse = client.startNodeAsync(request).get(); 659 Assert.assertEquals(expectedResponse, actualResponse); 660 661 List<String> actualRequests = mockService.getRequestPaths(); 662 Assert.assertEquals(1, actualRequests.size()); 663 664 String apiClientHeaderKey = 665 mockService 666 .getRequestHeaders() 667 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 668 .iterator() 669 .next(); 670 Assert.assertTrue( 671 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 672 .matcher(apiClientHeaderKey) 673 .matches()); 674 } 675 676 @Test startNodeExceptionTest()677 public void startNodeExceptionTest() throws Exception { 678 ApiException exception = 679 ApiExceptionFactory.createException( 680 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 681 mockService.addException(exception); 682 683 try { 684 StartNodeRequest request = 685 StartNodeRequest.newBuilder() 686 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 687 .build(); 688 client.startNodeAsync(request).get(); 689 Assert.fail("No exception raised"); 690 } catch (ExecutionException e) { 691 } 692 } 693 694 @Test updateNodeTest()695 public void updateNodeTest() throws Exception { 696 Node expectedResponse = 697 Node.newBuilder() 698 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 699 .setDescription("description-1724546052") 700 .setAcceleratorType("acceleratorType-82462651") 701 .setHealthDescription("healthDescription1231837184") 702 .setRuntimeVersion("runtimeVersion602071520") 703 .setNetworkConfig(NetworkConfig.newBuilder().build()) 704 .setCidrBlock("cidrBlock1646183801") 705 .setServiceAccount(ServiceAccount.newBuilder().build()) 706 .setCreateTime(Timestamp.newBuilder().build()) 707 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 708 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 709 .putAllLabels(new HashMap<String, String>()) 710 .putAllMetadata(new HashMap<String, String>()) 711 .addAllTags(new ArrayList<String>()) 712 .setId(3355) 713 .addAllDataDisks(new ArrayList<AttachedDisk>()) 714 .addAllSymptoms(new ArrayList<Symptom>()) 715 .setShieldedInstanceConfig(ShieldedInstanceConfig.newBuilder().build()) 716 .setAcceleratorConfig(AcceleratorConfig.newBuilder().build()) 717 .build(); 718 Operation resultOperation = 719 Operation.newBuilder() 720 .setName("updateNodeTest") 721 .setDone(true) 722 .setResponse(Any.pack(expectedResponse)) 723 .build(); 724 mockService.addResponse(resultOperation); 725 726 Node node = 727 Node.newBuilder() 728 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 729 .setDescription("description-1724546052") 730 .setAcceleratorType("acceleratorType-82462651") 731 .setHealthDescription("healthDescription1231837184") 732 .setRuntimeVersion("runtimeVersion602071520") 733 .setNetworkConfig(NetworkConfig.newBuilder().build()) 734 .setCidrBlock("cidrBlock1646183801") 735 .setServiceAccount(ServiceAccount.newBuilder().build()) 736 .setCreateTime(Timestamp.newBuilder().build()) 737 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 738 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 739 .putAllLabels(new HashMap<String, String>()) 740 .putAllMetadata(new HashMap<String, String>()) 741 .addAllTags(new ArrayList<String>()) 742 .setId(3355) 743 .addAllDataDisks(new ArrayList<AttachedDisk>()) 744 .addAllSymptoms(new ArrayList<Symptom>()) 745 .setShieldedInstanceConfig(ShieldedInstanceConfig.newBuilder().build()) 746 .setAcceleratorConfig(AcceleratorConfig.newBuilder().build()) 747 .build(); 748 FieldMask updateMask = FieldMask.newBuilder().build(); 749 750 Node actualResponse = client.updateNodeAsync(node, updateMask).get(); 751 Assert.assertEquals(expectedResponse, actualResponse); 752 753 List<String> actualRequests = mockService.getRequestPaths(); 754 Assert.assertEquals(1, actualRequests.size()); 755 756 String apiClientHeaderKey = 757 mockService 758 .getRequestHeaders() 759 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 760 .iterator() 761 .next(); 762 Assert.assertTrue( 763 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 764 .matcher(apiClientHeaderKey) 765 .matches()); 766 } 767 768 @Test updateNodeExceptionTest()769 public void updateNodeExceptionTest() throws Exception { 770 ApiException exception = 771 ApiExceptionFactory.createException( 772 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 773 mockService.addException(exception); 774 775 try { 776 Node node = 777 Node.newBuilder() 778 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 779 .setDescription("description-1724546052") 780 .setAcceleratorType("acceleratorType-82462651") 781 .setHealthDescription("healthDescription1231837184") 782 .setRuntimeVersion("runtimeVersion602071520") 783 .setNetworkConfig(NetworkConfig.newBuilder().build()) 784 .setCidrBlock("cidrBlock1646183801") 785 .setServiceAccount(ServiceAccount.newBuilder().build()) 786 .setCreateTime(Timestamp.newBuilder().build()) 787 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 788 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 789 .putAllLabels(new HashMap<String, String>()) 790 .putAllMetadata(new HashMap<String, String>()) 791 .addAllTags(new ArrayList<String>()) 792 .setId(3355) 793 .addAllDataDisks(new ArrayList<AttachedDisk>()) 794 .addAllSymptoms(new ArrayList<Symptom>()) 795 .setShieldedInstanceConfig(ShieldedInstanceConfig.newBuilder().build()) 796 .setAcceleratorConfig(AcceleratorConfig.newBuilder().build()) 797 .build(); 798 FieldMask updateMask = FieldMask.newBuilder().build(); 799 client.updateNodeAsync(node, updateMask).get(); 800 Assert.fail("No exception raised"); 801 } catch (ExecutionException e) { 802 } 803 } 804 805 @Test generateServiceIdentityTest()806 public void generateServiceIdentityTest() throws Exception { 807 GenerateServiceIdentityResponse expectedResponse = 808 GenerateServiceIdentityResponse.newBuilder() 809 .setIdentity(ServiceIdentity.newBuilder().build()) 810 .build(); 811 mockService.addResponse(expectedResponse); 812 813 GenerateServiceIdentityRequest request = 814 GenerateServiceIdentityRequest.newBuilder() 815 .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString()) 816 .build(); 817 818 GenerateServiceIdentityResponse actualResponse = client.generateServiceIdentity(request); 819 Assert.assertEquals(expectedResponse, actualResponse); 820 821 List<String> actualRequests = mockService.getRequestPaths(); 822 Assert.assertEquals(1, actualRequests.size()); 823 824 String apiClientHeaderKey = 825 mockService 826 .getRequestHeaders() 827 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 828 .iterator() 829 .next(); 830 Assert.assertTrue( 831 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 832 .matcher(apiClientHeaderKey) 833 .matches()); 834 } 835 836 @Test generateServiceIdentityExceptionTest()837 public void generateServiceIdentityExceptionTest() throws Exception { 838 ApiException exception = 839 ApiExceptionFactory.createException( 840 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 841 mockService.addException(exception); 842 843 try { 844 GenerateServiceIdentityRequest request = 845 GenerateServiceIdentityRequest.newBuilder() 846 .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString()) 847 .build(); 848 client.generateServiceIdentity(request); 849 Assert.fail("No exception raised"); 850 } catch (InvalidArgumentException e) { 851 // Expected exception. 852 } 853 } 854 855 @Test listAcceleratorTypesTest()856 public void listAcceleratorTypesTest() throws Exception { 857 AcceleratorType responsesElement = AcceleratorType.newBuilder().build(); 858 ListAcceleratorTypesResponse expectedResponse = 859 ListAcceleratorTypesResponse.newBuilder() 860 .setNextPageToken("") 861 .addAllAcceleratorTypes(Arrays.asList(responsesElement)) 862 .build(); 863 mockService.addResponse(expectedResponse); 864 865 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 866 867 ListAcceleratorTypesPagedResponse pagedListResponse = client.listAcceleratorTypes(parent); 868 869 List<AcceleratorType> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 870 871 Assert.assertEquals(1, resources.size()); 872 Assert.assertEquals(expectedResponse.getAcceleratorTypesList().get(0), resources.get(0)); 873 874 List<String> actualRequests = mockService.getRequestPaths(); 875 Assert.assertEquals(1, actualRequests.size()); 876 877 String apiClientHeaderKey = 878 mockService 879 .getRequestHeaders() 880 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 881 .iterator() 882 .next(); 883 Assert.assertTrue( 884 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 885 .matcher(apiClientHeaderKey) 886 .matches()); 887 } 888 889 @Test listAcceleratorTypesExceptionTest()890 public void listAcceleratorTypesExceptionTest() throws Exception { 891 ApiException exception = 892 ApiExceptionFactory.createException( 893 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 894 mockService.addException(exception); 895 896 try { 897 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 898 client.listAcceleratorTypes(parent); 899 Assert.fail("No exception raised"); 900 } catch (InvalidArgumentException e) { 901 // Expected exception. 902 } 903 } 904 905 @Test listAcceleratorTypesTest2()906 public void listAcceleratorTypesTest2() throws Exception { 907 AcceleratorType responsesElement = AcceleratorType.newBuilder().build(); 908 ListAcceleratorTypesResponse expectedResponse = 909 ListAcceleratorTypesResponse.newBuilder() 910 .setNextPageToken("") 911 .addAllAcceleratorTypes(Arrays.asList(responsesElement)) 912 .build(); 913 mockService.addResponse(expectedResponse); 914 915 String parent = "projects/project-5833/locations/location-5833"; 916 917 ListAcceleratorTypesPagedResponse pagedListResponse = client.listAcceleratorTypes(parent); 918 919 List<AcceleratorType> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 920 921 Assert.assertEquals(1, resources.size()); 922 Assert.assertEquals(expectedResponse.getAcceleratorTypesList().get(0), resources.get(0)); 923 924 List<String> actualRequests = mockService.getRequestPaths(); 925 Assert.assertEquals(1, actualRequests.size()); 926 927 String apiClientHeaderKey = 928 mockService 929 .getRequestHeaders() 930 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 931 .iterator() 932 .next(); 933 Assert.assertTrue( 934 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 935 .matcher(apiClientHeaderKey) 936 .matches()); 937 } 938 939 @Test listAcceleratorTypesExceptionTest2()940 public void listAcceleratorTypesExceptionTest2() throws Exception { 941 ApiException exception = 942 ApiExceptionFactory.createException( 943 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 944 mockService.addException(exception); 945 946 try { 947 String parent = "projects/project-5833/locations/location-5833"; 948 client.listAcceleratorTypes(parent); 949 Assert.fail("No exception raised"); 950 } catch (InvalidArgumentException e) { 951 // Expected exception. 952 } 953 } 954 955 @Test getAcceleratorTypeTest()956 public void getAcceleratorTypeTest() throws Exception { 957 AcceleratorType expectedResponse = 958 AcceleratorType.newBuilder() 959 .setName( 960 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]").toString()) 961 .setType("type3575610") 962 .addAllAcceleratorConfigs(new ArrayList<AcceleratorConfig>()) 963 .build(); 964 mockService.addResponse(expectedResponse); 965 966 AcceleratorTypeName name = 967 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]"); 968 969 AcceleratorType actualResponse = client.getAcceleratorType(name); 970 Assert.assertEquals(expectedResponse, actualResponse); 971 972 List<String> actualRequests = mockService.getRequestPaths(); 973 Assert.assertEquals(1, actualRequests.size()); 974 975 String apiClientHeaderKey = 976 mockService 977 .getRequestHeaders() 978 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 979 .iterator() 980 .next(); 981 Assert.assertTrue( 982 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 983 .matcher(apiClientHeaderKey) 984 .matches()); 985 } 986 987 @Test getAcceleratorTypeExceptionTest()988 public void getAcceleratorTypeExceptionTest() throws Exception { 989 ApiException exception = 990 ApiExceptionFactory.createException( 991 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 992 mockService.addException(exception); 993 994 try { 995 AcceleratorTypeName name = 996 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]"); 997 client.getAcceleratorType(name); 998 Assert.fail("No exception raised"); 999 } catch (InvalidArgumentException e) { 1000 // Expected exception. 1001 } 1002 } 1003 1004 @Test getAcceleratorTypeTest2()1005 public void getAcceleratorTypeTest2() throws Exception { 1006 AcceleratorType expectedResponse = 1007 AcceleratorType.newBuilder() 1008 .setName( 1009 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]").toString()) 1010 .setType("type3575610") 1011 .addAllAcceleratorConfigs(new ArrayList<AcceleratorConfig>()) 1012 .build(); 1013 mockService.addResponse(expectedResponse); 1014 1015 String name = 1016 "projects/project-7118/locations/location-7118/acceleratorTypes/acceleratorType-7118"; 1017 1018 AcceleratorType actualResponse = client.getAcceleratorType(name); 1019 Assert.assertEquals(expectedResponse, actualResponse); 1020 1021 List<String> actualRequests = mockService.getRequestPaths(); 1022 Assert.assertEquals(1, actualRequests.size()); 1023 1024 String apiClientHeaderKey = 1025 mockService 1026 .getRequestHeaders() 1027 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 1028 .iterator() 1029 .next(); 1030 Assert.assertTrue( 1031 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 1032 .matcher(apiClientHeaderKey) 1033 .matches()); 1034 } 1035 1036 @Test getAcceleratorTypeExceptionTest2()1037 public void getAcceleratorTypeExceptionTest2() throws Exception { 1038 ApiException exception = 1039 ApiExceptionFactory.createException( 1040 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 1041 mockService.addException(exception); 1042 1043 try { 1044 String name = 1045 "projects/project-7118/locations/location-7118/acceleratorTypes/acceleratorType-7118"; 1046 client.getAcceleratorType(name); 1047 Assert.fail("No exception raised"); 1048 } catch (InvalidArgumentException e) { 1049 // Expected exception. 1050 } 1051 } 1052 1053 @Test listRuntimeVersionsTest()1054 public void listRuntimeVersionsTest() throws Exception { 1055 RuntimeVersion responsesElement = RuntimeVersion.newBuilder().build(); 1056 ListRuntimeVersionsResponse expectedResponse = 1057 ListRuntimeVersionsResponse.newBuilder() 1058 .setNextPageToken("") 1059 .addAllRuntimeVersions(Arrays.asList(responsesElement)) 1060 .build(); 1061 mockService.addResponse(expectedResponse); 1062 1063 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 1064 1065 ListRuntimeVersionsPagedResponse pagedListResponse = client.listRuntimeVersions(parent); 1066 1067 List<RuntimeVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 1068 1069 Assert.assertEquals(1, resources.size()); 1070 Assert.assertEquals(expectedResponse.getRuntimeVersionsList().get(0), resources.get(0)); 1071 1072 List<String> actualRequests = mockService.getRequestPaths(); 1073 Assert.assertEquals(1, actualRequests.size()); 1074 1075 String apiClientHeaderKey = 1076 mockService 1077 .getRequestHeaders() 1078 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 1079 .iterator() 1080 .next(); 1081 Assert.assertTrue( 1082 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 1083 .matcher(apiClientHeaderKey) 1084 .matches()); 1085 } 1086 1087 @Test listRuntimeVersionsExceptionTest()1088 public void listRuntimeVersionsExceptionTest() throws Exception { 1089 ApiException exception = 1090 ApiExceptionFactory.createException( 1091 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 1092 mockService.addException(exception); 1093 1094 try { 1095 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 1096 client.listRuntimeVersions(parent); 1097 Assert.fail("No exception raised"); 1098 } catch (InvalidArgumentException e) { 1099 // Expected exception. 1100 } 1101 } 1102 1103 @Test listRuntimeVersionsTest2()1104 public void listRuntimeVersionsTest2() throws Exception { 1105 RuntimeVersion responsesElement = RuntimeVersion.newBuilder().build(); 1106 ListRuntimeVersionsResponse expectedResponse = 1107 ListRuntimeVersionsResponse.newBuilder() 1108 .setNextPageToken("") 1109 .addAllRuntimeVersions(Arrays.asList(responsesElement)) 1110 .build(); 1111 mockService.addResponse(expectedResponse); 1112 1113 String parent = "projects/project-5833/locations/location-5833"; 1114 1115 ListRuntimeVersionsPagedResponse pagedListResponse = client.listRuntimeVersions(parent); 1116 1117 List<RuntimeVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 1118 1119 Assert.assertEquals(1, resources.size()); 1120 Assert.assertEquals(expectedResponse.getRuntimeVersionsList().get(0), resources.get(0)); 1121 1122 List<String> actualRequests = mockService.getRequestPaths(); 1123 Assert.assertEquals(1, actualRequests.size()); 1124 1125 String apiClientHeaderKey = 1126 mockService 1127 .getRequestHeaders() 1128 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 1129 .iterator() 1130 .next(); 1131 Assert.assertTrue( 1132 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 1133 .matcher(apiClientHeaderKey) 1134 .matches()); 1135 } 1136 1137 @Test listRuntimeVersionsExceptionTest2()1138 public void listRuntimeVersionsExceptionTest2() throws Exception { 1139 ApiException exception = 1140 ApiExceptionFactory.createException( 1141 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 1142 mockService.addException(exception); 1143 1144 try { 1145 String parent = "projects/project-5833/locations/location-5833"; 1146 client.listRuntimeVersions(parent); 1147 Assert.fail("No exception raised"); 1148 } catch (InvalidArgumentException e) { 1149 // Expected exception. 1150 } 1151 } 1152 1153 @Test getRuntimeVersionTest()1154 public void getRuntimeVersionTest() throws Exception { 1155 RuntimeVersion expectedResponse = 1156 RuntimeVersion.newBuilder() 1157 .setName( 1158 RuntimeVersionName.of("[PROJECT]", "[LOCATION]", "[RUNTIME_VERSION]").toString()) 1159 .setVersion("version351608024") 1160 .build(); 1161 mockService.addResponse(expectedResponse); 1162 1163 RuntimeVersionName name = RuntimeVersionName.of("[PROJECT]", "[LOCATION]", "[RUNTIME_VERSION]"); 1164 1165 RuntimeVersion actualResponse = client.getRuntimeVersion(name); 1166 Assert.assertEquals(expectedResponse, actualResponse); 1167 1168 List<String> actualRequests = mockService.getRequestPaths(); 1169 Assert.assertEquals(1, actualRequests.size()); 1170 1171 String apiClientHeaderKey = 1172 mockService 1173 .getRequestHeaders() 1174 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 1175 .iterator() 1176 .next(); 1177 Assert.assertTrue( 1178 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 1179 .matcher(apiClientHeaderKey) 1180 .matches()); 1181 } 1182 1183 @Test getRuntimeVersionExceptionTest()1184 public void getRuntimeVersionExceptionTest() throws Exception { 1185 ApiException exception = 1186 ApiExceptionFactory.createException( 1187 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 1188 mockService.addException(exception); 1189 1190 try { 1191 RuntimeVersionName name = 1192 RuntimeVersionName.of("[PROJECT]", "[LOCATION]", "[RUNTIME_VERSION]"); 1193 client.getRuntimeVersion(name); 1194 Assert.fail("No exception raised"); 1195 } catch (InvalidArgumentException e) { 1196 // Expected exception. 1197 } 1198 } 1199 1200 @Test getRuntimeVersionTest2()1201 public void getRuntimeVersionTest2() throws Exception { 1202 RuntimeVersion expectedResponse = 1203 RuntimeVersion.newBuilder() 1204 .setName( 1205 RuntimeVersionName.of("[PROJECT]", "[LOCATION]", "[RUNTIME_VERSION]").toString()) 1206 .setVersion("version351608024") 1207 .build(); 1208 mockService.addResponse(expectedResponse); 1209 1210 String name = 1211 "projects/project-5927/locations/location-5927/runtimeVersions/runtimeVersion-5927"; 1212 1213 RuntimeVersion actualResponse = client.getRuntimeVersion(name); 1214 Assert.assertEquals(expectedResponse, actualResponse); 1215 1216 List<String> actualRequests = mockService.getRequestPaths(); 1217 Assert.assertEquals(1, actualRequests.size()); 1218 1219 String apiClientHeaderKey = 1220 mockService 1221 .getRequestHeaders() 1222 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 1223 .iterator() 1224 .next(); 1225 Assert.assertTrue( 1226 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 1227 .matcher(apiClientHeaderKey) 1228 .matches()); 1229 } 1230 1231 @Test getRuntimeVersionExceptionTest2()1232 public void getRuntimeVersionExceptionTest2() throws Exception { 1233 ApiException exception = 1234 ApiExceptionFactory.createException( 1235 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 1236 mockService.addException(exception); 1237 1238 try { 1239 String name = 1240 "projects/project-5927/locations/location-5927/runtimeVersions/runtimeVersion-5927"; 1241 client.getRuntimeVersion(name); 1242 Assert.fail("No exception raised"); 1243 } catch (InvalidArgumentException e) { 1244 // Expected exception. 1245 } 1246 } 1247 1248 @Test getGuestAttributesTest()1249 public void getGuestAttributesTest() throws Exception { 1250 GetGuestAttributesResponse expectedResponse = 1251 GetGuestAttributesResponse.newBuilder() 1252 .addAllGuestAttributes(new ArrayList<GuestAttributes>()) 1253 .build(); 1254 mockService.addResponse(expectedResponse); 1255 1256 GetGuestAttributesRequest request = 1257 GetGuestAttributesRequest.newBuilder() 1258 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 1259 .setQueryPath("queryPath-1807004403") 1260 .addAllWorkerIds(new ArrayList<String>()) 1261 .build(); 1262 1263 GetGuestAttributesResponse actualResponse = client.getGuestAttributes(request); 1264 Assert.assertEquals(expectedResponse, actualResponse); 1265 1266 List<String> actualRequests = mockService.getRequestPaths(); 1267 Assert.assertEquals(1, actualRequests.size()); 1268 1269 String apiClientHeaderKey = 1270 mockService 1271 .getRequestHeaders() 1272 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 1273 .iterator() 1274 .next(); 1275 Assert.assertTrue( 1276 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 1277 .matcher(apiClientHeaderKey) 1278 .matches()); 1279 } 1280 1281 @Test getGuestAttributesExceptionTest()1282 public void getGuestAttributesExceptionTest() throws Exception { 1283 ApiException exception = 1284 ApiExceptionFactory.createException( 1285 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 1286 mockService.addException(exception); 1287 1288 try { 1289 GetGuestAttributesRequest request = 1290 GetGuestAttributesRequest.newBuilder() 1291 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 1292 .setQueryPath("queryPath-1807004403") 1293 .addAllWorkerIds(new ArrayList<String>()) 1294 .build(); 1295 client.getGuestAttributes(request); 1296 Assert.fail("No exception raised"); 1297 } catch (InvalidArgumentException e) { 1298 // Expected exception. 1299 } 1300 } 1301 1302 @Test listLocationsTest()1303 public void listLocationsTest() throws Exception { 1304 Location responsesElement = Location.newBuilder().build(); 1305 ListLocationsResponse expectedResponse = 1306 ListLocationsResponse.newBuilder() 1307 .setNextPageToken("") 1308 .addAllLocations(Arrays.asList(responsesElement)) 1309 .build(); 1310 mockService.addResponse(expectedResponse); 1311 1312 ListLocationsRequest request = 1313 ListLocationsRequest.newBuilder() 1314 .setName("projects/project-3664") 1315 .setFilter("filter-1274492040") 1316 .setPageSize(883849137) 1317 .setPageToken("pageToken873572522") 1318 .build(); 1319 1320 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 1321 1322 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 1323 1324 Assert.assertEquals(1, resources.size()); 1325 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 1326 1327 List<String> actualRequests = mockService.getRequestPaths(); 1328 Assert.assertEquals(1, actualRequests.size()); 1329 1330 String apiClientHeaderKey = 1331 mockService 1332 .getRequestHeaders() 1333 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 1334 .iterator() 1335 .next(); 1336 Assert.assertTrue( 1337 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 1338 .matcher(apiClientHeaderKey) 1339 .matches()); 1340 } 1341 1342 @Test listLocationsExceptionTest()1343 public void listLocationsExceptionTest() throws Exception { 1344 ApiException exception = 1345 ApiExceptionFactory.createException( 1346 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 1347 mockService.addException(exception); 1348 1349 try { 1350 ListLocationsRequest request = 1351 ListLocationsRequest.newBuilder() 1352 .setName("projects/project-3664") 1353 .setFilter("filter-1274492040") 1354 .setPageSize(883849137) 1355 .setPageToken("pageToken873572522") 1356 .build(); 1357 client.listLocations(request); 1358 Assert.fail("No exception raised"); 1359 } catch (InvalidArgumentException e) { 1360 // Expected exception. 1361 } 1362 } 1363 1364 @Test getLocationTest()1365 public void getLocationTest() throws Exception { 1366 Location expectedResponse = 1367 Location.newBuilder() 1368 .setName("name3373707") 1369 .setLocationId("locationId1541836720") 1370 .setDisplayName("displayName1714148973") 1371 .putAllLabels(new HashMap<String, String>()) 1372 .setMetadata(Any.newBuilder().build()) 1373 .build(); 1374 mockService.addResponse(expectedResponse); 1375 1376 GetLocationRequest request = 1377 GetLocationRequest.newBuilder() 1378 .setName("projects/project-9062/locations/location-9062") 1379 .build(); 1380 1381 Location actualResponse = client.getLocation(request); 1382 Assert.assertEquals(expectedResponse, actualResponse); 1383 1384 List<String> actualRequests = mockService.getRequestPaths(); 1385 Assert.assertEquals(1, actualRequests.size()); 1386 1387 String apiClientHeaderKey = 1388 mockService 1389 .getRequestHeaders() 1390 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 1391 .iterator() 1392 .next(); 1393 Assert.assertTrue( 1394 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 1395 .matcher(apiClientHeaderKey) 1396 .matches()); 1397 } 1398 1399 @Test getLocationExceptionTest()1400 public void getLocationExceptionTest() throws Exception { 1401 ApiException exception = 1402 ApiExceptionFactory.createException( 1403 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 1404 mockService.addException(exception); 1405 1406 try { 1407 GetLocationRequest request = 1408 GetLocationRequest.newBuilder() 1409 .setName("projects/project-9062/locations/location-9062") 1410 .build(); 1411 client.getLocation(request); 1412 Assert.fail("No exception raised"); 1413 } catch (InvalidArgumentException e) { 1414 // Expected exception. 1415 } 1416 } 1417 } 1418