• 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.contentwarehouse.v1;
18 
19 import static com.google.cloud.contentwarehouse.v1.DocumentSchemaServiceClient.ListDocumentSchemasPagedResponse;
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.contentwarehouse.v1.stub.HttpJsonDocumentSchemaServiceStub;
31 import com.google.common.collect.Lists;
32 import com.google.protobuf.Empty;
33 import com.google.protobuf.Timestamp;
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 DocumentSchemaServiceClientHttpJsonTest {
48   private static MockHttpService mockService;
49   private static DocumentSchemaServiceClient client;
50 
51   @BeforeClass
startStaticServer()52   public static void startStaticServer() throws IOException {
53     mockService =
54         new MockHttpService(
55             HttpJsonDocumentSchemaServiceStub.getMethodDescriptors(),
56             DocumentSchemaServiceSettings.getDefaultEndpoint());
57     DocumentSchemaServiceSettings settings =
58         DocumentSchemaServiceSettings.newHttpJsonBuilder()
59             .setTransportChannelProvider(
60                 DocumentSchemaServiceSettings.defaultHttpJsonTransportProviderBuilder()
61                     .setHttpTransport(mockService)
62                     .build())
63             .setCredentialsProvider(NoCredentialsProvider.create())
64             .build();
65     client = DocumentSchemaServiceClient.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
createDocumentSchemaTest()82   public void createDocumentSchemaTest() throws Exception {
83     DocumentSchema expectedResponse =
84         DocumentSchema.newBuilder()
85             .setName(
86                 DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]").toString())
87             .setDisplayName("displayName1714148973")
88             .addAllPropertyDefinitions(new ArrayList<PropertyDefinition>())
89             .setDocumentIsFolder(true)
90             .setUpdateTime(Timestamp.newBuilder().build())
91             .setCreateTime(Timestamp.newBuilder().build())
92             .setDescription("description-1724546052")
93             .build();
94     mockService.addResponse(expectedResponse);
95 
96     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
97     DocumentSchema documentSchema = DocumentSchema.newBuilder().build();
98 
99     DocumentSchema actualResponse = client.createDocumentSchema(parent, documentSchema);
100     Assert.assertEquals(expectedResponse, actualResponse);
101 
102     List<String> actualRequests = mockService.getRequestPaths();
103     Assert.assertEquals(1, actualRequests.size());
104 
105     String apiClientHeaderKey =
106         mockService
107             .getRequestHeaders()
108             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
109             .iterator()
110             .next();
111     Assert.assertTrue(
112         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
113             .matcher(apiClientHeaderKey)
114             .matches());
115   }
116 
117   @Test
createDocumentSchemaExceptionTest()118   public void createDocumentSchemaExceptionTest() throws Exception {
119     ApiException exception =
120         ApiExceptionFactory.createException(
121             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
122     mockService.addException(exception);
123 
124     try {
125       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
126       DocumentSchema documentSchema = DocumentSchema.newBuilder().build();
127       client.createDocumentSchema(parent, documentSchema);
128       Assert.fail("No exception raised");
129     } catch (InvalidArgumentException e) {
130       // Expected exception.
131     }
132   }
133 
134   @Test
createDocumentSchemaTest2()135   public void createDocumentSchemaTest2() throws Exception {
136     DocumentSchema expectedResponse =
137         DocumentSchema.newBuilder()
138             .setName(
139                 DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]").toString())
140             .setDisplayName("displayName1714148973")
141             .addAllPropertyDefinitions(new ArrayList<PropertyDefinition>())
142             .setDocumentIsFolder(true)
143             .setUpdateTime(Timestamp.newBuilder().build())
144             .setCreateTime(Timestamp.newBuilder().build())
145             .setDescription("description-1724546052")
146             .build();
147     mockService.addResponse(expectedResponse);
148 
149     String parent = "projects/project-5833/locations/location-5833";
150     DocumentSchema documentSchema = DocumentSchema.newBuilder().build();
151 
152     DocumentSchema actualResponse = client.createDocumentSchema(parent, documentSchema);
153     Assert.assertEquals(expectedResponse, actualResponse);
154 
155     List<String> actualRequests = mockService.getRequestPaths();
156     Assert.assertEquals(1, actualRequests.size());
157 
158     String apiClientHeaderKey =
159         mockService
160             .getRequestHeaders()
161             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
162             .iterator()
163             .next();
164     Assert.assertTrue(
165         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
166             .matcher(apiClientHeaderKey)
167             .matches());
168   }
169 
170   @Test
createDocumentSchemaExceptionTest2()171   public void createDocumentSchemaExceptionTest2() throws Exception {
172     ApiException exception =
173         ApiExceptionFactory.createException(
174             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
175     mockService.addException(exception);
176 
177     try {
178       String parent = "projects/project-5833/locations/location-5833";
179       DocumentSchema documentSchema = DocumentSchema.newBuilder().build();
180       client.createDocumentSchema(parent, documentSchema);
181       Assert.fail("No exception raised");
182     } catch (InvalidArgumentException e) {
183       // Expected exception.
184     }
185   }
186 
187   @Test
updateDocumentSchemaTest()188   public void updateDocumentSchemaTest() throws Exception {
189     DocumentSchema expectedResponse =
190         DocumentSchema.newBuilder()
191             .setName(
192                 DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]").toString())
193             .setDisplayName("displayName1714148973")
194             .addAllPropertyDefinitions(new ArrayList<PropertyDefinition>())
195             .setDocumentIsFolder(true)
196             .setUpdateTime(Timestamp.newBuilder().build())
197             .setCreateTime(Timestamp.newBuilder().build())
198             .setDescription("description-1724546052")
199             .build();
200     mockService.addResponse(expectedResponse);
201 
202     DocumentSchemaName name = DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]");
203     DocumentSchema documentSchema = DocumentSchema.newBuilder().build();
204 
205     DocumentSchema actualResponse = client.updateDocumentSchema(name, documentSchema);
206     Assert.assertEquals(expectedResponse, actualResponse);
207 
208     List<String> actualRequests = mockService.getRequestPaths();
209     Assert.assertEquals(1, actualRequests.size());
210 
211     String apiClientHeaderKey =
212         mockService
213             .getRequestHeaders()
214             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
215             .iterator()
216             .next();
217     Assert.assertTrue(
218         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
219             .matcher(apiClientHeaderKey)
220             .matches());
221   }
222 
223   @Test
updateDocumentSchemaExceptionTest()224   public void updateDocumentSchemaExceptionTest() throws Exception {
225     ApiException exception =
226         ApiExceptionFactory.createException(
227             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
228     mockService.addException(exception);
229 
230     try {
231       DocumentSchemaName name =
232           DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]");
233       DocumentSchema documentSchema = DocumentSchema.newBuilder().build();
234       client.updateDocumentSchema(name, documentSchema);
235       Assert.fail("No exception raised");
236     } catch (InvalidArgumentException e) {
237       // Expected exception.
238     }
239   }
240 
241   @Test
updateDocumentSchemaTest2()242   public void updateDocumentSchemaTest2() throws Exception {
243     DocumentSchema expectedResponse =
244         DocumentSchema.newBuilder()
245             .setName(
246                 DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]").toString())
247             .setDisplayName("displayName1714148973")
248             .addAllPropertyDefinitions(new ArrayList<PropertyDefinition>())
249             .setDocumentIsFolder(true)
250             .setUpdateTime(Timestamp.newBuilder().build())
251             .setCreateTime(Timestamp.newBuilder().build())
252             .setDescription("description-1724546052")
253             .build();
254     mockService.addResponse(expectedResponse);
255 
256     String name = "projects/project-275/locations/location-275/documentSchemas/documentSchema-275";
257     DocumentSchema documentSchema = DocumentSchema.newBuilder().build();
258 
259     DocumentSchema actualResponse = client.updateDocumentSchema(name, documentSchema);
260     Assert.assertEquals(expectedResponse, actualResponse);
261 
262     List<String> actualRequests = mockService.getRequestPaths();
263     Assert.assertEquals(1, actualRequests.size());
264 
265     String apiClientHeaderKey =
266         mockService
267             .getRequestHeaders()
268             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
269             .iterator()
270             .next();
271     Assert.assertTrue(
272         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
273             .matcher(apiClientHeaderKey)
274             .matches());
275   }
276 
277   @Test
updateDocumentSchemaExceptionTest2()278   public void updateDocumentSchemaExceptionTest2() throws Exception {
279     ApiException exception =
280         ApiExceptionFactory.createException(
281             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
282     mockService.addException(exception);
283 
284     try {
285       String name =
286           "projects/project-275/locations/location-275/documentSchemas/documentSchema-275";
287       DocumentSchema documentSchema = DocumentSchema.newBuilder().build();
288       client.updateDocumentSchema(name, documentSchema);
289       Assert.fail("No exception raised");
290     } catch (InvalidArgumentException e) {
291       // Expected exception.
292     }
293   }
294 
295   @Test
getDocumentSchemaTest()296   public void getDocumentSchemaTest() throws Exception {
297     DocumentSchema expectedResponse =
298         DocumentSchema.newBuilder()
299             .setName(
300                 DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]").toString())
301             .setDisplayName("displayName1714148973")
302             .addAllPropertyDefinitions(new ArrayList<PropertyDefinition>())
303             .setDocumentIsFolder(true)
304             .setUpdateTime(Timestamp.newBuilder().build())
305             .setCreateTime(Timestamp.newBuilder().build())
306             .setDescription("description-1724546052")
307             .build();
308     mockService.addResponse(expectedResponse);
309 
310     DocumentSchemaName name = DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]");
311 
312     DocumentSchema actualResponse = client.getDocumentSchema(name);
313     Assert.assertEquals(expectedResponse, actualResponse);
314 
315     List<String> actualRequests = mockService.getRequestPaths();
316     Assert.assertEquals(1, actualRequests.size());
317 
318     String apiClientHeaderKey =
319         mockService
320             .getRequestHeaders()
321             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
322             .iterator()
323             .next();
324     Assert.assertTrue(
325         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
326             .matcher(apiClientHeaderKey)
327             .matches());
328   }
329 
330   @Test
getDocumentSchemaExceptionTest()331   public void getDocumentSchemaExceptionTest() throws Exception {
332     ApiException exception =
333         ApiExceptionFactory.createException(
334             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
335     mockService.addException(exception);
336 
337     try {
338       DocumentSchemaName name =
339           DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]");
340       client.getDocumentSchema(name);
341       Assert.fail("No exception raised");
342     } catch (InvalidArgumentException e) {
343       // Expected exception.
344     }
345   }
346 
347   @Test
getDocumentSchemaTest2()348   public void getDocumentSchemaTest2() throws Exception {
349     DocumentSchema expectedResponse =
350         DocumentSchema.newBuilder()
351             .setName(
352                 DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]").toString())
353             .setDisplayName("displayName1714148973")
354             .addAllPropertyDefinitions(new ArrayList<PropertyDefinition>())
355             .setDocumentIsFolder(true)
356             .setUpdateTime(Timestamp.newBuilder().build())
357             .setCreateTime(Timestamp.newBuilder().build())
358             .setDescription("description-1724546052")
359             .build();
360     mockService.addResponse(expectedResponse);
361 
362     String name = "projects/project-275/locations/location-275/documentSchemas/documentSchema-275";
363 
364     DocumentSchema actualResponse = client.getDocumentSchema(name);
365     Assert.assertEquals(expectedResponse, actualResponse);
366 
367     List<String> actualRequests = mockService.getRequestPaths();
368     Assert.assertEquals(1, actualRequests.size());
369 
370     String apiClientHeaderKey =
371         mockService
372             .getRequestHeaders()
373             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
374             .iterator()
375             .next();
376     Assert.assertTrue(
377         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
378             .matcher(apiClientHeaderKey)
379             .matches());
380   }
381 
382   @Test
getDocumentSchemaExceptionTest2()383   public void getDocumentSchemaExceptionTest2() throws Exception {
384     ApiException exception =
385         ApiExceptionFactory.createException(
386             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
387     mockService.addException(exception);
388 
389     try {
390       String name =
391           "projects/project-275/locations/location-275/documentSchemas/documentSchema-275";
392       client.getDocumentSchema(name);
393       Assert.fail("No exception raised");
394     } catch (InvalidArgumentException e) {
395       // Expected exception.
396     }
397   }
398 
399   @Test
deleteDocumentSchemaTest()400   public void deleteDocumentSchemaTest() throws Exception {
401     Empty expectedResponse = Empty.newBuilder().build();
402     mockService.addResponse(expectedResponse);
403 
404     DocumentSchemaName name = DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]");
405 
406     client.deleteDocumentSchema(name);
407 
408     List<String> actualRequests = mockService.getRequestPaths();
409     Assert.assertEquals(1, actualRequests.size());
410 
411     String apiClientHeaderKey =
412         mockService
413             .getRequestHeaders()
414             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
415             .iterator()
416             .next();
417     Assert.assertTrue(
418         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
419             .matcher(apiClientHeaderKey)
420             .matches());
421   }
422 
423   @Test
deleteDocumentSchemaExceptionTest()424   public void deleteDocumentSchemaExceptionTest() throws Exception {
425     ApiException exception =
426         ApiExceptionFactory.createException(
427             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
428     mockService.addException(exception);
429 
430     try {
431       DocumentSchemaName name =
432           DocumentSchemaName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT_SCHEMA]");
433       client.deleteDocumentSchema(name);
434       Assert.fail("No exception raised");
435     } catch (InvalidArgumentException e) {
436       // Expected exception.
437     }
438   }
439 
440   @Test
deleteDocumentSchemaTest2()441   public void deleteDocumentSchemaTest2() throws Exception {
442     Empty expectedResponse = Empty.newBuilder().build();
443     mockService.addResponse(expectedResponse);
444 
445     String name = "projects/project-275/locations/location-275/documentSchemas/documentSchema-275";
446 
447     client.deleteDocumentSchema(name);
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
deleteDocumentSchemaExceptionTest2()465   public void deleteDocumentSchemaExceptionTest2() 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       String name =
473           "projects/project-275/locations/location-275/documentSchemas/documentSchema-275";
474       client.deleteDocumentSchema(name);
475       Assert.fail("No exception raised");
476     } catch (InvalidArgumentException e) {
477       // Expected exception.
478     }
479   }
480 
481   @Test
listDocumentSchemasTest()482   public void listDocumentSchemasTest() throws Exception {
483     DocumentSchema responsesElement = DocumentSchema.newBuilder().build();
484     ListDocumentSchemasResponse expectedResponse =
485         ListDocumentSchemasResponse.newBuilder()
486             .setNextPageToken("")
487             .addAllDocumentSchemas(Arrays.asList(responsesElement))
488             .build();
489     mockService.addResponse(expectedResponse);
490 
491     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
492 
493     ListDocumentSchemasPagedResponse pagedListResponse = client.listDocumentSchemas(parent);
494 
495     List<DocumentSchema> resources = Lists.newArrayList(pagedListResponse.iterateAll());
496 
497     Assert.assertEquals(1, resources.size());
498     Assert.assertEquals(expectedResponse.getDocumentSchemasList().get(0), resources.get(0));
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
listDocumentSchemasExceptionTest()516   public void listDocumentSchemasExceptionTest() 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       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
524       client.listDocumentSchemas(parent);
525       Assert.fail("No exception raised");
526     } catch (InvalidArgumentException e) {
527       // Expected exception.
528     }
529   }
530 
531   @Test
listDocumentSchemasTest2()532   public void listDocumentSchemasTest2() throws Exception {
533     DocumentSchema responsesElement = DocumentSchema.newBuilder().build();
534     ListDocumentSchemasResponse expectedResponse =
535         ListDocumentSchemasResponse.newBuilder()
536             .setNextPageToken("")
537             .addAllDocumentSchemas(Arrays.asList(responsesElement))
538             .build();
539     mockService.addResponse(expectedResponse);
540 
541     String parent = "projects/project-5833/locations/location-5833";
542 
543     ListDocumentSchemasPagedResponse pagedListResponse = client.listDocumentSchemas(parent);
544 
545     List<DocumentSchema> resources = Lists.newArrayList(pagedListResponse.iterateAll());
546 
547     Assert.assertEquals(1, resources.size());
548     Assert.assertEquals(expectedResponse.getDocumentSchemasList().get(0), resources.get(0));
549 
550     List<String> actualRequests = mockService.getRequestPaths();
551     Assert.assertEquals(1, actualRequests.size());
552 
553     String apiClientHeaderKey =
554         mockService
555             .getRequestHeaders()
556             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
557             .iterator()
558             .next();
559     Assert.assertTrue(
560         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
561             .matcher(apiClientHeaderKey)
562             .matches());
563   }
564 
565   @Test
listDocumentSchemasExceptionTest2()566   public void listDocumentSchemasExceptionTest2() throws Exception {
567     ApiException exception =
568         ApiExceptionFactory.createException(
569             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
570     mockService.addException(exception);
571 
572     try {
573       String parent = "projects/project-5833/locations/location-5833";
574       client.listDocumentSchemas(parent);
575       Assert.fail("No exception raised");
576     } catch (InvalidArgumentException e) {
577       // Expected exception.
578     }
579   }
580 }
581