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