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