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.ControlServiceClient.ListControlsPagedResponse; 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.HttpJsonControlServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.protobuf.Empty; 33 import com.google.protobuf.FieldMask; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.Arrays; 37 import java.util.List; 38 import javax.annotation.Generated; 39 import org.junit.After; 40 import org.junit.AfterClass; 41 import org.junit.Assert; 42 import org.junit.Before; 43 import org.junit.BeforeClass; 44 import org.junit.Test; 45 46 @Generated("by gapic-generator-java") 47 public class ControlServiceClientHttpJsonTest { 48 private static MockHttpService mockService; 49 private static ControlServiceClient client; 50 51 @BeforeClass startStaticServer()52 public static void startStaticServer() throws IOException { 53 mockService = 54 new MockHttpService( 55 HttpJsonControlServiceStub.getMethodDescriptors(), 56 ControlServiceSettings.getDefaultEndpoint()); 57 ControlServiceSettings settings = 58 ControlServiceSettings.newHttpJsonBuilder() 59 .setTransportChannelProvider( 60 ControlServiceSettings.defaultHttpJsonTransportProviderBuilder() 61 .setHttpTransport(mockService) 62 .build()) 63 .setCredentialsProvider(NoCredentialsProvider.create()) 64 .build(); 65 client = ControlServiceClient.create(settings); 66 } 67 68 @AfterClass stopServer()69 public static void stopServer() { 70 client.close(); 71 } 72 73 @Before setUp()74 public void setUp() {} 75 76 @After tearDown()77 public void tearDown() throws Exception { 78 mockService.reset(); 79 } 80 81 @Test createControlTest()82 public void createControlTest() throws Exception { 83 Control expectedResponse = 84 Control.newBuilder() 85 .setName(ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]").toString()) 86 .setDisplayName("displayName1714148973") 87 .addAllAssociatedServingConfigIds(new ArrayList<String>()) 88 .addAllSolutionTypes(new ArrayList<SolutionType>()) 89 .addAllSearchSolutionUseCase(new ArrayList<SearchSolutionUseCase>()) 90 .build(); 91 mockService.addResponse(expectedResponse); 92 93 CatalogName parent = CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]"); 94 Control control = Control.newBuilder().build(); 95 String controlId = "controlId-395080872"; 96 97 Control actualResponse = client.createControl(parent, control, controlId); 98 Assert.assertEquals(expectedResponse, actualResponse); 99 100 List<String> actualRequests = mockService.getRequestPaths(); 101 Assert.assertEquals(1, actualRequests.size()); 102 103 String apiClientHeaderKey = 104 mockService 105 .getRequestHeaders() 106 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 107 .iterator() 108 .next(); 109 Assert.assertTrue( 110 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 111 .matcher(apiClientHeaderKey) 112 .matches()); 113 } 114 115 @Test createControlExceptionTest()116 public void createControlExceptionTest() throws Exception { 117 ApiException exception = 118 ApiExceptionFactory.createException( 119 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 120 mockService.addException(exception); 121 122 try { 123 CatalogName parent = CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]"); 124 Control control = Control.newBuilder().build(); 125 String controlId = "controlId-395080872"; 126 client.createControl(parent, control, controlId); 127 Assert.fail("No exception raised"); 128 } catch (InvalidArgumentException e) { 129 // Expected exception. 130 } 131 } 132 133 @Test createControlTest2()134 public void createControlTest2() throws Exception { 135 Control expectedResponse = 136 Control.newBuilder() 137 .setName(ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]").toString()) 138 .setDisplayName("displayName1714148973") 139 .addAllAssociatedServingConfigIds(new ArrayList<String>()) 140 .addAllSolutionTypes(new ArrayList<SolutionType>()) 141 .addAllSearchSolutionUseCase(new ArrayList<SearchSolutionUseCase>()) 142 .build(); 143 mockService.addResponse(expectedResponse); 144 145 String parent = "projects/project-6267/locations/location-6267/catalogs/catalog-6267"; 146 Control control = Control.newBuilder().build(); 147 String controlId = "controlId-395080872"; 148 149 Control actualResponse = client.createControl(parent, control, controlId); 150 Assert.assertEquals(expectedResponse, actualResponse); 151 152 List<String> actualRequests = mockService.getRequestPaths(); 153 Assert.assertEquals(1, actualRequests.size()); 154 155 String apiClientHeaderKey = 156 mockService 157 .getRequestHeaders() 158 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 159 .iterator() 160 .next(); 161 Assert.assertTrue( 162 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 163 .matcher(apiClientHeaderKey) 164 .matches()); 165 } 166 167 @Test createControlExceptionTest2()168 public void createControlExceptionTest2() throws Exception { 169 ApiException exception = 170 ApiExceptionFactory.createException( 171 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 172 mockService.addException(exception); 173 174 try { 175 String parent = "projects/project-6267/locations/location-6267/catalogs/catalog-6267"; 176 Control control = Control.newBuilder().build(); 177 String controlId = "controlId-395080872"; 178 client.createControl(parent, control, controlId); 179 Assert.fail("No exception raised"); 180 } catch (InvalidArgumentException e) { 181 // Expected exception. 182 } 183 } 184 185 @Test deleteControlTest()186 public void deleteControlTest() throws Exception { 187 Empty expectedResponse = Empty.newBuilder().build(); 188 mockService.addResponse(expectedResponse); 189 190 ControlName name = ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]"); 191 192 client.deleteControl(name); 193 194 List<String> actualRequests = mockService.getRequestPaths(); 195 Assert.assertEquals(1, actualRequests.size()); 196 197 String apiClientHeaderKey = 198 mockService 199 .getRequestHeaders() 200 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 201 .iterator() 202 .next(); 203 Assert.assertTrue( 204 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 205 .matcher(apiClientHeaderKey) 206 .matches()); 207 } 208 209 @Test deleteControlExceptionTest()210 public void deleteControlExceptionTest() throws Exception { 211 ApiException exception = 212 ApiExceptionFactory.createException( 213 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 214 mockService.addException(exception); 215 216 try { 217 ControlName name = ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]"); 218 client.deleteControl(name); 219 Assert.fail("No exception raised"); 220 } catch (InvalidArgumentException e) { 221 // Expected exception. 222 } 223 } 224 225 @Test deleteControlTest2()226 public void deleteControlTest2() throws Exception { 227 Empty expectedResponse = Empty.newBuilder().build(); 228 mockService.addResponse(expectedResponse); 229 230 String name = 231 "projects/project-7366/locations/location-7366/catalogs/catalog-7366/controls/control-7366"; 232 233 client.deleteControl(name); 234 235 List<String> actualRequests = mockService.getRequestPaths(); 236 Assert.assertEquals(1, actualRequests.size()); 237 238 String apiClientHeaderKey = 239 mockService 240 .getRequestHeaders() 241 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 242 .iterator() 243 .next(); 244 Assert.assertTrue( 245 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 246 .matcher(apiClientHeaderKey) 247 .matches()); 248 } 249 250 @Test deleteControlExceptionTest2()251 public void deleteControlExceptionTest2() throws Exception { 252 ApiException exception = 253 ApiExceptionFactory.createException( 254 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 255 mockService.addException(exception); 256 257 try { 258 String name = 259 "projects/project-7366/locations/location-7366/catalogs/catalog-7366/controls/control-7366"; 260 client.deleteControl(name); 261 Assert.fail("No exception raised"); 262 } catch (InvalidArgumentException e) { 263 // Expected exception. 264 } 265 } 266 267 @Test updateControlTest()268 public void updateControlTest() throws Exception { 269 Control expectedResponse = 270 Control.newBuilder() 271 .setName(ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]").toString()) 272 .setDisplayName("displayName1714148973") 273 .addAllAssociatedServingConfigIds(new ArrayList<String>()) 274 .addAllSolutionTypes(new ArrayList<SolutionType>()) 275 .addAllSearchSolutionUseCase(new ArrayList<SearchSolutionUseCase>()) 276 .build(); 277 mockService.addResponse(expectedResponse); 278 279 Control control = 280 Control.newBuilder() 281 .setName(ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]").toString()) 282 .setDisplayName("displayName1714148973") 283 .addAllAssociatedServingConfigIds(new ArrayList<String>()) 284 .addAllSolutionTypes(new ArrayList<SolutionType>()) 285 .addAllSearchSolutionUseCase(new ArrayList<SearchSolutionUseCase>()) 286 .build(); 287 FieldMask updateMask = FieldMask.newBuilder().build(); 288 289 Control actualResponse = client.updateControl(control, updateMask); 290 Assert.assertEquals(expectedResponse, actualResponse); 291 292 List<String> actualRequests = mockService.getRequestPaths(); 293 Assert.assertEquals(1, actualRequests.size()); 294 295 String apiClientHeaderKey = 296 mockService 297 .getRequestHeaders() 298 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 299 .iterator() 300 .next(); 301 Assert.assertTrue( 302 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 303 .matcher(apiClientHeaderKey) 304 .matches()); 305 } 306 307 @Test updateControlExceptionTest()308 public void updateControlExceptionTest() throws Exception { 309 ApiException exception = 310 ApiExceptionFactory.createException( 311 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 312 mockService.addException(exception); 313 314 try { 315 Control control = 316 Control.newBuilder() 317 .setName( 318 ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]").toString()) 319 .setDisplayName("displayName1714148973") 320 .addAllAssociatedServingConfigIds(new ArrayList<String>()) 321 .addAllSolutionTypes(new ArrayList<SolutionType>()) 322 .addAllSearchSolutionUseCase(new ArrayList<SearchSolutionUseCase>()) 323 .build(); 324 FieldMask updateMask = FieldMask.newBuilder().build(); 325 client.updateControl(control, updateMask); 326 Assert.fail("No exception raised"); 327 } catch (InvalidArgumentException e) { 328 // Expected exception. 329 } 330 } 331 332 @Test getControlTest()333 public void getControlTest() throws Exception { 334 Control expectedResponse = 335 Control.newBuilder() 336 .setName(ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]").toString()) 337 .setDisplayName("displayName1714148973") 338 .addAllAssociatedServingConfigIds(new ArrayList<String>()) 339 .addAllSolutionTypes(new ArrayList<SolutionType>()) 340 .addAllSearchSolutionUseCase(new ArrayList<SearchSolutionUseCase>()) 341 .build(); 342 mockService.addResponse(expectedResponse); 343 344 ControlName name = ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]"); 345 346 Control actualResponse = client.getControl(name); 347 Assert.assertEquals(expectedResponse, actualResponse); 348 349 List<String> actualRequests = mockService.getRequestPaths(); 350 Assert.assertEquals(1, actualRequests.size()); 351 352 String apiClientHeaderKey = 353 mockService 354 .getRequestHeaders() 355 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 356 .iterator() 357 .next(); 358 Assert.assertTrue( 359 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 360 .matcher(apiClientHeaderKey) 361 .matches()); 362 } 363 364 @Test getControlExceptionTest()365 public void getControlExceptionTest() throws Exception { 366 ApiException exception = 367 ApiExceptionFactory.createException( 368 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 369 mockService.addException(exception); 370 371 try { 372 ControlName name = ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]"); 373 client.getControl(name); 374 Assert.fail("No exception raised"); 375 } catch (InvalidArgumentException e) { 376 // Expected exception. 377 } 378 } 379 380 @Test getControlTest2()381 public void getControlTest2() throws Exception { 382 Control expectedResponse = 383 Control.newBuilder() 384 .setName(ControlName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[CONTROL]").toString()) 385 .setDisplayName("displayName1714148973") 386 .addAllAssociatedServingConfigIds(new ArrayList<String>()) 387 .addAllSolutionTypes(new ArrayList<SolutionType>()) 388 .addAllSearchSolutionUseCase(new ArrayList<SearchSolutionUseCase>()) 389 .build(); 390 mockService.addResponse(expectedResponse); 391 392 String name = 393 "projects/project-7366/locations/location-7366/catalogs/catalog-7366/controls/control-7366"; 394 395 Control actualResponse = client.getControl(name); 396 Assert.assertEquals(expectedResponse, actualResponse); 397 398 List<String> actualRequests = mockService.getRequestPaths(); 399 Assert.assertEquals(1, actualRequests.size()); 400 401 String apiClientHeaderKey = 402 mockService 403 .getRequestHeaders() 404 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 405 .iterator() 406 .next(); 407 Assert.assertTrue( 408 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 409 .matcher(apiClientHeaderKey) 410 .matches()); 411 } 412 413 @Test getControlExceptionTest2()414 public void getControlExceptionTest2() throws Exception { 415 ApiException exception = 416 ApiExceptionFactory.createException( 417 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 418 mockService.addException(exception); 419 420 try { 421 String name = 422 "projects/project-7366/locations/location-7366/catalogs/catalog-7366/controls/control-7366"; 423 client.getControl(name); 424 Assert.fail("No exception raised"); 425 } catch (InvalidArgumentException e) { 426 // Expected exception. 427 } 428 } 429 430 @Test listControlsTest()431 public void listControlsTest() throws Exception { 432 Control responsesElement = Control.newBuilder().build(); 433 ListControlsResponse expectedResponse = 434 ListControlsResponse.newBuilder() 435 .setNextPageToken("") 436 .addAllControls(Arrays.asList(responsesElement)) 437 .build(); 438 mockService.addResponse(expectedResponse); 439 440 CatalogName parent = CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]"); 441 442 ListControlsPagedResponse pagedListResponse = client.listControls(parent); 443 444 List<Control> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 445 446 Assert.assertEquals(1, resources.size()); 447 Assert.assertEquals(expectedResponse.getControlsList().get(0), resources.get(0)); 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 listControlsExceptionTest()465 public void listControlsExceptionTest() 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 CatalogName parent = CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]"); 473 client.listControls(parent); 474 Assert.fail("No exception raised"); 475 } catch (InvalidArgumentException e) { 476 // Expected exception. 477 } 478 } 479 480 @Test listControlsTest2()481 public void listControlsTest2() throws Exception { 482 Control responsesElement = Control.newBuilder().build(); 483 ListControlsResponse expectedResponse = 484 ListControlsResponse.newBuilder() 485 .setNextPageToken("") 486 .addAllControls(Arrays.asList(responsesElement)) 487 .build(); 488 mockService.addResponse(expectedResponse); 489 490 String parent = "projects/project-6267/locations/location-6267/catalogs/catalog-6267"; 491 492 ListControlsPagedResponse pagedListResponse = client.listControls(parent); 493 494 List<Control> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 495 496 Assert.assertEquals(1, resources.size()); 497 Assert.assertEquals(expectedResponse.getControlsList().get(0), resources.get(0)); 498 499 List<String> actualRequests = mockService.getRequestPaths(); 500 Assert.assertEquals(1, actualRequests.size()); 501 502 String apiClientHeaderKey = 503 mockService 504 .getRequestHeaders() 505 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 506 .iterator() 507 .next(); 508 Assert.assertTrue( 509 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 510 .matcher(apiClientHeaderKey) 511 .matches()); 512 } 513 514 @Test listControlsExceptionTest2()515 public void listControlsExceptionTest2() throws Exception { 516 ApiException exception = 517 ApiExceptionFactory.createException( 518 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 519 mockService.addException(exception); 520 521 try { 522 String parent = "projects/project-6267/locations/location-6267/catalogs/catalog-6267"; 523 client.listControls(parent); 524 Assert.fail("No exception raised"); 525 } catch (InvalidArgumentException e) { 526 // Expected exception. 527 } 528 } 529 } 530