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.WebhooksClient.ListLocationsPagedResponse; 20 import static com.google.cloud.dialogflow.cx.v3.WebhooksClient.ListWebhooksPagedResponse; 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.HttpJsonWebhooksStub; 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.protobuf.Any; 38 import com.google.protobuf.Duration; 39 import com.google.protobuf.Empty; 40 import com.google.protobuf.FieldMask; 41 import java.io.IOException; 42 import java.util.Arrays; 43 import java.util.HashMap; 44 import java.util.List; 45 import javax.annotation.Generated; 46 import org.junit.After; 47 import org.junit.AfterClass; 48 import org.junit.Assert; 49 import org.junit.Before; 50 import org.junit.BeforeClass; 51 import org.junit.Test; 52 53 @Generated("by gapic-generator-java") 54 public class WebhooksClientHttpJsonTest { 55 private static MockHttpService mockService; 56 private static WebhooksClient client; 57 58 @BeforeClass startStaticServer()59 public static void startStaticServer() throws IOException { 60 mockService = 61 new MockHttpService( 62 HttpJsonWebhooksStub.getMethodDescriptors(), WebhooksSettings.getDefaultEndpoint()); 63 WebhooksSettings settings = 64 WebhooksSettings.newHttpJsonBuilder() 65 .setTransportChannelProvider( 66 WebhooksSettings.defaultHttpJsonTransportProviderBuilder() 67 .setHttpTransport(mockService) 68 .build()) 69 .setCredentialsProvider(NoCredentialsProvider.create()) 70 .build(); 71 client = WebhooksClient.create(settings); 72 } 73 74 @AfterClass stopServer()75 public static void stopServer() { 76 client.close(); 77 } 78 79 @Before setUp()80 public void setUp() {} 81 82 @After tearDown()83 public void tearDown() throws Exception { 84 mockService.reset(); 85 } 86 87 @Test listWebhooksTest()88 public void listWebhooksTest() throws Exception { 89 Webhook responsesElement = Webhook.newBuilder().build(); 90 ListWebhooksResponse expectedResponse = 91 ListWebhooksResponse.newBuilder() 92 .setNextPageToken("") 93 .addAllWebhooks(Arrays.asList(responsesElement)) 94 .build(); 95 mockService.addResponse(expectedResponse); 96 97 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 98 99 ListWebhooksPagedResponse pagedListResponse = client.listWebhooks(parent); 100 101 List<Webhook> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 102 103 Assert.assertEquals(1, resources.size()); 104 Assert.assertEquals(expectedResponse.getWebhooksList().get(0), resources.get(0)); 105 106 List<String> actualRequests = mockService.getRequestPaths(); 107 Assert.assertEquals(1, actualRequests.size()); 108 109 String apiClientHeaderKey = 110 mockService 111 .getRequestHeaders() 112 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 113 .iterator() 114 .next(); 115 Assert.assertTrue( 116 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 117 .matcher(apiClientHeaderKey) 118 .matches()); 119 } 120 121 @Test listWebhooksExceptionTest()122 public void listWebhooksExceptionTest() throws Exception { 123 ApiException exception = 124 ApiExceptionFactory.createException( 125 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 126 mockService.addException(exception); 127 128 try { 129 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 130 client.listWebhooks(parent); 131 Assert.fail("No exception raised"); 132 } catch (InvalidArgumentException e) { 133 // Expected exception. 134 } 135 } 136 137 @Test listWebhooksTest2()138 public void listWebhooksTest2() throws Exception { 139 Webhook responsesElement = Webhook.newBuilder().build(); 140 ListWebhooksResponse expectedResponse = 141 ListWebhooksResponse.newBuilder() 142 .setNextPageToken("") 143 .addAllWebhooks(Arrays.asList(responsesElement)) 144 .build(); 145 mockService.addResponse(expectedResponse); 146 147 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 148 149 ListWebhooksPagedResponse pagedListResponse = client.listWebhooks(parent); 150 151 List<Webhook> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 152 153 Assert.assertEquals(1, resources.size()); 154 Assert.assertEquals(expectedResponse.getWebhooksList().get(0), resources.get(0)); 155 156 List<String> actualRequests = mockService.getRequestPaths(); 157 Assert.assertEquals(1, actualRequests.size()); 158 159 String apiClientHeaderKey = 160 mockService 161 .getRequestHeaders() 162 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 163 .iterator() 164 .next(); 165 Assert.assertTrue( 166 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 167 .matcher(apiClientHeaderKey) 168 .matches()); 169 } 170 171 @Test listWebhooksExceptionTest2()172 public void listWebhooksExceptionTest2() throws Exception { 173 ApiException exception = 174 ApiExceptionFactory.createException( 175 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 176 mockService.addException(exception); 177 178 try { 179 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 180 client.listWebhooks(parent); 181 Assert.fail("No exception raised"); 182 } catch (InvalidArgumentException e) { 183 // Expected exception. 184 } 185 } 186 187 @Test getWebhookTest()188 public void getWebhookTest() throws Exception { 189 Webhook expectedResponse = 190 Webhook.newBuilder() 191 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 192 .setDisplayName("displayName1714148973") 193 .setTimeout(Duration.newBuilder().build()) 194 .setDisabled(true) 195 .build(); 196 mockService.addResponse(expectedResponse); 197 198 WebhookName name = WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]"); 199 200 Webhook actualResponse = client.getWebhook(name); 201 Assert.assertEquals(expectedResponse, actualResponse); 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 getWebhookExceptionTest()219 public void getWebhookExceptionTest() 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 WebhookName name = WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]"); 227 client.getWebhook(name); 228 Assert.fail("No exception raised"); 229 } catch (InvalidArgumentException e) { 230 // Expected exception. 231 } 232 } 233 234 @Test getWebhookTest2()235 public void getWebhookTest2() throws Exception { 236 Webhook expectedResponse = 237 Webhook.newBuilder() 238 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 239 .setDisplayName("displayName1714148973") 240 .setTimeout(Duration.newBuilder().build()) 241 .setDisabled(true) 242 .build(); 243 mockService.addResponse(expectedResponse); 244 245 String name = 246 "projects/project-8440/locations/location-8440/agents/agent-8440/webhooks/webhook-8440"; 247 248 Webhook actualResponse = client.getWebhook(name); 249 Assert.assertEquals(expectedResponse, actualResponse); 250 251 List<String> actualRequests = mockService.getRequestPaths(); 252 Assert.assertEquals(1, actualRequests.size()); 253 254 String apiClientHeaderKey = 255 mockService 256 .getRequestHeaders() 257 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 258 .iterator() 259 .next(); 260 Assert.assertTrue( 261 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 262 .matcher(apiClientHeaderKey) 263 .matches()); 264 } 265 266 @Test getWebhookExceptionTest2()267 public void getWebhookExceptionTest2() throws Exception { 268 ApiException exception = 269 ApiExceptionFactory.createException( 270 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 271 mockService.addException(exception); 272 273 try { 274 String name = 275 "projects/project-8440/locations/location-8440/agents/agent-8440/webhooks/webhook-8440"; 276 client.getWebhook(name); 277 Assert.fail("No exception raised"); 278 } catch (InvalidArgumentException e) { 279 // Expected exception. 280 } 281 } 282 283 @Test createWebhookTest()284 public void createWebhookTest() throws Exception { 285 Webhook expectedResponse = 286 Webhook.newBuilder() 287 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 288 .setDisplayName("displayName1714148973") 289 .setTimeout(Duration.newBuilder().build()) 290 .setDisabled(true) 291 .build(); 292 mockService.addResponse(expectedResponse); 293 294 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 295 Webhook webhook = Webhook.newBuilder().build(); 296 297 Webhook actualResponse = client.createWebhook(parent, webhook); 298 Assert.assertEquals(expectedResponse, actualResponse); 299 300 List<String> actualRequests = mockService.getRequestPaths(); 301 Assert.assertEquals(1, actualRequests.size()); 302 303 String apiClientHeaderKey = 304 mockService 305 .getRequestHeaders() 306 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 307 .iterator() 308 .next(); 309 Assert.assertTrue( 310 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 311 .matcher(apiClientHeaderKey) 312 .matches()); 313 } 314 315 @Test createWebhookExceptionTest()316 public void createWebhookExceptionTest() throws Exception { 317 ApiException exception = 318 ApiExceptionFactory.createException( 319 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 320 mockService.addException(exception); 321 322 try { 323 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 324 Webhook webhook = Webhook.newBuilder().build(); 325 client.createWebhook(parent, webhook); 326 Assert.fail("No exception raised"); 327 } catch (InvalidArgumentException e) { 328 // Expected exception. 329 } 330 } 331 332 @Test createWebhookTest2()333 public void createWebhookTest2() throws Exception { 334 Webhook expectedResponse = 335 Webhook.newBuilder() 336 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 337 .setDisplayName("displayName1714148973") 338 .setTimeout(Duration.newBuilder().build()) 339 .setDisabled(true) 340 .build(); 341 mockService.addResponse(expectedResponse); 342 343 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 344 Webhook webhook = Webhook.newBuilder().build(); 345 346 Webhook actualResponse = client.createWebhook(parent, webhook); 347 Assert.assertEquals(expectedResponse, actualResponse); 348 349 List<String> actualRequests = mockService.getRequestPaths(); 350 Assert.assertEquals(1, actualRequests.size()); 351 352 String apiClientHeaderKey = 353 mockService 354 .getRequestHeaders() 355 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 356 .iterator() 357 .next(); 358 Assert.assertTrue( 359 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 360 .matcher(apiClientHeaderKey) 361 .matches()); 362 } 363 364 @Test createWebhookExceptionTest2()365 public void createWebhookExceptionTest2() throws Exception { 366 ApiException exception = 367 ApiExceptionFactory.createException( 368 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 369 mockService.addException(exception); 370 371 try { 372 String parent = "projects/project-1167/locations/location-1167/agents/agent-1167"; 373 Webhook webhook = Webhook.newBuilder().build(); 374 client.createWebhook(parent, webhook); 375 Assert.fail("No exception raised"); 376 } catch (InvalidArgumentException e) { 377 // Expected exception. 378 } 379 } 380 381 @Test updateWebhookTest()382 public void updateWebhookTest() throws Exception { 383 Webhook expectedResponse = 384 Webhook.newBuilder() 385 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 386 .setDisplayName("displayName1714148973") 387 .setTimeout(Duration.newBuilder().build()) 388 .setDisabled(true) 389 .build(); 390 mockService.addResponse(expectedResponse); 391 392 Webhook webhook = 393 Webhook.newBuilder() 394 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 395 .setDisplayName("displayName1714148973") 396 .setTimeout(Duration.newBuilder().build()) 397 .setDisabled(true) 398 .build(); 399 FieldMask updateMask = FieldMask.newBuilder().build(); 400 401 Webhook actualResponse = client.updateWebhook(webhook, updateMask); 402 Assert.assertEquals(expectedResponse, actualResponse); 403 404 List<String> actualRequests = mockService.getRequestPaths(); 405 Assert.assertEquals(1, actualRequests.size()); 406 407 String apiClientHeaderKey = 408 mockService 409 .getRequestHeaders() 410 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 411 .iterator() 412 .next(); 413 Assert.assertTrue( 414 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 415 .matcher(apiClientHeaderKey) 416 .matches()); 417 } 418 419 @Test updateWebhookExceptionTest()420 public void updateWebhookExceptionTest() throws Exception { 421 ApiException exception = 422 ApiExceptionFactory.createException( 423 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 424 mockService.addException(exception); 425 426 try { 427 Webhook webhook = 428 Webhook.newBuilder() 429 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 430 .setDisplayName("displayName1714148973") 431 .setTimeout(Duration.newBuilder().build()) 432 .setDisabled(true) 433 .build(); 434 FieldMask updateMask = FieldMask.newBuilder().build(); 435 client.updateWebhook(webhook, updateMask); 436 Assert.fail("No exception raised"); 437 } catch (InvalidArgumentException e) { 438 // Expected exception. 439 } 440 } 441 442 @Test deleteWebhookTest()443 public void deleteWebhookTest() throws Exception { 444 Empty expectedResponse = Empty.newBuilder().build(); 445 mockService.addResponse(expectedResponse); 446 447 WebhookName name = WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]"); 448 449 client.deleteWebhook(name); 450 451 List<String> actualRequests = mockService.getRequestPaths(); 452 Assert.assertEquals(1, actualRequests.size()); 453 454 String apiClientHeaderKey = 455 mockService 456 .getRequestHeaders() 457 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 458 .iterator() 459 .next(); 460 Assert.assertTrue( 461 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 462 .matcher(apiClientHeaderKey) 463 .matches()); 464 } 465 466 @Test deleteWebhookExceptionTest()467 public void deleteWebhookExceptionTest() throws Exception { 468 ApiException exception = 469 ApiExceptionFactory.createException( 470 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 471 mockService.addException(exception); 472 473 try { 474 WebhookName name = WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]"); 475 client.deleteWebhook(name); 476 Assert.fail("No exception raised"); 477 } catch (InvalidArgumentException e) { 478 // Expected exception. 479 } 480 } 481 482 @Test deleteWebhookTest2()483 public void deleteWebhookTest2() throws Exception { 484 Empty expectedResponse = Empty.newBuilder().build(); 485 mockService.addResponse(expectedResponse); 486 487 String name = 488 "projects/project-8440/locations/location-8440/agents/agent-8440/webhooks/webhook-8440"; 489 490 client.deleteWebhook(name); 491 492 List<String> actualRequests = mockService.getRequestPaths(); 493 Assert.assertEquals(1, actualRequests.size()); 494 495 String apiClientHeaderKey = 496 mockService 497 .getRequestHeaders() 498 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 499 .iterator() 500 .next(); 501 Assert.assertTrue( 502 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 503 .matcher(apiClientHeaderKey) 504 .matches()); 505 } 506 507 @Test deleteWebhookExceptionTest2()508 public void deleteWebhookExceptionTest2() throws Exception { 509 ApiException exception = 510 ApiExceptionFactory.createException( 511 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 512 mockService.addException(exception); 513 514 try { 515 String name = 516 "projects/project-8440/locations/location-8440/agents/agent-8440/webhooks/webhook-8440"; 517 client.deleteWebhook(name); 518 Assert.fail("No exception raised"); 519 } catch (InvalidArgumentException e) { 520 // Expected exception. 521 } 522 } 523 524 @Test listLocationsTest()525 public void listLocationsTest() throws Exception { 526 Location responsesElement = Location.newBuilder().build(); 527 ListLocationsResponse expectedResponse = 528 ListLocationsResponse.newBuilder() 529 .setNextPageToken("") 530 .addAllLocations(Arrays.asList(responsesElement)) 531 .build(); 532 mockService.addResponse(expectedResponse); 533 534 ListLocationsRequest request = 535 ListLocationsRequest.newBuilder() 536 .setName("projects/project-3664") 537 .setFilter("filter-1274492040") 538 .setPageSize(883849137) 539 .setPageToken("pageToken873572522") 540 .build(); 541 542 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 543 544 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 545 546 Assert.assertEquals(1, resources.size()); 547 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 548 549 List<String> actualRequests = mockService.getRequestPaths(); 550 Assert.assertEquals(1, actualRequests.size()); 551 552 String apiClientHeaderKey = 553 mockService 554 .getRequestHeaders() 555 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 556 .iterator() 557 .next(); 558 Assert.assertTrue( 559 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 560 .matcher(apiClientHeaderKey) 561 .matches()); 562 } 563 564 @Test listLocationsExceptionTest()565 public void listLocationsExceptionTest() throws Exception { 566 ApiException exception = 567 ApiExceptionFactory.createException( 568 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 569 mockService.addException(exception); 570 571 try { 572 ListLocationsRequest request = 573 ListLocationsRequest.newBuilder() 574 .setName("projects/project-3664") 575 .setFilter("filter-1274492040") 576 .setPageSize(883849137) 577 .setPageToken("pageToken873572522") 578 .build(); 579 client.listLocations(request); 580 Assert.fail("No exception raised"); 581 } catch (InvalidArgumentException e) { 582 // Expected exception. 583 } 584 } 585 586 @Test getLocationTest()587 public void getLocationTest() throws Exception { 588 Location expectedResponse = 589 Location.newBuilder() 590 .setName("name3373707") 591 .setLocationId("locationId1541836720") 592 .setDisplayName("displayName1714148973") 593 .putAllLabels(new HashMap<String, String>()) 594 .setMetadata(Any.newBuilder().build()) 595 .build(); 596 mockService.addResponse(expectedResponse); 597 598 GetLocationRequest request = 599 GetLocationRequest.newBuilder() 600 .setName("projects/project-9062/locations/location-9062") 601 .build(); 602 603 Location actualResponse = client.getLocation(request); 604 Assert.assertEquals(expectedResponse, actualResponse); 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 getLocationExceptionTest()622 public void getLocationExceptionTest() 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 GetLocationRequest request = 630 GetLocationRequest.newBuilder() 631 .setName("projects/project-9062/locations/location-9062") 632 .build(); 633 client.getLocation(request); 634 Assert.fail("No exception raised"); 635 } catch (InvalidArgumentException e) { 636 // Expected exception. 637 } 638 } 639 } 640