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.discoveryengine.v1beta; 18 19 import static com.google.cloud.discoveryengine.v1beta.SchemaServiceClient.ListSchemasPagedResponse; 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.discoveryengine.v1beta.stub.HttpJsonSchemaServiceStub; 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 java.io.IOException; 36 import java.util.Arrays; 37 import java.util.List; 38 import java.util.concurrent.ExecutionException; 39 import javax.annotation.Generated; 40 import org.junit.After; 41 import org.junit.AfterClass; 42 import org.junit.Assert; 43 import org.junit.Before; 44 import org.junit.BeforeClass; 45 import org.junit.Test; 46 47 @Generated("by gapic-generator-java") 48 public class SchemaServiceClientHttpJsonTest { 49 private static MockHttpService mockService; 50 private static SchemaServiceClient client; 51 52 @BeforeClass startStaticServer()53 public static void startStaticServer() throws IOException { 54 mockService = 55 new MockHttpService( 56 HttpJsonSchemaServiceStub.getMethodDescriptors(), 57 SchemaServiceSettings.getDefaultEndpoint()); 58 SchemaServiceSettings settings = 59 SchemaServiceSettings.newHttpJsonBuilder() 60 .setTransportChannelProvider( 61 SchemaServiceSettings.defaultHttpJsonTransportProviderBuilder() 62 .setHttpTransport(mockService) 63 .build()) 64 .setCredentialsProvider(NoCredentialsProvider.create()) 65 .build(); 66 client = SchemaServiceClient.create(settings); 67 } 68 69 @AfterClass stopServer()70 public static void stopServer() { 71 client.close(); 72 } 73 74 @Before setUp()75 public void setUp() {} 76 77 @After tearDown()78 public void tearDown() throws Exception { 79 mockService.reset(); 80 } 81 82 @Test getSchemaTest()83 public void getSchemaTest() throws Exception { 84 Schema expectedResponse = 85 Schema.newBuilder() 86 .setName( 87 SchemaName.ofProjectLocationDataStoreSchemaName( 88 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]") 89 .toString()) 90 .build(); 91 mockService.addResponse(expectedResponse); 92 93 SchemaName name = 94 SchemaName.ofProjectLocationDataStoreSchemaName( 95 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]"); 96 97 Schema actualResponse = client.getSchema(name); 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 getSchemaExceptionTest()116 public void getSchemaExceptionTest() 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 SchemaName name = 124 SchemaName.ofProjectLocationDataStoreSchemaName( 125 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]"); 126 client.getSchema(name); 127 Assert.fail("No exception raised"); 128 } catch (InvalidArgumentException e) { 129 // Expected exception. 130 } 131 } 132 133 @Test getSchemaTest2()134 public void getSchemaTest2() throws Exception { 135 Schema expectedResponse = 136 Schema.newBuilder() 137 .setName( 138 SchemaName.ofProjectLocationDataStoreSchemaName( 139 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]") 140 .toString()) 141 .build(); 142 mockService.addResponse(expectedResponse); 143 144 String name = 145 "projects/project-3094/locations/location-3094/dataStores/dataStore-3094/schemas/schema-3094"; 146 147 Schema actualResponse = client.getSchema(name); 148 Assert.assertEquals(expectedResponse, actualResponse); 149 150 List<String> actualRequests = mockService.getRequestPaths(); 151 Assert.assertEquals(1, actualRequests.size()); 152 153 String apiClientHeaderKey = 154 mockService 155 .getRequestHeaders() 156 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 157 .iterator() 158 .next(); 159 Assert.assertTrue( 160 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 161 .matcher(apiClientHeaderKey) 162 .matches()); 163 } 164 165 @Test getSchemaExceptionTest2()166 public void getSchemaExceptionTest2() throws Exception { 167 ApiException exception = 168 ApiExceptionFactory.createException( 169 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 170 mockService.addException(exception); 171 172 try { 173 String name = 174 "projects/project-3094/locations/location-3094/dataStores/dataStore-3094/schemas/schema-3094"; 175 client.getSchema(name); 176 Assert.fail("No exception raised"); 177 } catch (InvalidArgumentException e) { 178 // Expected exception. 179 } 180 } 181 182 @Test listSchemasTest()183 public void listSchemasTest() throws Exception { 184 Schema responsesElement = Schema.newBuilder().build(); 185 ListSchemasResponse expectedResponse = 186 ListSchemasResponse.newBuilder() 187 .setNextPageToken("") 188 .addAllSchemas(Arrays.asList(responsesElement)) 189 .build(); 190 mockService.addResponse(expectedResponse); 191 192 DataStoreName parent = 193 DataStoreName.ofProjectLocationDataStoreName("[PROJECT]", "[LOCATION]", "[DATA_STORE]"); 194 195 ListSchemasPagedResponse pagedListResponse = client.listSchemas(parent); 196 197 List<Schema> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 198 199 Assert.assertEquals(1, resources.size()); 200 Assert.assertEquals(expectedResponse.getSchemasList().get(0), resources.get(0)); 201 202 List<String> actualRequests = mockService.getRequestPaths(); 203 Assert.assertEquals(1, actualRequests.size()); 204 205 String apiClientHeaderKey = 206 mockService 207 .getRequestHeaders() 208 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 209 .iterator() 210 .next(); 211 Assert.assertTrue( 212 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 213 .matcher(apiClientHeaderKey) 214 .matches()); 215 } 216 217 @Test listSchemasExceptionTest()218 public void listSchemasExceptionTest() throws Exception { 219 ApiException exception = 220 ApiExceptionFactory.createException( 221 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 222 mockService.addException(exception); 223 224 try { 225 DataStoreName parent = 226 DataStoreName.ofProjectLocationDataStoreName("[PROJECT]", "[LOCATION]", "[DATA_STORE]"); 227 client.listSchemas(parent); 228 Assert.fail("No exception raised"); 229 } catch (InvalidArgumentException e) { 230 // Expected exception. 231 } 232 } 233 234 @Test listSchemasTest2()235 public void listSchemasTest2() throws Exception { 236 Schema responsesElement = Schema.newBuilder().build(); 237 ListSchemasResponse expectedResponse = 238 ListSchemasResponse.newBuilder() 239 .setNextPageToken("") 240 .addAllSchemas(Arrays.asList(responsesElement)) 241 .build(); 242 mockService.addResponse(expectedResponse); 243 244 String parent = "projects/project-3005/locations/location-3005/dataStores/dataStore-3005"; 245 246 ListSchemasPagedResponse pagedListResponse = client.listSchemas(parent); 247 248 List<Schema> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 249 250 Assert.assertEquals(1, resources.size()); 251 Assert.assertEquals(expectedResponse.getSchemasList().get(0), resources.get(0)); 252 253 List<String> actualRequests = mockService.getRequestPaths(); 254 Assert.assertEquals(1, actualRequests.size()); 255 256 String apiClientHeaderKey = 257 mockService 258 .getRequestHeaders() 259 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 260 .iterator() 261 .next(); 262 Assert.assertTrue( 263 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 264 .matcher(apiClientHeaderKey) 265 .matches()); 266 } 267 268 @Test listSchemasExceptionTest2()269 public void listSchemasExceptionTest2() throws Exception { 270 ApiException exception = 271 ApiExceptionFactory.createException( 272 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 273 mockService.addException(exception); 274 275 try { 276 String parent = "projects/project-3005/locations/location-3005/dataStores/dataStore-3005"; 277 client.listSchemas(parent); 278 Assert.fail("No exception raised"); 279 } catch (InvalidArgumentException e) { 280 // Expected exception. 281 } 282 } 283 284 @Test createSchemaTest()285 public void createSchemaTest() throws Exception { 286 Schema expectedResponse = 287 Schema.newBuilder() 288 .setName( 289 SchemaName.ofProjectLocationDataStoreSchemaName( 290 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]") 291 .toString()) 292 .build(); 293 Operation resultOperation = 294 Operation.newBuilder() 295 .setName("createSchemaTest") 296 .setDone(true) 297 .setResponse(Any.pack(expectedResponse)) 298 .build(); 299 mockService.addResponse(resultOperation); 300 301 DataStoreName parent = 302 DataStoreName.ofProjectLocationDataStoreName("[PROJECT]", "[LOCATION]", "[DATA_STORE]"); 303 Schema schema = Schema.newBuilder().build(); 304 String schemaId = "schemaId-697673060"; 305 306 Schema actualResponse = client.createSchemaAsync(parent, schema, schemaId).get(); 307 Assert.assertEquals(expectedResponse, actualResponse); 308 309 List<String> actualRequests = mockService.getRequestPaths(); 310 Assert.assertEquals(1, actualRequests.size()); 311 312 String apiClientHeaderKey = 313 mockService 314 .getRequestHeaders() 315 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 316 .iterator() 317 .next(); 318 Assert.assertTrue( 319 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 320 .matcher(apiClientHeaderKey) 321 .matches()); 322 } 323 324 @Test createSchemaExceptionTest()325 public void createSchemaExceptionTest() throws Exception { 326 ApiException exception = 327 ApiExceptionFactory.createException( 328 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 329 mockService.addException(exception); 330 331 try { 332 DataStoreName parent = 333 DataStoreName.ofProjectLocationDataStoreName("[PROJECT]", "[LOCATION]", "[DATA_STORE]"); 334 Schema schema = Schema.newBuilder().build(); 335 String schemaId = "schemaId-697673060"; 336 client.createSchemaAsync(parent, schema, schemaId).get(); 337 Assert.fail("No exception raised"); 338 } catch (ExecutionException e) { 339 } 340 } 341 342 @Test createSchemaTest2()343 public void createSchemaTest2() throws Exception { 344 Schema expectedResponse = 345 Schema.newBuilder() 346 .setName( 347 SchemaName.ofProjectLocationDataStoreSchemaName( 348 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]") 349 .toString()) 350 .build(); 351 Operation resultOperation = 352 Operation.newBuilder() 353 .setName("createSchemaTest") 354 .setDone(true) 355 .setResponse(Any.pack(expectedResponse)) 356 .build(); 357 mockService.addResponse(resultOperation); 358 359 String parent = "projects/project-3005/locations/location-3005/dataStores/dataStore-3005"; 360 Schema schema = Schema.newBuilder().build(); 361 String schemaId = "schemaId-697673060"; 362 363 Schema actualResponse = client.createSchemaAsync(parent, schema, schemaId).get(); 364 Assert.assertEquals(expectedResponse, actualResponse); 365 366 List<String> actualRequests = mockService.getRequestPaths(); 367 Assert.assertEquals(1, actualRequests.size()); 368 369 String apiClientHeaderKey = 370 mockService 371 .getRequestHeaders() 372 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 373 .iterator() 374 .next(); 375 Assert.assertTrue( 376 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 377 .matcher(apiClientHeaderKey) 378 .matches()); 379 } 380 381 @Test createSchemaExceptionTest2()382 public void createSchemaExceptionTest2() throws Exception { 383 ApiException exception = 384 ApiExceptionFactory.createException( 385 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 386 mockService.addException(exception); 387 388 try { 389 String parent = "projects/project-3005/locations/location-3005/dataStores/dataStore-3005"; 390 Schema schema = Schema.newBuilder().build(); 391 String schemaId = "schemaId-697673060"; 392 client.createSchemaAsync(parent, schema, schemaId).get(); 393 Assert.fail("No exception raised"); 394 } catch (ExecutionException e) { 395 } 396 } 397 398 @Test updateSchemaTest()399 public void updateSchemaTest() throws Exception { 400 Schema expectedResponse = 401 Schema.newBuilder() 402 .setName( 403 SchemaName.ofProjectLocationDataStoreSchemaName( 404 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]") 405 .toString()) 406 .build(); 407 Operation resultOperation = 408 Operation.newBuilder() 409 .setName("updateSchemaTest") 410 .setDone(true) 411 .setResponse(Any.pack(expectedResponse)) 412 .build(); 413 mockService.addResponse(resultOperation); 414 415 UpdateSchemaRequest request = 416 UpdateSchemaRequest.newBuilder() 417 .setSchema( 418 Schema.newBuilder() 419 .setName( 420 SchemaName.ofProjectLocationDataStoreSchemaName( 421 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]") 422 .toString()) 423 .build()) 424 .setAllowMissing(true) 425 .build(); 426 427 Schema actualResponse = client.updateSchemaAsync(request).get(); 428 Assert.assertEquals(expectedResponse, actualResponse); 429 430 List<String> actualRequests = mockService.getRequestPaths(); 431 Assert.assertEquals(1, actualRequests.size()); 432 433 String apiClientHeaderKey = 434 mockService 435 .getRequestHeaders() 436 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 437 .iterator() 438 .next(); 439 Assert.assertTrue( 440 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 441 .matcher(apiClientHeaderKey) 442 .matches()); 443 } 444 445 @Test updateSchemaExceptionTest()446 public void updateSchemaExceptionTest() throws Exception { 447 ApiException exception = 448 ApiExceptionFactory.createException( 449 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 450 mockService.addException(exception); 451 452 try { 453 UpdateSchemaRequest request = 454 UpdateSchemaRequest.newBuilder() 455 .setSchema( 456 Schema.newBuilder() 457 .setName( 458 SchemaName.ofProjectLocationDataStoreSchemaName( 459 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]") 460 .toString()) 461 .build()) 462 .setAllowMissing(true) 463 .build(); 464 client.updateSchemaAsync(request).get(); 465 Assert.fail("No exception raised"); 466 } catch (ExecutionException e) { 467 } 468 } 469 470 @Test deleteSchemaTest()471 public void deleteSchemaTest() throws Exception { 472 Empty expectedResponse = Empty.newBuilder().build(); 473 Operation resultOperation = 474 Operation.newBuilder() 475 .setName("deleteSchemaTest") 476 .setDone(true) 477 .setResponse(Any.pack(expectedResponse)) 478 .build(); 479 mockService.addResponse(resultOperation); 480 481 SchemaName name = 482 SchemaName.ofProjectLocationDataStoreSchemaName( 483 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]"); 484 485 client.deleteSchemaAsync(name).get(); 486 487 List<String> actualRequests = mockService.getRequestPaths(); 488 Assert.assertEquals(1, actualRequests.size()); 489 490 String apiClientHeaderKey = 491 mockService 492 .getRequestHeaders() 493 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 494 .iterator() 495 .next(); 496 Assert.assertTrue( 497 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 498 .matcher(apiClientHeaderKey) 499 .matches()); 500 } 501 502 @Test deleteSchemaExceptionTest()503 public void deleteSchemaExceptionTest() throws Exception { 504 ApiException exception = 505 ApiExceptionFactory.createException( 506 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 507 mockService.addException(exception); 508 509 try { 510 SchemaName name = 511 SchemaName.ofProjectLocationDataStoreSchemaName( 512 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]"); 513 client.deleteSchemaAsync(name).get(); 514 Assert.fail("No exception raised"); 515 } catch (ExecutionException e) { 516 } 517 } 518 519 @Test deleteSchemaTest2()520 public void deleteSchemaTest2() throws Exception { 521 Empty expectedResponse = Empty.newBuilder().build(); 522 Operation resultOperation = 523 Operation.newBuilder() 524 .setName("deleteSchemaTest") 525 .setDone(true) 526 .setResponse(Any.pack(expectedResponse)) 527 .build(); 528 mockService.addResponse(resultOperation); 529 530 String name = 531 "projects/project-3094/locations/location-3094/dataStores/dataStore-3094/schemas/schema-3094"; 532 533 client.deleteSchemaAsync(name).get(); 534 535 List<String> actualRequests = mockService.getRequestPaths(); 536 Assert.assertEquals(1, actualRequests.size()); 537 538 String apiClientHeaderKey = 539 mockService 540 .getRequestHeaders() 541 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 542 .iterator() 543 .next(); 544 Assert.assertTrue( 545 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 546 .matcher(apiClientHeaderKey) 547 .matches()); 548 } 549 550 @Test deleteSchemaExceptionTest2()551 public void deleteSchemaExceptionTest2() throws Exception { 552 ApiException exception = 553 ApiExceptionFactory.createException( 554 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 555 mockService.addException(exception); 556 557 try { 558 String name = 559 "projects/project-3094/locations/location-3094/dataStores/dataStore-3094/schemas/schema-3094"; 560 client.deleteSchemaAsync(name).get(); 561 Assert.fail("No exception raised"); 562 } catch (ExecutionException e) { 563 } 564 } 565 } 566