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.ContextsClient.ListContextsPagedResponse; 20 import static com.google.cloud.dialogflow.v2.ContextsClient.ListLocationsPagedResponse; 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.HttpJsonContextsStub; 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.Struct; 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 ContextsClientHttpJsonTest { 55 private static MockHttpService mockService; 56 private static ContextsClient client; 57 58 @BeforeClass startStaticServer()59 public static void startStaticServer() throws IOException { 60 mockService = 61 new MockHttpService( 62 HttpJsonContextsStub.getMethodDescriptors(), ContextsSettings.getDefaultEndpoint()); 63 ContextsSettings settings = 64 ContextsSettings.newHttpJsonBuilder() 65 .setTransportChannelProvider( 66 ContextsSettings.defaultHttpJsonTransportProviderBuilder() 67 .setHttpTransport(mockService) 68 .build()) 69 .setCredentialsProvider(NoCredentialsProvider.create()) 70 .build(); 71 client = ContextsClient.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 listContextsTest()88 public void listContextsTest() throws Exception { 89 Context responsesElement = Context.newBuilder().build(); 90 ListContextsResponse expectedResponse = 91 ListContextsResponse.newBuilder() 92 .setNextPageToken("") 93 .addAllContexts(Arrays.asList(responsesElement)) 94 .build(); 95 mockService.addResponse(expectedResponse); 96 97 SessionName parent = SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]"); 98 99 ListContextsPagedResponse pagedListResponse = client.listContexts(parent); 100 101 List<Context> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 102 103 Assert.assertEquals(1, resources.size()); 104 Assert.assertEquals(expectedResponse.getContextsList().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 listContextsExceptionTest()122 public void listContextsExceptionTest() 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 SessionName parent = SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]"); 130 client.listContexts(parent); 131 Assert.fail("No exception raised"); 132 } catch (InvalidArgumentException e) { 133 // Expected exception. 134 } 135 } 136 137 @Test listContextsTest2()138 public void listContextsTest2() throws Exception { 139 Context responsesElement = Context.newBuilder().build(); 140 ListContextsResponse expectedResponse = 141 ListContextsResponse.newBuilder() 142 .setNextPageToken("") 143 .addAllContexts(Arrays.asList(responsesElement)) 144 .build(); 145 mockService.addResponse(expectedResponse); 146 147 String parent = "projects/project-6494/agent/sessions/session-6494"; 148 149 ListContextsPagedResponse pagedListResponse = client.listContexts(parent); 150 151 List<Context> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 152 153 Assert.assertEquals(1, resources.size()); 154 Assert.assertEquals(expectedResponse.getContextsList().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 listContextsExceptionTest2()172 public void listContextsExceptionTest2() 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-6494/agent/sessions/session-6494"; 180 client.listContexts(parent); 181 Assert.fail("No exception raised"); 182 } catch (InvalidArgumentException e) { 183 // Expected exception. 184 } 185 } 186 187 @Test getContextTest()188 public void getContextTest() throws Exception { 189 Context expectedResponse = 190 Context.newBuilder() 191 .setName( 192 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]") 193 .toString()) 194 .setLifespanCount(1178775510) 195 .setParameters(Struct.newBuilder().build()) 196 .build(); 197 mockService.addResponse(expectedResponse); 198 199 ContextName name = 200 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]"); 201 202 Context actualResponse = client.getContext(name); 203 Assert.assertEquals(expectedResponse, actualResponse); 204 205 List<String> actualRequests = mockService.getRequestPaths(); 206 Assert.assertEquals(1, actualRequests.size()); 207 208 String apiClientHeaderKey = 209 mockService 210 .getRequestHeaders() 211 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 212 .iterator() 213 .next(); 214 Assert.assertTrue( 215 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 216 .matcher(apiClientHeaderKey) 217 .matches()); 218 } 219 220 @Test getContextExceptionTest()221 public void getContextExceptionTest() throws Exception { 222 ApiException exception = 223 ApiExceptionFactory.createException( 224 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 225 mockService.addException(exception); 226 227 try { 228 ContextName name = 229 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]"); 230 client.getContext(name); 231 Assert.fail("No exception raised"); 232 } catch (InvalidArgumentException e) { 233 // Expected exception. 234 } 235 } 236 237 @Test getContextTest2()238 public void getContextTest2() throws Exception { 239 Context expectedResponse = 240 Context.newBuilder() 241 .setName( 242 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]") 243 .toString()) 244 .setLifespanCount(1178775510) 245 .setParameters(Struct.newBuilder().build()) 246 .build(); 247 mockService.addResponse(expectedResponse); 248 249 String name = "projects/project-8115/agent/sessions/session-8115/contexts/context-8115"; 250 251 Context actualResponse = client.getContext(name); 252 Assert.assertEquals(expectedResponse, actualResponse); 253 254 List<String> actualRequests = mockService.getRequestPaths(); 255 Assert.assertEquals(1, actualRequests.size()); 256 257 String apiClientHeaderKey = 258 mockService 259 .getRequestHeaders() 260 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 261 .iterator() 262 .next(); 263 Assert.assertTrue( 264 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 265 .matcher(apiClientHeaderKey) 266 .matches()); 267 } 268 269 @Test getContextExceptionTest2()270 public void getContextExceptionTest2() throws Exception { 271 ApiException exception = 272 ApiExceptionFactory.createException( 273 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 274 mockService.addException(exception); 275 276 try { 277 String name = "projects/project-8115/agent/sessions/session-8115/contexts/context-8115"; 278 client.getContext(name); 279 Assert.fail("No exception raised"); 280 } catch (InvalidArgumentException e) { 281 // Expected exception. 282 } 283 } 284 285 @Test createContextTest()286 public void createContextTest() throws Exception { 287 Context expectedResponse = 288 Context.newBuilder() 289 .setName( 290 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]") 291 .toString()) 292 .setLifespanCount(1178775510) 293 .setParameters(Struct.newBuilder().build()) 294 .build(); 295 mockService.addResponse(expectedResponse); 296 297 SessionName parent = SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]"); 298 Context context = Context.newBuilder().build(); 299 300 Context actualResponse = client.createContext(parent, context); 301 Assert.assertEquals(expectedResponse, actualResponse); 302 303 List<String> actualRequests = mockService.getRequestPaths(); 304 Assert.assertEquals(1, actualRequests.size()); 305 306 String apiClientHeaderKey = 307 mockService 308 .getRequestHeaders() 309 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 310 .iterator() 311 .next(); 312 Assert.assertTrue( 313 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 314 .matcher(apiClientHeaderKey) 315 .matches()); 316 } 317 318 @Test createContextExceptionTest()319 public void createContextExceptionTest() throws Exception { 320 ApiException exception = 321 ApiExceptionFactory.createException( 322 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 323 mockService.addException(exception); 324 325 try { 326 SessionName parent = SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]"); 327 Context context = Context.newBuilder().build(); 328 client.createContext(parent, context); 329 Assert.fail("No exception raised"); 330 } catch (InvalidArgumentException e) { 331 // Expected exception. 332 } 333 } 334 335 @Test createContextTest2()336 public void createContextTest2() throws Exception { 337 Context expectedResponse = 338 Context.newBuilder() 339 .setName( 340 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]") 341 .toString()) 342 .setLifespanCount(1178775510) 343 .setParameters(Struct.newBuilder().build()) 344 .build(); 345 mockService.addResponse(expectedResponse); 346 347 String parent = "projects/project-6494/agent/sessions/session-6494"; 348 Context context = Context.newBuilder().build(); 349 350 Context actualResponse = client.createContext(parent, context); 351 Assert.assertEquals(expectedResponse, actualResponse); 352 353 List<String> actualRequests = mockService.getRequestPaths(); 354 Assert.assertEquals(1, actualRequests.size()); 355 356 String apiClientHeaderKey = 357 mockService 358 .getRequestHeaders() 359 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 360 .iterator() 361 .next(); 362 Assert.assertTrue( 363 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 364 .matcher(apiClientHeaderKey) 365 .matches()); 366 } 367 368 @Test createContextExceptionTest2()369 public void createContextExceptionTest2() throws Exception { 370 ApiException exception = 371 ApiExceptionFactory.createException( 372 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 373 mockService.addException(exception); 374 375 try { 376 String parent = "projects/project-6494/agent/sessions/session-6494"; 377 Context context = Context.newBuilder().build(); 378 client.createContext(parent, context); 379 Assert.fail("No exception raised"); 380 } catch (InvalidArgumentException e) { 381 // Expected exception. 382 } 383 } 384 385 @Test updateContextTest()386 public void updateContextTest() throws Exception { 387 Context expectedResponse = 388 Context.newBuilder() 389 .setName( 390 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]") 391 .toString()) 392 .setLifespanCount(1178775510) 393 .setParameters(Struct.newBuilder().build()) 394 .build(); 395 mockService.addResponse(expectedResponse); 396 397 Context context = 398 Context.newBuilder() 399 .setName( 400 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]") 401 .toString()) 402 .setLifespanCount(1178775510) 403 .setParameters(Struct.newBuilder().build()) 404 .build(); 405 FieldMask updateMask = FieldMask.newBuilder().build(); 406 407 Context actualResponse = client.updateContext(context, updateMask); 408 Assert.assertEquals(expectedResponse, actualResponse); 409 410 List<String> actualRequests = mockService.getRequestPaths(); 411 Assert.assertEquals(1, actualRequests.size()); 412 413 String apiClientHeaderKey = 414 mockService 415 .getRequestHeaders() 416 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 417 .iterator() 418 .next(); 419 Assert.assertTrue( 420 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 421 .matcher(apiClientHeaderKey) 422 .matches()); 423 } 424 425 @Test updateContextExceptionTest()426 public void updateContextExceptionTest() throws Exception { 427 ApiException exception = 428 ApiExceptionFactory.createException( 429 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 430 mockService.addException(exception); 431 432 try { 433 Context context = 434 Context.newBuilder() 435 .setName( 436 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]") 437 .toString()) 438 .setLifespanCount(1178775510) 439 .setParameters(Struct.newBuilder().build()) 440 .build(); 441 FieldMask updateMask = FieldMask.newBuilder().build(); 442 client.updateContext(context, updateMask); 443 Assert.fail("No exception raised"); 444 } catch (InvalidArgumentException e) { 445 // Expected exception. 446 } 447 } 448 449 @Test deleteContextTest()450 public void deleteContextTest() throws Exception { 451 Empty expectedResponse = Empty.newBuilder().build(); 452 mockService.addResponse(expectedResponse); 453 454 ContextName name = 455 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]"); 456 457 client.deleteContext(name); 458 459 List<String> actualRequests = mockService.getRequestPaths(); 460 Assert.assertEquals(1, actualRequests.size()); 461 462 String apiClientHeaderKey = 463 mockService 464 .getRequestHeaders() 465 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 466 .iterator() 467 .next(); 468 Assert.assertTrue( 469 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 470 .matcher(apiClientHeaderKey) 471 .matches()); 472 } 473 474 @Test deleteContextExceptionTest()475 public void deleteContextExceptionTest() throws Exception { 476 ApiException exception = 477 ApiExceptionFactory.createException( 478 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 479 mockService.addException(exception); 480 481 try { 482 ContextName name = 483 ContextName.ofProjectSessionContextName("[PROJECT]", "[SESSION]", "[CONTEXT]"); 484 client.deleteContext(name); 485 Assert.fail("No exception raised"); 486 } catch (InvalidArgumentException e) { 487 // Expected exception. 488 } 489 } 490 491 @Test deleteContextTest2()492 public void deleteContextTest2() throws Exception { 493 Empty expectedResponse = Empty.newBuilder().build(); 494 mockService.addResponse(expectedResponse); 495 496 String name = "projects/project-8115/agent/sessions/session-8115/contexts/context-8115"; 497 498 client.deleteContext(name); 499 500 List<String> actualRequests = mockService.getRequestPaths(); 501 Assert.assertEquals(1, actualRequests.size()); 502 503 String apiClientHeaderKey = 504 mockService 505 .getRequestHeaders() 506 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 507 .iterator() 508 .next(); 509 Assert.assertTrue( 510 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 511 .matcher(apiClientHeaderKey) 512 .matches()); 513 } 514 515 @Test deleteContextExceptionTest2()516 public void deleteContextExceptionTest2() throws Exception { 517 ApiException exception = 518 ApiExceptionFactory.createException( 519 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 520 mockService.addException(exception); 521 522 try { 523 String name = "projects/project-8115/agent/sessions/session-8115/contexts/context-8115"; 524 client.deleteContext(name); 525 Assert.fail("No exception raised"); 526 } catch (InvalidArgumentException e) { 527 // Expected exception. 528 } 529 } 530 531 @Test deleteAllContextsTest()532 public void deleteAllContextsTest() throws Exception { 533 Empty expectedResponse = Empty.newBuilder().build(); 534 mockService.addResponse(expectedResponse); 535 536 SessionName parent = SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]"); 537 538 client.deleteAllContexts(parent); 539 540 List<String> actualRequests = mockService.getRequestPaths(); 541 Assert.assertEquals(1, actualRequests.size()); 542 543 String apiClientHeaderKey = 544 mockService 545 .getRequestHeaders() 546 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 547 .iterator() 548 .next(); 549 Assert.assertTrue( 550 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 551 .matcher(apiClientHeaderKey) 552 .matches()); 553 } 554 555 @Test deleteAllContextsExceptionTest()556 public void deleteAllContextsExceptionTest() throws Exception { 557 ApiException exception = 558 ApiExceptionFactory.createException( 559 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 560 mockService.addException(exception); 561 562 try { 563 SessionName parent = SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]"); 564 client.deleteAllContexts(parent); 565 Assert.fail("No exception raised"); 566 } catch (InvalidArgumentException e) { 567 // Expected exception. 568 } 569 } 570 571 @Test deleteAllContextsTest2()572 public void deleteAllContextsTest2() throws Exception { 573 Empty expectedResponse = Empty.newBuilder().build(); 574 mockService.addResponse(expectedResponse); 575 576 String parent = "projects/project-6494/agent/sessions/session-6494"; 577 578 client.deleteAllContexts(parent); 579 580 List<String> actualRequests = mockService.getRequestPaths(); 581 Assert.assertEquals(1, actualRequests.size()); 582 583 String apiClientHeaderKey = 584 mockService 585 .getRequestHeaders() 586 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 587 .iterator() 588 .next(); 589 Assert.assertTrue( 590 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 591 .matcher(apiClientHeaderKey) 592 .matches()); 593 } 594 595 @Test deleteAllContextsExceptionTest2()596 public void deleteAllContextsExceptionTest2() throws Exception { 597 ApiException exception = 598 ApiExceptionFactory.createException( 599 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 600 mockService.addException(exception); 601 602 try { 603 String parent = "projects/project-6494/agent/sessions/session-6494"; 604 client.deleteAllContexts(parent); 605 Assert.fail("No exception raised"); 606 } catch (InvalidArgumentException e) { 607 // Expected exception. 608 } 609 } 610 611 @Test listLocationsTest()612 public void listLocationsTest() throws Exception { 613 Location responsesElement = Location.newBuilder().build(); 614 ListLocationsResponse expectedResponse = 615 ListLocationsResponse.newBuilder() 616 .setNextPageToken("") 617 .addAllLocations(Arrays.asList(responsesElement)) 618 .build(); 619 mockService.addResponse(expectedResponse); 620 621 ListLocationsRequest request = 622 ListLocationsRequest.newBuilder() 623 .setName("projects/project-3664") 624 .setFilter("filter-1274492040") 625 .setPageSize(883849137) 626 .setPageToken("pageToken873572522") 627 .build(); 628 629 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 630 631 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 632 633 Assert.assertEquals(1, resources.size()); 634 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 635 636 List<String> actualRequests = mockService.getRequestPaths(); 637 Assert.assertEquals(1, actualRequests.size()); 638 639 String apiClientHeaderKey = 640 mockService 641 .getRequestHeaders() 642 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 643 .iterator() 644 .next(); 645 Assert.assertTrue( 646 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 647 .matcher(apiClientHeaderKey) 648 .matches()); 649 } 650 651 @Test listLocationsExceptionTest()652 public void listLocationsExceptionTest() throws Exception { 653 ApiException exception = 654 ApiExceptionFactory.createException( 655 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 656 mockService.addException(exception); 657 658 try { 659 ListLocationsRequest request = 660 ListLocationsRequest.newBuilder() 661 .setName("projects/project-3664") 662 .setFilter("filter-1274492040") 663 .setPageSize(883849137) 664 .setPageToken("pageToken873572522") 665 .build(); 666 client.listLocations(request); 667 Assert.fail("No exception raised"); 668 } catch (InvalidArgumentException e) { 669 // Expected exception. 670 } 671 } 672 673 @Test getLocationTest()674 public void getLocationTest() throws Exception { 675 Location expectedResponse = 676 Location.newBuilder() 677 .setName("name3373707") 678 .setLocationId("locationId1541836720") 679 .setDisplayName("displayName1714148973") 680 .putAllLabels(new HashMap<String, String>()) 681 .setMetadata(Any.newBuilder().build()) 682 .build(); 683 mockService.addResponse(expectedResponse); 684 685 GetLocationRequest request = 686 GetLocationRequest.newBuilder() 687 .setName("projects/project-9062/locations/location-9062") 688 .build(); 689 690 Location actualResponse = client.getLocation(request); 691 Assert.assertEquals(expectedResponse, actualResponse); 692 693 List<String> actualRequests = mockService.getRequestPaths(); 694 Assert.assertEquals(1, actualRequests.size()); 695 696 String apiClientHeaderKey = 697 mockService 698 .getRequestHeaders() 699 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 700 .iterator() 701 .next(); 702 Assert.assertTrue( 703 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 704 .matcher(apiClientHeaderKey) 705 .matches()); 706 } 707 708 @Test getLocationExceptionTest()709 public void getLocationExceptionTest() throws Exception { 710 ApiException exception = 711 ApiExceptionFactory.createException( 712 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 713 mockService.addException(exception); 714 715 try { 716 GetLocationRequest request = 717 GetLocationRequest.newBuilder() 718 .setName("projects/project-9062/locations/location-9062") 719 .build(); 720 client.getLocation(request); 721 Assert.fail("No exception raised"); 722 } catch (InvalidArgumentException e) { 723 // Expected exception. 724 } 725 } 726 } 727