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.AgentsClient.ListAgentsPagedResponse; 20 import static com.google.cloud.dialogflow.cx.v3.AgentsClient.ListLocationsPagedResponse; 21 22 import com.google.api.gax.core.NoCredentialsProvider; 23 import com.google.api.gax.grpc.GaxGrpcProperties; 24 import com.google.api.gax.grpc.testing.LocalChannelProvider; 25 import com.google.api.gax.grpc.testing.MockGrpcService; 26 import com.google.api.gax.grpc.testing.MockServiceHelper; 27 import com.google.api.gax.rpc.ApiClientHeaderProvider; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.api.gax.rpc.StatusCode; 30 import com.google.cloud.location.GetLocationRequest; 31 import com.google.cloud.location.ListLocationsRequest; 32 import com.google.cloud.location.ListLocationsResponse; 33 import com.google.cloud.location.Location; 34 import com.google.common.collect.Lists; 35 import com.google.longrunning.Operation; 36 import com.google.protobuf.AbstractMessage; 37 import com.google.protobuf.Any; 38 import com.google.protobuf.Empty; 39 import com.google.protobuf.FieldMask; 40 import io.grpc.StatusRuntimeException; 41 import java.io.IOException; 42 import java.util.ArrayList; 43 import java.util.Arrays; 44 import java.util.HashMap; 45 import java.util.List; 46 import java.util.UUID; 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 AgentsClientTest { 58 private static MockAgents mockAgents; 59 private static MockLocations mockLocations; 60 private static MockServiceHelper mockServiceHelper; 61 private LocalChannelProvider channelProvider; 62 private AgentsClient client; 63 64 @BeforeClass startStaticServer()65 public static void startStaticServer() { 66 mockAgents = new MockAgents(); 67 mockLocations = new MockLocations(); 68 mockServiceHelper = 69 new MockServiceHelper( 70 UUID.randomUUID().toString(), 71 Arrays.<MockGrpcService>asList(mockAgents, mockLocations)); 72 mockServiceHelper.start(); 73 } 74 75 @AfterClass stopServer()76 public static void stopServer() { 77 mockServiceHelper.stop(); 78 } 79 80 @Before setUp()81 public void setUp() throws IOException { 82 mockServiceHelper.reset(); 83 channelProvider = mockServiceHelper.createChannelProvider(); 84 AgentsSettings settings = 85 AgentsSettings.newBuilder() 86 .setTransportChannelProvider(channelProvider) 87 .setCredentialsProvider(NoCredentialsProvider.create()) 88 .build(); 89 client = AgentsClient.create(settings); 90 } 91 92 @After tearDown()93 public void tearDown() throws Exception { 94 client.close(); 95 } 96 97 @Test listAgentsTest()98 public void listAgentsTest() throws Exception { 99 Agent responsesElement = Agent.newBuilder().build(); 100 ListAgentsResponse expectedResponse = 101 ListAgentsResponse.newBuilder() 102 .setNextPageToken("") 103 .addAllAgents(Arrays.asList(responsesElement)) 104 .build(); 105 mockAgents.addResponse(expectedResponse); 106 107 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 108 109 ListAgentsPagedResponse pagedListResponse = client.listAgents(parent); 110 111 List<Agent> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 112 113 Assert.assertEquals(1, resources.size()); 114 Assert.assertEquals(expectedResponse.getAgentsList().get(0), resources.get(0)); 115 116 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 117 Assert.assertEquals(1, actualRequests.size()); 118 ListAgentsRequest actualRequest = ((ListAgentsRequest) actualRequests.get(0)); 119 120 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 121 Assert.assertTrue( 122 channelProvider.isHeaderSent( 123 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 124 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 125 } 126 127 @Test listAgentsExceptionTest()128 public void listAgentsExceptionTest() throws Exception { 129 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 130 mockAgents.addException(exception); 131 132 try { 133 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 134 client.listAgents(parent); 135 Assert.fail("No exception raised"); 136 } catch (InvalidArgumentException e) { 137 // Expected exception. 138 } 139 } 140 141 @Test listAgentsTest2()142 public void listAgentsTest2() throws Exception { 143 Agent responsesElement = Agent.newBuilder().build(); 144 ListAgentsResponse expectedResponse = 145 ListAgentsResponse.newBuilder() 146 .setNextPageToken("") 147 .addAllAgents(Arrays.asList(responsesElement)) 148 .build(); 149 mockAgents.addResponse(expectedResponse); 150 151 String parent = "parent-995424086"; 152 153 ListAgentsPagedResponse pagedListResponse = client.listAgents(parent); 154 155 List<Agent> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 156 157 Assert.assertEquals(1, resources.size()); 158 Assert.assertEquals(expectedResponse.getAgentsList().get(0), resources.get(0)); 159 160 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 161 Assert.assertEquals(1, actualRequests.size()); 162 ListAgentsRequest actualRequest = ((ListAgentsRequest) actualRequests.get(0)); 163 164 Assert.assertEquals(parent, actualRequest.getParent()); 165 Assert.assertTrue( 166 channelProvider.isHeaderSent( 167 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 168 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 169 } 170 171 @Test listAgentsExceptionTest2()172 public void listAgentsExceptionTest2() throws Exception { 173 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 174 mockAgents.addException(exception); 175 176 try { 177 String parent = "parent-995424086"; 178 client.listAgents(parent); 179 Assert.fail("No exception raised"); 180 } catch (InvalidArgumentException e) { 181 // Expected exception. 182 } 183 } 184 185 @Test getAgentTest()186 public void getAgentTest() throws Exception { 187 Agent expectedResponse = 188 Agent.newBuilder() 189 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 190 .setDisplayName("displayName1714148973") 191 .setDefaultLanguageCode("defaultLanguageCode2069633606") 192 .addAllSupportedLanguageCodes(new ArrayList<String>()) 193 .setTimeZone("timeZone-2077180903") 194 .setDescription("description-1724546052") 195 .setAvatarUri("avatarUri-428646061") 196 .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build()) 197 .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString()) 198 .setSecuritySettings( 199 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]") 200 .toString()) 201 .setEnableStackdriverLogging(true) 202 .setEnableSpellCorrection(true) 203 .setLocked(true) 204 .setAdvancedSettings(AdvancedSettings.newBuilder().build()) 205 .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build()) 206 .build(); 207 mockAgents.addResponse(expectedResponse); 208 209 AgentName name = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 210 211 Agent actualResponse = client.getAgent(name); 212 Assert.assertEquals(expectedResponse, actualResponse); 213 214 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 215 Assert.assertEquals(1, actualRequests.size()); 216 GetAgentRequest actualRequest = ((GetAgentRequest) actualRequests.get(0)); 217 218 Assert.assertEquals(name.toString(), actualRequest.getName()); 219 Assert.assertTrue( 220 channelProvider.isHeaderSent( 221 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 222 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 223 } 224 225 @Test getAgentExceptionTest()226 public void getAgentExceptionTest() throws Exception { 227 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 228 mockAgents.addException(exception); 229 230 try { 231 AgentName name = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 232 client.getAgent(name); 233 Assert.fail("No exception raised"); 234 } catch (InvalidArgumentException e) { 235 // Expected exception. 236 } 237 } 238 239 @Test getAgentTest2()240 public void getAgentTest2() throws Exception { 241 Agent expectedResponse = 242 Agent.newBuilder() 243 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 244 .setDisplayName("displayName1714148973") 245 .setDefaultLanguageCode("defaultLanguageCode2069633606") 246 .addAllSupportedLanguageCodes(new ArrayList<String>()) 247 .setTimeZone("timeZone-2077180903") 248 .setDescription("description-1724546052") 249 .setAvatarUri("avatarUri-428646061") 250 .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build()) 251 .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString()) 252 .setSecuritySettings( 253 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]") 254 .toString()) 255 .setEnableStackdriverLogging(true) 256 .setEnableSpellCorrection(true) 257 .setLocked(true) 258 .setAdvancedSettings(AdvancedSettings.newBuilder().build()) 259 .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build()) 260 .build(); 261 mockAgents.addResponse(expectedResponse); 262 263 String name = "name3373707"; 264 265 Agent actualResponse = client.getAgent(name); 266 Assert.assertEquals(expectedResponse, actualResponse); 267 268 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 269 Assert.assertEquals(1, actualRequests.size()); 270 GetAgentRequest actualRequest = ((GetAgentRequest) actualRequests.get(0)); 271 272 Assert.assertEquals(name, actualRequest.getName()); 273 Assert.assertTrue( 274 channelProvider.isHeaderSent( 275 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 276 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 277 } 278 279 @Test getAgentExceptionTest2()280 public void getAgentExceptionTest2() throws Exception { 281 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 282 mockAgents.addException(exception); 283 284 try { 285 String name = "name3373707"; 286 client.getAgent(name); 287 Assert.fail("No exception raised"); 288 } catch (InvalidArgumentException e) { 289 // Expected exception. 290 } 291 } 292 293 @Test createAgentTest()294 public void createAgentTest() throws Exception { 295 Agent expectedResponse = 296 Agent.newBuilder() 297 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 298 .setDisplayName("displayName1714148973") 299 .setDefaultLanguageCode("defaultLanguageCode2069633606") 300 .addAllSupportedLanguageCodes(new ArrayList<String>()) 301 .setTimeZone("timeZone-2077180903") 302 .setDescription("description-1724546052") 303 .setAvatarUri("avatarUri-428646061") 304 .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build()) 305 .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString()) 306 .setSecuritySettings( 307 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]") 308 .toString()) 309 .setEnableStackdriverLogging(true) 310 .setEnableSpellCorrection(true) 311 .setLocked(true) 312 .setAdvancedSettings(AdvancedSettings.newBuilder().build()) 313 .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build()) 314 .build(); 315 mockAgents.addResponse(expectedResponse); 316 317 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 318 Agent agent = Agent.newBuilder().build(); 319 320 Agent actualResponse = client.createAgent(parent, agent); 321 Assert.assertEquals(expectedResponse, actualResponse); 322 323 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 324 Assert.assertEquals(1, actualRequests.size()); 325 CreateAgentRequest actualRequest = ((CreateAgentRequest) actualRequests.get(0)); 326 327 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 328 Assert.assertEquals(agent, actualRequest.getAgent()); 329 Assert.assertTrue( 330 channelProvider.isHeaderSent( 331 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 332 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 333 } 334 335 @Test createAgentExceptionTest()336 public void createAgentExceptionTest() throws Exception { 337 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 338 mockAgents.addException(exception); 339 340 try { 341 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 342 Agent agent = Agent.newBuilder().build(); 343 client.createAgent(parent, agent); 344 Assert.fail("No exception raised"); 345 } catch (InvalidArgumentException e) { 346 // Expected exception. 347 } 348 } 349 350 @Test createAgentTest2()351 public void createAgentTest2() throws Exception { 352 Agent expectedResponse = 353 Agent.newBuilder() 354 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 355 .setDisplayName("displayName1714148973") 356 .setDefaultLanguageCode("defaultLanguageCode2069633606") 357 .addAllSupportedLanguageCodes(new ArrayList<String>()) 358 .setTimeZone("timeZone-2077180903") 359 .setDescription("description-1724546052") 360 .setAvatarUri("avatarUri-428646061") 361 .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build()) 362 .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString()) 363 .setSecuritySettings( 364 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]") 365 .toString()) 366 .setEnableStackdriverLogging(true) 367 .setEnableSpellCorrection(true) 368 .setLocked(true) 369 .setAdvancedSettings(AdvancedSettings.newBuilder().build()) 370 .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build()) 371 .build(); 372 mockAgents.addResponse(expectedResponse); 373 374 String parent = "parent-995424086"; 375 Agent agent = Agent.newBuilder().build(); 376 377 Agent actualResponse = client.createAgent(parent, agent); 378 Assert.assertEquals(expectedResponse, actualResponse); 379 380 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 381 Assert.assertEquals(1, actualRequests.size()); 382 CreateAgentRequest actualRequest = ((CreateAgentRequest) actualRequests.get(0)); 383 384 Assert.assertEquals(parent, actualRequest.getParent()); 385 Assert.assertEquals(agent, actualRequest.getAgent()); 386 Assert.assertTrue( 387 channelProvider.isHeaderSent( 388 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 389 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 390 } 391 392 @Test createAgentExceptionTest2()393 public void createAgentExceptionTest2() throws Exception { 394 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 395 mockAgents.addException(exception); 396 397 try { 398 String parent = "parent-995424086"; 399 Agent agent = Agent.newBuilder().build(); 400 client.createAgent(parent, agent); 401 Assert.fail("No exception raised"); 402 } catch (InvalidArgumentException e) { 403 // Expected exception. 404 } 405 } 406 407 @Test updateAgentTest()408 public void updateAgentTest() throws Exception { 409 Agent expectedResponse = 410 Agent.newBuilder() 411 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 412 .setDisplayName("displayName1714148973") 413 .setDefaultLanguageCode("defaultLanguageCode2069633606") 414 .addAllSupportedLanguageCodes(new ArrayList<String>()) 415 .setTimeZone("timeZone-2077180903") 416 .setDescription("description-1724546052") 417 .setAvatarUri("avatarUri-428646061") 418 .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build()) 419 .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString()) 420 .setSecuritySettings( 421 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]") 422 .toString()) 423 .setEnableStackdriverLogging(true) 424 .setEnableSpellCorrection(true) 425 .setLocked(true) 426 .setAdvancedSettings(AdvancedSettings.newBuilder().build()) 427 .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build()) 428 .build(); 429 mockAgents.addResponse(expectedResponse); 430 431 Agent agent = Agent.newBuilder().build(); 432 FieldMask updateMask = FieldMask.newBuilder().build(); 433 434 Agent actualResponse = client.updateAgent(agent, updateMask); 435 Assert.assertEquals(expectedResponse, actualResponse); 436 437 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 438 Assert.assertEquals(1, actualRequests.size()); 439 UpdateAgentRequest actualRequest = ((UpdateAgentRequest) actualRequests.get(0)); 440 441 Assert.assertEquals(agent, actualRequest.getAgent()); 442 Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); 443 Assert.assertTrue( 444 channelProvider.isHeaderSent( 445 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 446 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 447 } 448 449 @Test updateAgentExceptionTest()450 public void updateAgentExceptionTest() throws Exception { 451 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 452 mockAgents.addException(exception); 453 454 try { 455 Agent agent = Agent.newBuilder().build(); 456 FieldMask updateMask = FieldMask.newBuilder().build(); 457 client.updateAgent(agent, updateMask); 458 Assert.fail("No exception raised"); 459 } catch (InvalidArgumentException e) { 460 // Expected exception. 461 } 462 } 463 464 @Test deleteAgentTest()465 public void deleteAgentTest() throws Exception { 466 Empty expectedResponse = Empty.newBuilder().build(); 467 mockAgents.addResponse(expectedResponse); 468 469 AgentName name = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 470 471 client.deleteAgent(name); 472 473 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 474 Assert.assertEquals(1, actualRequests.size()); 475 DeleteAgentRequest actualRequest = ((DeleteAgentRequest) actualRequests.get(0)); 476 477 Assert.assertEquals(name.toString(), actualRequest.getName()); 478 Assert.assertTrue( 479 channelProvider.isHeaderSent( 480 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 481 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 482 } 483 484 @Test deleteAgentExceptionTest()485 public void deleteAgentExceptionTest() throws Exception { 486 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 487 mockAgents.addException(exception); 488 489 try { 490 AgentName name = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 491 client.deleteAgent(name); 492 Assert.fail("No exception raised"); 493 } catch (InvalidArgumentException e) { 494 // Expected exception. 495 } 496 } 497 498 @Test deleteAgentTest2()499 public void deleteAgentTest2() throws Exception { 500 Empty expectedResponse = Empty.newBuilder().build(); 501 mockAgents.addResponse(expectedResponse); 502 503 String name = "name3373707"; 504 505 client.deleteAgent(name); 506 507 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 508 Assert.assertEquals(1, actualRequests.size()); 509 DeleteAgentRequest actualRequest = ((DeleteAgentRequest) actualRequests.get(0)); 510 511 Assert.assertEquals(name, actualRequest.getName()); 512 Assert.assertTrue( 513 channelProvider.isHeaderSent( 514 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 515 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 516 } 517 518 @Test deleteAgentExceptionTest2()519 public void deleteAgentExceptionTest2() throws Exception { 520 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 521 mockAgents.addException(exception); 522 523 try { 524 String name = "name3373707"; 525 client.deleteAgent(name); 526 Assert.fail("No exception raised"); 527 } catch (InvalidArgumentException e) { 528 // Expected exception. 529 } 530 } 531 532 @Test exportAgentTest()533 public void exportAgentTest() throws Exception { 534 ExportAgentResponse expectedResponse = ExportAgentResponse.newBuilder().build(); 535 Operation resultOperation = 536 Operation.newBuilder() 537 .setName("exportAgentTest") 538 .setDone(true) 539 .setResponse(Any.pack(expectedResponse)) 540 .build(); 541 mockAgents.addResponse(resultOperation); 542 543 ExportAgentRequest request = 544 ExportAgentRequest.newBuilder() 545 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 546 .setAgentUri("agentUri1469149223") 547 .setEnvironment( 548 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 549 .toString()) 550 .build(); 551 552 ExportAgentResponse actualResponse = client.exportAgentAsync(request).get(); 553 Assert.assertEquals(expectedResponse, actualResponse); 554 555 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 556 Assert.assertEquals(1, actualRequests.size()); 557 ExportAgentRequest actualRequest = ((ExportAgentRequest) actualRequests.get(0)); 558 559 Assert.assertEquals(request.getName(), actualRequest.getName()); 560 Assert.assertEquals(request.getAgentUri(), actualRequest.getAgentUri()); 561 Assert.assertEquals(request.getDataFormat(), actualRequest.getDataFormat()); 562 Assert.assertEquals(request.getEnvironment(), actualRequest.getEnvironment()); 563 Assert.assertTrue( 564 channelProvider.isHeaderSent( 565 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 566 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 567 } 568 569 @Test exportAgentExceptionTest()570 public void exportAgentExceptionTest() throws Exception { 571 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 572 mockAgents.addException(exception); 573 574 try { 575 ExportAgentRequest request = 576 ExportAgentRequest.newBuilder() 577 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 578 .setAgentUri("agentUri1469149223") 579 .setEnvironment( 580 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]") 581 .toString()) 582 .build(); 583 client.exportAgentAsync(request).get(); 584 Assert.fail("No exception raised"); 585 } catch (ExecutionException e) { 586 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 587 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 588 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 589 } 590 } 591 592 @Test restoreAgentTest()593 public void restoreAgentTest() throws Exception { 594 Empty expectedResponse = Empty.newBuilder().build(); 595 Operation resultOperation = 596 Operation.newBuilder() 597 .setName("restoreAgentTest") 598 .setDone(true) 599 .setResponse(Any.pack(expectedResponse)) 600 .build(); 601 mockAgents.addResponse(resultOperation); 602 603 RestoreAgentRequest request = 604 RestoreAgentRequest.newBuilder() 605 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 606 .build(); 607 608 client.restoreAgentAsync(request).get(); 609 610 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 611 Assert.assertEquals(1, actualRequests.size()); 612 RestoreAgentRequest actualRequest = ((RestoreAgentRequest) actualRequests.get(0)); 613 614 Assert.assertEquals(request.getName(), actualRequest.getName()); 615 Assert.assertEquals(request.getAgentUri(), actualRequest.getAgentUri()); 616 Assert.assertEquals(request.getAgentContent(), actualRequest.getAgentContent()); 617 Assert.assertEquals(request.getRestoreOption(), actualRequest.getRestoreOption()); 618 Assert.assertTrue( 619 channelProvider.isHeaderSent( 620 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 621 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 622 } 623 624 @Test restoreAgentExceptionTest()625 public void restoreAgentExceptionTest() throws Exception { 626 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 627 mockAgents.addException(exception); 628 629 try { 630 RestoreAgentRequest request = 631 RestoreAgentRequest.newBuilder() 632 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 633 .build(); 634 client.restoreAgentAsync(request).get(); 635 Assert.fail("No exception raised"); 636 } catch (ExecutionException e) { 637 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 638 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 639 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 640 } 641 } 642 643 @Test validateAgentTest()644 public void validateAgentTest() throws Exception { 645 AgentValidationResult expectedResponse = 646 AgentValidationResult.newBuilder() 647 .setName(AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 648 .addAllFlowValidationResults(new ArrayList<FlowValidationResult>()) 649 .build(); 650 mockAgents.addResponse(expectedResponse); 651 652 ValidateAgentRequest request = 653 ValidateAgentRequest.newBuilder() 654 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 655 .setLanguageCode("languageCode-2092349083") 656 .build(); 657 658 AgentValidationResult actualResponse = client.validateAgent(request); 659 Assert.assertEquals(expectedResponse, actualResponse); 660 661 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 662 Assert.assertEquals(1, actualRequests.size()); 663 ValidateAgentRequest actualRequest = ((ValidateAgentRequest) actualRequests.get(0)); 664 665 Assert.assertEquals(request.getName(), actualRequest.getName()); 666 Assert.assertEquals(request.getLanguageCode(), actualRequest.getLanguageCode()); 667 Assert.assertTrue( 668 channelProvider.isHeaderSent( 669 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 670 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 671 } 672 673 @Test validateAgentExceptionTest()674 public void validateAgentExceptionTest() throws Exception { 675 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 676 mockAgents.addException(exception); 677 678 try { 679 ValidateAgentRequest request = 680 ValidateAgentRequest.newBuilder() 681 .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 682 .setLanguageCode("languageCode-2092349083") 683 .build(); 684 client.validateAgent(request); 685 Assert.fail("No exception raised"); 686 } catch (InvalidArgumentException e) { 687 // Expected exception. 688 } 689 } 690 691 @Test getAgentValidationResultTest()692 public void getAgentValidationResultTest() throws Exception { 693 AgentValidationResult expectedResponse = 694 AgentValidationResult.newBuilder() 695 .setName(AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 696 .addAllFlowValidationResults(new ArrayList<FlowValidationResult>()) 697 .build(); 698 mockAgents.addResponse(expectedResponse); 699 700 AgentValidationResultName name = 701 AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 702 703 AgentValidationResult actualResponse = client.getAgentValidationResult(name); 704 Assert.assertEquals(expectedResponse, actualResponse); 705 706 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 707 Assert.assertEquals(1, actualRequests.size()); 708 GetAgentValidationResultRequest actualRequest = 709 ((GetAgentValidationResultRequest) actualRequests.get(0)); 710 711 Assert.assertEquals(name.toString(), actualRequest.getName()); 712 Assert.assertTrue( 713 channelProvider.isHeaderSent( 714 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 715 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 716 } 717 718 @Test getAgentValidationResultExceptionTest()719 public void getAgentValidationResultExceptionTest() throws Exception { 720 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 721 mockAgents.addException(exception); 722 723 try { 724 AgentValidationResultName name = 725 AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 726 client.getAgentValidationResult(name); 727 Assert.fail("No exception raised"); 728 } catch (InvalidArgumentException e) { 729 // Expected exception. 730 } 731 } 732 733 @Test getAgentValidationResultTest2()734 public void getAgentValidationResultTest2() throws Exception { 735 AgentValidationResult expectedResponse = 736 AgentValidationResult.newBuilder() 737 .setName(AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString()) 738 .addAllFlowValidationResults(new ArrayList<FlowValidationResult>()) 739 .build(); 740 mockAgents.addResponse(expectedResponse); 741 742 String name = "name3373707"; 743 744 AgentValidationResult actualResponse = client.getAgentValidationResult(name); 745 Assert.assertEquals(expectedResponse, actualResponse); 746 747 List<AbstractMessage> actualRequests = mockAgents.getRequests(); 748 Assert.assertEquals(1, actualRequests.size()); 749 GetAgentValidationResultRequest actualRequest = 750 ((GetAgentValidationResultRequest) actualRequests.get(0)); 751 752 Assert.assertEquals(name, actualRequest.getName()); 753 Assert.assertTrue( 754 channelProvider.isHeaderSent( 755 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 756 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 757 } 758 759 @Test getAgentValidationResultExceptionTest2()760 public void getAgentValidationResultExceptionTest2() throws Exception { 761 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 762 mockAgents.addException(exception); 763 764 try { 765 String name = "name3373707"; 766 client.getAgentValidationResult(name); 767 Assert.fail("No exception raised"); 768 } catch (InvalidArgumentException e) { 769 // Expected exception. 770 } 771 } 772 773 @Test listLocationsTest()774 public void listLocationsTest() throws Exception { 775 Location responsesElement = Location.newBuilder().build(); 776 ListLocationsResponse expectedResponse = 777 ListLocationsResponse.newBuilder() 778 .setNextPageToken("") 779 .addAllLocations(Arrays.asList(responsesElement)) 780 .build(); 781 mockLocations.addResponse(expectedResponse); 782 783 ListLocationsRequest request = 784 ListLocationsRequest.newBuilder() 785 .setName("name3373707") 786 .setFilter("filter-1274492040") 787 .setPageSize(883849137) 788 .setPageToken("pageToken873572522") 789 .build(); 790 791 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 792 793 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 794 795 Assert.assertEquals(1, resources.size()); 796 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 797 798 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 799 Assert.assertEquals(1, actualRequests.size()); 800 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 801 802 Assert.assertEquals(request.getName(), actualRequest.getName()); 803 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 804 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 805 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 806 Assert.assertTrue( 807 channelProvider.isHeaderSent( 808 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 809 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 810 } 811 812 @Test listLocationsExceptionTest()813 public void listLocationsExceptionTest() throws Exception { 814 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 815 mockLocations.addException(exception); 816 817 try { 818 ListLocationsRequest request = 819 ListLocationsRequest.newBuilder() 820 .setName("name3373707") 821 .setFilter("filter-1274492040") 822 .setPageSize(883849137) 823 .setPageToken("pageToken873572522") 824 .build(); 825 client.listLocations(request); 826 Assert.fail("No exception raised"); 827 } catch (InvalidArgumentException e) { 828 // Expected exception. 829 } 830 } 831 832 @Test getLocationTest()833 public void getLocationTest() throws Exception { 834 Location expectedResponse = 835 Location.newBuilder() 836 .setName("name3373707") 837 .setLocationId("locationId1541836720") 838 .setDisplayName("displayName1714148973") 839 .putAllLabels(new HashMap<String, String>()) 840 .setMetadata(Any.newBuilder().build()) 841 .build(); 842 mockLocations.addResponse(expectedResponse); 843 844 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 845 846 Location actualResponse = client.getLocation(request); 847 Assert.assertEquals(expectedResponse, actualResponse); 848 849 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 850 Assert.assertEquals(1, actualRequests.size()); 851 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 852 853 Assert.assertEquals(request.getName(), actualRequest.getName()); 854 Assert.assertTrue( 855 channelProvider.isHeaderSent( 856 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 857 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 858 } 859 860 @Test getLocationExceptionTest()861 public void getLocationExceptionTest() throws Exception { 862 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 863 mockLocations.addException(exception); 864 865 try { 866 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 867 client.getLocation(request); 868 Assert.fail("No exception raised"); 869 } catch (InvalidArgumentException e) { 870 // Expected exception. 871 } 872 } 873 } 874