• 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.DocumentLinkServiceClient.ListLinkedSourcesPagedResponse;
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.HttpJsonDocumentLinkServiceStub;
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 DocumentLinkServiceClientHttpJsonTest {
48   private static MockHttpService mockService;
49   private static DocumentLinkServiceClient client;
50 
51   @BeforeClass
startStaticServer()52   public static void startStaticServer() throws IOException {
53     mockService =
54         new MockHttpService(
55             HttpJsonDocumentLinkServiceStub.getMethodDescriptors(),
56             DocumentLinkServiceSettings.getDefaultEndpoint());
57     DocumentLinkServiceSettings settings =
58         DocumentLinkServiceSettings.newHttpJsonBuilder()
59             .setTransportChannelProvider(
60                 DocumentLinkServiceSettings.defaultHttpJsonTransportProviderBuilder()
61                     .setHttpTransport(mockService)
62                     .build())
63             .setCredentialsProvider(NoCredentialsProvider.create())
64             .build();
65     client = DocumentLinkServiceClient.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
listLinkedTargetsTest()82   public void listLinkedTargetsTest() throws Exception {
83     ListLinkedTargetsResponse expectedResponse =
84         ListLinkedTargetsResponse.newBuilder()
85             .addAllDocumentLinks(new ArrayList<DocumentLink>())
86             .setNextPageToken("nextPageToken-1386094857")
87             .build();
88     mockService.addResponse(expectedResponse);
89 
90     DocumentName parent =
91         DocumentName.ofProjectLocationDocumentName("[PROJECT]", "[LOCATION]", "[DOCUMENT]");
92 
93     ListLinkedTargetsResponse actualResponse = client.listLinkedTargets(parent);
94     Assert.assertEquals(expectedResponse, actualResponse);
95 
96     List<String> actualRequests = mockService.getRequestPaths();
97     Assert.assertEquals(1, actualRequests.size());
98 
99     String apiClientHeaderKey =
100         mockService
101             .getRequestHeaders()
102             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
103             .iterator()
104             .next();
105     Assert.assertTrue(
106         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
107             .matcher(apiClientHeaderKey)
108             .matches());
109   }
110 
111   @Test
listLinkedTargetsExceptionTest()112   public void listLinkedTargetsExceptionTest() throws Exception {
113     ApiException exception =
114         ApiExceptionFactory.createException(
115             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
116     mockService.addException(exception);
117 
118     try {
119       DocumentName parent =
120           DocumentName.ofProjectLocationDocumentName("[PROJECT]", "[LOCATION]", "[DOCUMENT]");
121       client.listLinkedTargets(parent);
122       Assert.fail("No exception raised");
123     } catch (InvalidArgumentException e) {
124       // Expected exception.
125     }
126   }
127 
128   @Test
listLinkedTargetsTest2()129   public void listLinkedTargetsTest2() throws Exception {
130     ListLinkedTargetsResponse expectedResponse =
131         ListLinkedTargetsResponse.newBuilder()
132             .addAllDocumentLinks(new ArrayList<DocumentLink>())
133             .setNextPageToken("nextPageToken-1386094857")
134             .build();
135     mockService.addResponse(expectedResponse);
136 
137     String parent = "projects/project-6989/locations/location-6989/documents/document-6989";
138 
139     ListLinkedTargetsResponse actualResponse = client.listLinkedTargets(parent);
140     Assert.assertEquals(expectedResponse, actualResponse);
141 
142     List<String> actualRequests = mockService.getRequestPaths();
143     Assert.assertEquals(1, actualRequests.size());
144 
145     String apiClientHeaderKey =
146         mockService
147             .getRequestHeaders()
148             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
149             .iterator()
150             .next();
151     Assert.assertTrue(
152         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
153             .matcher(apiClientHeaderKey)
154             .matches());
155   }
156 
157   @Test
listLinkedTargetsExceptionTest2()158   public void listLinkedTargetsExceptionTest2() throws Exception {
159     ApiException exception =
160         ApiExceptionFactory.createException(
161             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
162     mockService.addException(exception);
163 
164     try {
165       String parent = "projects/project-6989/locations/location-6989/documents/document-6989";
166       client.listLinkedTargets(parent);
167       Assert.fail("No exception raised");
168     } catch (InvalidArgumentException e) {
169       // Expected exception.
170     }
171   }
172 
173   @Test
listLinkedSourcesTest()174   public void listLinkedSourcesTest() throws Exception {
175     DocumentLink responsesElement = DocumentLink.newBuilder().build();
176     ListLinkedSourcesResponse expectedResponse =
177         ListLinkedSourcesResponse.newBuilder()
178             .setNextPageToken("")
179             .addAllDocumentLinks(Arrays.asList(responsesElement))
180             .build();
181     mockService.addResponse(expectedResponse);
182 
183     DocumentName parent =
184         DocumentName.ofProjectLocationDocumentName("[PROJECT]", "[LOCATION]", "[DOCUMENT]");
185 
186     ListLinkedSourcesPagedResponse pagedListResponse = client.listLinkedSources(parent);
187 
188     List<DocumentLink> resources = Lists.newArrayList(pagedListResponse.iterateAll());
189 
190     Assert.assertEquals(1, resources.size());
191     Assert.assertEquals(expectedResponse.getDocumentLinksList().get(0), resources.get(0));
192 
193     List<String> actualRequests = mockService.getRequestPaths();
194     Assert.assertEquals(1, actualRequests.size());
195 
196     String apiClientHeaderKey =
197         mockService
198             .getRequestHeaders()
199             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
200             .iterator()
201             .next();
202     Assert.assertTrue(
203         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
204             .matcher(apiClientHeaderKey)
205             .matches());
206   }
207 
208   @Test
listLinkedSourcesExceptionTest()209   public void listLinkedSourcesExceptionTest() throws Exception {
210     ApiException exception =
211         ApiExceptionFactory.createException(
212             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
213     mockService.addException(exception);
214 
215     try {
216       DocumentName parent =
217           DocumentName.ofProjectLocationDocumentName("[PROJECT]", "[LOCATION]", "[DOCUMENT]");
218       client.listLinkedSources(parent);
219       Assert.fail("No exception raised");
220     } catch (InvalidArgumentException e) {
221       // Expected exception.
222     }
223   }
224 
225   @Test
listLinkedSourcesTest2()226   public void listLinkedSourcesTest2() throws Exception {
227     DocumentLink responsesElement = DocumentLink.newBuilder().build();
228     ListLinkedSourcesResponse expectedResponse =
229         ListLinkedSourcesResponse.newBuilder()
230             .setNextPageToken("")
231             .addAllDocumentLinks(Arrays.asList(responsesElement))
232             .build();
233     mockService.addResponse(expectedResponse);
234 
235     String parent = "projects/project-6989/locations/location-6989/documents/document-6989";
236 
237     ListLinkedSourcesPagedResponse pagedListResponse = client.listLinkedSources(parent);
238 
239     List<DocumentLink> resources = Lists.newArrayList(pagedListResponse.iterateAll());
240 
241     Assert.assertEquals(1, resources.size());
242     Assert.assertEquals(expectedResponse.getDocumentLinksList().get(0), resources.get(0));
243 
244     List<String> actualRequests = mockService.getRequestPaths();
245     Assert.assertEquals(1, actualRequests.size());
246 
247     String apiClientHeaderKey =
248         mockService
249             .getRequestHeaders()
250             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
251             .iterator()
252             .next();
253     Assert.assertTrue(
254         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
255             .matcher(apiClientHeaderKey)
256             .matches());
257   }
258 
259   @Test
listLinkedSourcesExceptionTest2()260   public void listLinkedSourcesExceptionTest2() throws Exception {
261     ApiException exception =
262         ApiExceptionFactory.createException(
263             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
264     mockService.addException(exception);
265 
266     try {
267       String parent = "projects/project-6989/locations/location-6989/documents/document-6989";
268       client.listLinkedSources(parent);
269       Assert.fail("No exception raised");
270     } catch (InvalidArgumentException e) {
271       // Expected exception.
272     }
273   }
274 
275   @Test
createDocumentLinkTest()276   public void createDocumentLinkTest() throws Exception {
277     DocumentLink expectedResponse =
278         DocumentLink.newBuilder()
279             .setName(
280                 DocumentLinkName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT]", "[DOCUMENT_LINK]")
281                     .toString())
282             .setSourceDocumentReference(DocumentReference.newBuilder().build())
283             .setTargetDocumentReference(DocumentReference.newBuilder().build())
284             .setDescription("description-1724546052")
285             .setUpdateTime(Timestamp.newBuilder().build())
286             .setCreateTime(Timestamp.newBuilder().build())
287             .build();
288     mockService.addResponse(expectedResponse);
289 
290     DocumentName parent =
291         DocumentName.ofProjectLocationDocumentName("[PROJECT]", "[LOCATION]", "[DOCUMENT]");
292     DocumentLink documentLink = DocumentLink.newBuilder().build();
293 
294     DocumentLink actualResponse = client.createDocumentLink(parent, documentLink);
295     Assert.assertEquals(expectedResponse, actualResponse);
296 
297     List<String> actualRequests = mockService.getRequestPaths();
298     Assert.assertEquals(1, actualRequests.size());
299 
300     String apiClientHeaderKey =
301         mockService
302             .getRequestHeaders()
303             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
304             .iterator()
305             .next();
306     Assert.assertTrue(
307         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
308             .matcher(apiClientHeaderKey)
309             .matches());
310   }
311 
312   @Test
createDocumentLinkExceptionTest()313   public void createDocumentLinkExceptionTest() throws Exception {
314     ApiException exception =
315         ApiExceptionFactory.createException(
316             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
317     mockService.addException(exception);
318 
319     try {
320       DocumentName parent =
321           DocumentName.ofProjectLocationDocumentName("[PROJECT]", "[LOCATION]", "[DOCUMENT]");
322       DocumentLink documentLink = DocumentLink.newBuilder().build();
323       client.createDocumentLink(parent, documentLink);
324       Assert.fail("No exception raised");
325     } catch (InvalidArgumentException e) {
326       // Expected exception.
327     }
328   }
329 
330   @Test
createDocumentLinkTest2()331   public void createDocumentLinkTest2() throws Exception {
332     DocumentLink expectedResponse =
333         DocumentLink.newBuilder()
334             .setName(
335                 DocumentLinkName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT]", "[DOCUMENT_LINK]")
336                     .toString())
337             .setSourceDocumentReference(DocumentReference.newBuilder().build())
338             .setTargetDocumentReference(DocumentReference.newBuilder().build())
339             .setDescription("description-1724546052")
340             .setUpdateTime(Timestamp.newBuilder().build())
341             .setCreateTime(Timestamp.newBuilder().build())
342             .build();
343     mockService.addResponse(expectedResponse);
344 
345     String parent = "projects/project-6989/locations/location-6989/documents/document-6989";
346     DocumentLink documentLink = DocumentLink.newBuilder().build();
347 
348     DocumentLink actualResponse = client.createDocumentLink(parent, documentLink);
349     Assert.assertEquals(expectedResponse, actualResponse);
350 
351     List<String> actualRequests = mockService.getRequestPaths();
352     Assert.assertEquals(1, actualRequests.size());
353 
354     String apiClientHeaderKey =
355         mockService
356             .getRequestHeaders()
357             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
358             .iterator()
359             .next();
360     Assert.assertTrue(
361         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
362             .matcher(apiClientHeaderKey)
363             .matches());
364   }
365 
366   @Test
createDocumentLinkExceptionTest2()367   public void createDocumentLinkExceptionTest2() throws Exception {
368     ApiException exception =
369         ApiExceptionFactory.createException(
370             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
371     mockService.addException(exception);
372 
373     try {
374       String parent = "projects/project-6989/locations/location-6989/documents/document-6989";
375       DocumentLink documentLink = DocumentLink.newBuilder().build();
376       client.createDocumentLink(parent, documentLink);
377       Assert.fail("No exception raised");
378     } catch (InvalidArgumentException e) {
379       // Expected exception.
380     }
381   }
382 
383   @Test
deleteDocumentLinkTest()384   public void deleteDocumentLinkTest() throws Exception {
385     Empty expectedResponse = Empty.newBuilder().build();
386     mockService.addResponse(expectedResponse);
387 
388     DocumentLinkName name =
389         DocumentLinkName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT]", "[DOCUMENT_LINK]");
390 
391     client.deleteDocumentLink(name);
392 
393     List<String> actualRequests = mockService.getRequestPaths();
394     Assert.assertEquals(1, actualRequests.size());
395 
396     String apiClientHeaderKey =
397         mockService
398             .getRequestHeaders()
399             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
400             .iterator()
401             .next();
402     Assert.assertTrue(
403         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
404             .matcher(apiClientHeaderKey)
405             .matches());
406   }
407 
408   @Test
deleteDocumentLinkExceptionTest()409   public void deleteDocumentLinkExceptionTest() throws Exception {
410     ApiException exception =
411         ApiExceptionFactory.createException(
412             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
413     mockService.addException(exception);
414 
415     try {
416       DocumentLinkName name =
417           DocumentLinkName.of("[PROJECT]", "[LOCATION]", "[DOCUMENT]", "[DOCUMENT_LINK]");
418       client.deleteDocumentLink(name);
419       Assert.fail("No exception raised");
420     } catch (InvalidArgumentException e) {
421       // Expected exception.
422     }
423   }
424 
425   @Test
deleteDocumentLinkTest2()426   public void deleteDocumentLinkTest2() throws Exception {
427     Empty expectedResponse = Empty.newBuilder().build();
428     mockService.addResponse(expectedResponse);
429 
430     String name =
431         "projects/project-4440/locations/location-4440/documents/document-4440/documentLinks/documentLink-4440";
432 
433     client.deleteDocumentLink(name);
434 
435     List<String> actualRequests = mockService.getRequestPaths();
436     Assert.assertEquals(1, actualRequests.size());
437 
438     String apiClientHeaderKey =
439         mockService
440             .getRequestHeaders()
441             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
442             .iterator()
443             .next();
444     Assert.assertTrue(
445         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
446             .matcher(apiClientHeaderKey)
447             .matches());
448   }
449 
450   @Test
deleteDocumentLinkExceptionTest2()451   public void deleteDocumentLinkExceptionTest2() throws Exception {
452     ApiException exception =
453         ApiExceptionFactory.createException(
454             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
455     mockService.addException(exception);
456 
457     try {
458       String name =
459           "projects/project-4440/locations/location-4440/documents/document-4440/documentLinks/documentLink-4440";
460       client.deleteDocumentLink(name);
461       Assert.fail("No exception raised");
462     } catch (InvalidArgumentException e) {
463       // Expected exception.
464     }
465   }
466 }
467