• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.grpc.GaxGrpcProperties;
23 import com.google.api.gax.grpc.testing.LocalChannelProvider;
24 import com.google.api.gax.grpc.testing.MockGrpcService;
25 import com.google.api.gax.grpc.testing.MockServiceHelper;
26 import com.google.api.gax.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.api.gax.rpc.StatusCode;
29 import com.google.common.collect.Lists;
30 import com.google.longrunning.Operation;
31 import com.google.protobuf.AbstractMessage;
32 import com.google.protobuf.Any;
33 import com.google.protobuf.Empty;
34 import io.grpc.StatusRuntimeException;
35 import java.io.IOException;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.UUID;
39 import java.util.concurrent.ExecutionException;
40 import javax.annotation.Generated;
41 import org.junit.After;
42 import org.junit.AfterClass;
43 import org.junit.Assert;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 
48 @Generated("by gapic-generator-java")
49 public class SchemaServiceClientTest {
50   private static MockSchemaService mockSchemaService;
51   private static MockServiceHelper mockServiceHelper;
52   private LocalChannelProvider channelProvider;
53   private SchemaServiceClient client;
54 
55   @BeforeClass
startStaticServer()56   public static void startStaticServer() {
57     mockSchemaService = new MockSchemaService();
58     mockServiceHelper =
59         new MockServiceHelper(
60             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockSchemaService));
61     mockServiceHelper.start();
62   }
63 
64   @AfterClass
stopServer()65   public static void stopServer() {
66     mockServiceHelper.stop();
67   }
68 
69   @Before
setUp()70   public void setUp() throws IOException {
71     mockServiceHelper.reset();
72     channelProvider = mockServiceHelper.createChannelProvider();
73     SchemaServiceSettings settings =
74         SchemaServiceSettings.newBuilder()
75             .setTransportChannelProvider(channelProvider)
76             .setCredentialsProvider(NoCredentialsProvider.create())
77             .build();
78     client = SchemaServiceClient.create(settings);
79   }
80 
81   @After
tearDown()82   public void tearDown() throws Exception {
83     client.close();
84   }
85 
86   @Test
getSchemaTest()87   public void getSchemaTest() throws Exception {
88     Schema expectedResponse =
89         Schema.newBuilder()
90             .setName(
91                 SchemaName.ofProjectLocationDataStoreSchemaName(
92                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]")
93                     .toString())
94             .build();
95     mockSchemaService.addResponse(expectedResponse);
96 
97     SchemaName name =
98         SchemaName.ofProjectLocationDataStoreSchemaName(
99             "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]");
100 
101     Schema actualResponse = client.getSchema(name);
102     Assert.assertEquals(expectedResponse, actualResponse);
103 
104     List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
105     Assert.assertEquals(1, actualRequests.size());
106     GetSchemaRequest actualRequest = ((GetSchemaRequest) actualRequests.get(0));
107 
108     Assert.assertEquals(name.toString(), actualRequest.getName());
109     Assert.assertTrue(
110         channelProvider.isHeaderSent(
111             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
112             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
113   }
114 
115   @Test
getSchemaExceptionTest()116   public void getSchemaExceptionTest() throws Exception {
117     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
118     mockSchemaService.addException(exception);
119 
120     try {
121       SchemaName name =
122           SchemaName.ofProjectLocationDataStoreSchemaName(
123               "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]");
124       client.getSchema(name);
125       Assert.fail("No exception raised");
126     } catch (InvalidArgumentException e) {
127       // Expected exception.
128     }
129   }
130 
131   @Test
getSchemaTest2()132   public void getSchemaTest2() throws Exception {
133     Schema expectedResponse =
134         Schema.newBuilder()
135             .setName(
136                 SchemaName.ofProjectLocationDataStoreSchemaName(
137                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]")
138                     .toString())
139             .build();
140     mockSchemaService.addResponse(expectedResponse);
141 
142     String name = "name3373707";
143 
144     Schema actualResponse = client.getSchema(name);
145     Assert.assertEquals(expectedResponse, actualResponse);
146 
147     List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
148     Assert.assertEquals(1, actualRequests.size());
149     GetSchemaRequest actualRequest = ((GetSchemaRequest) actualRequests.get(0));
150 
151     Assert.assertEquals(name, actualRequest.getName());
152     Assert.assertTrue(
153         channelProvider.isHeaderSent(
154             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
155             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
156   }
157 
158   @Test
getSchemaExceptionTest2()159   public void getSchemaExceptionTest2() throws Exception {
160     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
161     mockSchemaService.addException(exception);
162 
163     try {
164       String name = "name3373707";
165       client.getSchema(name);
166       Assert.fail("No exception raised");
167     } catch (InvalidArgumentException e) {
168       // Expected exception.
169     }
170   }
171 
172   @Test
listSchemasTest()173   public void listSchemasTest() throws Exception {
174     Schema responsesElement = Schema.newBuilder().build();
175     ListSchemasResponse expectedResponse =
176         ListSchemasResponse.newBuilder()
177             .setNextPageToken("")
178             .addAllSchemas(Arrays.asList(responsesElement))
179             .build();
180     mockSchemaService.addResponse(expectedResponse);
181 
182     DataStoreName parent =
183         DataStoreName.ofProjectLocationDataStoreName("[PROJECT]", "[LOCATION]", "[DATA_STORE]");
184 
185     ListSchemasPagedResponse pagedListResponse = client.listSchemas(parent);
186 
187     List<Schema> resources = Lists.newArrayList(pagedListResponse.iterateAll());
188 
189     Assert.assertEquals(1, resources.size());
190     Assert.assertEquals(expectedResponse.getSchemasList().get(0), resources.get(0));
191 
192     List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
193     Assert.assertEquals(1, actualRequests.size());
194     ListSchemasRequest actualRequest = ((ListSchemasRequest) actualRequests.get(0));
195 
196     Assert.assertEquals(parent.toString(), actualRequest.getParent());
197     Assert.assertTrue(
198         channelProvider.isHeaderSent(
199             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
200             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
201   }
202 
203   @Test
listSchemasExceptionTest()204   public void listSchemasExceptionTest() throws Exception {
205     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
206     mockSchemaService.addException(exception);
207 
208     try {
209       DataStoreName parent =
210           DataStoreName.ofProjectLocationDataStoreName("[PROJECT]", "[LOCATION]", "[DATA_STORE]");
211       client.listSchemas(parent);
212       Assert.fail("No exception raised");
213     } catch (InvalidArgumentException e) {
214       // Expected exception.
215     }
216   }
217 
218   @Test
listSchemasTest2()219   public void listSchemasTest2() throws Exception {
220     Schema responsesElement = Schema.newBuilder().build();
221     ListSchemasResponse expectedResponse =
222         ListSchemasResponse.newBuilder()
223             .setNextPageToken("")
224             .addAllSchemas(Arrays.asList(responsesElement))
225             .build();
226     mockSchemaService.addResponse(expectedResponse);
227 
228     String parent = "parent-995424086";
229 
230     ListSchemasPagedResponse pagedListResponse = client.listSchemas(parent);
231 
232     List<Schema> resources = Lists.newArrayList(pagedListResponse.iterateAll());
233 
234     Assert.assertEquals(1, resources.size());
235     Assert.assertEquals(expectedResponse.getSchemasList().get(0), resources.get(0));
236 
237     List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
238     Assert.assertEquals(1, actualRequests.size());
239     ListSchemasRequest actualRequest = ((ListSchemasRequest) actualRequests.get(0));
240 
241     Assert.assertEquals(parent, actualRequest.getParent());
242     Assert.assertTrue(
243         channelProvider.isHeaderSent(
244             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
245             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
246   }
247 
248   @Test
listSchemasExceptionTest2()249   public void listSchemasExceptionTest2() throws Exception {
250     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
251     mockSchemaService.addException(exception);
252 
253     try {
254       String parent = "parent-995424086";
255       client.listSchemas(parent);
256       Assert.fail("No exception raised");
257     } catch (InvalidArgumentException e) {
258       // Expected exception.
259     }
260   }
261 
262   @Test
createSchemaTest()263   public void createSchemaTest() throws Exception {
264     Schema expectedResponse =
265         Schema.newBuilder()
266             .setName(
267                 SchemaName.ofProjectLocationDataStoreSchemaName(
268                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]")
269                     .toString())
270             .build();
271     Operation resultOperation =
272         Operation.newBuilder()
273             .setName("createSchemaTest")
274             .setDone(true)
275             .setResponse(Any.pack(expectedResponse))
276             .build();
277     mockSchemaService.addResponse(resultOperation);
278 
279     DataStoreName parent =
280         DataStoreName.ofProjectLocationDataStoreName("[PROJECT]", "[LOCATION]", "[DATA_STORE]");
281     Schema schema = Schema.newBuilder().build();
282     String schemaId = "schemaId-697673060";
283 
284     Schema actualResponse = client.createSchemaAsync(parent, schema, schemaId).get();
285     Assert.assertEquals(expectedResponse, actualResponse);
286 
287     List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
288     Assert.assertEquals(1, actualRequests.size());
289     CreateSchemaRequest actualRequest = ((CreateSchemaRequest) actualRequests.get(0));
290 
291     Assert.assertEquals(parent.toString(), actualRequest.getParent());
292     Assert.assertEquals(schema, actualRequest.getSchema());
293     Assert.assertEquals(schemaId, actualRequest.getSchemaId());
294     Assert.assertTrue(
295         channelProvider.isHeaderSent(
296             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
297             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
298   }
299 
300   @Test
createSchemaExceptionTest()301   public void createSchemaExceptionTest() throws Exception {
302     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
303     mockSchemaService.addException(exception);
304 
305     try {
306       DataStoreName parent =
307           DataStoreName.ofProjectLocationDataStoreName("[PROJECT]", "[LOCATION]", "[DATA_STORE]");
308       Schema schema = Schema.newBuilder().build();
309       String schemaId = "schemaId-697673060";
310       client.createSchemaAsync(parent, schema, schemaId).get();
311       Assert.fail("No exception raised");
312     } catch (ExecutionException e) {
313       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
314       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
315       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
316     }
317   }
318 
319   @Test
createSchemaTest2()320   public void createSchemaTest2() throws Exception {
321     Schema expectedResponse =
322         Schema.newBuilder()
323             .setName(
324                 SchemaName.ofProjectLocationDataStoreSchemaName(
325                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]")
326                     .toString())
327             .build();
328     Operation resultOperation =
329         Operation.newBuilder()
330             .setName("createSchemaTest")
331             .setDone(true)
332             .setResponse(Any.pack(expectedResponse))
333             .build();
334     mockSchemaService.addResponse(resultOperation);
335 
336     String parent = "parent-995424086";
337     Schema schema = Schema.newBuilder().build();
338     String schemaId = "schemaId-697673060";
339 
340     Schema actualResponse = client.createSchemaAsync(parent, schema, schemaId).get();
341     Assert.assertEquals(expectedResponse, actualResponse);
342 
343     List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
344     Assert.assertEquals(1, actualRequests.size());
345     CreateSchemaRequest actualRequest = ((CreateSchemaRequest) actualRequests.get(0));
346 
347     Assert.assertEquals(parent, actualRequest.getParent());
348     Assert.assertEquals(schema, actualRequest.getSchema());
349     Assert.assertEquals(schemaId, actualRequest.getSchemaId());
350     Assert.assertTrue(
351         channelProvider.isHeaderSent(
352             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
353             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
354   }
355 
356   @Test
createSchemaExceptionTest2()357   public void createSchemaExceptionTest2() throws Exception {
358     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
359     mockSchemaService.addException(exception);
360 
361     try {
362       String parent = "parent-995424086";
363       Schema schema = Schema.newBuilder().build();
364       String schemaId = "schemaId-697673060";
365       client.createSchemaAsync(parent, schema, schemaId).get();
366       Assert.fail("No exception raised");
367     } catch (ExecutionException e) {
368       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
369       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
370       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
371     }
372   }
373 
374   @Test
updateSchemaTest()375   public void updateSchemaTest() throws Exception {
376     Schema expectedResponse =
377         Schema.newBuilder()
378             .setName(
379                 SchemaName.ofProjectLocationDataStoreSchemaName(
380                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]")
381                     .toString())
382             .build();
383     Operation resultOperation =
384         Operation.newBuilder()
385             .setName("updateSchemaTest")
386             .setDone(true)
387             .setResponse(Any.pack(expectedResponse))
388             .build();
389     mockSchemaService.addResponse(resultOperation);
390 
391     UpdateSchemaRequest request =
392         UpdateSchemaRequest.newBuilder()
393             .setSchema(Schema.newBuilder().build())
394             .setAllowMissing(true)
395             .build();
396 
397     Schema actualResponse = client.updateSchemaAsync(request).get();
398     Assert.assertEquals(expectedResponse, actualResponse);
399 
400     List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
401     Assert.assertEquals(1, actualRequests.size());
402     UpdateSchemaRequest actualRequest = ((UpdateSchemaRequest) actualRequests.get(0));
403 
404     Assert.assertEquals(request.getSchema(), actualRequest.getSchema());
405     Assert.assertEquals(request.getAllowMissing(), actualRequest.getAllowMissing());
406     Assert.assertTrue(
407         channelProvider.isHeaderSent(
408             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
409             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
410   }
411 
412   @Test
updateSchemaExceptionTest()413   public void updateSchemaExceptionTest() throws Exception {
414     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
415     mockSchemaService.addException(exception);
416 
417     try {
418       UpdateSchemaRequest request =
419           UpdateSchemaRequest.newBuilder()
420               .setSchema(Schema.newBuilder().build())
421               .setAllowMissing(true)
422               .build();
423       client.updateSchemaAsync(request).get();
424       Assert.fail("No exception raised");
425     } catch (ExecutionException e) {
426       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
427       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
428       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
429     }
430   }
431 
432   @Test
deleteSchemaTest()433   public void deleteSchemaTest() throws Exception {
434     Empty expectedResponse = Empty.newBuilder().build();
435     Operation resultOperation =
436         Operation.newBuilder()
437             .setName("deleteSchemaTest")
438             .setDone(true)
439             .setResponse(Any.pack(expectedResponse))
440             .build();
441     mockSchemaService.addResponse(resultOperation);
442 
443     SchemaName name =
444         SchemaName.ofProjectLocationDataStoreSchemaName(
445             "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]");
446 
447     client.deleteSchemaAsync(name).get();
448 
449     List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
450     Assert.assertEquals(1, actualRequests.size());
451     DeleteSchemaRequest actualRequest = ((DeleteSchemaRequest) actualRequests.get(0));
452 
453     Assert.assertEquals(name.toString(), actualRequest.getName());
454     Assert.assertTrue(
455         channelProvider.isHeaderSent(
456             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
457             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
458   }
459 
460   @Test
deleteSchemaExceptionTest()461   public void deleteSchemaExceptionTest() throws Exception {
462     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
463     mockSchemaService.addException(exception);
464 
465     try {
466       SchemaName name =
467           SchemaName.ofProjectLocationDataStoreSchemaName(
468               "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SCHEMA]");
469       client.deleteSchemaAsync(name).get();
470       Assert.fail("No exception raised");
471     } catch (ExecutionException e) {
472       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
473       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
474       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
475     }
476   }
477 
478   @Test
deleteSchemaTest2()479   public void deleteSchemaTest2() throws Exception {
480     Empty expectedResponse = Empty.newBuilder().build();
481     Operation resultOperation =
482         Operation.newBuilder()
483             .setName("deleteSchemaTest")
484             .setDone(true)
485             .setResponse(Any.pack(expectedResponse))
486             .build();
487     mockSchemaService.addResponse(resultOperation);
488 
489     String name = "name3373707";
490 
491     client.deleteSchemaAsync(name).get();
492 
493     List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
494     Assert.assertEquals(1, actualRequests.size());
495     DeleteSchemaRequest actualRequest = ((DeleteSchemaRequest) actualRequests.get(0));
496 
497     Assert.assertEquals(name, actualRequest.getName());
498     Assert.assertTrue(
499         channelProvider.isHeaderSent(
500             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
501             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
502   }
503 
504   @Test
deleteSchemaExceptionTest2()505   public void deleteSchemaExceptionTest2() throws Exception {
506     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
507     mockSchemaService.addException(exception);
508 
509     try {
510       String name = "name3373707";
511       client.deleteSchemaAsync(name).get();
512       Assert.fail("No exception raised");
513     } catch (ExecutionException e) {
514       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
515       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
516       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
517     }
518   }
519 }
520