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.v2; 18 19 import static com.google.cloud.dialogflow.v2.VersionsClient.ListLocationsPagedResponse; 20 import static com.google.cloud.dialogflow.v2.VersionsClient.ListVersionsPagedResponse; 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.Empty; 37 import com.google.protobuf.FieldMask; 38 import com.google.protobuf.Timestamp; 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 VersionsClientTest { 55 private static MockLocations mockLocations; 56 private static MockServiceHelper mockServiceHelper; 57 private static MockVersions mockVersions; 58 private LocalChannelProvider channelProvider; 59 private VersionsClient client; 60 61 @BeforeClass startStaticServer()62 public static void startStaticServer() { 63 mockVersions = new MockVersions(); 64 mockLocations = new MockLocations(); 65 mockServiceHelper = 66 new MockServiceHelper( 67 UUID.randomUUID().toString(), 68 Arrays.<MockGrpcService>asList(mockVersions, 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 VersionsSettings settings = 82 VersionsSettings.newBuilder() 83 .setTransportChannelProvider(channelProvider) 84 .setCredentialsProvider(NoCredentialsProvider.create()) 85 .build(); 86 client = VersionsClient.create(settings); 87 } 88 89 @After tearDown()90 public void tearDown() throws Exception { 91 client.close(); 92 } 93 94 @Test listVersionsTest()95 public void listVersionsTest() throws Exception { 96 Version responsesElement = Version.newBuilder().build(); 97 ListVersionsResponse expectedResponse = 98 ListVersionsResponse.newBuilder() 99 .setNextPageToken("") 100 .addAllVersions(Arrays.asList(responsesElement)) 101 .build(); 102 mockVersions.addResponse(expectedResponse); 103 104 AgentName parent = AgentName.ofProjectName("[PROJECT]"); 105 106 ListVersionsPagedResponse pagedListResponse = client.listVersions(parent); 107 108 List<Version> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 109 110 Assert.assertEquals(1, resources.size()); 111 Assert.assertEquals(expectedResponse.getVersionsList().get(0), resources.get(0)); 112 113 List<AbstractMessage> actualRequests = mockVersions.getRequests(); 114 Assert.assertEquals(1, actualRequests.size()); 115 ListVersionsRequest actualRequest = ((ListVersionsRequest) 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 listVersionsExceptionTest()125 public void listVersionsExceptionTest() throws Exception { 126 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 127 mockVersions.addException(exception); 128 129 try { 130 AgentName parent = AgentName.ofProjectName("[PROJECT]"); 131 client.listVersions(parent); 132 Assert.fail("No exception raised"); 133 } catch (InvalidArgumentException e) { 134 // Expected exception. 135 } 136 } 137 138 @Test listVersionsTest2()139 public void listVersionsTest2() throws Exception { 140 Version responsesElement = Version.newBuilder().build(); 141 ListVersionsResponse expectedResponse = 142 ListVersionsResponse.newBuilder() 143 .setNextPageToken("") 144 .addAllVersions(Arrays.asList(responsesElement)) 145 .build(); 146 mockVersions.addResponse(expectedResponse); 147 148 String parent = "parent-995424086"; 149 150 ListVersionsPagedResponse pagedListResponse = client.listVersions(parent); 151 152 List<Version> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 153 154 Assert.assertEquals(1, resources.size()); 155 Assert.assertEquals(expectedResponse.getVersionsList().get(0), resources.get(0)); 156 157 List<AbstractMessage> actualRequests = mockVersions.getRequests(); 158 Assert.assertEquals(1, actualRequests.size()); 159 ListVersionsRequest actualRequest = ((ListVersionsRequest) 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 listVersionsExceptionTest2()169 public void listVersionsExceptionTest2() throws Exception { 170 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 171 mockVersions.addException(exception); 172 173 try { 174 String parent = "parent-995424086"; 175 client.listVersions(parent); 176 Assert.fail("No exception raised"); 177 } catch (InvalidArgumentException e) { 178 // Expected exception. 179 } 180 } 181 182 @Test getVersionTest()183 public void getVersionTest() throws Exception { 184 Version expectedResponse = 185 Version.newBuilder() 186 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 187 .setDescription("description-1724546052") 188 .setVersionNumber(135927952) 189 .setCreateTime(Timestamp.newBuilder().build()) 190 .build(); 191 mockVersions.addResponse(expectedResponse); 192 193 VersionName name = VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]"); 194 195 Version actualResponse = client.getVersion(name); 196 Assert.assertEquals(expectedResponse, actualResponse); 197 198 List<AbstractMessage> actualRequests = mockVersions.getRequests(); 199 Assert.assertEquals(1, actualRequests.size()); 200 GetVersionRequest actualRequest = ((GetVersionRequest) 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 getVersionExceptionTest()210 public void getVersionExceptionTest() throws Exception { 211 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 212 mockVersions.addException(exception); 213 214 try { 215 VersionName name = VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]"); 216 client.getVersion(name); 217 Assert.fail("No exception raised"); 218 } catch (InvalidArgumentException e) { 219 // Expected exception. 220 } 221 } 222 223 @Test getVersionTest2()224 public void getVersionTest2() throws Exception { 225 Version expectedResponse = 226 Version.newBuilder() 227 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 228 .setDescription("description-1724546052") 229 .setVersionNumber(135927952) 230 .setCreateTime(Timestamp.newBuilder().build()) 231 .build(); 232 mockVersions.addResponse(expectedResponse); 233 234 String name = "name3373707"; 235 236 Version actualResponse = client.getVersion(name); 237 Assert.assertEquals(expectedResponse, actualResponse); 238 239 List<AbstractMessage> actualRequests = mockVersions.getRequests(); 240 Assert.assertEquals(1, actualRequests.size()); 241 GetVersionRequest actualRequest = ((GetVersionRequest) 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 getVersionExceptionTest2()251 public void getVersionExceptionTest2() throws Exception { 252 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 253 mockVersions.addException(exception); 254 255 try { 256 String name = "name3373707"; 257 client.getVersion(name); 258 Assert.fail("No exception raised"); 259 } catch (InvalidArgumentException e) { 260 // Expected exception. 261 } 262 } 263 264 @Test createVersionTest()265 public void createVersionTest() throws Exception { 266 Version expectedResponse = 267 Version.newBuilder() 268 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 269 .setDescription("description-1724546052") 270 .setVersionNumber(135927952) 271 .setCreateTime(Timestamp.newBuilder().build()) 272 .build(); 273 mockVersions.addResponse(expectedResponse); 274 275 AgentName parent = AgentName.ofProjectName("[PROJECT]"); 276 Version version = Version.newBuilder().build(); 277 278 Version actualResponse = client.createVersion(parent, version); 279 Assert.assertEquals(expectedResponse, actualResponse); 280 281 List<AbstractMessage> actualRequests = mockVersions.getRequests(); 282 Assert.assertEquals(1, actualRequests.size()); 283 CreateVersionRequest actualRequest = ((CreateVersionRequest) actualRequests.get(0)); 284 285 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 286 Assert.assertEquals(version, actualRequest.getVersion()); 287 Assert.assertTrue( 288 channelProvider.isHeaderSent( 289 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 290 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 291 } 292 293 @Test createVersionExceptionTest()294 public void createVersionExceptionTest() throws Exception { 295 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 296 mockVersions.addException(exception); 297 298 try { 299 AgentName parent = AgentName.ofProjectName("[PROJECT]"); 300 Version version = Version.newBuilder().build(); 301 client.createVersion(parent, version); 302 Assert.fail("No exception raised"); 303 } catch (InvalidArgumentException e) { 304 // Expected exception. 305 } 306 } 307 308 @Test createVersionTest2()309 public void createVersionTest2() throws Exception { 310 Version expectedResponse = 311 Version.newBuilder() 312 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 313 .setDescription("description-1724546052") 314 .setVersionNumber(135927952) 315 .setCreateTime(Timestamp.newBuilder().build()) 316 .build(); 317 mockVersions.addResponse(expectedResponse); 318 319 String parent = "parent-995424086"; 320 Version version = Version.newBuilder().build(); 321 322 Version actualResponse = client.createVersion(parent, version); 323 Assert.assertEquals(expectedResponse, actualResponse); 324 325 List<AbstractMessage> actualRequests = mockVersions.getRequests(); 326 Assert.assertEquals(1, actualRequests.size()); 327 CreateVersionRequest actualRequest = ((CreateVersionRequest) actualRequests.get(0)); 328 329 Assert.assertEquals(parent, actualRequest.getParent()); 330 Assert.assertEquals(version, actualRequest.getVersion()); 331 Assert.assertTrue( 332 channelProvider.isHeaderSent( 333 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 334 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 335 } 336 337 @Test createVersionExceptionTest2()338 public void createVersionExceptionTest2() throws Exception { 339 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 340 mockVersions.addException(exception); 341 342 try { 343 String parent = "parent-995424086"; 344 Version version = Version.newBuilder().build(); 345 client.createVersion(parent, version); 346 Assert.fail("No exception raised"); 347 } catch (InvalidArgumentException e) { 348 // Expected exception. 349 } 350 } 351 352 @Test updateVersionTest()353 public void updateVersionTest() throws Exception { 354 Version expectedResponse = 355 Version.newBuilder() 356 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 357 .setDescription("description-1724546052") 358 .setVersionNumber(135927952) 359 .setCreateTime(Timestamp.newBuilder().build()) 360 .build(); 361 mockVersions.addResponse(expectedResponse); 362 363 Version version = Version.newBuilder().build(); 364 FieldMask updateMask = FieldMask.newBuilder().build(); 365 366 Version actualResponse = client.updateVersion(version, updateMask); 367 Assert.assertEquals(expectedResponse, actualResponse); 368 369 List<AbstractMessage> actualRequests = mockVersions.getRequests(); 370 Assert.assertEquals(1, actualRequests.size()); 371 UpdateVersionRequest actualRequest = ((UpdateVersionRequest) actualRequests.get(0)); 372 373 Assert.assertEquals(version, actualRequest.getVersion()); 374 Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); 375 Assert.assertTrue( 376 channelProvider.isHeaderSent( 377 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 378 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 379 } 380 381 @Test updateVersionExceptionTest()382 public void updateVersionExceptionTest() throws Exception { 383 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 384 mockVersions.addException(exception); 385 386 try { 387 Version version = Version.newBuilder().build(); 388 FieldMask updateMask = FieldMask.newBuilder().build(); 389 client.updateVersion(version, updateMask); 390 Assert.fail("No exception raised"); 391 } catch (InvalidArgumentException e) { 392 // Expected exception. 393 } 394 } 395 396 @Test deleteVersionTest()397 public void deleteVersionTest() throws Exception { 398 Empty expectedResponse = Empty.newBuilder().build(); 399 mockVersions.addResponse(expectedResponse); 400 401 VersionName name = VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]"); 402 403 client.deleteVersion(name); 404 405 List<AbstractMessage> actualRequests = mockVersions.getRequests(); 406 Assert.assertEquals(1, actualRequests.size()); 407 DeleteVersionRequest actualRequest = ((DeleteVersionRequest) 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 deleteVersionExceptionTest()417 public void deleteVersionExceptionTest() throws Exception { 418 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 419 mockVersions.addException(exception); 420 421 try { 422 VersionName name = VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]"); 423 client.deleteVersion(name); 424 Assert.fail("No exception raised"); 425 } catch (InvalidArgumentException e) { 426 // Expected exception. 427 } 428 } 429 430 @Test deleteVersionTest2()431 public void deleteVersionTest2() throws Exception { 432 Empty expectedResponse = Empty.newBuilder().build(); 433 mockVersions.addResponse(expectedResponse); 434 435 String name = "name3373707"; 436 437 client.deleteVersion(name); 438 439 List<AbstractMessage> actualRequests = mockVersions.getRequests(); 440 Assert.assertEquals(1, actualRequests.size()); 441 DeleteVersionRequest actualRequest = ((DeleteVersionRequest) 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 deleteVersionExceptionTest2()451 public void deleteVersionExceptionTest2() throws Exception { 452 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 453 mockVersions.addException(exception); 454 455 try { 456 String name = "name3373707"; 457 client.deleteVersion(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