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.retail.v2beta; 18 19 import static com.google.cloud.retail.v2beta.ModelServiceClient.ListModelsPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 23 import com.google.api.gax.httpjson.testing.MockHttpService; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.ApiException; 26 import com.google.api.gax.rpc.ApiExceptionFactory; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.api.gax.rpc.testing.FakeStatusCode; 30 import com.google.cloud.retail.v2beta.stub.HttpJsonModelServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.longrunning.Operation; 33 import com.google.protobuf.Any; 34 import com.google.protobuf.Empty; 35 import com.google.protobuf.FieldMask; 36 import com.google.protobuf.Timestamp; 37 import java.io.IOException; 38 import java.util.ArrayList; 39 import java.util.Arrays; 40 import java.util.List; 41 import java.util.concurrent.ExecutionException; 42 import javax.annotation.Generated; 43 import org.junit.After; 44 import org.junit.AfterClass; 45 import org.junit.Assert; 46 import org.junit.Before; 47 import org.junit.BeforeClass; 48 import org.junit.Test; 49 50 @Generated("by gapic-generator-java") 51 public class ModelServiceClientHttpJsonTest { 52 private static MockHttpService mockService; 53 private static ModelServiceClient client; 54 55 @BeforeClass startStaticServer()56 public static void startStaticServer() throws IOException { 57 mockService = 58 new MockHttpService( 59 HttpJsonModelServiceStub.getMethodDescriptors(), 60 ModelServiceSettings.getDefaultEndpoint()); 61 ModelServiceSettings settings = 62 ModelServiceSettings.newHttpJsonBuilder() 63 .setTransportChannelProvider( 64 ModelServiceSettings.defaultHttpJsonTransportProviderBuilder() 65 .setHttpTransport(mockService) 66 .build()) 67 .setCredentialsProvider(NoCredentialsProvider.create()) 68 .build(); 69 client = ModelServiceClient.create(settings); 70 } 71 72 @AfterClass stopServer()73 public static void stopServer() { 74 client.close(); 75 } 76 77 @Before setUp()78 public void setUp() {} 79 80 @After tearDown()81 public void tearDown() throws Exception { 82 mockService.reset(); 83 } 84 85 @Test createModelTest()86 public void createModelTest() throws Exception { 87 Model expectedResponse = 88 Model.newBuilder() 89 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 90 .setDisplayName("displayName1714148973") 91 .setCreateTime(Timestamp.newBuilder().build()) 92 .setUpdateTime(Timestamp.newBuilder().build()) 93 .setType("type3575610") 94 .setOptimizationObjective("optimizationObjective-1014459828") 95 .setLastTuneTime(Timestamp.newBuilder().build()) 96 .setTuningOperation("tuningOperation-1269747150") 97 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 98 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 99 .build(); 100 Operation resultOperation = 101 Operation.newBuilder() 102 .setName("createModelTest") 103 .setDone(true) 104 .setResponse(Any.pack(expectedResponse)) 105 .build(); 106 mockService.addResponse(resultOperation); 107 108 CatalogName parent = CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]"); 109 Model model = Model.newBuilder().build(); 110 111 Model actualResponse = client.createModelAsync(parent, model).get(); 112 Assert.assertEquals(expectedResponse, actualResponse); 113 114 List<String> actualRequests = mockService.getRequestPaths(); 115 Assert.assertEquals(1, actualRequests.size()); 116 117 String apiClientHeaderKey = 118 mockService 119 .getRequestHeaders() 120 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 121 .iterator() 122 .next(); 123 Assert.assertTrue( 124 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 125 .matcher(apiClientHeaderKey) 126 .matches()); 127 } 128 129 @Test createModelExceptionTest()130 public void createModelExceptionTest() throws Exception { 131 ApiException exception = 132 ApiExceptionFactory.createException( 133 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 134 mockService.addException(exception); 135 136 try { 137 CatalogName parent = CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]"); 138 Model model = Model.newBuilder().build(); 139 client.createModelAsync(parent, model).get(); 140 Assert.fail("No exception raised"); 141 } catch (ExecutionException e) { 142 } 143 } 144 145 @Test createModelTest2()146 public void createModelTest2() throws Exception { 147 Model expectedResponse = 148 Model.newBuilder() 149 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 150 .setDisplayName("displayName1714148973") 151 .setCreateTime(Timestamp.newBuilder().build()) 152 .setUpdateTime(Timestamp.newBuilder().build()) 153 .setType("type3575610") 154 .setOptimizationObjective("optimizationObjective-1014459828") 155 .setLastTuneTime(Timestamp.newBuilder().build()) 156 .setTuningOperation("tuningOperation-1269747150") 157 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 158 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 159 .build(); 160 Operation resultOperation = 161 Operation.newBuilder() 162 .setName("createModelTest") 163 .setDone(true) 164 .setResponse(Any.pack(expectedResponse)) 165 .build(); 166 mockService.addResponse(resultOperation); 167 168 String parent = "projects/project-6267/locations/location-6267/catalogs/catalog-6267"; 169 Model model = Model.newBuilder().build(); 170 171 Model actualResponse = client.createModelAsync(parent, model).get(); 172 Assert.assertEquals(expectedResponse, actualResponse); 173 174 List<String> actualRequests = mockService.getRequestPaths(); 175 Assert.assertEquals(1, actualRequests.size()); 176 177 String apiClientHeaderKey = 178 mockService 179 .getRequestHeaders() 180 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 181 .iterator() 182 .next(); 183 Assert.assertTrue( 184 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 185 .matcher(apiClientHeaderKey) 186 .matches()); 187 } 188 189 @Test createModelExceptionTest2()190 public void createModelExceptionTest2() throws Exception { 191 ApiException exception = 192 ApiExceptionFactory.createException( 193 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 194 mockService.addException(exception); 195 196 try { 197 String parent = "projects/project-6267/locations/location-6267/catalogs/catalog-6267"; 198 Model model = Model.newBuilder().build(); 199 client.createModelAsync(parent, model).get(); 200 Assert.fail("No exception raised"); 201 } catch (ExecutionException e) { 202 } 203 } 204 205 @Test getModelTest()206 public void getModelTest() throws Exception { 207 Model expectedResponse = 208 Model.newBuilder() 209 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 210 .setDisplayName("displayName1714148973") 211 .setCreateTime(Timestamp.newBuilder().build()) 212 .setUpdateTime(Timestamp.newBuilder().build()) 213 .setType("type3575610") 214 .setOptimizationObjective("optimizationObjective-1014459828") 215 .setLastTuneTime(Timestamp.newBuilder().build()) 216 .setTuningOperation("tuningOperation-1269747150") 217 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 218 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 219 .build(); 220 mockService.addResponse(expectedResponse); 221 222 ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]"); 223 224 Model actualResponse = client.getModel(name); 225 Assert.assertEquals(expectedResponse, actualResponse); 226 227 List<String> actualRequests = mockService.getRequestPaths(); 228 Assert.assertEquals(1, actualRequests.size()); 229 230 String apiClientHeaderKey = 231 mockService 232 .getRequestHeaders() 233 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 234 .iterator() 235 .next(); 236 Assert.assertTrue( 237 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 238 .matcher(apiClientHeaderKey) 239 .matches()); 240 } 241 242 @Test getModelExceptionTest()243 public void getModelExceptionTest() throws Exception { 244 ApiException exception = 245 ApiExceptionFactory.createException( 246 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 247 mockService.addException(exception); 248 249 try { 250 ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]"); 251 client.getModel(name); 252 Assert.fail("No exception raised"); 253 } catch (InvalidArgumentException e) { 254 // Expected exception. 255 } 256 } 257 258 @Test getModelTest2()259 public void getModelTest2() throws Exception { 260 Model expectedResponse = 261 Model.newBuilder() 262 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 263 .setDisplayName("displayName1714148973") 264 .setCreateTime(Timestamp.newBuilder().build()) 265 .setUpdateTime(Timestamp.newBuilder().build()) 266 .setType("type3575610") 267 .setOptimizationObjective("optimizationObjective-1014459828") 268 .setLastTuneTime(Timestamp.newBuilder().build()) 269 .setTuningOperation("tuningOperation-1269747150") 270 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 271 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 272 .build(); 273 mockService.addResponse(expectedResponse); 274 275 String name = 276 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 277 278 Model actualResponse = client.getModel(name); 279 Assert.assertEquals(expectedResponse, actualResponse); 280 281 List<String> actualRequests = mockService.getRequestPaths(); 282 Assert.assertEquals(1, actualRequests.size()); 283 284 String apiClientHeaderKey = 285 mockService 286 .getRequestHeaders() 287 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 288 .iterator() 289 .next(); 290 Assert.assertTrue( 291 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 292 .matcher(apiClientHeaderKey) 293 .matches()); 294 } 295 296 @Test getModelExceptionTest2()297 public void getModelExceptionTest2() throws Exception { 298 ApiException exception = 299 ApiExceptionFactory.createException( 300 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 301 mockService.addException(exception); 302 303 try { 304 String name = 305 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 306 client.getModel(name); 307 Assert.fail("No exception raised"); 308 } catch (InvalidArgumentException e) { 309 // Expected exception. 310 } 311 } 312 313 @Test pauseModelTest()314 public void pauseModelTest() throws Exception { 315 Model expectedResponse = 316 Model.newBuilder() 317 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 318 .setDisplayName("displayName1714148973") 319 .setCreateTime(Timestamp.newBuilder().build()) 320 .setUpdateTime(Timestamp.newBuilder().build()) 321 .setType("type3575610") 322 .setOptimizationObjective("optimizationObjective-1014459828") 323 .setLastTuneTime(Timestamp.newBuilder().build()) 324 .setTuningOperation("tuningOperation-1269747150") 325 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 326 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 327 .build(); 328 mockService.addResponse(expectedResponse); 329 330 ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]"); 331 332 Model actualResponse = client.pauseModel(name); 333 Assert.assertEquals(expectedResponse, actualResponse); 334 335 List<String> actualRequests = mockService.getRequestPaths(); 336 Assert.assertEquals(1, actualRequests.size()); 337 338 String apiClientHeaderKey = 339 mockService 340 .getRequestHeaders() 341 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 342 .iterator() 343 .next(); 344 Assert.assertTrue( 345 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 346 .matcher(apiClientHeaderKey) 347 .matches()); 348 } 349 350 @Test pauseModelExceptionTest()351 public void pauseModelExceptionTest() throws Exception { 352 ApiException exception = 353 ApiExceptionFactory.createException( 354 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 355 mockService.addException(exception); 356 357 try { 358 ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]"); 359 client.pauseModel(name); 360 Assert.fail("No exception raised"); 361 } catch (InvalidArgumentException e) { 362 // Expected exception. 363 } 364 } 365 366 @Test pauseModelTest2()367 public void pauseModelTest2() throws Exception { 368 Model expectedResponse = 369 Model.newBuilder() 370 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 371 .setDisplayName("displayName1714148973") 372 .setCreateTime(Timestamp.newBuilder().build()) 373 .setUpdateTime(Timestamp.newBuilder().build()) 374 .setType("type3575610") 375 .setOptimizationObjective("optimizationObjective-1014459828") 376 .setLastTuneTime(Timestamp.newBuilder().build()) 377 .setTuningOperation("tuningOperation-1269747150") 378 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 379 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 380 .build(); 381 mockService.addResponse(expectedResponse); 382 383 String name = 384 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 385 386 Model actualResponse = client.pauseModel(name); 387 Assert.assertEquals(expectedResponse, actualResponse); 388 389 List<String> actualRequests = mockService.getRequestPaths(); 390 Assert.assertEquals(1, actualRequests.size()); 391 392 String apiClientHeaderKey = 393 mockService 394 .getRequestHeaders() 395 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 396 .iterator() 397 .next(); 398 Assert.assertTrue( 399 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 400 .matcher(apiClientHeaderKey) 401 .matches()); 402 } 403 404 @Test pauseModelExceptionTest2()405 public void pauseModelExceptionTest2() throws Exception { 406 ApiException exception = 407 ApiExceptionFactory.createException( 408 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 409 mockService.addException(exception); 410 411 try { 412 String name = 413 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 414 client.pauseModel(name); 415 Assert.fail("No exception raised"); 416 } catch (InvalidArgumentException e) { 417 // Expected exception. 418 } 419 } 420 421 @Test resumeModelTest()422 public void resumeModelTest() throws Exception { 423 Model expectedResponse = 424 Model.newBuilder() 425 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 426 .setDisplayName("displayName1714148973") 427 .setCreateTime(Timestamp.newBuilder().build()) 428 .setUpdateTime(Timestamp.newBuilder().build()) 429 .setType("type3575610") 430 .setOptimizationObjective("optimizationObjective-1014459828") 431 .setLastTuneTime(Timestamp.newBuilder().build()) 432 .setTuningOperation("tuningOperation-1269747150") 433 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 434 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 435 .build(); 436 mockService.addResponse(expectedResponse); 437 438 String name = 439 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 440 441 Model actualResponse = client.resumeModel(name); 442 Assert.assertEquals(expectedResponse, actualResponse); 443 444 List<String> actualRequests = mockService.getRequestPaths(); 445 Assert.assertEquals(1, actualRequests.size()); 446 447 String apiClientHeaderKey = 448 mockService 449 .getRequestHeaders() 450 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 451 .iterator() 452 .next(); 453 Assert.assertTrue( 454 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 455 .matcher(apiClientHeaderKey) 456 .matches()); 457 } 458 459 @Test resumeModelExceptionTest()460 public void resumeModelExceptionTest() throws Exception { 461 ApiException exception = 462 ApiExceptionFactory.createException( 463 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 464 mockService.addException(exception); 465 466 try { 467 String name = 468 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 469 client.resumeModel(name); 470 Assert.fail("No exception raised"); 471 } catch (InvalidArgumentException e) { 472 // Expected exception. 473 } 474 } 475 476 @Test deleteModelTest()477 public void deleteModelTest() throws Exception { 478 Empty expectedResponse = Empty.newBuilder().build(); 479 mockService.addResponse(expectedResponse); 480 481 ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]"); 482 483 client.deleteModel(name); 484 485 List<String> actualRequests = mockService.getRequestPaths(); 486 Assert.assertEquals(1, actualRequests.size()); 487 488 String apiClientHeaderKey = 489 mockService 490 .getRequestHeaders() 491 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 492 .iterator() 493 .next(); 494 Assert.assertTrue( 495 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 496 .matcher(apiClientHeaderKey) 497 .matches()); 498 } 499 500 @Test deleteModelExceptionTest()501 public void deleteModelExceptionTest() throws Exception { 502 ApiException exception = 503 ApiExceptionFactory.createException( 504 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 505 mockService.addException(exception); 506 507 try { 508 ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]"); 509 client.deleteModel(name); 510 Assert.fail("No exception raised"); 511 } catch (InvalidArgumentException e) { 512 // Expected exception. 513 } 514 } 515 516 @Test deleteModelTest2()517 public void deleteModelTest2() throws Exception { 518 Empty expectedResponse = Empty.newBuilder().build(); 519 mockService.addResponse(expectedResponse); 520 521 String name = 522 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 523 524 client.deleteModel(name); 525 526 List<String> actualRequests = mockService.getRequestPaths(); 527 Assert.assertEquals(1, actualRequests.size()); 528 529 String apiClientHeaderKey = 530 mockService 531 .getRequestHeaders() 532 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 533 .iterator() 534 .next(); 535 Assert.assertTrue( 536 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 537 .matcher(apiClientHeaderKey) 538 .matches()); 539 } 540 541 @Test deleteModelExceptionTest2()542 public void deleteModelExceptionTest2() throws Exception { 543 ApiException exception = 544 ApiExceptionFactory.createException( 545 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 546 mockService.addException(exception); 547 548 try { 549 String name = 550 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 551 client.deleteModel(name); 552 Assert.fail("No exception raised"); 553 } catch (InvalidArgumentException e) { 554 // Expected exception. 555 } 556 } 557 558 @Test listModelsTest()559 public void listModelsTest() throws Exception { 560 Model responsesElement = Model.newBuilder().build(); 561 ListModelsResponse expectedResponse = 562 ListModelsResponse.newBuilder() 563 .setNextPageToken("") 564 .addAllModels(Arrays.asList(responsesElement)) 565 .build(); 566 mockService.addResponse(expectedResponse); 567 568 CatalogName parent = CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]"); 569 570 ListModelsPagedResponse pagedListResponse = client.listModels(parent); 571 572 List<Model> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 573 574 Assert.assertEquals(1, resources.size()); 575 Assert.assertEquals(expectedResponse.getModelsList().get(0), resources.get(0)); 576 577 List<String> actualRequests = mockService.getRequestPaths(); 578 Assert.assertEquals(1, actualRequests.size()); 579 580 String apiClientHeaderKey = 581 mockService 582 .getRequestHeaders() 583 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 584 .iterator() 585 .next(); 586 Assert.assertTrue( 587 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 588 .matcher(apiClientHeaderKey) 589 .matches()); 590 } 591 592 @Test listModelsExceptionTest()593 public void listModelsExceptionTest() throws Exception { 594 ApiException exception = 595 ApiExceptionFactory.createException( 596 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 597 mockService.addException(exception); 598 599 try { 600 CatalogName parent = CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]"); 601 client.listModels(parent); 602 Assert.fail("No exception raised"); 603 } catch (InvalidArgumentException e) { 604 // Expected exception. 605 } 606 } 607 608 @Test listModelsTest2()609 public void listModelsTest2() throws Exception { 610 Model responsesElement = Model.newBuilder().build(); 611 ListModelsResponse expectedResponse = 612 ListModelsResponse.newBuilder() 613 .setNextPageToken("") 614 .addAllModels(Arrays.asList(responsesElement)) 615 .build(); 616 mockService.addResponse(expectedResponse); 617 618 String parent = "projects/project-6267/locations/location-6267/catalogs/catalog-6267"; 619 620 ListModelsPagedResponse pagedListResponse = client.listModels(parent); 621 622 List<Model> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 623 624 Assert.assertEquals(1, resources.size()); 625 Assert.assertEquals(expectedResponse.getModelsList().get(0), resources.get(0)); 626 627 List<String> actualRequests = mockService.getRequestPaths(); 628 Assert.assertEquals(1, actualRequests.size()); 629 630 String apiClientHeaderKey = 631 mockService 632 .getRequestHeaders() 633 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 634 .iterator() 635 .next(); 636 Assert.assertTrue( 637 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 638 .matcher(apiClientHeaderKey) 639 .matches()); 640 } 641 642 @Test listModelsExceptionTest2()643 public void listModelsExceptionTest2() throws Exception { 644 ApiException exception = 645 ApiExceptionFactory.createException( 646 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 647 mockService.addException(exception); 648 649 try { 650 String parent = "projects/project-6267/locations/location-6267/catalogs/catalog-6267"; 651 client.listModels(parent); 652 Assert.fail("No exception raised"); 653 } catch (InvalidArgumentException e) { 654 // Expected exception. 655 } 656 } 657 658 @Test updateModelTest()659 public void updateModelTest() throws Exception { 660 Model expectedResponse = 661 Model.newBuilder() 662 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 663 .setDisplayName("displayName1714148973") 664 .setCreateTime(Timestamp.newBuilder().build()) 665 .setUpdateTime(Timestamp.newBuilder().build()) 666 .setType("type3575610") 667 .setOptimizationObjective("optimizationObjective-1014459828") 668 .setLastTuneTime(Timestamp.newBuilder().build()) 669 .setTuningOperation("tuningOperation-1269747150") 670 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 671 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 672 .build(); 673 mockService.addResponse(expectedResponse); 674 675 Model model = 676 Model.newBuilder() 677 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 678 .setDisplayName("displayName1714148973") 679 .setCreateTime(Timestamp.newBuilder().build()) 680 .setUpdateTime(Timestamp.newBuilder().build()) 681 .setType("type3575610") 682 .setOptimizationObjective("optimizationObjective-1014459828") 683 .setLastTuneTime(Timestamp.newBuilder().build()) 684 .setTuningOperation("tuningOperation-1269747150") 685 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 686 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 687 .build(); 688 FieldMask updateMask = FieldMask.newBuilder().build(); 689 690 Model actualResponse = client.updateModel(model, updateMask); 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 updateModelExceptionTest()709 public void updateModelExceptionTest() 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 Model model = 717 Model.newBuilder() 718 .setName(ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]").toString()) 719 .setDisplayName("displayName1714148973") 720 .setCreateTime(Timestamp.newBuilder().build()) 721 .setUpdateTime(Timestamp.newBuilder().build()) 722 .setType("type3575610") 723 .setOptimizationObjective("optimizationObjective-1014459828") 724 .setLastTuneTime(Timestamp.newBuilder().build()) 725 .setTuningOperation("tuningOperation-1269747150") 726 .setFilteringOption(RecommendationsFilteringOption.forNumber(0)) 727 .addAllServingConfigLists(new ArrayList<Model.ServingConfigList>()) 728 .build(); 729 FieldMask updateMask = FieldMask.newBuilder().build(); 730 client.updateModel(model, updateMask); 731 Assert.fail("No exception raised"); 732 } catch (InvalidArgumentException e) { 733 // Expected exception. 734 } 735 } 736 737 @Test tuneModelTest()738 public void tuneModelTest() throws Exception { 739 TuneModelResponse expectedResponse = TuneModelResponse.newBuilder().build(); 740 Operation resultOperation = 741 Operation.newBuilder() 742 .setName("tuneModelTest") 743 .setDone(true) 744 .setResponse(Any.pack(expectedResponse)) 745 .build(); 746 mockService.addResponse(resultOperation); 747 748 ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]"); 749 750 TuneModelResponse actualResponse = client.tuneModelAsync(name).get(); 751 Assert.assertEquals(expectedResponse, actualResponse); 752 753 List<String> actualRequests = mockService.getRequestPaths(); 754 Assert.assertEquals(1, actualRequests.size()); 755 756 String apiClientHeaderKey = 757 mockService 758 .getRequestHeaders() 759 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 760 .iterator() 761 .next(); 762 Assert.assertTrue( 763 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 764 .matcher(apiClientHeaderKey) 765 .matches()); 766 } 767 768 @Test tuneModelExceptionTest()769 public void tuneModelExceptionTest() throws Exception { 770 ApiException exception = 771 ApiExceptionFactory.createException( 772 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 773 mockService.addException(exception); 774 775 try { 776 ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[MODEL]"); 777 client.tuneModelAsync(name).get(); 778 Assert.fail("No exception raised"); 779 } catch (ExecutionException e) { 780 } 781 } 782 783 @Test tuneModelTest2()784 public void tuneModelTest2() throws Exception { 785 TuneModelResponse expectedResponse = TuneModelResponse.newBuilder().build(); 786 Operation resultOperation = 787 Operation.newBuilder() 788 .setName("tuneModelTest") 789 .setDone(true) 790 .setResponse(Any.pack(expectedResponse)) 791 .build(); 792 mockService.addResponse(resultOperation); 793 794 String name = 795 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 796 797 TuneModelResponse actualResponse = client.tuneModelAsync(name).get(); 798 Assert.assertEquals(expectedResponse, actualResponse); 799 800 List<String> actualRequests = mockService.getRequestPaths(); 801 Assert.assertEquals(1, actualRequests.size()); 802 803 String apiClientHeaderKey = 804 mockService 805 .getRequestHeaders() 806 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 807 .iterator() 808 .next(); 809 Assert.assertTrue( 810 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 811 .matcher(apiClientHeaderKey) 812 .matches()); 813 } 814 815 @Test tuneModelExceptionTest2()816 public void tuneModelExceptionTest2() throws Exception { 817 ApiException exception = 818 ApiExceptionFactory.createException( 819 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 820 mockService.addException(exception); 821 822 try { 823 String name = 824 "projects/project-7594/locations/location-7594/catalogs/catalog-7594/models/model-7594"; 825 client.tuneModelAsync(name).get(); 826 Assert.fail("No exception raised"); 827 } catch (ExecutionException e) { 828 } 829 } 830 } 831