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.v1; 18 19 import static com.google.cloud.tpu.v1.TpuClient.ListAcceleratorTypesPagedResponse; 20 import static com.google.cloud.tpu.v1.TpuClient.ListLocationsPagedResponse; 21 import static com.google.cloud.tpu.v1.TpuClient.ListNodesPagedResponse; 22 import static com.google.cloud.tpu.v1.TpuClient.ListTensorFlowVersionsPagedResponse; 23 24 import com.google.api.gax.core.NoCredentialsProvider; 25 import com.google.api.gax.grpc.GaxGrpcProperties; 26 import com.google.api.gax.grpc.testing.LocalChannelProvider; 27 import com.google.api.gax.grpc.testing.MockGrpcService; 28 import com.google.api.gax.grpc.testing.MockServiceHelper; 29 import com.google.api.gax.rpc.ApiClientHeaderProvider; 30 import com.google.api.gax.rpc.InvalidArgumentException; 31 import com.google.api.gax.rpc.StatusCode; 32 import com.google.cloud.location.GetLocationRequest; 33 import com.google.cloud.location.ListLocationsRequest; 34 import com.google.cloud.location.ListLocationsResponse; 35 import com.google.cloud.location.Location; 36 import com.google.common.collect.Lists; 37 import com.google.longrunning.Operation; 38 import com.google.protobuf.AbstractMessage; 39 import com.google.protobuf.Any; 40 import com.google.protobuf.Timestamp; 41 import io.grpc.StatusRuntimeException; 42 import java.io.IOException; 43 import java.util.ArrayList; 44 import java.util.Arrays; 45 import java.util.HashMap; 46 import java.util.List; 47 import java.util.UUID; 48 import java.util.concurrent.ExecutionException; 49 import javax.annotation.Generated; 50 import org.junit.After; 51 import org.junit.AfterClass; 52 import org.junit.Assert; 53 import org.junit.Before; 54 import org.junit.BeforeClass; 55 import org.junit.Test; 56 57 @Generated("by gapic-generator-java") 58 public class TpuClientTest { 59 private static MockLocations mockLocations; 60 private static MockServiceHelper mockServiceHelper; 61 private static MockTpu mockTpu; 62 private LocalChannelProvider channelProvider; 63 private TpuClient client; 64 65 @BeforeClass startStaticServer()66 public static void startStaticServer() { 67 mockTpu = new MockTpu(); 68 mockLocations = new MockLocations(); 69 mockServiceHelper = 70 new MockServiceHelper( 71 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockTpu, mockLocations)); 72 mockServiceHelper.start(); 73 } 74 75 @AfterClass stopServer()76 public static void stopServer() { 77 mockServiceHelper.stop(); 78 } 79 80 @Before setUp()81 public void setUp() throws IOException { 82 mockServiceHelper.reset(); 83 channelProvider = mockServiceHelper.createChannelProvider(); 84 TpuSettings settings = 85 TpuSettings.newBuilder() 86 .setTransportChannelProvider(channelProvider) 87 .setCredentialsProvider(NoCredentialsProvider.create()) 88 .build(); 89 client = TpuClient.create(settings); 90 } 91 92 @After tearDown()93 public void tearDown() throws Exception { 94 client.close(); 95 } 96 97 @Test listNodesTest()98 public void listNodesTest() throws Exception { 99 Node responsesElement = Node.newBuilder().build(); 100 ListNodesResponse expectedResponse = 101 ListNodesResponse.newBuilder() 102 .setNextPageToken("") 103 .addAllNodes(Arrays.asList(responsesElement)) 104 .build(); 105 mockTpu.addResponse(expectedResponse); 106 107 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 108 109 ListNodesPagedResponse pagedListResponse = client.listNodes(parent); 110 111 List<Node> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 112 113 Assert.assertEquals(1, resources.size()); 114 Assert.assertEquals(expectedResponse.getNodesList().get(0), resources.get(0)); 115 116 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 117 Assert.assertEquals(1, actualRequests.size()); 118 ListNodesRequest actualRequest = ((ListNodesRequest) actualRequests.get(0)); 119 120 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 121 Assert.assertTrue( 122 channelProvider.isHeaderSent( 123 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 124 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 125 } 126 127 @Test listNodesExceptionTest()128 public void listNodesExceptionTest() throws Exception { 129 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 130 mockTpu.addException(exception); 131 132 try { 133 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 134 client.listNodes(parent); 135 Assert.fail("No exception raised"); 136 } catch (InvalidArgumentException e) { 137 // Expected exception. 138 } 139 } 140 141 @Test listNodesTest2()142 public void listNodesTest2() throws Exception { 143 Node responsesElement = Node.newBuilder().build(); 144 ListNodesResponse expectedResponse = 145 ListNodesResponse.newBuilder() 146 .setNextPageToken("") 147 .addAllNodes(Arrays.asList(responsesElement)) 148 .build(); 149 mockTpu.addResponse(expectedResponse); 150 151 String parent = "parent-995424086"; 152 153 ListNodesPagedResponse pagedListResponse = client.listNodes(parent); 154 155 List<Node> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 156 157 Assert.assertEquals(1, resources.size()); 158 Assert.assertEquals(expectedResponse.getNodesList().get(0), resources.get(0)); 159 160 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 161 Assert.assertEquals(1, actualRequests.size()); 162 ListNodesRequest actualRequest = ((ListNodesRequest) actualRequests.get(0)); 163 164 Assert.assertEquals(parent, actualRequest.getParent()); 165 Assert.assertTrue( 166 channelProvider.isHeaderSent( 167 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 168 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 169 } 170 171 @Test listNodesExceptionTest2()172 public void listNodesExceptionTest2() throws Exception { 173 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 174 mockTpu.addException(exception); 175 176 try { 177 String parent = "parent-995424086"; 178 client.listNodes(parent); 179 Assert.fail("No exception raised"); 180 } catch (InvalidArgumentException e) { 181 // Expected exception. 182 } 183 } 184 185 @Test getNodeTest()186 public void getNodeTest() throws Exception { 187 Node expectedResponse = 188 Node.newBuilder() 189 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 190 .setDescription("description-1724546052") 191 .setAcceleratorType("acceleratorType-82462651") 192 .setIpAddress("ipAddress1634032845") 193 .setPort("port3446913") 194 .setHealthDescription("healthDescription1231837184") 195 .setTensorflowVersion("tensorflowVersion-470125103") 196 .setNetwork("network1843485230") 197 .setCidrBlock("cidrBlock1646183801") 198 .setServiceAccount("serviceAccount1079137720") 199 .setCreateTime(Timestamp.newBuilder().build()) 200 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 201 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 202 .putAllLabels(new HashMap<String, String>()) 203 .setUseServiceNetworking(true) 204 .addAllSymptoms(new ArrayList<Symptom>()) 205 .build(); 206 mockTpu.addResponse(expectedResponse); 207 208 NodeName name = NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]"); 209 210 Node actualResponse = client.getNode(name); 211 Assert.assertEquals(expectedResponse, actualResponse); 212 213 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 214 Assert.assertEquals(1, actualRequests.size()); 215 GetNodeRequest actualRequest = ((GetNodeRequest) actualRequests.get(0)); 216 217 Assert.assertEquals(name.toString(), actualRequest.getName()); 218 Assert.assertTrue( 219 channelProvider.isHeaderSent( 220 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 221 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 222 } 223 224 @Test getNodeExceptionTest()225 public void getNodeExceptionTest() throws Exception { 226 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 227 mockTpu.addException(exception); 228 229 try { 230 NodeName name = NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]"); 231 client.getNode(name); 232 Assert.fail("No exception raised"); 233 } catch (InvalidArgumentException e) { 234 // Expected exception. 235 } 236 } 237 238 @Test getNodeTest2()239 public void getNodeTest2() throws Exception { 240 Node expectedResponse = 241 Node.newBuilder() 242 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 243 .setDescription("description-1724546052") 244 .setAcceleratorType("acceleratorType-82462651") 245 .setIpAddress("ipAddress1634032845") 246 .setPort("port3446913") 247 .setHealthDescription("healthDescription1231837184") 248 .setTensorflowVersion("tensorflowVersion-470125103") 249 .setNetwork("network1843485230") 250 .setCidrBlock("cidrBlock1646183801") 251 .setServiceAccount("serviceAccount1079137720") 252 .setCreateTime(Timestamp.newBuilder().build()) 253 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 254 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 255 .putAllLabels(new HashMap<String, String>()) 256 .setUseServiceNetworking(true) 257 .addAllSymptoms(new ArrayList<Symptom>()) 258 .build(); 259 mockTpu.addResponse(expectedResponse); 260 261 String name = "name3373707"; 262 263 Node actualResponse = client.getNode(name); 264 Assert.assertEquals(expectedResponse, actualResponse); 265 266 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 267 Assert.assertEquals(1, actualRequests.size()); 268 GetNodeRequest actualRequest = ((GetNodeRequest) actualRequests.get(0)); 269 270 Assert.assertEquals(name, actualRequest.getName()); 271 Assert.assertTrue( 272 channelProvider.isHeaderSent( 273 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 274 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 275 } 276 277 @Test getNodeExceptionTest2()278 public void getNodeExceptionTest2() throws Exception { 279 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 280 mockTpu.addException(exception); 281 282 try { 283 String name = "name3373707"; 284 client.getNode(name); 285 Assert.fail("No exception raised"); 286 } catch (InvalidArgumentException e) { 287 // Expected exception. 288 } 289 } 290 291 @Test createNodeTest()292 public void createNodeTest() throws Exception { 293 Node expectedResponse = 294 Node.newBuilder() 295 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 296 .setDescription("description-1724546052") 297 .setAcceleratorType("acceleratorType-82462651") 298 .setIpAddress("ipAddress1634032845") 299 .setPort("port3446913") 300 .setHealthDescription("healthDescription1231837184") 301 .setTensorflowVersion("tensorflowVersion-470125103") 302 .setNetwork("network1843485230") 303 .setCidrBlock("cidrBlock1646183801") 304 .setServiceAccount("serviceAccount1079137720") 305 .setCreateTime(Timestamp.newBuilder().build()) 306 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 307 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 308 .putAllLabels(new HashMap<String, String>()) 309 .setUseServiceNetworking(true) 310 .addAllSymptoms(new ArrayList<Symptom>()) 311 .build(); 312 Operation resultOperation = 313 Operation.newBuilder() 314 .setName("createNodeTest") 315 .setDone(true) 316 .setResponse(Any.pack(expectedResponse)) 317 .build(); 318 mockTpu.addResponse(resultOperation); 319 320 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 321 Node node = Node.newBuilder().build(); 322 String nodeId = "nodeId-1040171331"; 323 324 Node actualResponse = client.createNodeAsync(parent, node, nodeId).get(); 325 Assert.assertEquals(expectedResponse, actualResponse); 326 327 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 328 Assert.assertEquals(1, actualRequests.size()); 329 CreateNodeRequest actualRequest = ((CreateNodeRequest) actualRequests.get(0)); 330 331 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 332 Assert.assertEquals(node, actualRequest.getNode()); 333 Assert.assertEquals(nodeId, actualRequest.getNodeId()); 334 Assert.assertTrue( 335 channelProvider.isHeaderSent( 336 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 337 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 338 } 339 340 @Test createNodeExceptionTest()341 public void createNodeExceptionTest() throws Exception { 342 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 343 mockTpu.addException(exception); 344 345 try { 346 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 347 Node node = Node.newBuilder().build(); 348 String nodeId = "nodeId-1040171331"; 349 client.createNodeAsync(parent, node, nodeId).get(); 350 Assert.fail("No exception raised"); 351 } catch (ExecutionException e) { 352 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 353 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 354 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 355 } 356 } 357 358 @Test createNodeTest2()359 public void createNodeTest2() throws Exception { 360 Node expectedResponse = 361 Node.newBuilder() 362 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 363 .setDescription("description-1724546052") 364 .setAcceleratorType("acceleratorType-82462651") 365 .setIpAddress("ipAddress1634032845") 366 .setPort("port3446913") 367 .setHealthDescription("healthDescription1231837184") 368 .setTensorflowVersion("tensorflowVersion-470125103") 369 .setNetwork("network1843485230") 370 .setCidrBlock("cidrBlock1646183801") 371 .setServiceAccount("serviceAccount1079137720") 372 .setCreateTime(Timestamp.newBuilder().build()) 373 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 374 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 375 .putAllLabels(new HashMap<String, String>()) 376 .setUseServiceNetworking(true) 377 .addAllSymptoms(new ArrayList<Symptom>()) 378 .build(); 379 Operation resultOperation = 380 Operation.newBuilder() 381 .setName("createNodeTest") 382 .setDone(true) 383 .setResponse(Any.pack(expectedResponse)) 384 .build(); 385 mockTpu.addResponse(resultOperation); 386 387 String parent = "parent-995424086"; 388 Node node = Node.newBuilder().build(); 389 String nodeId = "nodeId-1040171331"; 390 391 Node actualResponse = client.createNodeAsync(parent, node, nodeId).get(); 392 Assert.assertEquals(expectedResponse, actualResponse); 393 394 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 395 Assert.assertEquals(1, actualRequests.size()); 396 CreateNodeRequest actualRequest = ((CreateNodeRequest) actualRequests.get(0)); 397 398 Assert.assertEquals(parent, actualRequest.getParent()); 399 Assert.assertEquals(node, actualRequest.getNode()); 400 Assert.assertEquals(nodeId, actualRequest.getNodeId()); 401 Assert.assertTrue( 402 channelProvider.isHeaderSent( 403 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 404 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 405 } 406 407 @Test createNodeExceptionTest2()408 public void createNodeExceptionTest2() throws Exception { 409 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 410 mockTpu.addException(exception); 411 412 try { 413 String parent = "parent-995424086"; 414 Node node = Node.newBuilder().build(); 415 String nodeId = "nodeId-1040171331"; 416 client.createNodeAsync(parent, node, nodeId).get(); 417 Assert.fail("No exception raised"); 418 } catch (ExecutionException e) { 419 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 420 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 421 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 422 } 423 } 424 425 @Test deleteNodeTest()426 public void deleteNodeTest() throws Exception { 427 Node expectedResponse = 428 Node.newBuilder() 429 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 430 .setDescription("description-1724546052") 431 .setAcceleratorType("acceleratorType-82462651") 432 .setIpAddress("ipAddress1634032845") 433 .setPort("port3446913") 434 .setHealthDescription("healthDescription1231837184") 435 .setTensorflowVersion("tensorflowVersion-470125103") 436 .setNetwork("network1843485230") 437 .setCidrBlock("cidrBlock1646183801") 438 .setServiceAccount("serviceAccount1079137720") 439 .setCreateTime(Timestamp.newBuilder().build()) 440 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 441 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 442 .putAllLabels(new HashMap<String, String>()) 443 .setUseServiceNetworking(true) 444 .addAllSymptoms(new ArrayList<Symptom>()) 445 .build(); 446 Operation resultOperation = 447 Operation.newBuilder() 448 .setName("deleteNodeTest") 449 .setDone(true) 450 .setResponse(Any.pack(expectedResponse)) 451 .build(); 452 mockTpu.addResponse(resultOperation); 453 454 NodeName name = NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]"); 455 456 Node actualResponse = client.deleteNodeAsync(name).get(); 457 Assert.assertEquals(expectedResponse, actualResponse); 458 459 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 460 Assert.assertEquals(1, actualRequests.size()); 461 DeleteNodeRequest actualRequest = ((DeleteNodeRequest) actualRequests.get(0)); 462 463 Assert.assertEquals(name.toString(), actualRequest.getName()); 464 Assert.assertTrue( 465 channelProvider.isHeaderSent( 466 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 467 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 468 } 469 470 @Test deleteNodeExceptionTest()471 public void deleteNodeExceptionTest() throws Exception { 472 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 473 mockTpu.addException(exception); 474 475 try { 476 NodeName name = NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]"); 477 client.deleteNodeAsync(name).get(); 478 Assert.fail("No exception raised"); 479 } catch (ExecutionException e) { 480 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 481 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 482 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 483 } 484 } 485 486 @Test deleteNodeTest2()487 public void deleteNodeTest2() throws Exception { 488 Node expectedResponse = 489 Node.newBuilder() 490 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 491 .setDescription("description-1724546052") 492 .setAcceleratorType("acceleratorType-82462651") 493 .setIpAddress("ipAddress1634032845") 494 .setPort("port3446913") 495 .setHealthDescription("healthDescription1231837184") 496 .setTensorflowVersion("tensorflowVersion-470125103") 497 .setNetwork("network1843485230") 498 .setCidrBlock("cidrBlock1646183801") 499 .setServiceAccount("serviceAccount1079137720") 500 .setCreateTime(Timestamp.newBuilder().build()) 501 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 502 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 503 .putAllLabels(new HashMap<String, String>()) 504 .setUseServiceNetworking(true) 505 .addAllSymptoms(new ArrayList<Symptom>()) 506 .build(); 507 Operation resultOperation = 508 Operation.newBuilder() 509 .setName("deleteNodeTest") 510 .setDone(true) 511 .setResponse(Any.pack(expectedResponse)) 512 .build(); 513 mockTpu.addResponse(resultOperation); 514 515 String name = "name3373707"; 516 517 Node actualResponse = client.deleteNodeAsync(name).get(); 518 Assert.assertEquals(expectedResponse, actualResponse); 519 520 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 521 Assert.assertEquals(1, actualRequests.size()); 522 DeleteNodeRequest actualRequest = ((DeleteNodeRequest) actualRequests.get(0)); 523 524 Assert.assertEquals(name, actualRequest.getName()); 525 Assert.assertTrue( 526 channelProvider.isHeaderSent( 527 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 528 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 529 } 530 531 @Test deleteNodeExceptionTest2()532 public void deleteNodeExceptionTest2() throws Exception { 533 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 534 mockTpu.addException(exception); 535 536 try { 537 String name = "name3373707"; 538 client.deleteNodeAsync(name).get(); 539 Assert.fail("No exception raised"); 540 } catch (ExecutionException e) { 541 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 542 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 543 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 544 } 545 } 546 547 @Test reimageNodeTest()548 public void reimageNodeTest() throws Exception { 549 Node expectedResponse = 550 Node.newBuilder() 551 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 552 .setDescription("description-1724546052") 553 .setAcceleratorType("acceleratorType-82462651") 554 .setIpAddress("ipAddress1634032845") 555 .setPort("port3446913") 556 .setHealthDescription("healthDescription1231837184") 557 .setTensorflowVersion("tensorflowVersion-470125103") 558 .setNetwork("network1843485230") 559 .setCidrBlock("cidrBlock1646183801") 560 .setServiceAccount("serviceAccount1079137720") 561 .setCreateTime(Timestamp.newBuilder().build()) 562 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 563 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 564 .putAllLabels(new HashMap<String, String>()) 565 .setUseServiceNetworking(true) 566 .addAllSymptoms(new ArrayList<Symptom>()) 567 .build(); 568 Operation resultOperation = 569 Operation.newBuilder() 570 .setName("reimageNodeTest") 571 .setDone(true) 572 .setResponse(Any.pack(expectedResponse)) 573 .build(); 574 mockTpu.addResponse(resultOperation); 575 576 ReimageNodeRequest request = 577 ReimageNodeRequest.newBuilder() 578 .setName("name3373707") 579 .setTensorflowVersion("tensorflowVersion-470125103") 580 .build(); 581 582 Node actualResponse = client.reimageNodeAsync(request).get(); 583 Assert.assertEquals(expectedResponse, actualResponse); 584 585 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 586 Assert.assertEquals(1, actualRequests.size()); 587 ReimageNodeRequest actualRequest = ((ReimageNodeRequest) actualRequests.get(0)); 588 589 Assert.assertEquals(request.getName(), actualRequest.getName()); 590 Assert.assertEquals(request.getTensorflowVersion(), actualRequest.getTensorflowVersion()); 591 Assert.assertTrue( 592 channelProvider.isHeaderSent( 593 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 594 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 595 } 596 597 @Test reimageNodeExceptionTest()598 public void reimageNodeExceptionTest() throws Exception { 599 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 600 mockTpu.addException(exception); 601 602 try { 603 ReimageNodeRequest request = 604 ReimageNodeRequest.newBuilder() 605 .setName("name3373707") 606 .setTensorflowVersion("tensorflowVersion-470125103") 607 .build(); 608 client.reimageNodeAsync(request).get(); 609 Assert.fail("No exception raised"); 610 } catch (ExecutionException e) { 611 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 612 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 613 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 614 } 615 } 616 617 @Test stopNodeTest()618 public void stopNodeTest() throws Exception { 619 Node expectedResponse = 620 Node.newBuilder() 621 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 622 .setDescription("description-1724546052") 623 .setAcceleratorType("acceleratorType-82462651") 624 .setIpAddress("ipAddress1634032845") 625 .setPort("port3446913") 626 .setHealthDescription("healthDescription1231837184") 627 .setTensorflowVersion("tensorflowVersion-470125103") 628 .setNetwork("network1843485230") 629 .setCidrBlock("cidrBlock1646183801") 630 .setServiceAccount("serviceAccount1079137720") 631 .setCreateTime(Timestamp.newBuilder().build()) 632 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 633 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 634 .putAllLabels(new HashMap<String, String>()) 635 .setUseServiceNetworking(true) 636 .addAllSymptoms(new ArrayList<Symptom>()) 637 .build(); 638 Operation resultOperation = 639 Operation.newBuilder() 640 .setName("stopNodeTest") 641 .setDone(true) 642 .setResponse(Any.pack(expectedResponse)) 643 .build(); 644 mockTpu.addResponse(resultOperation); 645 646 StopNodeRequest request = StopNodeRequest.newBuilder().setName("name3373707").build(); 647 648 Node actualResponse = client.stopNodeAsync(request).get(); 649 Assert.assertEquals(expectedResponse, actualResponse); 650 651 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 652 Assert.assertEquals(1, actualRequests.size()); 653 StopNodeRequest actualRequest = ((StopNodeRequest) actualRequests.get(0)); 654 655 Assert.assertEquals(request.getName(), actualRequest.getName()); 656 Assert.assertTrue( 657 channelProvider.isHeaderSent( 658 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 659 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 660 } 661 662 @Test stopNodeExceptionTest()663 public void stopNodeExceptionTest() throws Exception { 664 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 665 mockTpu.addException(exception); 666 667 try { 668 StopNodeRequest request = StopNodeRequest.newBuilder().setName("name3373707").build(); 669 client.stopNodeAsync(request).get(); 670 Assert.fail("No exception raised"); 671 } catch (ExecutionException e) { 672 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 673 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 674 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 675 } 676 } 677 678 @Test startNodeTest()679 public void startNodeTest() throws Exception { 680 Node expectedResponse = 681 Node.newBuilder() 682 .setName(NodeName.of("[PROJECT]", "[LOCATION]", "[NODE]").toString()) 683 .setDescription("description-1724546052") 684 .setAcceleratorType("acceleratorType-82462651") 685 .setIpAddress("ipAddress1634032845") 686 .setPort("port3446913") 687 .setHealthDescription("healthDescription1231837184") 688 .setTensorflowVersion("tensorflowVersion-470125103") 689 .setNetwork("network1843485230") 690 .setCidrBlock("cidrBlock1646183801") 691 .setServiceAccount("serviceAccount1079137720") 692 .setCreateTime(Timestamp.newBuilder().build()) 693 .setSchedulingConfig(SchedulingConfig.newBuilder().build()) 694 .addAllNetworkEndpoints(new ArrayList<NetworkEndpoint>()) 695 .putAllLabels(new HashMap<String, String>()) 696 .setUseServiceNetworking(true) 697 .addAllSymptoms(new ArrayList<Symptom>()) 698 .build(); 699 Operation resultOperation = 700 Operation.newBuilder() 701 .setName("startNodeTest") 702 .setDone(true) 703 .setResponse(Any.pack(expectedResponse)) 704 .build(); 705 mockTpu.addResponse(resultOperation); 706 707 StartNodeRequest request = StartNodeRequest.newBuilder().setName("name3373707").build(); 708 709 Node actualResponse = client.startNodeAsync(request).get(); 710 Assert.assertEquals(expectedResponse, actualResponse); 711 712 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 713 Assert.assertEquals(1, actualRequests.size()); 714 StartNodeRequest actualRequest = ((StartNodeRequest) actualRequests.get(0)); 715 716 Assert.assertEquals(request.getName(), actualRequest.getName()); 717 Assert.assertTrue( 718 channelProvider.isHeaderSent( 719 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 720 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 721 } 722 723 @Test startNodeExceptionTest()724 public void startNodeExceptionTest() throws Exception { 725 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 726 mockTpu.addException(exception); 727 728 try { 729 StartNodeRequest request = StartNodeRequest.newBuilder().setName("name3373707").build(); 730 client.startNodeAsync(request).get(); 731 Assert.fail("No exception raised"); 732 } catch (ExecutionException e) { 733 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 734 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 735 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 736 } 737 } 738 739 @Test listTensorFlowVersionsTest()740 public void listTensorFlowVersionsTest() throws Exception { 741 TensorFlowVersion responsesElement = TensorFlowVersion.newBuilder().build(); 742 ListTensorFlowVersionsResponse expectedResponse = 743 ListTensorFlowVersionsResponse.newBuilder() 744 .setNextPageToken("") 745 .addAllTensorflowVersions(Arrays.asList(responsesElement)) 746 .build(); 747 mockTpu.addResponse(expectedResponse); 748 749 TensorFlowVersionName parent = 750 TensorFlowVersionName.of("[PROJECT]", "[LOCATION]", "[TENSOR_FLOW_VERSION]"); 751 752 ListTensorFlowVersionsPagedResponse pagedListResponse = client.listTensorFlowVersions(parent); 753 754 List<TensorFlowVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 755 756 Assert.assertEquals(1, resources.size()); 757 Assert.assertEquals(expectedResponse.getTensorflowVersionsList().get(0), resources.get(0)); 758 759 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 760 Assert.assertEquals(1, actualRequests.size()); 761 ListTensorFlowVersionsRequest actualRequest = 762 ((ListTensorFlowVersionsRequest) actualRequests.get(0)); 763 764 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 765 Assert.assertTrue( 766 channelProvider.isHeaderSent( 767 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 768 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 769 } 770 771 @Test listTensorFlowVersionsExceptionTest()772 public void listTensorFlowVersionsExceptionTest() throws Exception { 773 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 774 mockTpu.addException(exception); 775 776 try { 777 TensorFlowVersionName parent = 778 TensorFlowVersionName.of("[PROJECT]", "[LOCATION]", "[TENSOR_FLOW_VERSION]"); 779 client.listTensorFlowVersions(parent); 780 Assert.fail("No exception raised"); 781 } catch (InvalidArgumentException e) { 782 // Expected exception. 783 } 784 } 785 786 @Test listTensorFlowVersionsTest2()787 public void listTensorFlowVersionsTest2() throws Exception { 788 TensorFlowVersion responsesElement = TensorFlowVersion.newBuilder().build(); 789 ListTensorFlowVersionsResponse expectedResponse = 790 ListTensorFlowVersionsResponse.newBuilder() 791 .setNextPageToken("") 792 .addAllTensorflowVersions(Arrays.asList(responsesElement)) 793 .build(); 794 mockTpu.addResponse(expectedResponse); 795 796 String parent = "parent-995424086"; 797 798 ListTensorFlowVersionsPagedResponse pagedListResponse = client.listTensorFlowVersions(parent); 799 800 List<TensorFlowVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 801 802 Assert.assertEquals(1, resources.size()); 803 Assert.assertEquals(expectedResponse.getTensorflowVersionsList().get(0), resources.get(0)); 804 805 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 806 Assert.assertEquals(1, actualRequests.size()); 807 ListTensorFlowVersionsRequest actualRequest = 808 ((ListTensorFlowVersionsRequest) actualRequests.get(0)); 809 810 Assert.assertEquals(parent, actualRequest.getParent()); 811 Assert.assertTrue( 812 channelProvider.isHeaderSent( 813 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 814 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 815 } 816 817 @Test listTensorFlowVersionsExceptionTest2()818 public void listTensorFlowVersionsExceptionTest2() throws Exception { 819 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 820 mockTpu.addException(exception); 821 822 try { 823 String parent = "parent-995424086"; 824 client.listTensorFlowVersions(parent); 825 Assert.fail("No exception raised"); 826 } catch (InvalidArgumentException e) { 827 // Expected exception. 828 } 829 } 830 831 @Test getTensorFlowVersionTest()832 public void getTensorFlowVersionTest() throws Exception { 833 TensorFlowVersion expectedResponse = 834 TensorFlowVersion.newBuilder() 835 .setName( 836 TensorFlowVersionName.of("[PROJECT]", "[LOCATION]", "[TENSOR_FLOW_VERSION]") 837 .toString()) 838 .setVersion("version351608024") 839 .build(); 840 mockTpu.addResponse(expectedResponse); 841 842 TensorFlowVersionName name = 843 TensorFlowVersionName.of("[PROJECT]", "[LOCATION]", "[TENSOR_FLOW_VERSION]"); 844 845 TensorFlowVersion actualResponse = client.getTensorFlowVersion(name); 846 Assert.assertEquals(expectedResponse, actualResponse); 847 848 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 849 Assert.assertEquals(1, actualRequests.size()); 850 GetTensorFlowVersionRequest actualRequest = 851 ((GetTensorFlowVersionRequest) actualRequests.get(0)); 852 853 Assert.assertEquals(name.toString(), actualRequest.getName()); 854 Assert.assertTrue( 855 channelProvider.isHeaderSent( 856 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 857 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 858 } 859 860 @Test getTensorFlowVersionExceptionTest()861 public void getTensorFlowVersionExceptionTest() throws Exception { 862 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 863 mockTpu.addException(exception); 864 865 try { 866 TensorFlowVersionName name = 867 TensorFlowVersionName.of("[PROJECT]", "[LOCATION]", "[TENSOR_FLOW_VERSION]"); 868 client.getTensorFlowVersion(name); 869 Assert.fail("No exception raised"); 870 } catch (InvalidArgumentException e) { 871 // Expected exception. 872 } 873 } 874 875 @Test getTensorFlowVersionTest2()876 public void getTensorFlowVersionTest2() throws Exception { 877 TensorFlowVersion expectedResponse = 878 TensorFlowVersion.newBuilder() 879 .setName( 880 TensorFlowVersionName.of("[PROJECT]", "[LOCATION]", "[TENSOR_FLOW_VERSION]") 881 .toString()) 882 .setVersion("version351608024") 883 .build(); 884 mockTpu.addResponse(expectedResponse); 885 886 String name = "name3373707"; 887 888 TensorFlowVersion actualResponse = client.getTensorFlowVersion(name); 889 Assert.assertEquals(expectedResponse, actualResponse); 890 891 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 892 Assert.assertEquals(1, actualRequests.size()); 893 GetTensorFlowVersionRequest actualRequest = 894 ((GetTensorFlowVersionRequest) actualRequests.get(0)); 895 896 Assert.assertEquals(name, actualRequest.getName()); 897 Assert.assertTrue( 898 channelProvider.isHeaderSent( 899 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 900 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 901 } 902 903 @Test getTensorFlowVersionExceptionTest2()904 public void getTensorFlowVersionExceptionTest2() throws Exception { 905 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 906 mockTpu.addException(exception); 907 908 try { 909 String name = "name3373707"; 910 client.getTensorFlowVersion(name); 911 Assert.fail("No exception raised"); 912 } catch (InvalidArgumentException e) { 913 // Expected exception. 914 } 915 } 916 917 @Test listAcceleratorTypesTest()918 public void listAcceleratorTypesTest() throws Exception { 919 AcceleratorType responsesElement = AcceleratorType.newBuilder().build(); 920 ListAcceleratorTypesResponse expectedResponse = 921 ListAcceleratorTypesResponse.newBuilder() 922 .setNextPageToken("") 923 .addAllAcceleratorTypes(Arrays.asList(responsesElement)) 924 .build(); 925 mockTpu.addResponse(expectedResponse); 926 927 AcceleratorTypeName parent = 928 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]"); 929 930 ListAcceleratorTypesPagedResponse pagedListResponse = client.listAcceleratorTypes(parent); 931 932 List<AcceleratorType> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 933 934 Assert.assertEquals(1, resources.size()); 935 Assert.assertEquals(expectedResponse.getAcceleratorTypesList().get(0), resources.get(0)); 936 937 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 938 Assert.assertEquals(1, actualRequests.size()); 939 ListAcceleratorTypesRequest actualRequest = 940 ((ListAcceleratorTypesRequest) actualRequests.get(0)); 941 942 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 943 Assert.assertTrue( 944 channelProvider.isHeaderSent( 945 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 946 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 947 } 948 949 @Test listAcceleratorTypesExceptionTest()950 public void listAcceleratorTypesExceptionTest() throws Exception { 951 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 952 mockTpu.addException(exception); 953 954 try { 955 AcceleratorTypeName parent = 956 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]"); 957 client.listAcceleratorTypes(parent); 958 Assert.fail("No exception raised"); 959 } catch (InvalidArgumentException e) { 960 // Expected exception. 961 } 962 } 963 964 @Test listAcceleratorTypesTest2()965 public void listAcceleratorTypesTest2() throws Exception { 966 AcceleratorType responsesElement = AcceleratorType.newBuilder().build(); 967 ListAcceleratorTypesResponse expectedResponse = 968 ListAcceleratorTypesResponse.newBuilder() 969 .setNextPageToken("") 970 .addAllAcceleratorTypes(Arrays.asList(responsesElement)) 971 .build(); 972 mockTpu.addResponse(expectedResponse); 973 974 String parent = "parent-995424086"; 975 976 ListAcceleratorTypesPagedResponse pagedListResponse = client.listAcceleratorTypes(parent); 977 978 List<AcceleratorType> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 979 980 Assert.assertEquals(1, resources.size()); 981 Assert.assertEquals(expectedResponse.getAcceleratorTypesList().get(0), resources.get(0)); 982 983 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 984 Assert.assertEquals(1, actualRequests.size()); 985 ListAcceleratorTypesRequest actualRequest = 986 ((ListAcceleratorTypesRequest) actualRequests.get(0)); 987 988 Assert.assertEquals(parent, actualRequest.getParent()); 989 Assert.assertTrue( 990 channelProvider.isHeaderSent( 991 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 992 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 993 } 994 995 @Test listAcceleratorTypesExceptionTest2()996 public void listAcceleratorTypesExceptionTest2() throws Exception { 997 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 998 mockTpu.addException(exception); 999 1000 try { 1001 String parent = "parent-995424086"; 1002 client.listAcceleratorTypes(parent); 1003 Assert.fail("No exception raised"); 1004 } catch (InvalidArgumentException e) { 1005 // Expected exception. 1006 } 1007 } 1008 1009 @Test getAcceleratorTypeTest()1010 public void getAcceleratorTypeTest() throws Exception { 1011 AcceleratorType expectedResponse = 1012 AcceleratorType.newBuilder() 1013 .setName( 1014 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]").toString()) 1015 .setType("type3575610") 1016 .build(); 1017 mockTpu.addResponse(expectedResponse); 1018 1019 AcceleratorTypeName name = 1020 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]"); 1021 1022 AcceleratorType actualResponse = client.getAcceleratorType(name); 1023 Assert.assertEquals(expectedResponse, actualResponse); 1024 1025 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 1026 Assert.assertEquals(1, actualRequests.size()); 1027 GetAcceleratorTypeRequest actualRequest = ((GetAcceleratorTypeRequest) actualRequests.get(0)); 1028 1029 Assert.assertEquals(name.toString(), actualRequest.getName()); 1030 Assert.assertTrue( 1031 channelProvider.isHeaderSent( 1032 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 1033 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 1034 } 1035 1036 @Test getAcceleratorTypeExceptionTest()1037 public void getAcceleratorTypeExceptionTest() throws Exception { 1038 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 1039 mockTpu.addException(exception); 1040 1041 try { 1042 AcceleratorTypeName name = 1043 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]"); 1044 client.getAcceleratorType(name); 1045 Assert.fail("No exception raised"); 1046 } catch (InvalidArgumentException e) { 1047 // Expected exception. 1048 } 1049 } 1050 1051 @Test getAcceleratorTypeTest2()1052 public void getAcceleratorTypeTest2() throws Exception { 1053 AcceleratorType expectedResponse = 1054 AcceleratorType.newBuilder() 1055 .setName( 1056 AcceleratorTypeName.of("[PROJECT]", "[LOCATION]", "[ACCELERATOR_TYPE]").toString()) 1057 .setType("type3575610") 1058 .build(); 1059 mockTpu.addResponse(expectedResponse); 1060 1061 String name = "name3373707"; 1062 1063 AcceleratorType actualResponse = client.getAcceleratorType(name); 1064 Assert.assertEquals(expectedResponse, actualResponse); 1065 1066 List<AbstractMessage> actualRequests = mockTpu.getRequests(); 1067 Assert.assertEquals(1, actualRequests.size()); 1068 GetAcceleratorTypeRequest actualRequest = ((GetAcceleratorTypeRequest) actualRequests.get(0)); 1069 1070 Assert.assertEquals(name, actualRequest.getName()); 1071 Assert.assertTrue( 1072 channelProvider.isHeaderSent( 1073 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 1074 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 1075 } 1076 1077 @Test getAcceleratorTypeExceptionTest2()1078 public void getAcceleratorTypeExceptionTest2() throws Exception { 1079 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 1080 mockTpu.addException(exception); 1081 1082 try { 1083 String name = "name3373707"; 1084 client.getAcceleratorType(name); 1085 Assert.fail("No exception raised"); 1086 } catch (InvalidArgumentException e) { 1087 // Expected exception. 1088 } 1089 } 1090 1091 @Test listLocationsTest()1092 public void listLocationsTest() throws Exception { 1093 Location responsesElement = Location.newBuilder().build(); 1094 ListLocationsResponse expectedResponse = 1095 ListLocationsResponse.newBuilder() 1096 .setNextPageToken("") 1097 .addAllLocations(Arrays.asList(responsesElement)) 1098 .build(); 1099 mockLocations.addResponse(expectedResponse); 1100 1101 ListLocationsRequest request = 1102 ListLocationsRequest.newBuilder() 1103 .setName("name3373707") 1104 .setFilter("filter-1274492040") 1105 .setPageSize(883849137) 1106 .setPageToken("pageToken873572522") 1107 .build(); 1108 1109 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 1110 1111 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 1112 1113 Assert.assertEquals(1, resources.size()); 1114 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 1115 1116 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 1117 Assert.assertEquals(1, actualRequests.size()); 1118 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 1119 1120 Assert.assertEquals(request.getName(), actualRequest.getName()); 1121 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 1122 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 1123 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 1124 Assert.assertTrue( 1125 channelProvider.isHeaderSent( 1126 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 1127 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 1128 } 1129 1130 @Test listLocationsExceptionTest()1131 public void listLocationsExceptionTest() throws Exception { 1132 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 1133 mockLocations.addException(exception); 1134 1135 try { 1136 ListLocationsRequest request = 1137 ListLocationsRequest.newBuilder() 1138 .setName("name3373707") 1139 .setFilter("filter-1274492040") 1140 .setPageSize(883849137) 1141 .setPageToken("pageToken873572522") 1142 .build(); 1143 client.listLocations(request); 1144 Assert.fail("No exception raised"); 1145 } catch (InvalidArgumentException e) { 1146 // Expected exception. 1147 } 1148 } 1149 1150 @Test getLocationTest()1151 public void getLocationTest() throws Exception { 1152 Location expectedResponse = 1153 Location.newBuilder() 1154 .setName("name3373707") 1155 .setLocationId("locationId1541836720") 1156 .setDisplayName("displayName1714148973") 1157 .putAllLabels(new HashMap<String, String>()) 1158 .setMetadata(Any.newBuilder().build()) 1159 .build(); 1160 mockLocations.addResponse(expectedResponse); 1161 1162 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 1163 1164 Location actualResponse = client.getLocation(request); 1165 Assert.assertEquals(expectedResponse, actualResponse); 1166 1167 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 1168 Assert.assertEquals(1, actualRequests.size()); 1169 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 1170 1171 Assert.assertEquals(request.getName(), actualRequest.getName()); 1172 Assert.assertTrue( 1173 channelProvider.isHeaderSent( 1174 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 1175 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 1176 } 1177 1178 @Test getLocationExceptionTest()1179 public void getLocationExceptionTest() throws Exception { 1180 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 1181 mockLocations.addException(exception); 1182 1183 try { 1184 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 1185 client.getLocation(request); 1186 Assert.fail("No exception raised"); 1187 } catch (InvalidArgumentException e) { 1188 // Expected exception. 1189 } 1190 } 1191 } 1192