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.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.cloud.location.GetLocationRequest; 30 import com.google.cloud.location.ListLocationsRequest; 31 import com.google.cloud.location.ListLocationsResponse; 32 import com.google.cloud.location.Location; 33 import com.google.common.collect.Lists; 34 import com.google.protobuf.AbstractMessage; 35 import com.google.protobuf.Any; 36 import com.google.protobuf.Duration; 37 import com.google.protobuf.Empty; 38 import com.google.protobuf.FieldMask; 39 import io.grpc.StatusRuntimeException; 40 import java.io.IOException; 41 import java.util.Arrays; 42 import java.util.HashMap; 43 import java.util.List; 44 import java.util.UUID; 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 WebhooksClientTest { 55 private static MockLocations mockLocations; 56 private static MockServiceHelper mockServiceHelper; 57 private static MockWebhooks mockWebhooks; 58 private LocalChannelProvider channelProvider; 59 private WebhooksClient client; 60 61 @BeforeClass startStaticServer()62 public static void startStaticServer() { 63 mockWebhooks = new MockWebhooks(); 64 mockLocations = new MockLocations(); 65 mockServiceHelper = 66 new MockServiceHelper( 67 UUID.randomUUID().toString(), 68 Arrays.<MockGrpcService>asList(mockWebhooks, mockLocations)); 69 mockServiceHelper.start(); 70 } 71 72 @AfterClass stopServer()73 public static void stopServer() { 74 mockServiceHelper.stop(); 75 } 76 77 @Before setUp()78 public void setUp() throws IOException { 79 mockServiceHelper.reset(); 80 channelProvider = mockServiceHelper.createChannelProvider(); 81 WebhooksSettings settings = 82 WebhooksSettings.newBuilder() 83 .setTransportChannelProvider(channelProvider) 84 .setCredentialsProvider(NoCredentialsProvider.create()) 85 .build(); 86 client = WebhooksClient.create(settings); 87 } 88 89 @After tearDown()90 public void tearDown() throws Exception { 91 client.close(); 92 } 93 94 @Test listWebhooksTest()95 public void listWebhooksTest() throws Exception { 96 Webhook responsesElement = Webhook.newBuilder().build(); 97 ListWebhooksResponse expectedResponse = 98 ListWebhooksResponse.newBuilder() 99 .setNextPageToken("") 100 .addAllWebhooks(Arrays.asList(responsesElement)) 101 .build(); 102 mockWebhooks.addResponse(expectedResponse); 103 104 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 105 106 ListWebhooksPagedResponse pagedListResponse = client.listWebhooks(parent); 107 108 List<Webhook> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 109 110 Assert.assertEquals(1, resources.size()); 111 Assert.assertEquals(expectedResponse.getWebhooksList().get(0), resources.get(0)); 112 113 List<AbstractMessage> actualRequests = mockWebhooks.getRequests(); 114 Assert.assertEquals(1, actualRequests.size()); 115 ListWebhooksRequest actualRequest = ((ListWebhooksRequest) actualRequests.get(0)); 116 117 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 118 Assert.assertTrue( 119 channelProvider.isHeaderSent( 120 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 121 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 122 } 123 124 @Test listWebhooksExceptionTest()125 public void listWebhooksExceptionTest() throws Exception { 126 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 127 mockWebhooks.addException(exception); 128 129 try { 130 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 131 client.listWebhooks(parent); 132 Assert.fail("No exception raised"); 133 } catch (InvalidArgumentException e) { 134 // Expected exception. 135 } 136 } 137 138 @Test listWebhooksTest2()139 public void listWebhooksTest2() throws Exception { 140 Webhook responsesElement = Webhook.newBuilder().build(); 141 ListWebhooksResponse expectedResponse = 142 ListWebhooksResponse.newBuilder() 143 .setNextPageToken("") 144 .addAllWebhooks(Arrays.asList(responsesElement)) 145 .build(); 146 mockWebhooks.addResponse(expectedResponse); 147 148 String parent = "parent-995424086"; 149 150 ListWebhooksPagedResponse pagedListResponse = client.listWebhooks(parent); 151 152 List<Webhook> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 153 154 Assert.assertEquals(1, resources.size()); 155 Assert.assertEquals(expectedResponse.getWebhooksList().get(0), resources.get(0)); 156 157 List<AbstractMessage> actualRequests = mockWebhooks.getRequests(); 158 Assert.assertEquals(1, actualRequests.size()); 159 ListWebhooksRequest actualRequest = ((ListWebhooksRequest) actualRequests.get(0)); 160 161 Assert.assertEquals(parent, actualRequest.getParent()); 162 Assert.assertTrue( 163 channelProvider.isHeaderSent( 164 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 165 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 166 } 167 168 @Test listWebhooksExceptionTest2()169 public void listWebhooksExceptionTest2() throws Exception { 170 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 171 mockWebhooks.addException(exception); 172 173 try { 174 String parent = "parent-995424086"; 175 client.listWebhooks(parent); 176 Assert.fail("No exception raised"); 177 } catch (InvalidArgumentException e) { 178 // Expected exception. 179 } 180 } 181 182 @Test getWebhookTest()183 public void getWebhookTest() throws Exception { 184 Webhook expectedResponse = 185 Webhook.newBuilder() 186 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 187 .setDisplayName("displayName1714148973") 188 .setTimeout(Duration.newBuilder().build()) 189 .setDisabled(true) 190 .build(); 191 mockWebhooks.addResponse(expectedResponse); 192 193 WebhookName name = WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]"); 194 195 Webhook actualResponse = client.getWebhook(name); 196 Assert.assertEquals(expectedResponse, actualResponse); 197 198 List<AbstractMessage> actualRequests = mockWebhooks.getRequests(); 199 Assert.assertEquals(1, actualRequests.size()); 200 GetWebhookRequest actualRequest = ((GetWebhookRequest) actualRequests.get(0)); 201 202 Assert.assertEquals(name.toString(), actualRequest.getName()); 203 Assert.assertTrue( 204 channelProvider.isHeaderSent( 205 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 206 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 207 } 208 209 @Test getWebhookExceptionTest()210 public void getWebhookExceptionTest() throws Exception { 211 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 212 mockWebhooks.addException(exception); 213 214 try { 215 WebhookName name = WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]"); 216 client.getWebhook(name); 217 Assert.fail("No exception raised"); 218 } catch (InvalidArgumentException e) { 219 // Expected exception. 220 } 221 } 222 223 @Test getWebhookTest2()224 public void getWebhookTest2() throws Exception { 225 Webhook expectedResponse = 226 Webhook.newBuilder() 227 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 228 .setDisplayName("displayName1714148973") 229 .setTimeout(Duration.newBuilder().build()) 230 .setDisabled(true) 231 .build(); 232 mockWebhooks.addResponse(expectedResponse); 233 234 String name = "name3373707"; 235 236 Webhook actualResponse = client.getWebhook(name); 237 Assert.assertEquals(expectedResponse, actualResponse); 238 239 List<AbstractMessage> actualRequests = mockWebhooks.getRequests(); 240 Assert.assertEquals(1, actualRequests.size()); 241 GetWebhookRequest actualRequest = ((GetWebhookRequest) actualRequests.get(0)); 242 243 Assert.assertEquals(name, actualRequest.getName()); 244 Assert.assertTrue( 245 channelProvider.isHeaderSent( 246 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 247 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 248 } 249 250 @Test getWebhookExceptionTest2()251 public void getWebhookExceptionTest2() throws Exception { 252 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 253 mockWebhooks.addException(exception); 254 255 try { 256 String name = "name3373707"; 257 client.getWebhook(name); 258 Assert.fail("No exception raised"); 259 } catch (InvalidArgumentException e) { 260 // Expected exception. 261 } 262 } 263 264 @Test createWebhookTest()265 public void createWebhookTest() throws Exception { 266 Webhook expectedResponse = 267 Webhook.newBuilder() 268 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 269 .setDisplayName("displayName1714148973") 270 .setTimeout(Duration.newBuilder().build()) 271 .setDisabled(true) 272 .build(); 273 mockWebhooks.addResponse(expectedResponse); 274 275 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 276 Webhook webhook = Webhook.newBuilder().build(); 277 278 Webhook actualResponse = client.createWebhook(parent, webhook); 279 Assert.assertEquals(expectedResponse, actualResponse); 280 281 List<AbstractMessage> actualRequests = mockWebhooks.getRequests(); 282 Assert.assertEquals(1, actualRequests.size()); 283 CreateWebhookRequest actualRequest = ((CreateWebhookRequest) actualRequests.get(0)); 284 285 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 286 Assert.assertEquals(webhook, actualRequest.getWebhook()); 287 Assert.assertTrue( 288 channelProvider.isHeaderSent( 289 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 290 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 291 } 292 293 @Test createWebhookExceptionTest()294 public void createWebhookExceptionTest() throws Exception { 295 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 296 mockWebhooks.addException(exception); 297 298 try { 299 AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]"); 300 Webhook webhook = Webhook.newBuilder().build(); 301 client.createWebhook(parent, webhook); 302 Assert.fail("No exception raised"); 303 } catch (InvalidArgumentException e) { 304 // Expected exception. 305 } 306 } 307 308 @Test createWebhookTest2()309 public void createWebhookTest2() throws Exception { 310 Webhook expectedResponse = 311 Webhook.newBuilder() 312 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 313 .setDisplayName("displayName1714148973") 314 .setTimeout(Duration.newBuilder().build()) 315 .setDisabled(true) 316 .build(); 317 mockWebhooks.addResponse(expectedResponse); 318 319 String parent = "parent-995424086"; 320 Webhook webhook = Webhook.newBuilder().build(); 321 322 Webhook actualResponse = client.createWebhook(parent, webhook); 323 Assert.assertEquals(expectedResponse, actualResponse); 324 325 List<AbstractMessage> actualRequests = mockWebhooks.getRequests(); 326 Assert.assertEquals(1, actualRequests.size()); 327 CreateWebhookRequest actualRequest = ((CreateWebhookRequest) actualRequests.get(0)); 328 329 Assert.assertEquals(parent, actualRequest.getParent()); 330 Assert.assertEquals(webhook, actualRequest.getWebhook()); 331 Assert.assertTrue( 332 channelProvider.isHeaderSent( 333 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 334 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 335 } 336 337 @Test createWebhookExceptionTest2()338 public void createWebhookExceptionTest2() throws Exception { 339 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 340 mockWebhooks.addException(exception); 341 342 try { 343 String parent = "parent-995424086"; 344 Webhook webhook = Webhook.newBuilder().build(); 345 client.createWebhook(parent, webhook); 346 Assert.fail("No exception raised"); 347 } catch (InvalidArgumentException e) { 348 // Expected exception. 349 } 350 } 351 352 @Test updateWebhookTest()353 public void updateWebhookTest() throws Exception { 354 Webhook expectedResponse = 355 Webhook.newBuilder() 356 .setName(WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]").toString()) 357 .setDisplayName("displayName1714148973") 358 .setTimeout(Duration.newBuilder().build()) 359 .setDisabled(true) 360 .build(); 361 mockWebhooks.addResponse(expectedResponse); 362 363 Webhook webhook = Webhook.newBuilder().build(); 364 FieldMask updateMask = FieldMask.newBuilder().build(); 365 366 Webhook actualResponse = client.updateWebhook(webhook, updateMask); 367 Assert.assertEquals(expectedResponse, actualResponse); 368 369 List<AbstractMessage> actualRequests = mockWebhooks.getRequests(); 370 Assert.assertEquals(1, actualRequests.size()); 371 UpdateWebhookRequest actualRequest = ((UpdateWebhookRequest) actualRequests.get(0)); 372 373 Assert.assertEquals(webhook, actualRequest.getWebhook()); 374 Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); 375 Assert.assertTrue( 376 channelProvider.isHeaderSent( 377 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 378 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 379 } 380 381 @Test updateWebhookExceptionTest()382 public void updateWebhookExceptionTest() throws Exception { 383 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 384 mockWebhooks.addException(exception); 385 386 try { 387 Webhook webhook = Webhook.newBuilder().build(); 388 FieldMask updateMask = FieldMask.newBuilder().build(); 389 client.updateWebhook(webhook, updateMask); 390 Assert.fail("No exception raised"); 391 } catch (InvalidArgumentException e) { 392 // Expected exception. 393 } 394 } 395 396 @Test deleteWebhookTest()397 public void deleteWebhookTest() throws Exception { 398 Empty expectedResponse = Empty.newBuilder().build(); 399 mockWebhooks.addResponse(expectedResponse); 400 401 WebhookName name = WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]"); 402 403 client.deleteWebhook(name); 404 405 List<AbstractMessage> actualRequests = mockWebhooks.getRequests(); 406 Assert.assertEquals(1, actualRequests.size()); 407 DeleteWebhookRequest actualRequest = ((DeleteWebhookRequest) actualRequests.get(0)); 408 409 Assert.assertEquals(name.toString(), actualRequest.getName()); 410 Assert.assertTrue( 411 channelProvider.isHeaderSent( 412 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 413 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 414 } 415 416 @Test deleteWebhookExceptionTest()417 public void deleteWebhookExceptionTest() throws Exception { 418 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 419 mockWebhooks.addException(exception); 420 421 try { 422 WebhookName name = WebhookName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[WEBHOOK]"); 423 client.deleteWebhook(name); 424 Assert.fail("No exception raised"); 425 } catch (InvalidArgumentException e) { 426 // Expected exception. 427 } 428 } 429 430 @Test deleteWebhookTest2()431 public void deleteWebhookTest2() throws Exception { 432 Empty expectedResponse = Empty.newBuilder().build(); 433 mockWebhooks.addResponse(expectedResponse); 434 435 String name = "name3373707"; 436 437 client.deleteWebhook(name); 438 439 List<AbstractMessage> actualRequests = mockWebhooks.getRequests(); 440 Assert.assertEquals(1, actualRequests.size()); 441 DeleteWebhookRequest actualRequest = ((DeleteWebhookRequest) actualRequests.get(0)); 442 443 Assert.assertEquals(name, actualRequest.getName()); 444 Assert.assertTrue( 445 channelProvider.isHeaderSent( 446 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 447 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 448 } 449 450 @Test deleteWebhookExceptionTest2()451 public void deleteWebhookExceptionTest2() throws Exception { 452 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 453 mockWebhooks.addException(exception); 454 455 try { 456 String name = "name3373707"; 457 client.deleteWebhook(name); 458 Assert.fail("No exception raised"); 459 } catch (InvalidArgumentException e) { 460 // Expected exception. 461 } 462 } 463 464 @Test listLocationsTest()465 public void listLocationsTest() throws Exception { 466 Location responsesElement = Location.newBuilder().build(); 467 ListLocationsResponse expectedResponse = 468 ListLocationsResponse.newBuilder() 469 .setNextPageToken("") 470 .addAllLocations(Arrays.asList(responsesElement)) 471 .build(); 472 mockLocations.addResponse(expectedResponse); 473 474 ListLocationsRequest request = 475 ListLocationsRequest.newBuilder() 476 .setName("name3373707") 477 .setFilter("filter-1274492040") 478 .setPageSize(883849137) 479 .setPageToken("pageToken873572522") 480 .build(); 481 482 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 483 484 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 485 486 Assert.assertEquals(1, resources.size()); 487 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 488 489 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 490 Assert.assertEquals(1, actualRequests.size()); 491 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 492 493 Assert.assertEquals(request.getName(), actualRequest.getName()); 494 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 495 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 496 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 497 Assert.assertTrue( 498 channelProvider.isHeaderSent( 499 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 500 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 501 } 502 503 @Test listLocationsExceptionTest()504 public void listLocationsExceptionTest() throws Exception { 505 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 506 mockLocations.addException(exception); 507 508 try { 509 ListLocationsRequest request = 510 ListLocationsRequest.newBuilder() 511 .setName("name3373707") 512 .setFilter("filter-1274492040") 513 .setPageSize(883849137) 514 .setPageToken("pageToken873572522") 515 .build(); 516 client.listLocations(request); 517 Assert.fail("No exception raised"); 518 } catch (InvalidArgumentException e) { 519 // Expected exception. 520 } 521 } 522 523 @Test getLocationTest()524 public void getLocationTest() throws Exception { 525 Location expectedResponse = 526 Location.newBuilder() 527 .setName("name3373707") 528 .setLocationId("locationId1541836720") 529 .setDisplayName("displayName1714148973") 530 .putAllLabels(new HashMap<String, String>()) 531 .setMetadata(Any.newBuilder().build()) 532 .build(); 533 mockLocations.addResponse(expectedResponse); 534 535 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 536 537 Location actualResponse = client.getLocation(request); 538 Assert.assertEquals(expectedResponse, actualResponse); 539 540 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 541 Assert.assertEquals(1, actualRequests.size()); 542 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 543 544 Assert.assertEquals(request.getName(), actualRequest.getName()); 545 Assert.assertTrue( 546 channelProvider.isHeaderSent( 547 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 548 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 549 } 550 551 @Test getLocationExceptionTest()552 public void getLocationExceptionTest() throws Exception { 553 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 554 mockLocations.addException(exception); 555 556 try { 557 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 558 client.getLocation(request); 559 Assert.fail("No exception raised"); 560 } catch (InvalidArgumentException e) { 561 // Expected exception. 562 } 563 } 564 } 565