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.dialogflow.cx.v3; 18 19 import static com.google.cloud.dialogflow.cx.v3.TestCasesClient.ListLocationsPagedResponse; 20 import static com.google.cloud.dialogflow.cx.v3.TestCasesClient.ListTestCaseResultsPagedResponse; 21 import static com.google.cloud.dialogflow.cx.v3.TestCasesClient.ListTestCasesPagedResponse; 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.FieldMask; 41 import com.google.protobuf.Timestamp; 42 import io.grpc.StatusRuntimeException; 43 import java.io.IOException; 44 import java.util.ArrayList; 45 import java.util.Arrays; 46 import java.util.HashMap; 47 import java.util.List; 48 import java.util.UUID; 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 TestCasesClientTest { 60 private static MockLocations mockLocations; 61 private static MockServiceHelper mockServiceHelper; 62 private static MockTestCases mockTestCases; 63 private LocalChannelProvider channelProvider; 64 private TestCasesClient client; 65 66 @BeforeClass startStaticServer()67 public static void startStaticServer() { 68 mockTestCases = new MockTestCases(); 69 mockLocations = new MockLocations(); 70 mockServiceHelper = 71 new MockServiceHelper( 72 UUID.randomUUID().toString(), 73 Arrays.<MockGrpcService>asList(mockTestCases, mockLocations)); 74 mockServiceHelper.start(); 75 } 76 77 @AfterClass stopServer()78 public static void stopServer() { 79 mockServiceHelper.stop(); 80 } 81 82 @Before setUp()83 public void setUp() throws IOException { 84 mockServiceHelper.reset(); 85 channelProvider = mockServiceHelper.createChannelProvider(); 86 TestCasesSettings settings = 87 TestCasesSettings.newBuilder() 88 .setTransportChannelProvider(channelProvider) 89 .setCredentialsProvider(NoCredentialsProvider.create()) 90 .build(); 91 client = TestCasesClient.create(settings); 92 } 93 94 @After tearDown()95 public void tearDown() throws Exception { 96 client.close(); 97 } 98 99 @Test listTestCasesTest()100 public void listTestCasesTest() throws Exception { 101 TestCase responsesElement = TestCase.newBuilder().build(); 102 ListTestCasesResponse expectedResponse = 103 ListTestCasesResponse.newBuilder() 104 .setNextPageToken("") 105 .addAllTestCases(Arrays.asList(responsesElement)) 106 .build(); 107 mockTestCases.addResponse(expectedResponse); 108 109 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 110 111 ListTestCasesPagedResponse pagedListResponse = client.listTestCases(parent); 112 113 List<TestCase> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 114 115 Assert.assertEquals(1, resources.size()); 116 Assert.assertEquals(expectedResponse.getTestCasesList().get(0), resources.get(0)); 117 118 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 119 Assert.assertEquals(1, actualRequests.size()); 120 ListTestCasesRequest actualRequest = ((ListTestCasesRequest) actualRequests.get(0)); 121 122 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 123 Assert.assertTrue( 124 channelProvider.isHeaderSent( 125 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 126 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 127 } 128 129 @Test listTestCasesExceptionTest()130 public void listTestCasesExceptionTest() throws Exception { 131 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 132 mockTestCases.addException(exception); 133 134 try { 135 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 136 client.listTestCases(parent); 137 Assert.fail("No exception raised"); 138 } catch (InvalidArgumentException e) { 139 // Expected exception. 140 } 141 } 142 143 @Test listTestCasesTest2()144 public void listTestCasesTest2() throws Exception { 145 TestCase responsesElement = TestCase.newBuilder().build(); 146 ListTestCasesResponse expectedResponse = 147 ListTestCasesResponse.newBuilder() 148 .setNextPageToken("") 149 .addAllTestCases(Arrays.asList(responsesElement)) 150 .build(); 151 mockTestCases.addResponse(expectedResponse); 152 153 String parent = "parent-995424086"; 154 155 ListTestCasesPagedResponse pagedListResponse = client.listTestCases(parent); 156 157 List<TestCase> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 158 159 Assert.assertEquals(1, resources.size()); 160 Assert.assertEquals(expectedResponse.getTestCasesList().get(0), resources.get(0)); 161 162 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 163 Assert.assertEquals(1, actualRequests.size()); 164 ListTestCasesRequest actualRequest = ((ListTestCasesRequest) actualRequests.get(0)); 165 166 Assert.assertEquals(parent, actualRequest.getParent()); 167 Assert.assertTrue( 168 channelProvider.isHeaderSent( 169 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 170 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 171 } 172 173 @Test listTestCasesExceptionTest2()174 public void listTestCasesExceptionTest2() throws Exception { 175 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 176 mockTestCases.addException(exception); 177 178 try { 179 String parent = "parent-995424086"; 180 client.listTestCases(parent); 181 Assert.fail("No exception raised"); 182 } catch (InvalidArgumentException e) { 183 // Expected exception. 184 } 185 } 186 187 @Test batchDeleteTestCasesTest()188 public void batchDeleteTestCasesTest() throws Exception { 189 Empty expectedResponse = Empty.newBuilder().build(); 190 mockTestCases.addResponse(expectedResponse); 191 192 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 193 194 client.batchDeleteTestCases(parent); 195 196 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 197 Assert.assertEquals(1, actualRequests.size()); 198 BatchDeleteTestCasesRequest actualRequest = 199 ((BatchDeleteTestCasesRequest) actualRequests.get(0)); 200 201 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 202 Assert.assertTrue( 203 channelProvider.isHeaderSent( 204 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 205 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 206 } 207 208 @Test batchDeleteTestCasesExceptionTest()209 public void batchDeleteTestCasesExceptionTest() throws Exception { 210 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 211 mockTestCases.addException(exception); 212 213 try { 214 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 215 client.batchDeleteTestCases(parent); 216 Assert.fail("No exception raised"); 217 } catch (InvalidArgumentException e) { 218 // Expected exception. 219 } 220 } 221 222 @Test batchDeleteTestCasesTest2()223 public void batchDeleteTestCasesTest2() throws Exception { 224 Empty expectedResponse = Empty.newBuilder().build(); 225 mockTestCases.addResponse(expectedResponse); 226 227 String parent = "parent-995424086"; 228 229 client.batchDeleteTestCases(parent); 230 231 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 232 Assert.assertEquals(1, actualRequests.size()); 233 BatchDeleteTestCasesRequest actualRequest = 234 ((BatchDeleteTestCasesRequest) actualRequests.get(0)); 235 236 Assert.assertEquals(parent, actualRequest.getParent()); 237 Assert.assertTrue( 238 channelProvider.isHeaderSent( 239 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 240 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 241 } 242 243 @Test batchDeleteTestCasesExceptionTest2()244 public void batchDeleteTestCasesExceptionTest2() throws Exception { 245 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 246 mockTestCases.addException(exception); 247 248 try { 249 String parent = "parent-995424086"; 250 client.batchDeleteTestCases(parent); 251 Assert.fail("No exception raised"); 252 } catch (InvalidArgumentException e) { 253 // Expected exception. 254 } 255 } 256 257 @Test getTestCaseTest()258 public void getTestCaseTest() throws Exception { 259 TestCase expectedResponse = 260 TestCase.newBuilder() 261 .setName( 262 TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]").toString()) 263 .addAllTags(new ArrayList<String>()) 264 .setDisplayName("displayName1714148973") 265 .setNotes("notes105008833") 266 .setTestConfig(TestConfig.newBuilder().build()) 267 .addAllTestCaseConversationTurns(new ArrayList<ConversationTurn>()) 268 .setCreationTime(Timestamp.newBuilder().build()) 269 .setLastTestResult(TestCaseResult.newBuilder().build()) 270 .build(); 271 mockTestCases.addResponse(expectedResponse); 272 273 TestCaseName name = TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]"); 274 275 TestCase actualResponse = client.getTestCase(name); 276 Assert.assertEquals(expectedResponse, actualResponse); 277 278 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 279 Assert.assertEquals(1, actualRequests.size()); 280 GetTestCaseRequest actualRequest = ((GetTestCaseRequest) actualRequests.get(0)); 281 282 Assert.assertEquals(name.toString(), actualRequest.getName()); 283 Assert.assertTrue( 284 channelProvider.isHeaderSent( 285 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 286 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 287 } 288 289 @Test getTestCaseExceptionTest()290 public void getTestCaseExceptionTest() throws Exception { 291 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 292 mockTestCases.addException(exception); 293 294 try { 295 TestCaseName name = TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]"); 296 client.getTestCase(name); 297 Assert.fail("No exception raised"); 298 } catch (InvalidArgumentException e) { 299 // Expected exception. 300 } 301 } 302 303 @Test getTestCaseTest2()304 public void getTestCaseTest2() throws Exception { 305 TestCase expectedResponse = 306 TestCase.newBuilder() 307 .setName( 308 TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]").toString()) 309 .addAllTags(new ArrayList<String>()) 310 .setDisplayName("displayName1714148973") 311 .setNotes("notes105008833") 312 .setTestConfig(TestConfig.newBuilder().build()) 313 .addAllTestCaseConversationTurns(new ArrayList<ConversationTurn>()) 314 .setCreationTime(Timestamp.newBuilder().build()) 315 .setLastTestResult(TestCaseResult.newBuilder().build()) 316 .build(); 317 mockTestCases.addResponse(expectedResponse); 318 319 String name = "name3373707"; 320 321 TestCase actualResponse = client.getTestCase(name); 322 Assert.assertEquals(expectedResponse, actualResponse); 323 324 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 325 Assert.assertEquals(1, actualRequests.size()); 326 GetTestCaseRequest actualRequest = ((GetTestCaseRequest) actualRequests.get(0)); 327 328 Assert.assertEquals(name, actualRequest.getName()); 329 Assert.assertTrue( 330 channelProvider.isHeaderSent( 331 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 332 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 333 } 334 335 @Test getTestCaseExceptionTest2()336 public void getTestCaseExceptionTest2() throws Exception { 337 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 338 mockTestCases.addException(exception); 339 340 try { 341 String name = "name3373707"; 342 client.getTestCase(name); 343 Assert.fail("No exception raised"); 344 } catch (InvalidArgumentException e) { 345 // Expected exception. 346 } 347 } 348 349 @Test createTestCaseTest()350 public void createTestCaseTest() throws Exception { 351 TestCase expectedResponse = 352 TestCase.newBuilder() 353 .setName( 354 TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]").toString()) 355 .addAllTags(new ArrayList<String>()) 356 .setDisplayName("displayName1714148973") 357 .setNotes("notes105008833") 358 .setTestConfig(TestConfig.newBuilder().build()) 359 .addAllTestCaseConversationTurns(new ArrayList<ConversationTurn>()) 360 .setCreationTime(Timestamp.newBuilder().build()) 361 .setLastTestResult(TestCaseResult.newBuilder().build()) 362 .build(); 363 mockTestCases.addResponse(expectedResponse); 364 365 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 366 TestCase testCase = TestCase.newBuilder().build(); 367 368 TestCase actualResponse = client.createTestCase(parent, testCase); 369 Assert.assertEquals(expectedResponse, actualResponse); 370 371 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 372 Assert.assertEquals(1, actualRequests.size()); 373 CreateTestCaseRequest actualRequest = ((CreateTestCaseRequest) actualRequests.get(0)); 374 375 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 376 Assert.assertEquals(testCase, actualRequest.getTestCase()); 377 Assert.assertTrue( 378 channelProvider.isHeaderSent( 379 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 380 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 381 } 382 383 @Test createTestCaseExceptionTest()384 public void createTestCaseExceptionTest() throws Exception { 385 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 386 mockTestCases.addException(exception); 387 388 try { 389 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 390 TestCase testCase = TestCase.newBuilder().build(); 391 client.createTestCase(parent, testCase); 392 Assert.fail("No exception raised"); 393 } catch (InvalidArgumentException e) { 394 // Expected exception. 395 } 396 } 397 398 @Test createTestCaseTest2()399 public void createTestCaseTest2() throws Exception { 400 TestCase expectedResponse = 401 TestCase.newBuilder() 402 .setName( 403 TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]").toString()) 404 .addAllTags(new ArrayList<String>()) 405 .setDisplayName("displayName1714148973") 406 .setNotes("notes105008833") 407 .setTestConfig(TestConfig.newBuilder().build()) 408 .addAllTestCaseConversationTurns(new ArrayList<ConversationTurn>()) 409 .setCreationTime(Timestamp.newBuilder().build()) 410 .setLastTestResult(TestCaseResult.newBuilder().build()) 411 .build(); 412 mockTestCases.addResponse(expectedResponse); 413 414 String parent = "parent-995424086"; 415 TestCase testCase = TestCase.newBuilder().build(); 416 417 TestCase actualResponse = client.createTestCase(parent, testCase); 418 Assert.assertEquals(expectedResponse, actualResponse); 419 420 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 421 Assert.assertEquals(1, actualRequests.size()); 422 CreateTestCaseRequest actualRequest = ((CreateTestCaseRequest) actualRequests.get(0)); 423 424 Assert.assertEquals(parent, actualRequest.getParent()); 425 Assert.assertEquals(testCase, actualRequest.getTestCase()); 426 Assert.assertTrue( 427 channelProvider.isHeaderSent( 428 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 429 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 430 } 431 432 @Test createTestCaseExceptionTest2()433 public void createTestCaseExceptionTest2() throws Exception { 434 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 435 mockTestCases.addException(exception); 436 437 try { 438 String parent = "parent-995424086"; 439 TestCase testCase = TestCase.newBuilder().build(); 440 client.createTestCase(parent, testCase); 441 Assert.fail("No exception raised"); 442 } catch (InvalidArgumentException e) { 443 // Expected exception. 444 } 445 } 446 447 @Test updateTestCaseTest()448 public void updateTestCaseTest() throws Exception { 449 TestCase expectedResponse = 450 TestCase.newBuilder() 451 .setName( 452 TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]").toString()) 453 .addAllTags(new ArrayList<String>()) 454 .setDisplayName("displayName1714148973") 455 .setNotes("notes105008833") 456 .setTestConfig(TestConfig.newBuilder().build()) 457 .addAllTestCaseConversationTurns(new ArrayList<ConversationTurn>()) 458 .setCreationTime(Timestamp.newBuilder().build()) 459 .setLastTestResult(TestCaseResult.newBuilder().build()) 460 .build(); 461 mockTestCases.addResponse(expectedResponse); 462 463 TestCase testCase = TestCase.newBuilder().build(); 464 FieldMask updateMask = FieldMask.newBuilder().build(); 465 466 TestCase actualResponse = client.updateTestCase(testCase, updateMask); 467 Assert.assertEquals(expectedResponse, actualResponse); 468 469 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 470 Assert.assertEquals(1, actualRequests.size()); 471 UpdateTestCaseRequest actualRequest = ((UpdateTestCaseRequest) actualRequests.get(0)); 472 473 Assert.assertEquals(testCase, actualRequest.getTestCase()); 474 Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); 475 Assert.assertTrue( 476 channelProvider.isHeaderSent( 477 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 478 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 479 } 480 481 @Test updateTestCaseExceptionTest()482 public void updateTestCaseExceptionTest() throws Exception { 483 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 484 mockTestCases.addException(exception); 485 486 try { 487 TestCase testCase = TestCase.newBuilder().build(); 488 FieldMask updateMask = FieldMask.newBuilder().build(); 489 client.updateTestCase(testCase, updateMask); 490 Assert.fail("No exception raised"); 491 } catch (InvalidArgumentException e) { 492 // Expected exception. 493 } 494 } 495 496 @Test runTestCaseTest()497 public void runTestCaseTest() throws Exception { 498 RunTestCaseResponse expectedResponse = 499 RunTestCaseResponse.newBuilder().setResult(TestCaseResult.newBuilder().build()).build(); 500 Operation resultOperation = 501 Operation.newBuilder() 502 .setName("runTestCaseTest") 503 .setDone(true) 504 .setResponse(Any.pack(expectedResponse)) 505 .build(); 506 mockTestCases.addResponse(resultOperation); 507 508 RunTestCaseRequest request = 509 RunTestCaseRequest.newBuilder() 510 .setName( 511 TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]").toString()) 512 .setEnvironment( 513 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 514 .toString()) 515 .build(); 516 517 RunTestCaseResponse actualResponse = client.runTestCaseAsync(request).get(); 518 Assert.assertEquals(expectedResponse, actualResponse); 519 520 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 521 Assert.assertEquals(1, actualRequests.size()); 522 RunTestCaseRequest actualRequest = ((RunTestCaseRequest) actualRequests.get(0)); 523 524 Assert.assertEquals(request.getName(), actualRequest.getName()); 525 Assert.assertEquals(request.getEnvironment(), actualRequest.getEnvironment()); 526 Assert.assertTrue( 527 channelProvider.isHeaderSent( 528 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 529 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 530 } 531 532 @Test runTestCaseExceptionTest()533 public void runTestCaseExceptionTest() throws Exception { 534 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 535 mockTestCases.addException(exception); 536 537 try { 538 RunTestCaseRequest request = 539 RunTestCaseRequest.newBuilder() 540 .setName( 541 TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]").toString()) 542 .setEnvironment( 543 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 544 .toString()) 545 .build(); 546 client.runTestCaseAsync(request).get(); 547 Assert.fail("No exception raised"); 548 } catch (ExecutionException e) { 549 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 550 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 551 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 552 } 553 } 554 555 @Test batchRunTestCasesTest()556 public void batchRunTestCasesTest() throws Exception { 557 BatchRunTestCasesResponse expectedResponse = 558 BatchRunTestCasesResponse.newBuilder() 559 .addAllResults(new ArrayList<TestCaseResult>()) 560 .build(); 561 Operation resultOperation = 562 Operation.newBuilder() 563 .setName("batchRunTestCasesTest") 564 .setDone(true) 565 .setResponse(Any.pack(expectedResponse)) 566 .build(); 567 mockTestCases.addResponse(resultOperation); 568 569 BatchRunTestCasesRequest request = 570 BatchRunTestCasesRequest.newBuilder() 571 .setParent(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 572 .setEnvironment( 573 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 574 .toString()) 575 .addAllTestCases(new ArrayList<String>()) 576 .build(); 577 578 BatchRunTestCasesResponse actualResponse = client.batchRunTestCasesAsync(request).get(); 579 Assert.assertEquals(expectedResponse, actualResponse); 580 581 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 582 Assert.assertEquals(1, actualRequests.size()); 583 BatchRunTestCasesRequest actualRequest = ((BatchRunTestCasesRequest) actualRequests.get(0)); 584 585 Assert.assertEquals(request.getParent(), actualRequest.getParent()); 586 Assert.assertEquals(request.getEnvironment(), actualRequest.getEnvironment()); 587 Assert.assertEquals(request.getTestCasesList(), actualRequest.getTestCasesList()); 588 Assert.assertTrue( 589 channelProvider.isHeaderSent( 590 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 591 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 592 } 593 594 @Test batchRunTestCasesExceptionTest()595 public void batchRunTestCasesExceptionTest() throws Exception { 596 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 597 mockTestCases.addException(exception); 598 599 try { 600 BatchRunTestCasesRequest request = 601 BatchRunTestCasesRequest.newBuilder() 602 .setParent(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 603 .setEnvironment( 604 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 605 .toString()) 606 .addAllTestCases(new ArrayList<String>()) 607 .build(); 608 client.batchRunTestCasesAsync(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 calculateCoverageTest()618 public void calculateCoverageTest() throws Exception { 619 CalculateCoverageResponse expectedResponse = 620 CalculateCoverageResponse.newBuilder() 621 .setAgent(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 622 .build(); 623 mockTestCases.addResponse(expectedResponse); 624 625 CalculateCoverageRequest request = 626 CalculateCoverageRequest.newBuilder() 627 .setAgent(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 628 .build(); 629 630 CalculateCoverageResponse actualResponse = client.calculateCoverage(request); 631 Assert.assertEquals(expectedResponse, actualResponse); 632 633 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 634 Assert.assertEquals(1, actualRequests.size()); 635 CalculateCoverageRequest actualRequest = ((CalculateCoverageRequest) actualRequests.get(0)); 636 637 Assert.assertEquals(request.getAgent(), actualRequest.getAgent()); 638 Assert.assertEquals(request.getType(), actualRequest.getType()); 639 Assert.assertTrue( 640 channelProvider.isHeaderSent( 641 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 642 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 643 } 644 645 @Test calculateCoverageExceptionTest()646 public void calculateCoverageExceptionTest() throws Exception { 647 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 648 mockTestCases.addException(exception); 649 650 try { 651 CalculateCoverageRequest request = 652 CalculateCoverageRequest.newBuilder() 653 .setAgent(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 654 .build(); 655 client.calculateCoverage(request); 656 Assert.fail("No exception raised"); 657 } catch (InvalidArgumentException e) { 658 // Expected exception. 659 } 660 } 661 662 @Test importTestCasesTest()663 public void importTestCasesTest() throws Exception { 664 ImportTestCasesResponse expectedResponse = 665 ImportTestCasesResponse.newBuilder().addAllNames(new ArrayList<String>()).build(); 666 Operation resultOperation = 667 Operation.newBuilder() 668 .setName("importTestCasesTest") 669 .setDone(true) 670 .setResponse(Any.pack(expectedResponse)) 671 .build(); 672 mockTestCases.addResponse(resultOperation); 673 674 ImportTestCasesRequest request = 675 ImportTestCasesRequest.newBuilder() 676 .setParent(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 677 .build(); 678 679 ImportTestCasesResponse actualResponse = client.importTestCasesAsync(request).get(); 680 Assert.assertEquals(expectedResponse, actualResponse); 681 682 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 683 Assert.assertEquals(1, actualRequests.size()); 684 ImportTestCasesRequest actualRequest = ((ImportTestCasesRequest) actualRequests.get(0)); 685 686 Assert.assertEquals(request.getParent(), actualRequest.getParent()); 687 Assert.assertEquals(request.getGcsUri(), actualRequest.getGcsUri()); 688 Assert.assertEquals(request.getContent(), actualRequest.getContent()); 689 Assert.assertTrue( 690 channelProvider.isHeaderSent( 691 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 692 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 693 } 694 695 @Test importTestCasesExceptionTest()696 public void importTestCasesExceptionTest() throws Exception { 697 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 698 mockTestCases.addException(exception); 699 700 try { 701 ImportTestCasesRequest request = 702 ImportTestCasesRequest.newBuilder() 703 .setParent(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 704 .build(); 705 client.importTestCasesAsync(request).get(); 706 Assert.fail("No exception raised"); 707 } catch (ExecutionException e) { 708 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 709 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 710 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 711 } 712 } 713 714 @Test exportTestCasesTest()715 public void exportTestCasesTest() throws Exception { 716 ExportTestCasesResponse expectedResponse = ExportTestCasesResponse.newBuilder().build(); 717 Operation resultOperation = 718 Operation.newBuilder() 719 .setName("exportTestCasesTest") 720 .setDone(true) 721 .setResponse(Any.pack(expectedResponse)) 722 .build(); 723 mockTestCases.addResponse(resultOperation); 724 725 ExportTestCasesRequest request = 726 ExportTestCasesRequest.newBuilder() 727 .setParent(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 728 .setFilter("filter-1274492040") 729 .build(); 730 731 ExportTestCasesResponse actualResponse = client.exportTestCasesAsync(request).get(); 732 Assert.assertEquals(expectedResponse, actualResponse); 733 734 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 735 Assert.assertEquals(1, actualRequests.size()); 736 ExportTestCasesRequest actualRequest = ((ExportTestCasesRequest) actualRequests.get(0)); 737 738 Assert.assertEquals(request.getParent(), actualRequest.getParent()); 739 Assert.assertEquals(request.getGcsUri(), actualRequest.getGcsUri()); 740 Assert.assertEquals(request.getDataFormat(), actualRequest.getDataFormat()); 741 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 742 Assert.assertTrue( 743 channelProvider.isHeaderSent( 744 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 745 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 746 } 747 748 @Test exportTestCasesExceptionTest()749 public void exportTestCasesExceptionTest() throws Exception { 750 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 751 mockTestCases.addException(exception); 752 753 try { 754 ExportTestCasesRequest request = 755 ExportTestCasesRequest.newBuilder() 756 .setParent(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 757 .setFilter("filter-1274492040") 758 .build(); 759 client.exportTestCasesAsync(request).get(); 760 Assert.fail("No exception raised"); 761 } catch (ExecutionException e) { 762 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 763 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 764 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 765 } 766 } 767 768 @Test listTestCaseResultsTest()769 public void listTestCaseResultsTest() throws Exception { 770 TestCaseResult responsesElement = TestCaseResult.newBuilder().build(); 771 ListTestCaseResultsResponse expectedResponse = 772 ListTestCaseResultsResponse.newBuilder() 773 .setNextPageToken("") 774 .addAllTestCaseResults(Arrays.asList(responsesElement)) 775 .build(); 776 mockTestCases.addResponse(expectedResponse); 777 778 TestCaseName parent = TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]"); 779 780 ListTestCaseResultsPagedResponse pagedListResponse = client.listTestCaseResults(parent); 781 782 List<TestCaseResult> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 783 784 Assert.assertEquals(1, resources.size()); 785 Assert.assertEquals(expectedResponse.getTestCaseResultsList().get(0), resources.get(0)); 786 787 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 788 Assert.assertEquals(1, actualRequests.size()); 789 ListTestCaseResultsRequest actualRequest = ((ListTestCaseResultsRequest) actualRequests.get(0)); 790 791 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 792 Assert.assertTrue( 793 channelProvider.isHeaderSent( 794 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 795 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 796 } 797 798 @Test listTestCaseResultsExceptionTest()799 public void listTestCaseResultsExceptionTest() throws Exception { 800 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 801 mockTestCases.addException(exception); 802 803 try { 804 TestCaseName parent = TestCaseName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]"); 805 client.listTestCaseResults(parent); 806 Assert.fail("No exception raised"); 807 } catch (InvalidArgumentException e) { 808 // Expected exception. 809 } 810 } 811 812 @Test listTestCaseResultsTest2()813 public void listTestCaseResultsTest2() throws Exception { 814 TestCaseResult responsesElement = TestCaseResult.newBuilder().build(); 815 ListTestCaseResultsResponse expectedResponse = 816 ListTestCaseResultsResponse.newBuilder() 817 .setNextPageToken("") 818 .addAllTestCaseResults(Arrays.asList(responsesElement)) 819 .build(); 820 mockTestCases.addResponse(expectedResponse); 821 822 String parent = "parent-995424086"; 823 824 ListTestCaseResultsPagedResponse pagedListResponse = client.listTestCaseResults(parent); 825 826 List<TestCaseResult> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 827 828 Assert.assertEquals(1, resources.size()); 829 Assert.assertEquals(expectedResponse.getTestCaseResultsList().get(0), resources.get(0)); 830 831 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 832 Assert.assertEquals(1, actualRequests.size()); 833 ListTestCaseResultsRequest actualRequest = ((ListTestCaseResultsRequest) actualRequests.get(0)); 834 835 Assert.assertEquals(parent, actualRequest.getParent()); 836 Assert.assertTrue( 837 channelProvider.isHeaderSent( 838 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 839 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 840 } 841 842 @Test listTestCaseResultsExceptionTest2()843 public void listTestCaseResultsExceptionTest2() throws Exception { 844 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 845 mockTestCases.addException(exception); 846 847 try { 848 String parent = "parent-995424086"; 849 client.listTestCaseResults(parent); 850 Assert.fail("No exception raised"); 851 } catch (InvalidArgumentException e) { 852 // Expected exception. 853 } 854 } 855 856 @Test getTestCaseResultTest()857 public void getTestCaseResultTest() throws Exception { 858 TestCaseResult expectedResponse = 859 TestCaseResult.newBuilder() 860 .setName( 861 TestCaseResultName.of( 862 "[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]", "[RESULT]") 863 .toString()) 864 .setEnvironment( 865 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 866 .toString()) 867 .addAllConversationTurns(new ArrayList<ConversationTurn>()) 868 .setTestResult(TestResult.forNumber(0)) 869 .setTestTime(Timestamp.newBuilder().build()) 870 .build(); 871 mockTestCases.addResponse(expectedResponse); 872 873 TestCaseResultName name = 874 TestCaseResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]", "[RESULT]"); 875 876 TestCaseResult actualResponse = client.getTestCaseResult(name); 877 Assert.assertEquals(expectedResponse, actualResponse); 878 879 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 880 Assert.assertEquals(1, actualRequests.size()); 881 GetTestCaseResultRequest actualRequest = ((GetTestCaseResultRequest) actualRequests.get(0)); 882 883 Assert.assertEquals(name.toString(), actualRequest.getName()); 884 Assert.assertTrue( 885 channelProvider.isHeaderSent( 886 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 887 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 888 } 889 890 @Test getTestCaseResultExceptionTest()891 public void getTestCaseResultExceptionTest() throws Exception { 892 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 893 mockTestCases.addException(exception); 894 895 try { 896 TestCaseResultName name = 897 TestCaseResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]", "[RESULT]"); 898 client.getTestCaseResult(name); 899 Assert.fail("No exception raised"); 900 } catch (InvalidArgumentException e) { 901 // Expected exception. 902 } 903 } 904 905 @Test getTestCaseResultTest2()906 public void getTestCaseResultTest2() throws Exception { 907 TestCaseResult expectedResponse = 908 TestCaseResult.newBuilder() 909 .setName( 910 TestCaseResultName.of( 911 "[PROJECT]", "[LOCATION]", "[AGENT]", "[TEST_CASE]", "[RESULT]") 912 .toString()) 913 .setEnvironment( 914 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 915 .toString()) 916 .addAllConversationTurns(new ArrayList<ConversationTurn>()) 917 .setTestResult(TestResult.forNumber(0)) 918 .setTestTime(Timestamp.newBuilder().build()) 919 .build(); 920 mockTestCases.addResponse(expectedResponse); 921 922 String name = "name3373707"; 923 924 TestCaseResult actualResponse = client.getTestCaseResult(name); 925 Assert.assertEquals(expectedResponse, actualResponse); 926 927 List<AbstractMessage> actualRequests = mockTestCases.getRequests(); 928 Assert.assertEquals(1, actualRequests.size()); 929 GetTestCaseResultRequest actualRequest = ((GetTestCaseResultRequest) actualRequests.get(0)); 930 931 Assert.assertEquals(name, actualRequest.getName()); 932 Assert.assertTrue( 933 channelProvider.isHeaderSent( 934 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 935 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 936 } 937 938 @Test getTestCaseResultExceptionTest2()939 public void getTestCaseResultExceptionTest2() throws Exception { 940 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 941 mockTestCases.addException(exception); 942 943 try { 944 String name = "name3373707"; 945 client.getTestCaseResult(name); 946 Assert.fail("No exception raised"); 947 } catch (InvalidArgumentException e) { 948 // Expected exception. 949 } 950 } 951 952 @Test listLocationsTest()953 public void listLocationsTest() throws Exception { 954 Location responsesElement = Location.newBuilder().build(); 955 ListLocationsResponse expectedResponse = 956 ListLocationsResponse.newBuilder() 957 .setNextPageToken("") 958 .addAllLocations(Arrays.asList(responsesElement)) 959 .build(); 960 mockLocations.addResponse(expectedResponse); 961 962 ListLocationsRequest request = 963 ListLocationsRequest.newBuilder() 964 .setName("name3373707") 965 .setFilter("filter-1274492040") 966 .setPageSize(883849137) 967 .setPageToken("pageToken873572522") 968 .build(); 969 970 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 971 972 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 973 974 Assert.assertEquals(1, resources.size()); 975 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 976 977 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 978 Assert.assertEquals(1, actualRequests.size()); 979 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 980 981 Assert.assertEquals(request.getName(), actualRequest.getName()); 982 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 983 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 984 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 985 Assert.assertTrue( 986 channelProvider.isHeaderSent( 987 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 988 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 989 } 990 991 @Test listLocationsExceptionTest()992 public void listLocationsExceptionTest() throws Exception { 993 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 994 mockLocations.addException(exception); 995 996 try { 997 ListLocationsRequest request = 998 ListLocationsRequest.newBuilder() 999 .setName("name3373707") 1000 .setFilter("filter-1274492040") 1001 .setPageSize(883849137) 1002 .setPageToken("pageToken873572522") 1003 .build(); 1004 client.listLocations(request); 1005 Assert.fail("No exception raised"); 1006 } catch (InvalidArgumentException e) { 1007 // Expected exception. 1008 } 1009 } 1010 1011 @Test getLocationTest()1012 public void getLocationTest() throws Exception { 1013 Location expectedResponse = 1014 Location.newBuilder() 1015 .setName("name3373707") 1016 .setLocationId("locationId1541836720") 1017 .setDisplayName("displayName1714148973") 1018 .putAllLabels(new HashMap<String, String>()) 1019 .setMetadata(Any.newBuilder().build()) 1020 .build(); 1021 mockLocations.addResponse(expectedResponse); 1022 1023 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 1024 1025 Location actualResponse = client.getLocation(request); 1026 Assert.assertEquals(expectedResponse, actualResponse); 1027 1028 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 1029 Assert.assertEquals(1, actualRequests.size()); 1030 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 1031 1032 Assert.assertEquals(request.getName(), actualRequest.getName()); 1033 Assert.assertTrue( 1034 channelProvider.isHeaderSent( 1035 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 1036 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 1037 } 1038 1039 @Test getLocationExceptionTest()1040 public void getLocationExceptionTest() throws Exception { 1041 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 1042 mockLocations.addException(exception); 1043 1044 try { 1045 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 1046 client.getLocation(request); 1047 Assert.fail("No exception raised"); 1048 } catch (InvalidArgumentException e) { 1049 // Expected exception. 1050 } 1051 } 1052 } 1053