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.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.v2.stub.HttpJsonVersionsStub; 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.Empty; 39 import com.google.protobuf.FieldMask; 40 import com.google.protobuf.Timestamp; 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 VersionsClientHttpJsonTest { 55 private static MockHttpService mockService; 56 private static VersionsClient client; 57 58 @BeforeClass startStaticServer()59 public static void startStaticServer() throws IOException { 60 mockService = 61 new MockHttpService( 62 HttpJsonVersionsStub.getMethodDescriptors(), VersionsSettings.getDefaultEndpoint()); 63 VersionsSettings settings = 64 VersionsSettings.newHttpJsonBuilder() 65 .setTransportChannelProvider( 66 VersionsSettings.defaultHttpJsonTransportProviderBuilder() 67 .setHttpTransport(mockService) 68 .build()) 69 .setCredentialsProvider(NoCredentialsProvider.create()) 70 .build(); 71 client = VersionsClient.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 listVersionsTest()88 public void listVersionsTest() throws Exception { 89 Version responsesElement = Version.newBuilder().build(); 90 ListVersionsResponse expectedResponse = 91 ListVersionsResponse.newBuilder() 92 .setNextPageToken("") 93 .addAllVersions(Arrays.asList(responsesElement)) 94 .build(); 95 mockService.addResponse(expectedResponse); 96 97 AgentName parent = AgentName.ofProjectName("[PROJECT]"); 98 99 ListVersionsPagedResponse pagedListResponse = client.listVersions(parent); 100 101 List<Version> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 102 103 Assert.assertEquals(1, resources.size()); 104 Assert.assertEquals(expectedResponse.getVersionsList().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 listVersionsExceptionTest()122 public void listVersionsExceptionTest() 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.ofProjectName("[PROJECT]"); 130 client.listVersions(parent); 131 Assert.fail("No exception raised"); 132 } catch (InvalidArgumentException e) { 133 // Expected exception. 134 } 135 } 136 137 @Test listVersionsTest2()138 public void listVersionsTest2() throws Exception { 139 Version responsesElement = Version.newBuilder().build(); 140 ListVersionsResponse expectedResponse = 141 ListVersionsResponse.newBuilder() 142 .setNextPageToken("") 143 .addAllVersions(Arrays.asList(responsesElement)) 144 .build(); 145 mockService.addResponse(expectedResponse); 146 147 String parent = "projects/project-2469/agent"; 148 149 ListVersionsPagedResponse pagedListResponse = client.listVersions(parent); 150 151 List<Version> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 152 153 Assert.assertEquals(1, resources.size()); 154 Assert.assertEquals(expectedResponse.getVersionsList().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 listVersionsExceptionTest2()172 public void listVersionsExceptionTest2() 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-2469/agent"; 180 client.listVersions(parent); 181 Assert.fail("No exception raised"); 182 } catch (InvalidArgumentException e) { 183 // Expected exception. 184 } 185 } 186 187 @Test getVersionTest()188 public void getVersionTest() throws Exception { 189 Version expectedResponse = 190 Version.newBuilder() 191 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 192 .setDescription("description-1724546052") 193 .setVersionNumber(135927952) 194 .setCreateTime(Timestamp.newBuilder().build()) 195 .build(); 196 mockService.addResponse(expectedResponse); 197 198 VersionName name = VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]"); 199 200 Version actualResponse = client.getVersion(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 getVersionExceptionTest()219 public void getVersionExceptionTest() 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 VersionName name = VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]"); 227 client.getVersion(name); 228 Assert.fail("No exception raised"); 229 } catch (InvalidArgumentException e) { 230 // Expected exception. 231 } 232 } 233 234 @Test getVersionTest2()235 public void getVersionTest2() throws Exception { 236 Version expectedResponse = 237 Version.newBuilder() 238 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 239 .setDescription("description-1724546052") 240 .setVersionNumber(135927952) 241 .setCreateTime(Timestamp.newBuilder().build()) 242 .build(); 243 mockService.addResponse(expectedResponse); 244 245 String name = "projects/project-9087/agent/versions/version-9087"; 246 247 Version actualResponse = client.getVersion(name); 248 Assert.assertEquals(expectedResponse, actualResponse); 249 250 List<String> actualRequests = mockService.getRequestPaths(); 251 Assert.assertEquals(1, actualRequests.size()); 252 253 String apiClientHeaderKey = 254 mockService 255 .getRequestHeaders() 256 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 257 .iterator() 258 .next(); 259 Assert.assertTrue( 260 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 261 .matcher(apiClientHeaderKey) 262 .matches()); 263 } 264 265 @Test getVersionExceptionTest2()266 public void getVersionExceptionTest2() throws Exception { 267 ApiException exception = 268 ApiExceptionFactory.createException( 269 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 270 mockService.addException(exception); 271 272 try { 273 String name = "projects/project-9087/agent/versions/version-9087"; 274 client.getVersion(name); 275 Assert.fail("No exception raised"); 276 } catch (InvalidArgumentException e) { 277 // Expected exception. 278 } 279 } 280 281 @Test createVersionTest()282 public void createVersionTest() throws Exception { 283 Version expectedResponse = 284 Version.newBuilder() 285 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 286 .setDescription("description-1724546052") 287 .setVersionNumber(135927952) 288 .setCreateTime(Timestamp.newBuilder().build()) 289 .build(); 290 mockService.addResponse(expectedResponse); 291 292 AgentName parent = AgentName.ofProjectName("[PROJECT]"); 293 Version version = Version.newBuilder().build(); 294 295 Version actualResponse = client.createVersion(parent, version); 296 Assert.assertEquals(expectedResponse, actualResponse); 297 298 List<String> actualRequests = mockService.getRequestPaths(); 299 Assert.assertEquals(1, actualRequests.size()); 300 301 String apiClientHeaderKey = 302 mockService 303 .getRequestHeaders() 304 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 305 .iterator() 306 .next(); 307 Assert.assertTrue( 308 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 309 .matcher(apiClientHeaderKey) 310 .matches()); 311 } 312 313 @Test createVersionExceptionTest()314 public void createVersionExceptionTest() throws Exception { 315 ApiException exception = 316 ApiExceptionFactory.createException( 317 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 318 mockService.addException(exception); 319 320 try { 321 AgentName parent = AgentName.ofProjectName("[PROJECT]"); 322 Version version = Version.newBuilder().build(); 323 client.createVersion(parent, version); 324 Assert.fail("No exception raised"); 325 } catch (InvalidArgumentException e) { 326 // Expected exception. 327 } 328 } 329 330 @Test createVersionTest2()331 public void createVersionTest2() throws Exception { 332 Version expectedResponse = 333 Version.newBuilder() 334 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 335 .setDescription("description-1724546052") 336 .setVersionNumber(135927952) 337 .setCreateTime(Timestamp.newBuilder().build()) 338 .build(); 339 mockService.addResponse(expectedResponse); 340 341 String parent = "projects/project-2469/agent"; 342 Version version = Version.newBuilder().build(); 343 344 Version actualResponse = client.createVersion(parent, version); 345 Assert.assertEquals(expectedResponse, actualResponse); 346 347 List<String> actualRequests = mockService.getRequestPaths(); 348 Assert.assertEquals(1, actualRequests.size()); 349 350 String apiClientHeaderKey = 351 mockService 352 .getRequestHeaders() 353 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 354 .iterator() 355 .next(); 356 Assert.assertTrue( 357 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 358 .matcher(apiClientHeaderKey) 359 .matches()); 360 } 361 362 @Test createVersionExceptionTest2()363 public void createVersionExceptionTest2() throws Exception { 364 ApiException exception = 365 ApiExceptionFactory.createException( 366 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 367 mockService.addException(exception); 368 369 try { 370 String parent = "projects/project-2469/agent"; 371 Version version = Version.newBuilder().build(); 372 client.createVersion(parent, version); 373 Assert.fail("No exception raised"); 374 } catch (InvalidArgumentException e) { 375 // Expected exception. 376 } 377 } 378 379 @Test updateVersionTest()380 public void updateVersionTest() throws Exception { 381 Version expectedResponse = 382 Version.newBuilder() 383 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 384 .setDescription("description-1724546052") 385 .setVersionNumber(135927952) 386 .setCreateTime(Timestamp.newBuilder().build()) 387 .build(); 388 mockService.addResponse(expectedResponse); 389 390 Version version = 391 Version.newBuilder() 392 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 393 .setDescription("description-1724546052") 394 .setVersionNumber(135927952) 395 .setCreateTime(Timestamp.newBuilder().build()) 396 .build(); 397 FieldMask updateMask = FieldMask.newBuilder().build(); 398 399 Version actualResponse = client.updateVersion(version, updateMask); 400 Assert.assertEquals(expectedResponse, actualResponse); 401 402 List<String> actualRequests = mockService.getRequestPaths(); 403 Assert.assertEquals(1, actualRequests.size()); 404 405 String apiClientHeaderKey = 406 mockService 407 .getRequestHeaders() 408 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 409 .iterator() 410 .next(); 411 Assert.assertTrue( 412 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 413 .matcher(apiClientHeaderKey) 414 .matches()); 415 } 416 417 @Test updateVersionExceptionTest()418 public void updateVersionExceptionTest() throws Exception { 419 ApiException exception = 420 ApiExceptionFactory.createException( 421 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 422 mockService.addException(exception); 423 424 try { 425 Version version = 426 Version.newBuilder() 427 .setName(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString()) 428 .setDescription("description-1724546052") 429 .setVersionNumber(135927952) 430 .setCreateTime(Timestamp.newBuilder().build()) 431 .build(); 432 FieldMask updateMask = FieldMask.newBuilder().build(); 433 client.updateVersion(version, updateMask); 434 Assert.fail("No exception raised"); 435 } catch (InvalidArgumentException e) { 436 // Expected exception. 437 } 438 } 439 440 @Test deleteVersionTest()441 public void deleteVersionTest() throws Exception { 442 Empty expectedResponse = Empty.newBuilder().build(); 443 mockService.addResponse(expectedResponse); 444 445 VersionName name = VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]"); 446 447 client.deleteVersion(name); 448 449 List<String> actualRequests = mockService.getRequestPaths(); 450 Assert.assertEquals(1, actualRequests.size()); 451 452 String apiClientHeaderKey = 453 mockService 454 .getRequestHeaders() 455 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 456 .iterator() 457 .next(); 458 Assert.assertTrue( 459 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 460 .matcher(apiClientHeaderKey) 461 .matches()); 462 } 463 464 @Test deleteVersionExceptionTest()465 public void deleteVersionExceptionTest() throws Exception { 466 ApiException exception = 467 ApiExceptionFactory.createException( 468 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 469 mockService.addException(exception); 470 471 try { 472 VersionName name = VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]"); 473 client.deleteVersion(name); 474 Assert.fail("No exception raised"); 475 } catch (InvalidArgumentException e) { 476 // Expected exception. 477 } 478 } 479 480 @Test deleteVersionTest2()481 public void deleteVersionTest2() throws Exception { 482 Empty expectedResponse = Empty.newBuilder().build(); 483 mockService.addResponse(expectedResponse); 484 485 String name = "projects/project-9087/agent/versions/version-9087"; 486 487 client.deleteVersion(name); 488 489 List<String> actualRequests = mockService.getRequestPaths(); 490 Assert.assertEquals(1, actualRequests.size()); 491 492 String apiClientHeaderKey = 493 mockService 494 .getRequestHeaders() 495 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 496 .iterator() 497 .next(); 498 Assert.assertTrue( 499 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 500 .matcher(apiClientHeaderKey) 501 .matches()); 502 } 503 504 @Test deleteVersionExceptionTest2()505 public void deleteVersionExceptionTest2() throws Exception { 506 ApiException exception = 507 ApiExceptionFactory.createException( 508 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 509 mockService.addException(exception); 510 511 try { 512 String name = "projects/project-9087/agent/versions/version-9087"; 513 client.deleteVersion(name); 514 Assert.fail("No exception raised"); 515 } catch (InvalidArgumentException e) { 516 // Expected exception. 517 } 518 } 519 520 @Test listLocationsTest()521 public void listLocationsTest() throws Exception { 522 Location responsesElement = Location.newBuilder().build(); 523 ListLocationsResponse expectedResponse = 524 ListLocationsResponse.newBuilder() 525 .setNextPageToken("") 526 .addAllLocations(Arrays.asList(responsesElement)) 527 .build(); 528 mockService.addResponse(expectedResponse); 529 530 ListLocationsRequest request = 531 ListLocationsRequest.newBuilder() 532 .setName("projects/project-3664") 533 .setFilter("filter-1274492040") 534 .setPageSize(883849137) 535 .setPageToken("pageToken873572522") 536 .build(); 537 538 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 539 540 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 541 542 Assert.assertEquals(1, resources.size()); 543 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 544 545 List<String> actualRequests = mockService.getRequestPaths(); 546 Assert.assertEquals(1, actualRequests.size()); 547 548 String apiClientHeaderKey = 549 mockService 550 .getRequestHeaders() 551 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 552 .iterator() 553 .next(); 554 Assert.assertTrue( 555 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 556 .matcher(apiClientHeaderKey) 557 .matches()); 558 } 559 560 @Test listLocationsExceptionTest()561 public void listLocationsExceptionTest() throws Exception { 562 ApiException exception = 563 ApiExceptionFactory.createException( 564 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 565 mockService.addException(exception); 566 567 try { 568 ListLocationsRequest request = 569 ListLocationsRequest.newBuilder() 570 .setName("projects/project-3664") 571 .setFilter("filter-1274492040") 572 .setPageSize(883849137) 573 .setPageToken("pageToken873572522") 574 .build(); 575 client.listLocations(request); 576 Assert.fail("No exception raised"); 577 } catch (InvalidArgumentException e) { 578 // Expected exception. 579 } 580 } 581 582 @Test getLocationTest()583 public void getLocationTest() throws Exception { 584 Location expectedResponse = 585 Location.newBuilder() 586 .setName("name3373707") 587 .setLocationId("locationId1541836720") 588 .setDisplayName("displayName1714148973") 589 .putAllLabels(new HashMap<String, String>()) 590 .setMetadata(Any.newBuilder().build()) 591 .build(); 592 mockService.addResponse(expectedResponse); 593 594 GetLocationRequest request = 595 GetLocationRequest.newBuilder() 596 .setName("projects/project-9062/locations/location-9062") 597 .build(); 598 599 Location actualResponse = client.getLocation(request); 600 Assert.assertEquals(expectedResponse, actualResponse); 601 602 List<String> actualRequests = mockService.getRequestPaths(); 603 Assert.assertEquals(1, actualRequests.size()); 604 605 String apiClientHeaderKey = 606 mockService 607 .getRequestHeaders() 608 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 609 .iterator() 610 .next(); 611 Assert.assertTrue( 612 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 613 .matcher(apiClientHeaderKey) 614 .matches()); 615 } 616 617 @Test getLocationExceptionTest()618 public void getLocationExceptionTest() throws Exception { 619 ApiException exception = 620 ApiExceptionFactory.createException( 621 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 622 mockService.addException(exception); 623 624 try { 625 GetLocationRequest request = 626 GetLocationRequest.newBuilder() 627 .setName("projects/project-9062/locations/location-9062") 628 .build(); 629 client.getLocation(request); 630 Assert.fail("No exception raised"); 631 } catch (InvalidArgumentException e) { 632 // Expected exception. 633 } 634 } 635 } 636