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