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.batch.v1; 18 19 import static com.google.cloud.batch.v1.BatchServiceClient.ListJobsPagedResponse; 20 import static com.google.cloud.batch.v1.BatchServiceClient.ListLocationsPagedResponse; 21 import static com.google.cloud.batch.v1.BatchServiceClient.ListTasksPagedResponse; 22 23 import com.google.api.gax.core.NoCredentialsProvider; 24 import com.google.api.gax.grpc.GaxGrpcProperties; 25 import com.google.api.gax.grpc.testing.LocalChannelProvider; 26 import com.google.api.gax.grpc.testing.MockGrpcService; 27 import com.google.api.gax.grpc.testing.MockServiceHelper; 28 import com.google.api.gax.rpc.ApiClientHeaderProvider; 29 import com.google.api.gax.rpc.InvalidArgumentException; 30 import com.google.api.gax.rpc.StatusCode; 31 import com.google.cloud.location.GetLocationRequest; 32 import com.google.cloud.location.ListLocationsRequest; 33 import com.google.cloud.location.ListLocationsResponse; 34 import com.google.cloud.location.Location; 35 import com.google.common.collect.Lists; 36 import com.google.longrunning.Operation; 37 import com.google.protobuf.AbstractMessage; 38 import com.google.protobuf.Any; 39 import com.google.protobuf.Empty; 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 BatchServiceClientTest { 59 private static MockBatchService mockBatchService; 60 private static MockLocations mockLocations; 61 private static MockServiceHelper mockServiceHelper; 62 private LocalChannelProvider channelProvider; 63 private BatchServiceClient client; 64 65 @BeforeClass startStaticServer()66 public static void startStaticServer() { 67 mockBatchService = new MockBatchService(); 68 mockLocations = new MockLocations(); 69 mockServiceHelper = 70 new MockServiceHelper( 71 UUID.randomUUID().toString(), 72 Arrays.<MockGrpcService>asList(mockBatchService, mockLocations)); 73 mockServiceHelper.start(); 74 } 75 76 @AfterClass stopServer()77 public static void stopServer() { 78 mockServiceHelper.stop(); 79 } 80 81 @Before setUp()82 public void setUp() throws IOException { 83 mockServiceHelper.reset(); 84 channelProvider = mockServiceHelper.createChannelProvider(); 85 BatchServiceSettings settings = 86 BatchServiceSettings.newBuilder() 87 .setTransportChannelProvider(channelProvider) 88 .setCredentialsProvider(NoCredentialsProvider.create()) 89 .build(); 90 client = BatchServiceClient.create(settings); 91 } 92 93 @After tearDown()94 public void tearDown() throws Exception { 95 client.close(); 96 } 97 98 @Test createJobTest()99 public void createJobTest() throws Exception { 100 Job expectedResponse = 101 Job.newBuilder() 102 .setName(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString()) 103 .setUid("uid115792") 104 .setPriority(-1165461084) 105 .addAllTaskGroups(new ArrayList<TaskGroup>()) 106 .setAllocationPolicy(AllocationPolicy.newBuilder().build()) 107 .putAllLabels(new HashMap<String, String>()) 108 .setStatus(JobStatus.newBuilder().build()) 109 .setCreateTime(Timestamp.newBuilder().build()) 110 .setUpdateTime(Timestamp.newBuilder().build()) 111 .setLogsPolicy(LogsPolicy.newBuilder().build()) 112 .addAllNotifications(new ArrayList<JobNotification>()) 113 .build(); 114 mockBatchService.addResponse(expectedResponse); 115 116 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 117 Job job = Job.newBuilder().build(); 118 String jobId = "jobId101296568"; 119 120 Job actualResponse = client.createJob(parent, job, jobId); 121 Assert.assertEquals(expectedResponse, actualResponse); 122 123 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 124 Assert.assertEquals(1, actualRequests.size()); 125 CreateJobRequest actualRequest = ((CreateJobRequest) actualRequests.get(0)); 126 127 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 128 Assert.assertEquals(job, actualRequest.getJob()); 129 Assert.assertEquals(jobId, actualRequest.getJobId()); 130 Assert.assertTrue( 131 channelProvider.isHeaderSent( 132 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 133 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 134 } 135 136 @Test createJobExceptionTest()137 public void createJobExceptionTest() throws Exception { 138 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 139 mockBatchService.addException(exception); 140 141 try { 142 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 143 Job job = Job.newBuilder().build(); 144 String jobId = "jobId101296568"; 145 client.createJob(parent, job, jobId); 146 Assert.fail("No exception raised"); 147 } catch (InvalidArgumentException e) { 148 // Expected exception. 149 } 150 } 151 152 @Test createJobTest2()153 public void createJobTest2() throws Exception { 154 Job expectedResponse = 155 Job.newBuilder() 156 .setName(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString()) 157 .setUid("uid115792") 158 .setPriority(-1165461084) 159 .addAllTaskGroups(new ArrayList<TaskGroup>()) 160 .setAllocationPolicy(AllocationPolicy.newBuilder().build()) 161 .putAllLabels(new HashMap<String, String>()) 162 .setStatus(JobStatus.newBuilder().build()) 163 .setCreateTime(Timestamp.newBuilder().build()) 164 .setUpdateTime(Timestamp.newBuilder().build()) 165 .setLogsPolicy(LogsPolicy.newBuilder().build()) 166 .addAllNotifications(new ArrayList<JobNotification>()) 167 .build(); 168 mockBatchService.addResponse(expectedResponse); 169 170 String parent = "parent-995424086"; 171 Job job = Job.newBuilder().build(); 172 String jobId = "jobId101296568"; 173 174 Job actualResponse = client.createJob(parent, job, jobId); 175 Assert.assertEquals(expectedResponse, actualResponse); 176 177 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 178 Assert.assertEquals(1, actualRequests.size()); 179 CreateJobRequest actualRequest = ((CreateJobRequest) actualRequests.get(0)); 180 181 Assert.assertEquals(parent, actualRequest.getParent()); 182 Assert.assertEquals(job, actualRequest.getJob()); 183 Assert.assertEquals(jobId, actualRequest.getJobId()); 184 Assert.assertTrue( 185 channelProvider.isHeaderSent( 186 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 187 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 188 } 189 190 @Test createJobExceptionTest2()191 public void createJobExceptionTest2() throws Exception { 192 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 193 mockBatchService.addException(exception); 194 195 try { 196 String parent = "parent-995424086"; 197 Job job = Job.newBuilder().build(); 198 String jobId = "jobId101296568"; 199 client.createJob(parent, job, jobId); 200 Assert.fail("No exception raised"); 201 } catch (InvalidArgumentException e) { 202 // Expected exception. 203 } 204 } 205 206 @Test getJobTest()207 public void getJobTest() throws Exception { 208 Job expectedResponse = 209 Job.newBuilder() 210 .setName(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString()) 211 .setUid("uid115792") 212 .setPriority(-1165461084) 213 .addAllTaskGroups(new ArrayList<TaskGroup>()) 214 .setAllocationPolicy(AllocationPolicy.newBuilder().build()) 215 .putAllLabels(new HashMap<String, String>()) 216 .setStatus(JobStatus.newBuilder().build()) 217 .setCreateTime(Timestamp.newBuilder().build()) 218 .setUpdateTime(Timestamp.newBuilder().build()) 219 .setLogsPolicy(LogsPolicy.newBuilder().build()) 220 .addAllNotifications(new ArrayList<JobNotification>()) 221 .build(); 222 mockBatchService.addResponse(expectedResponse); 223 224 JobName name = JobName.of("[PROJECT]", "[LOCATION]", "[JOB]"); 225 226 Job actualResponse = client.getJob(name); 227 Assert.assertEquals(expectedResponse, actualResponse); 228 229 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 230 Assert.assertEquals(1, actualRequests.size()); 231 GetJobRequest actualRequest = ((GetJobRequest) actualRequests.get(0)); 232 233 Assert.assertEquals(name.toString(), actualRequest.getName()); 234 Assert.assertTrue( 235 channelProvider.isHeaderSent( 236 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 237 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 238 } 239 240 @Test getJobExceptionTest()241 public void getJobExceptionTest() throws Exception { 242 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 243 mockBatchService.addException(exception); 244 245 try { 246 JobName name = JobName.of("[PROJECT]", "[LOCATION]", "[JOB]"); 247 client.getJob(name); 248 Assert.fail("No exception raised"); 249 } catch (InvalidArgumentException e) { 250 // Expected exception. 251 } 252 } 253 254 @Test getJobTest2()255 public void getJobTest2() throws Exception { 256 Job expectedResponse = 257 Job.newBuilder() 258 .setName(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString()) 259 .setUid("uid115792") 260 .setPriority(-1165461084) 261 .addAllTaskGroups(new ArrayList<TaskGroup>()) 262 .setAllocationPolicy(AllocationPolicy.newBuilder().build()) 263 .putAllLabels(new HashMap<String, String>()) 264 .setStatus(JobStatus.newBuilder().build()) 265 .setCreateTime(Timestamp.newBuilder().build()) 266 .setUpdateTime(Timestamp.newBuilder().build()) 267 .setLogsPolicy(LogsPolicy.newBuilder().build()) 268 .addAllNotifications(new ArrayList<JobNotification>()) 269 .build(); 270 mockBatchService.addResponse(expectedResponse); 271 272 String name = "name3373707"; 273 274 Job actualResponse = client.getJob(name); 275 Assert.assertEquals(expectedResponse, actualResponse); 276 277 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 278 Assert.assertEquals(1, actualRequests.size()); 279 GetJobRequest actualRequest = ((GetJobRequest) actualRequests.get(0)); 280 281 Assert.assertEquals(name, actualRequest.getName()); 282 Assert.assertTrue( 283 channelProvider.isHeaderSent( 284 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 285 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 286 } 287 288 @Test getJobExceptionTest2()289 public void getJobExceptionTest2() throws Exception { 290 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 291 mockBatchService.addException(exception); 292 293 try { 294 String name = "name3373707"; 295 client.getJob(name); 296 Assert.fail("No exception raised"); 297 } catch (InvalidArgumentException e) { 298 // Expected exception. 299 } 300 } 301 302 @Test deleteJobTest()303 public void deleteJobTest() throws Exception { 304 Empty expectedResponse = Empty.newBuilder().build(); 305 Operation resultOperation = 306 Operation.newBuilder() 307 .setName("deleteJobTest") 308 .setDone(true) 309 .setResponse(Any.pack(expectedResponse)) 310 .build(); 311 mockBatchService.addResponse(resultOperation); 312 313 String name = "name3373707"; 314 315 client.deleteJobAsync(name).get(); 316 317 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 318 Assert.assertEquals(1, actualRequests.size()); 319 DeleteJobRequest actualRequest = ((DeleteJobRequest) actualRequests.get(0)); 320 321 Assert.assertEquals(name, actualRequest.getName()); 322 Assert.assertTrue( 323 channelProvider.isHeaderSent( 324 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 325 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 326 } 327 328 @Test deleteJobExceptionTest()329 public void deleteJobExceptionTest() throws Exception { 330 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 331 mockBatchService.addException(exception); 332 333 try { 334 String name = "name3373707"; 335 client.deleteJobAsync(name).get(); 336 Assert.fail("No exception raised"); 337 } catch (ExecutionException e) { 338 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 339 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 340 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 341 } 342 } 343 344 @Test listJobsTest()345 public void listJobsTest() throws Exception { 346 Job responsesElement = Job.newBuilder().build(); 347 ListJobsResponse expectedResponse = 348 ListJobsResponse.newBuilder() 349 .setNextPageToken("") 350 .addAllJobs(Arrays.asList(responsesElement)) 351 .build(); 352 mockBatchService.addResponse(expectedResponse); 353 354 String parent = "parent-995424086"; 355 356 ListJobsPagedResponse pagedListResponse = client.listJobs(parent); 357 358 List<Job> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 359 360 Assert.assertEquals(1, resources.size()); 361 Assert.assertEquals(expectedResponse.getJobsList().get(0), resources.get(0)); 362 363 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 364 Assert.assertEquals(1, actualRequests.size()); 365 ListJobsRequest actualRequest = ((ListJobsRequest) actualRequests.get(0)); 366 367 Assert.assertEquals(parent, actualRequest.getParent()); 368 Assert.assertTrue( 369 channelProvider.isHeaderSent( 370 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 371 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 372 } 373 374 @Test listJobsExceptionTest()375 public void listJobsExceptionTest() throws Exception { 376 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 377 mockBatchService.addException(exception); 378 379 try { 380 String parent = "parent-995424086"; 381 client.listJobs(parent); 382 Assert.fail("No exception raised"); 383 } catch (InvalidArgumentException e) { 384 // Expected exception. 385 } 386 } 387 388 @Test getTaskTest()389 public void getTaskTest() throws Exception { 390 Task expectedResponse = 391 Task.newBuilder() 392 .setName( 393 TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]", "[TASK]") 394 .toString()) 395 .setStatus(TaskStatus.newBuilder().build()) 396 .build(); 397 mockBatchService.addResponse(expectedResponse); 398 399 TaskName name = TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]", "[TASK]"); 400 401 Task actualResponse = client.getTask(name); 402 Assert.assertEquals(expectedResponse, actualResponse); 403 404 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 405 Assert.assertEquals(1, actualRequests.size()); 406 GetTaskRequest actualRequest = ((GetTaskRequest) actualRequests.get(0)); 407 408 Assert.assertEquals(name.toString(), actualRequest.getName()); 409 Assert.assertTrue( 410 channelProvider.isHeaderSent( 411 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 412 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 413 } 414 415 @Test getTaskExceptionTest()416 public void getTaskExceptionTest() throws Exception { 417 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 418 mockBatchService.addException(exception); 419 420 try { 421 TaskName name = TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]", "[TASK]"); 422 client.getTask(name); 423 Assert.fail("No exception raised"); 424 } catch (InvalidArgumentException e) { 425 // Expected exception. 426 } 427 } 428 429 @Test getTaskTest2()430 public void getTaskTest2() throws Exception { 431 Task expectedResponse = 432 Task.newBuilder() 433 .setName( 434 TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]", "[TASK]") 435 .toString()) 436 .setStatus(TaskStatus.newBuilder().build()) 437 .build(); 438 mockBatchService.addResponse(expectedResponse); 439 440 String name = "name3373707"; 441 442 Task actualResponse = client.getTask(name); 443 Assert.assertEquals(expectedResponse, actualResponse); 444 445 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 446 Assert.assertEquals(1, actualRequests.size()); 447 GetTaskRequest actualRequest = ((GetTaskRequest) actualRequests.get(0)); 448 449 Assert.assertEquals(name, actualRequest.getName()); 450 Assert.assertTrue( 451 channelProvider.isHeaderSent( 452 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 453 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 454 } 455 456 @Test getTaskExceptionTest2()457 public void getTaskExceptionTest2() throws Exception { 458 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 459 mockBatchService.addException(exception); 460 461 try { 462 String name = "name3373707"; 463 client.getTask(name); 464 Assert.fail("No exception raised"); 465 } catch (InvalidArgumentException e) { 466 // Expected exception. 467 } 468 } 469 470 @Test listTasksTest()471 public void listTasksTest() throws Exception { 472 Task responsesElement = Task.newBuilder().build(); 473 ListTasksResponse expectedResponse = 474 ListTasksResponse.newBuilder() 475 .setNextPageToken("") 476 .addAllTasks(Arrays.asList(responsesElement)) 477 .build(); 478 mockBatchService.addResponse(expectedResponse); 479 480 TaskGroupName parent = TaskGroupName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]"); 481 482 ListTasksPagedResponse pagedListResponse = client.listTasks(parent); 483 484 List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 485 486 Assert.assertEquals(1, resources.size()); 487 Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0)); 488 489 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 490 Assert.assertEquals(1, actualRequests.size()); 491 ListTasksRequest actualRequest = ((ListTasksRequest) actualRequests.get(0)); 492 493 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 494 Assert.assertTrue( 495 channelProvider.isHeaderSent( 496 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 497 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 498 } 499 500 @Test listTasksExceptionTest()501 public void listTasksExceptionTest() throws Exception { 502 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 503 mockBatchService.addException(exception); 504 505 try { 506 TaskGroupName parent = TaskGroupName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]"); 507 client.listTasks(parent); 508 Assert.fail("No exception raised"); 509 } catch (InvalidArgumentException e) { 510 // Expected exception. 511 } 512 } 513 514 @Test listTasksTest2()515 public void listTasksTest2() throws Exception { 516 Task responsesElement = Task.newBuilder().build(); 517 ListTasksResponse expectedResponse = 518 ListTasksResponse.newBuilder() 519 .setNextPageToken("") 520 .addAllTasks(Arrays.asList(responsesElement)) 521 .build(); 522 mockBatchService.addResponse(expectedResponse); 523 524 String parent = "parent-995424086"; 525 526 ListTasksPagedResponse pagedListResponse = client.listTasks(parent); 527 528 List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 529 530 Assert.assertEquals(1, resources.size()); 531 Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0)); 532 533 List<AbstractMessage> actualRequests = mockBatchService.getRequests(); 534 Assert.assertEquals(1, actualRequests.size()); 535 ListTasksRequest actualRequest = ((ListTasksRequest) actualRequests.get(0)); 536 537 Assert.assertEquals(parent, actualRequest.getParent()); 538 Assert.assertTrue( 539 channelProvider.isHeaderSent( 540 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 541 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 542 } 543 544 @Test listTasksExceptionTest2()545 public void listTasksExceptionTest2() throws Exception { 546 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 547 mockBatchService.addException(exception); 548 549 try { 550 String parent = "parent-995424086"; 551 client.listTasks(parent); 552 Assert.fail("No exception raised"); 553 } catch (InvalidArgumentException e) { 554 // Expected exception. 555 } 556 } 557 558 @Test listLocationsTest()559 public void listLocationsTest() throws Exception { 560 Location responsesElement = Location.newBuilder().build(); 561 ListLocationsResponse expectedResponse = 562 ListLocationsResponse.newBuilder() 563 .setNextPageToken("") 564 .addAllLocations(Arrays.asList(responsesElement)) 565 .build(); 566 mockLocations.addResponse(expectedResponse); 567 568 ListLocationsRequest request = 569 ListLocationsRequest.newBuilder() 570 .setName("name3373707") 571 .setFilter("filter-1274492040") 572 .setPageSize(883849137) 573 .setPageToken("pageToken873572522") 574 .build(); 575 576 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 577 578 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 579 580 Assert.assertEquals(1, resources.size()); 581 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 582 583 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 584 Assert.assertEquals(1, actualRequests.size()); 585 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 586 587 Assert.assertEquals(request.getName(), actualRequest.getName()); 588 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 589 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 590 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 591 Assert.assertTrue( 592 channelProvider.isHeaderSent( 593 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 594 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 595 } 596 597 @Test listLocationsExceptionTest()598 public void listLocationsExceptionTest() throws Exception { 599 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 600 mockLocations.addException(exception); 601 602 try { 603 ListLocationsRequest request = 604 ListLocationsRequest.newBuilder() 605 .setName("name3373707") 606 .setFilter("filter-1274492040") 607 .setPageSize(883849137) 608 .setPageToken("pageToken873572522") 609 .build(); 610 client.listLocations(request); 611 Assert.fail("No exception raised"); 612 } catch (InvalidArgumentException e) { 613 // Expected exception. 614 } 615 } 616 617 @Test getLocationTest()618 public void getLocationTest() throws Exception { 619 Location expectedResponse = 620 Location.newBuilder() 621 .setName("name3373707") 622 .setLocationId("locationId1541836720") 623 .setDisplayName("displayName1714148973") 624 .putAllLabels(new HashMap<String, String>()) 625 .setMetadata(Any.newBuilder().build()) 626 .build(); 627 mockLocations.addResponse(expectedResponse); 628 629 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 630 631 Location actualResponse = client.getLocation(request); 632 Assert.assertEquals(expectedResponse, actualResponse); 633 634 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 635 Assert.assertEquals(1, actualRequests.size()); 636 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 637 638 Assert.assertEquals(request.getName(), actualRequest.getName()); 639 Assert.assertTrue( 640 channelProvider.isHeaderSent( 641 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 642 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 643 } 644 645 @Test getLocationExceptionTest()646 public void getLocationExceptionTest() throws Exception { 647 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 648 mockLocations.addException(exception); 649 650 try { 651 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 652 client.getLocation(request); 653 Assert.fail("No exception raised"); 654 } catch (InvalidArgumentException e) { 655 // Expected exception. 656 } 657 } 658 } 659