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.EnvironmentsClient.ListContinuousTestResultsPagedResponse; 20 import static com.google.cloud.dialogflow.cx.v3.EnvironmentsClient.ListEnvironmentsPagedResponse; 21 import static com.google.cloud.dialogflow.cx.v3.EnvironmentsClient.ListLocationsPagedResponse; 22 import static com.google.cloud.dialogflow.cx.v3.EnvironmentsClient.LookupEnvironmentHistoryPagedResponse; 23 24 import com.google.api.gax.core.NoCredentialsProvider; 25 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 26 import com.google.api.gax.httpjson.testing.MockHttpService; 27 import com.google.api.gax.rpc.ApiClientHeaderProvider; 28 import com.google.api.gax.rpc.ApiException; 29 import com.google.api.gax.rpc.ApiExceptionFactory; 30 import com.google.api.gax.rpc.InvalidArgumentException; 31 import com.google.api.gax.rpc.StatusCode; 32 import com.google.api.gax.rpc.testing.FakeStatusCode; 33 import com.google.cloud.dialogflow.cx.v3.stub.HttpJsonEnvironmentsStub; 34 import com.google.cloud.location.GetLocationRequest; 35 import com.google.cloud.location.ListLocationsRequest; 36 import com.google.cloud.location.ListLocationsResponse; 37 import com.google.cloud.location.Location; 38 import com.google.common.collect.Lists; 39 import com.google.longrunning.Operation; 40 import com.google.protobuf.Any; 41 import com.google.protobuf.Empty; 42 import com.google.protobuf.FieldMask; 43 import com.google.protobuf.Timestamp; 44 import java.io.IOException; 45 import java.util.ArrayList; 46 import java.util.Arrays; 47 import java.util.HashMap; 48 import java.util.List; 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 EnvironmentsClientHttpJsonTest { 60 private static MockHttpService mockService; 61 private static EnvironmentsClient client; 62 63 @BeforeClass startStaticServer()64 public static void startStaticServer() throws IOException { 65 mockService = 66 new MockHttpService( 67 HttpJsonEnvironmentsStub.getMethodDescriptors(), 68 EnvironmentsSettings.getDefaultEndpoint()); 69 EnvironmentsSettings settings = 70 EnvironmentsSettings.newHttpJsonBuilder() 71 .setTransportChannelProvider( 72 EnvironmentsSettings.defaultHttpJsonTransportProviderBuilder() 73 .setHttpTransport(mockService) 74 .build()) 75 .setCredentialsProvider(NoCredentialsProvider.create()) 76 .build(); 77 client = EnvironmentsClient.create(settings); 78 } 79 80 @AfterClass stopServer()81 public static void stopServer() { 82 client.close(); 83 } 84 85 @Before setUp()86 public void setUp() {} 87 88 @After tearDown()89 public void tearDown() throws Exception { 90 mockService.reset(); 91 } 92 93 @Test listEnvironmentsTest()94 public void listEnvironmentsTest() throws Exception { 95 Environment responsesElement = Environment.newBuilder().build(); 96 ListEnvironmentsResponse expectedResponse = 97 ListEnvironmentsResponse.newBuilder() 98 .setNextPageToken("") 99 .addAllEnvironments(Arrays.asList(responsesElement)) 100 .build(); 101 mockService.addResponse(expectedResponse); 102 103 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 104 105 ListEnvironmentsPagedResponse pagedListResponse = client.listEnvironments(parent); 106 107 List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 108 109 Assert.assertEquals(1, resources.size()); 110 Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0)); 111 112 List<String> actualRequests = mockService.getRequestPaths(); 113 Assert.assertEquals(1, actualRequests.size()); 114 115 String apiClientHeaderKey = 116 mockService 117 .getRequestHeaders() 118 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 119 .iterator() 120 .next(); 121 Assert.assertTrue( 122 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 123 .matcher(apiClientHeaderKey) 124 .matches()); 125 } 126 127 @Test listEnvironmentsExceptionTest()128 public void listEnvironmentsExceptionTest() throws Exception { 129 ApiException exception = 130 ApiExceptionFactory.createException( 131 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 132 mockService.addException(exception); 133 134 try { 135 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 136 client.listEnvironments(parent); 137 Assert.fail("No exception raised"); 138 } catch (InvalidArgumentException e) { 139 // Expected exception. 140 } 141 } 142 143 @Test listEnvironmentsTest2()144 public void listEnvironmentsTest2() throws Exception { 145 Environment responsesElement = Environment.newBuilder().build(); 146 ListEnvironmentsResponse expectedResponse = 147 ListEnvironmentsResponse.newBuilder() 148 .setNextPageToken("") 149 .addAllEnvironments(Arrays.asList(responsesElement)) 150 .build(); 151 mockService.addResponse(expectedResponse); 152 153 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 154 155 ListEnvironmentsPagedResponse pagedListResponse = client.listEnvironments(parent); 156 157 List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 158 159 Assert.assertEquals(1, resources.size()); 160 Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0)); 161 162 List<String> actualRequests = mockService.getRequestPaths(); 163 Assert.assertEquals(1, actualRequests.size()); 164 165 String apiClientHeaderKey = 166 mockService 167 .getRequestHeaders() 168 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 169 .iterator() 170 .next(); 171 Assert.assertTrue( 172 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 173 .matcher(apiClientHeaderKey) 174 .matches()); 175 } 176 177 @Test listEnvironmentsExceptionTest2()178 public void listEnvironmentsExceptionTest2() throws Exception { 179 ApiException exception = 180 ApiExceptionFactory.createException( 181 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 182 mockService.addException(exception); 183 184 try { 185 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 186 client.listEnvironments(parent); 187 Assert.fail("No exception raised"); 188 } catch (InvalidArgumentException e) { 189 // Expected exception. 190 } 191 } 192 193 @Test getEnvironmentTest()194 public void getEnvironmentTest() throws Exception { 195 Environment expectedResponse = 196 Environment.newBuilder() 197 .setName( 198 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 199 .toString()) 200 .setDisplayName("displayName1714148973") 201 .setDescription("description-1724546052") 202 .addAllVersionConfigs(new ArrayList<Environment.VersionConfig>()) 203 .setUpdateTime(Timestamp.newBuilder().build()) 204 .setTestCasesConfig(Environment.TestCasesConfig.newBuilder().build()) 205 .setWebhookConfig(Environment.WebhookConfig.newBuilder().build()) 206 .build(); 207 mockService.addResponse(expectedResponse); 208 209 EnvironmentName name = 210 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 211 212 Environment actualResponse = client.getEnvironment(name); 213 Assert.assertEquals(expectedResponse, actualResponse); 214 215 List<String> actualRequests = mockService.getRequestPaths(); 216 Assert.assertEquals(1, actualRequests.size()); 217 218 String apiClientHeaderKey = 219 mockService 220 .getRequestHeaders() 221 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 222 .iterator() 223 .next(); 224 Assert.assertTrue( 225 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 226 .matcher(apiClientHeaderKey) 227 .matches()); 228 } 229 230 @Test getEnvironmentExceptionTest()231 public void getEnvironmentExceptionTest() throws Exception { 232 ApiException exception = 233 ApiExceptionFactory.createException( 234 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 235 mockService.addException(exception); 236 237 try { 238 EnvironmentName name = 239 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 240 client.getEnvironment(name); 241 Assert.fail("No exception raised"); 242 } catch (InvalidArgumentException e) { 243 // Expected exception. 244 } 245 } 246 247 @Test getEnvironmentTest2()248 public void getEnvironmentTest2() throws Exception { 249 Environment expectedResponse = 250 Environment.newBuilder() 251 .setName( 252 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 253 .toString()) 254 .setDisplayName("displayName1714148973") 255 .setDescription("description-1724546052") 256 .addAllVersionConfigs(new ArrayList<Environment.VersionConfig>()) 257 .setUpdateTime(Timestamp.newBuilder().build()) 258 .setTestCasesConfig(Environment.TestCasesConfig.newBuilder().build()) 259 .setWebhookConfig(Environment.WebhookConfig.newBuilder().build()) 260 .build(); 261 mockService.addResponse(expectedResponse); 262 263 String name = 264 "projects/project-8900/locations/location-8900/agents/agent-8900/environments/environment-8900"; 265 266 Environment actualResponse = client.getEnvironment(name); 267 Assert.assertEquals(expectedResponse, actualResponse); 268 269 List<String> actualRequests = mockService.getRequestPaths(); 270 Assert.assertEquals(1, actualRequests.size()); 271 272 String apiClientHeaderKey = 273 mockService 274 .getRequestHeaders() 275 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 276 .iterator() 277 .next(); 278 Assert.assertTrue( 279 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 280 .matcher(apiClientHeaderKey) 281 .matches()); 282 } 283 284 @Test getEnvironmentExceptionTest2()285 public void getEnvironmentExceptionTest2() throws Exception { 286 ApiException exception = 287 ApiExceptionFactory.createException( 288 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 289 mockService.addException(exception); 290 291 try { 292 String name = 293 "projects/project-8900/locations/location-8900/agents/agent-8900/environments/environment-8900"; 294 client.getEnvironment(name); 295 Assert.fail("No exception raised"); 296 } catch (InvalidArgumentException e) { 297 // Expected exception. 298 } 299 } 300 301 @Test createEnvironmentTest()302 public void createEnvironmentTest() throws Exception { 303 Environment expectedResponse = 304 Environment.newBuilder() 305 .setName( 306 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 307 .toString()) 308 .setDisplayName("displayName1714148973") 309 .setDescription("description-1724546052") 310 .addAllVersionConfigs(new ArrayList<Environment.VersionConfig>()) 311 .setUpdateTime(Timestamp.newBuilder().build()) 312 .setTestCasesConfig(Environment.TestCasesConfig.newBuilder().build()) 313 .setWebhookConfig(Environment.WebhookConfig.newBuilder().build()) 314 .build(); 315 Operation resultOperation = 316 Operation.newBuilder() 317 .setName("createEnvironmentTest") 318 .setDone(true) 319 .setResponse(Any.pack(expectedResponse)) 320 .build(); 321 mockService.addResponse(resultOperation); 322 323 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 324 Environment environment = Environment.newBuilder().build(); 325 326 Environment actualResponse = client.createEnvironmentAsync(parent, environment).get(); 327 Assert.assertEquals(expectedResponse, actualResponse); 328 329 List<String> actualRequests = mockService.getRequestPaths(); 330 Assert.assertEquals(1, actualRequests.size()); 331 332 String apiClientHeaderKey = 333 mockService 334 .getRequestHeaders() 335 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 336 .iterator() 337 .next(); 338 Assert.assertTrue( 339 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 340 .matcher(apiClientHeaderKey) 341 .matches()); 342 } 343 344 @Test createEnvironmentExceptionTest()345 public void createEnvironmentExceptionTest() throws Exception { 346 ApiException exception = 347 ApiExceptionFactory.createException( 348 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 349 mockService.addException(exception); 350 351 try { 352 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 353 Environment environment = Environment.newBuilder().build(); 354 client.createEnvironmentAsync(parent, environment).get(); 355 Assert.fail("No exception raised"); 356 } catch (ExecutionException e) { 357 } 358 } 359 360 @Test createEnvironmentTest2()361 public void createEnvironmentTest2() throws Exception { 362 Environment expectedResponse = 363 Environment.newBuilder() 364 .setName( 365 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 366 .toString()) 367 .setDisplayName("displayName1714148973") 368 .setDescription("description-1724546052") 369 .addAllVersionConfigs(new ArrayList<Environment.VersionConfig>()) 370 .setUpdateTime(Timestamp.newBuilder().build()) 371 .setTestCasesConfig(Environment.TestCasesConfig.newBuilder().build()) 372 .setWebhookConfig(Environment.WebhookConfig.newBuilder().build()) 373 .build(); 374 Operation resultOperation = 375 Operation.newBuilder() 376 .setName("createEnvironmentTest") 377 .setDone(true) 378 .setResponse(Any.pack(expectedResponse)) 379 .build(); 380 mockService.addResponse(resultOperation); 381 382 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 383 Environment environment = Environment.newBuilder().build(); 384 385 Environment actualResponse = client.createEnvironmentAsync(parent, environment).get(); 386 Assert.assertEquals(expectedResponse, actualResponse); 387 388 List<String> actualRequests = mockService.getRequestPaths(); 389 Assert.assertEquals(1, actualRequests.size()); 390 391 String apiClientHeaderKey = 392 mockService 393 .getRequestHeaders() 394 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 395 .iterator() 396 .next(); 397 Assert.assertTrue( 398 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 399 .matcher(apiClientHeaderKey) 400 .matches()); 401 } 402 403 @Test createEnvironmentExceptionTest2()404 public void createEnvironmentExceptionTest2() throws Exception { 405 ApiException exception = 406 ApiExceptionFactory.createException( 407 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 408 mockService.addException(exception); 409 410 try { 411 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 412 Environment environment = Environment.newBuilder().build(); 413 client.createEnvironmentAsync(parent, environment).get(); 414 Assert.fail("No exception raised"); 415 } catch (ExecutionException e) { 416 } 417 } 418 419 @Test updateEnvironmentTest()420 public void updateEnvironmentTest() throws Exception { 421 Environment expectedResponse = 422 Environment.newBuilder() 423 .setName( 424 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 425 .toString()) 426 .setDisplayName("displayName1714148973") 427 .setDescription("description-1724546052") 428 .addAllVersionConfigs(new ArrayList<Environment.VersionConfig>()) 429 .setUpdateTime(Timestamp.newBuilder().build()) 430 .setTestCasesConfig(Environment.TestCasesConfig.newBuilder().build()) 431 .setWebhookConfig(Environment.WebhookConfig.newBuilder().build()) 432 .build(); 433 Operation resultOperation = 434 Operation.newBuilder() 435 .setName("updateEnvironmentTest") 436 .setDone(true) 437 .setResponse(Any.pack(expectedResponse)) 438 .build(); 439 mockService.addResponse(resultOperation); 440 441 Environment environment = 442 Environment.newBuilder() 443 .setName( 444 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 445 .toString()) 446 .setDisplayName("displayName1714148973") 447 .setDescription("description-1724546052") 448 .addAllVersionConfigs(new ArrayList<Environment.VersionConfig>()) 449 .setUpdateTime(Timestamp.newBuilder().build()) 450 .setTestCasesConfig(Environment.TestCasesConfig.newBuilder().build()) 451 .setWebhookConfig(Environment.WebhookConfig.newBuilder().build()) 452 .build(); 453 FieldMask updateMask = FieldMask.newBuilder().build(); 454 455 Environment actualResponse = client.updateEnvironmentAsync(environment, updateMask).get(); 456 Assert.assertEquals(expectedResponse, actualResponse); 457 458 List<String> actualRequests = mockService.getRequestPaths(); 459 Assert.assertEquals(1, actualRequests.size()); 460 461 String apiClientHeaderKey = 462 mockService 463 .getRequestHeaders() 464 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 465 .iterator() 466 .next(); 467 Assert.assertTrue( 468 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 469 .matcher(apiClientHeaderKey) 470 .matches()); 471 } 472 473 @Test updateEnvironmentExceptionTest()474 public void updateEnvironmentExceptionTest() throws Exception { 475 ApiException exception = 476 ApiExceptionFactory.createException( 477 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 478 mockService.addException(exception); 479 480 try { 481 Environment environment = 482 Environment.newBuilder() 483 .setName( 484 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 485 .toString()) 486 .setDisplayName("displayName1714148973") 487 .setDescription("description-1724546052") 488 .addAllVersionConfigs(new ArrayList<Environment.VersionConfig>()) 489 .setUpdateTime(Timestamp.newBuilder().build()) 490 .setTestCasesConfig(Environment.TestCasesConfig.newBuilder().build()) 491 .setWebhookConfig(Environment.WebhookConfig.newBuilder().build()) 492 .build(); 493 FieldMask updateMask = FieldMask.newBuilder().build(); 494 client.updateEnvironmentAsync(environment, updateMask).get(); 495 Assert.fail("No exception raised"); 496 } catch (ExecutionException e) { 497 } 498 } 499 500 @Test deleteEnvironmentTest()501 public void deleteEnvironmentTest() throws Exception { 502 Empty expectedResponse = Empty.newBuilder().build(); 503 mockService.addResponse(expectedResponse); 504 505 EnvironmentName name = 506 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 507 508 client.deleteEnvironment(name); 509 510 List<String> actualRequests = mockService.getRequestPaths(); 511 Assert.assertEquals(1, actualRequests.size()); 512 513 String apiClientHeaderKey = 514 mockService 515 .getRequestHeaders() 516 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 517 .iterator() 518 .next(); 519 Assert.assertTrue( 520 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 521 .matcher(apiClientHeaderKey) 522 .matches()); 523 } 524 525 @Test deleteEnvironmentExceptionTest()526 public void deleteEnvironmentExceptionTest() throws Exception { 527 ApiException exception = 528 ApiExceptionFactory.createException( 529 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 530 mockService.addException(exception); 531 532 try { 533 EnvironmentName name = 534 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 535 client.deleteEnvironment(name); 536 Assert.fail("No exception raised"); 537 } catch (InvalidArgumentException e) { 538 // Expected exception. 539 } 540 } 541 542 @Test deleteEnvironmentTest2()543 public void deleteEnvironmentTest2() throws Exception { 544 Empty expectedResponse = Empty.newBuilder().build(); 545 mockService.addResponse(expectedResponse); 546 547 String name = 548 "projects/project-8900/locations/location-8900/agents/agent-8900/environments/environment-8900"; 549 550 client.deleteEnvironment(name); 551 552 List<String> actualRequests = mockService.getRequestPaths(); 553 Assert.assertEquals(1, actualRequests.size()); 554 555 String apiClientHeaderKey = 556 mockService 557 .getRequestHeaders() 558 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 559 .iterator() 560 .next(); 561 Assert.assertTrue( 562 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 563 .matcher(apiClientHeaderKey) 564 .matches()); 565 } 566 567 @Test deleteEnvironmentExceptionTest2()568 public void deleteEnvironmentExceptionTest2() throws Exception { 569 ApiException exception = 570 ApiExceptionFactory.createException( 571 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 572 mockService.addException(exception); 573 574 try { 575 String name = 576 "projects/project-8900/locations/location-8900/agents/agent-8900/environments/environment-8900"; 577 client.deleteEnvironment(name); 578 Assert.fail("No exception raised"); 579 } catch (InvalidArgumentException e) { 580 // Expected exception. 581 } 582 } 583 584 @Test lookupEnvironmentHistoryTest()585 public void lookupEnvironmentHistoryTest() throws Exception { 586 Environment responsesElement = Environment.newBuilder().build(); 587 LookupEnvironmentHistoryResponse expectedResponse = 588 LookupEnvironmentHistoryResponse.newBuilder() 589 .setNextPageToken("") 590 .addAllEnvironments(Arrays.asList(responsesElement)) 591 .build(); 592 mockService.addResponse(expectedResponse); 593 594 EnvironmentName name = 595 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 596 597 LookupEnvironmentHistoryPagedResponse pagedListResponse = client.lookupEnvironmentHistory(name); 598 599 List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 600 601 Assert.assertEquals(1, resources.size()); 602 Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0)); 603 604 List<String> actualRequests = mockService.getRequestPaths(); 605 Assert.assertEquals(1, actualRequests.size()); 606 607 String apiClientHeaderKey = 608 mockService 609 .getRequestHeaders() 610 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 611 .iterator() 612 .next(); 613 Assert.assertTrue( 614 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 615 .matcher(apiClientHeaderKey) 616 .matches()); 617 } 618 619 @Test lookupEnvironmentHistoryExceptionTest()620 public void lookupEnvironmentHistoryExceptionTest() throws Exception { 621 ApiException exception = 622 ApiExceptionFactory.createException( 623 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 624 mockService.addException(exception); 625 626 try { 627 EnvironmentName name = 628 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 629 client.lookupEnvironmentHistory(name); 630 Assert.fail("No exception raised"); 631 } catch (InvalidArgumentException e) { 632 // Expected exception. 633 } 634 } 635 636 @Test lookupEnvironmentHistoryTest2()637 public void lookupEnvironmentHistoryTest2() throws Exception { 638 Environment responsesElement = Environment.newBuilder().build(); 639 LookupEnvironmentHistoryResponse expectedResponse = 640 LookupEnvironmentHistoryResponse.newBuilder() 641 .setNextPageToken("") 642 .addAllEnvironments(Arrays.asList(responsesElement)) 643 .build(); 644 mockService.addResponse(expectedResponse); 645 646 String name = 647 "projects/project-8900/locations/location-8900/agents/agent-8900/environments/environment-8900"; 648 649 LookupEnvironmentHistoryPagedResponse pagedListResponse = client.lookupEnvironmentHistory(name); 650 651 List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 652 653 Assert.assertEquals(1, resources.size()); 654 Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0)); 655 656 List<String> actualRequests = mockService.getRequestPaths(); 657 Assert.assertEquals(1, actualRequests.size()); 658 659 String apiClientHeaderKey = 660 mockService 661 .getRequestHeaders() 662 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 663 .iterator() 664 .next(); 665 Assert.assertTrue( 666 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 667 .matcher(apiClientHeaderKey) 668 .matches()); 669 } 670 671 @Test lookupEnvironmentHistoryExceptionTest2()672 public void lookupEnvironmentHistoryExceptionTest2() throws Exception { 673 ApiException exception = 674 ApiExceptionFactory.createException( 675 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 676 mockService.addException(exception); 677 678 try { 679 String name = 680 "projects/project-8900/locations/location-8900/agents/agent-8900/environments/environment-8900"; 681 client.lookupEnvironmentHistory(name); 682 Assert.fail("No exception raised"); 683 } catch (InvalidArgumentException e) { 684 // Expected exception. 685 } 686 } 687 688 @Test runContinuousTestTest()689 public void runContinuousTestTest() throws Exception { 690 RunContinuousTestResponse expectedResponse = 691 RunContinuousTestResponse.newBuilder() 692 .setContinuousTestResult(ContinuousTestResult.newBuilder().build()) 693 .build(); 694 Operation resultOperation = 695 Operation.newBuilder() 696 .setName("runContinuousTestTest") 697 .setDone(true) 698 .setResponse(Any.pack(expectedResponse)) 699 .build(); 700 mockService.addResponse(resultOperation); 701 702 RunContinuousTestRequest request = 703 RunContinuousTestRequest.newBuilder() 704 .setEnvironment( 705 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 706 .toString()) 707 .build(); 708 709 RunContinuousTestResponse actualResponse = client.runContinuousTestAsync(request).get(); 710 Assert.assertEquals(expectedResponse, actualResponse); 711 712 List<String> actualRequests = mockService.getRequestPaths(); 713 Assert.assertEquals(1, actualRequests.size()); 714 715 String apiClientHeaderKey = 716 mockService 717 .getRequestHeaders() 718 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 719 .iterator() 720 .next(); 721 Assert.assertTrue( 722 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 723 .matcher(apiClientHeaderKey) 724 .matches()); 725 } 726 727 @Test runContinuousTestExceptionTest()728 public void runContinuousTestExceptionTest() throws Exception { 729 ApiException exception = 730 ApiExceptionFactory.createException( 731 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 732 mockService.addException(exception); 733 734 try { 735 RunContinuousTestRequest request = 736 RunContinuousTestRequest.newBuilder() 737 .setEnvironment( 738 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 739 .toString()) 740 .build(); 741 client.runContinuousTestAsync(request).get(); 742 Assert.fail("No exception raised"); 743 } catch (ExecutionException e) { 744 } 745 } 746 747 @Test listContinuousTestResultsTest()748 public void listContinuousTestResultsTest() throws Exception { 749 ContinuousTestResult responsesElement = ContinuousTestResult.newBuilder().build(); 750 ListContinuousTestResultsResponse expectedResponse = 751 ListContinuousTestResultsResponse.newBuilder() 752 .setNextPageToken("") 753 .addAllContinuousTestResults(Arrays.asList(responsesElement)) 754 .build(); 755 mockService.addResponse(expectedResponse); 756 757 EnvironmentName parent = 758 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 759 760 ListContinuousTestResultsPagedResponse pagedListResponse = 761 client.listContinuousTestResults(parent); 762 763 List<ContinuousTestResult> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 764 765 Assert.assertEquals(1, resources.size()); 766 Assert.assertEquals(expectedResponse.getContinuousTestResultsList().get(0), resources.get(0)); 767 768 List<String> actualRequests = mockService.getRequestPaths(); 769 Assert.assertEquals(1, actualRequests.size()); 770 771 String apiClientHeaderKey = 772 mockService 773 .getRequestHeaders() 774 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 775 .iterator() 776 .next(); 777 Assert.assertTrue( 778 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 779 .matcher(apiClientHeaderKey) 780 .matches()); 781 } 782 783 @Test listContinuousTestResultsExceptionTest()784 public void listContinuousTestResultsExceptionTest() throws Exception { 785 ApiException exception = 786 ApiExceptionFactory.createException( 787 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 788 mockService.addException(exception); 789 790 try { 791 EnvironmentName parent = 792 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 793 client.listContinuousTestResults(parent); 794 Assert.fail("No exception raised"); 795 } catch (InvalidArgumentException e) { 796 // Expected exception. 797 } 798 } 799 800 @Test listContinuousTestResultsTest2()801 public void listContinuousTestResultsTest2() throws Exception { 802 ContinuousTestResult responsesElement = ContinuousTestResult.newBuilder().build(); 803 ListContinuousTestResultsResponse expectedResponse = 804 ListContinuousTestResultsResponse.newBuilder() 805 .setNextPageToken("") 806 .addAllContinuousTestResults(Arrays.asList(responsesElement)) 807 .build(); 808 mockService.addResponse(expectedResponse); 809 810 String parent = 811 "projects/project-5835/locations/location-5835/agents/agent-5835/environments/environment-5835"; 812 813 ListContinuousTestResultsPagedResponse pagedListResponse = 814 client.listContinuousTestResults(parent); 815 816 List<ContinuousTestResult> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 817 818 Assert.assertEquals(1, resources.size()); 819 Assert.assertEquals(expectedResponse.getContinuousTestResultsList().get(0), resources.get(0)); 820 821 List<String> actualRequests = mockService.getRequestPaths(); 822 Assert.assertEquals(1, actualRequests.size()); 823 824 String apiClientHeaderKey = 825 mockService 826 .getRequestHeaders() 827 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 828 .iterator() 829 .next(); 830 Assert.assertTrue( 831 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 832 .matcher(apiClientHeaderKey) 833 .matches()); 834 } 835 836 @Test listContinuousTestResultsExceptionTest2()837 public void listContinuousTestResultsExceptionTest2() throws Exception { 838 ApiException exception = 839 ApiExceptionFactory.createException( 840 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 841 mockService.addException(exception); 842 843 try { 844 String parent = 845 "projects/project-5835/locations/location-5835/agents/agent-5835/environments/environment-5835"; 846 client.listContinuousTestResults(parent); 847 Assert.fail("No exception raised"); 848 } catch (InvalidArgumentException e) { 849 // Expected exception. 850 } 851 } 852 853 @Test deployFlowTest()854 public void deployFlowTest() throws Exception { 855 DeployFlowResponse expectedResponse = 856 DeployFlowResponse.newBuilder() 857 .setEnvironment(Environment.newBuilder().build()) 858 .setDeployment("deployment1939520197") 859 .build(); 860 Operation resultOperation = 861 Operation.newBuilder() 862 .setName("deployFlowTest") 863 .setDone(true) 864 .setResponse(Any.pack(expectedResponse)) 865 .build(); 866 mockService.addResponse(resultOperation); 867 868 DeployFlowRequest request = 869 DeployFlowRequest.newBuilder() 870 .setEnvironment( 871 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 872 .toString()) 873 .setFlowVersion( 874 VersionName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]", "[VERSION]") 875 .toString()) 876 .build(); 877 878 DeployFlowResponse actualResponse = client.deployFlowAsync(request).get(); 879 Assert.assertEquals(expectedResponse, actualResponse); 880 881 List<String> actualRequests = mockService.getRequestPaths(); 882 Assert.assertEquals(1, actualRequests.size()); 883 884 String apiClientHeaderKey = 885 mockService 886 .getRequestHeaders() 887 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 888 .iterator() 889 .next(); 890 Assert.assertTrue( 891 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 892 .matcher(apiClientHeaderKey) 893 .matches()); 894 } 895 896 @Test deployFlowExceptionTest()897 public void deployFlowExceptionTest() throws Exception { 898 ApiException exception = 899 ApiExceptionFactory.createException( 900 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 901 mockService.addException(exception); 902 903 try { 904 DeployFlowRequest request = 905 DeployFlowRequest.newBuilder() 906 .setEnvironment( 907 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 908 .toString()) 909 .setFlowVersion( 910 VersionName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]", "[VERSION]") 911 .toString()) 912 .build(); 913 client.deployFlowAsync(request).get(); 914 Assert.fail("No exception raised"); 915 } catch (ExecutionException e) { 916 } 917 } 918 919 @Test listLocationsTest()920 public void listLocationsTest() throws Exception { 921 Location responsesElement = Location.newBuilder().build(); 922 ListLocationsResponse expectedResponse = 923 ListLocationsResponse.newBuilder() 924 .setNextPageToken("") 925 .addAllLocations(Arrays.asList(responsesElement)) 926 .build(); 927 mockService.addResponse(expectedResponse); 928 929 ListLocationsRequest request = 930 ListLocationsRequest.newBuilder() 931 .setName("projects/project-3664") 932 .setFilter("filter-1274492040") 933 .setPageSize(883849137) 934 .setPageToken("pageToken873572522") 935 .build(); 936 937 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 938 939 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 940 941 Assert.assertEquals(1, resources.size()); 942 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 943 944 List<String> actualRequests = mockService.getRequestPaths(); 945 Assert.assertEquals(1, actualRequests.size()); 946 947 String apiClientHeaderKey = 948 mockService 949 .getRequestHeaders() 950 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 951 .iterator() 952 .next(); 953 Assert.assertTrue( 954 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 955 .matcher(apiClientHeaderKey) 956 .matches()); 957 } 958 959 @Test listLocationsExceptionTest()960 public void listLocationsExceptionTest() throws Exception { 961 ApiException exception = 962 ApiExceptionFactory.createException( 963 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 964 mockService.addException(exception); 965 966 try { 967 ListLocationsRequest request = 968 ListLocationsRequest.newBuilder() 969 .setName("projects/project-3664") 970 .setFilter("filter-1274492040") 971 .setPageSize(883849137) 972 .setPageToken("pageToken873572522") 973 .build(); 974 client.listLocations(request); 975 Assert.fail("No exception raised"); 976 } catch (InvalidArgumentException e) { 977 // Expected exception. 978 } 979 } 980 981 @Test getLocationTest()982 public void getLocationTest() throws Exception { 983 Location expectedResponse = 984 Location.newBuilder() 985 .setName("name3373707") 986 .setLocationId("locationId1541836720") 987 .setDisplayName("displayName1714148973") 988 .putAllLabels(new HashMap<String, String>()) 989 .setMetadata(Any.newBuilder().build()) 990 .build(); 991 mockService.addResponse(expectedResponse); 992 993 GetLocationRequest request = 994 GetLocationRequest.newBuilder() 995 .setName("projects/project-9062/locations/location-9062") 996 .build(); 997 998 Location actualResponse = client.getLocation(request); 999 Assert.assertEquals(expectedResponse, actualResponse); 1000 1001 List<String> actualRequests = mockService.getRequestPaths(); 1002 Assert.assertEquals(1, actualRequests.size()); 1003 1004 String apiClientHeaderKey = 1005 mockService 1006 .getRequestHeaders() 1007 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 1008 .iterator() 1009 .next(); 1010 Assert.assertTrue( 1011 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 1012 .matcher(apiClientHeaderKey) 1013 .matches()); 1014 } 1015 1016 @Test getLocationExceptionTest()1017 public void getLocationExceptionTest() throws Exception { 1018 ApiException exception = 1019 ApiExceptionFactory.createException( 1020 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 1021 mockService.addException(exception); 1022 1023 try { 1024 GetLocationRequest request = 1025 GetLocationRequest.newBuilder() 1026 .setName("projects/project-9062/locations/location-9062") 1027 .build(); 1028 client.getLocation(request); 1029 Assert.fail("No exception raised"); 1030 } catch (InvalidArgumentException e) { 1031 // Expected exception. 1032 } 1033 } 1034 } 1035