• 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.DocumentServiceClient.ListDocumentsPagedResponse;
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.HttpJsonDocumentServiceStub;
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 com.google.protobuf.Struct;
36 import com.google.rpc.Status;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.concurrent.ExecutionException;
42 import javax.annotation.Generated;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 
50 @Generated("by gapic-generator-java")
51 public class DocumentServiceClientHttpJsonTest {
52   private static MockHttpService mockService;
53   private static DocumentServiceClient client;
54 
55   @BeforeClass
startStaticServer()56   public static void startStaticServer() throws IOException {
57     mockService =
58         new MockHttpService(
59             HttpJsonDocumentServiceStub.getMethodDescriptors(),
60             DocumentServiceSettings.getDefaultEndpoint());
61     DocumentServiceSettings settings =
62         DocumentServiceSettings.newHttpJsonBuilder()
63             .setTransportChannelProvider(
64                 DocumentServiceSettings.defaultHttpJsonTransportProviderBuilder()
65                     .setHttpTransport(mockService)
66                     .build())
67             .setCredentialsProvider(NoCredentialsProvider.create())
68             .build();
69     client = DocumentServiceClient.create(settings);
70   }
71 
72   @AfterClass
stopServer()73   public static void stopServer() {
74     client.close();
75   }
76 
77   @Before
setUp()78   public void setUp() {}
79 
80   @After
tearDown()81   public void tearDown() throws Exception {
82     mockService.reset();
83   }
84 
85   @Test
getDocumentTest()86   public void getDocumentTest() throws Exception {
87     Document expectedResponse =
88         Document.newBuilder()
89             .setName(
90                 DocumentName.ofProjectLocationDataStoreBranchDocumentName(
91                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]")
92                     .toString())
93             .setId("id3355")
94             .setSchemaId("schemaId-697673060")
95             .setContent(Document.Content.newBuilder().build())
96             .setParentDocumentId("parentDocumentId1990105056")
97             .setDerivedStructData(Struct.newBuilder().build())
98             .build();
99     mockService.addResponse(expectedResponse);
100 
101     DocumentName name =
102         DocumentName.ofProjectLocationDataStoreBranchDocumentName(
103             "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]");
104 
105     Document actualResponse = client.getDocument(name);
106     Assert.assertEquals(expectedResponse, actualResponse);
107 
108     List<String> actualRequests = mockService.getRequestPaths();
109     Assert.assertEquals(1, actualRequests.size());
110 
111     String apiClientHeaderKey =
112         mockService
113             .getRequestHeaders()
114             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
115             .iterator()
116             .next();
117     Assert.assertTrue(
118         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
119             .matcher(apiClientHeaderKey)
120             .matches());
121   }
122 
123   @Test
getDocumentExceptionTest()124   public void getDocumentExceptionTest() throws Exception {
125     ApiException exception =
126         ApiExceptionFactory.createException(
127             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
128     mockService.addException(exception);
129 
130     try {
131       DocumentName name =
132           DocumentName.ofProjectLocationDataStoreBranchDocumentName(
133               "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]");
134       client.getDocument(name);
135       Assert.fail("No exception raised");
136     } catch (InvalidArgumentException e) {
137       // Expected exception.
138     }
139   }
140 
141   @Test
getDocumentTest2()142   public void getDocumentTest2() throws Exception {
143     Document expectedResponse =
144         Document.newBuilder()
145             .setName(
146                 DocumentName.ofProjectLocationDataStoreBranchDocumentName(
147                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]")
148                     .toString())
149             .setId("id3355")
150             .setSchemaId("schemaId-697673060")
151             .setContent(Document.Content.newBuilder().build())
152             .setParentDocumentId("parentDocumentId1990105056")
153             .setDerivedStructData(Struct.newBuilder().build())
154             .build();
155     mockService.addResponse(expectedResponse);
156 
157     String name =
158         "projects/project-2446/locations/location-2446/dataStores/dataStore-2446/branches/branche-2446/documents/document-2446";
159 
160     Document actualResponse = client.getDocument(name);
161     Assert.assertEquals(expectedResponse, actualResponse);
162 
163     List<String> actualRequests = mockService.getRequestPaths();
164     Assert.assertEquals(1, actualRequests.size());
165 
166     String apiClientHeaderKey =
167         mockService
168             .getRequestHeaders()
169             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
170             .iterator()
171             .next();
172     Assert.assertTrue(
173         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
174             .matcher(apiClientHeaderKey)
175             .matches());
176   }
177 
178   @Test
getDocumentExceptionTest2()179   public void getDocumentExceptionTest2() throws Exception {
180     ApiException exception =
181         ApiExceptionFactory.createException(
182             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
183     mockService.addException(exception);
184 
185     try {
186       String name =
187           "projects/project-2446/locations/location-2446/dataStores/dataStore-2446/branches/branche-2446/documents/document-2446";
188       client.getDocument(name);
189       Assert.fail("No exception raised");
190     } catch (InvalidArgumentException e) {
191       // Expected exception.
192     }
193   }
194 
195   @Test
listDocumentsTest()196   public void listDocumentsTest() throws Exception {
197     Document responsesElement = Document.newBuilder().build();
198     ListDocumentsResponse expectedResponse =
199         ListDocumentsResponse.newBuilder()
200             .setNextPageToken("")
201             .addAllDocuments(Arrays.asList(responsesElement))
202             .build();
203     mockService.addResponse(expectedResponse);
204 
205     BranchName parent =
206         BranchName.ofProjectLocationDataStoreBranchName(
207             "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]");
208 
209     ListDocumentsPagedResponse pagedListResponse = client.listDocuments(parent);
210 
211     List<Document> resources = Lists.newArrayList(pagedListResponse.iterateAll());
212 
213     Assert.assertEquals(1, resources.size());
214     Assert.assertEquals(expectedResponse.getDocumentsList().get(0), resources.get(0));
215 
216     List<String> actualRequests = mockService.getRequestPaths();
217     Assert.assertEquals(1, actualRequests.size());
218 
219     String apiClientHeaderKey =
220         mockService
221             .getRequestHeaders()
222             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
223             .iterator()
224             .next();
225     Assert.assertTrue(
226         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
227             .matcher(apiClientHeaderKey)
228             .matches());
229   }
230 
231   @Test
listDocumentsExceptionTest()232   public void listDocumentsExceptionTest() throws Exception {
233     ApiException exception =
234         ApiExceptionFactory.createException(
235             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
236     mockService.addException(exception);
237 
238     try {
239       BranchName parent =
240           BranchName.ofProjectLocationDataStoreBranchName(
241               "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]");
242       client.listDocuments(parent);
243       Assert.fail("No exception raised");
244     } catch (InvalidArgumentException e) {
245       // Expected exception.
246     }
247   }
248 
249   @Test
listDocumentsTest2()250   public void listDocumentsTest2() throws Exception {
251     Document responsesElement = Document.newBuilder().build();
252     ListDocumentsResponse expectedResponse =
253         ListDocumentsResponse.newBuilder()
254             .setNextPageToken("")
255             .addAllDocuments(Arrays.asList(responsesElement))
256             .build();
257     mockService.addResponse(expectedResponse);
258 
259     String parent =
260         "projects/project-3187/locations/location-3187/dataStores/dataStore-3187/branches/branche-3187";
261 
262     ListDocumentsPagedResponse pagedListResponse = client.listDocuments(parent);
263 
264     List<Document> resources = Lists.newArrayList(pagedListResponse.iterateAll());
265 
266     Assert.assertEquals(1, resources.size());
267     Assert.assertEquals(expectedResponse.getDocumentsList().get(0), resources.get(0));
268 
269     List<String> actualRequests = mockService.getRequestPaths();
270     Assert.assertEquals(1, actualRequests.size());
271 
272     String apiClientHeaderKey =
273         mockService
274             .getRequestHeaders()
275             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
276             .iterator()
277             .next();
278     Assert.assertTrue(
279         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
280             .matcher(apiClientHeaderKey)
281             .matches());
282   }
283 
284   @Test
listDocumentsExceptionTest2()285   public void listDocumentsExceptionTest2() throws Exception {
286     ApiException exception =
287         ApiExceptionFactory.createException(
288             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
289     mockService.addException(exception);
290 
291     try {
292       String parent =
293           "projects/project-3187/locations/location-3187/dataStores/dataStore-3187/branches/branche-3187";
294       client.listDocuments(parent);
295       Assert.fail("No exception raised");
296     } catch (InvalidArgumentException e) {
297       // Expected exception.
298     }
299   }
300 
301   @Test
createDocumentTest()302   public void createDocumentTest() throws Exception {
303     Document expectedResponse =
304         Document.newBuilder()
305             .setName(
306                 DocumentName.ofProjectLocationDataStoreBranchDocumentName(
307                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]")
308                     .toString())
309             .setId("id3355")
310             .setSchemaId("schemaId-697673060")
311             .setContent(Document.Content.newBuilder().build())
312             .setParentDocumentId("parentDocumentId1990105056")
313             .setDerivedStructData(Struct.newBuilder().build())
314             .build();
315     mockService.addResponse(expectedResponse);
316 
317     BranchName parent =
318         BranchName.ofProjectLocationDataStoreBranchName(
319             "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]");
320     Document document = Document.newBuilder().build();
321     String documentId = "documentId-814940266";
322 
323     Document actualResponse = client.createDocument(parent, document, documentId);
324     Assert.assertEquals(expectedResponse, actualResponse);
325 
326     List<String> actualRequests = mockService.getRequestPaths();
327     Assert.assertEquals(1, actualRequests.size());
328 
329     String apiClientHeaderKey =
330         mockService
331             .getRequestHeaders()
332             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
333             .iterator()
334             .next();
335     Assert.assertTrue(
336         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
337             .matcher(apiClientHeaderKey)
338             .matches());
339   }
340 
341   @Test
createDocumentExceptionTest()342   public void createDocumentExceptionTest() throws Exception {
343     ApiException exception =
344         ApiExceptionFactory.createException(
345             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
346     mockService.addException(exception);
347 
348     try {
349       BranchName parent =
350           BranchName.ofProjectLocationDataStoreBranchName(
351               "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]");
352       Document document = Document.newBuilder().build();
353       String documentId = "documentId-814940266";
354       client.createDocument(parent, document, documentId);
355       Assert.fail("No exception raised");
356     } catch (InvalidArgumentException e) {
357       // Expected exception.
358     }
359   }
360 
361   @Test
createDocumentTest2()362   public void createDocumentTest2() throws Exception {
363     Document expectedResponse =
364         Document.newBuilder()
365             .setName(
366                 DocumentName.ofProjectLocationDataStoreBranchDocumentName(
367                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]")
368                     .toString())
369             .setId("id3355")
370             .setSchemaId("schemaId-697673060")
371             .setContent(Document.Content.newBuilder().build())
372             .setParentDocumentId("parentDocumentId1990105056")
373             .setDerivedStructData(Struct.newBuilder().build())
374             .build();
375     mockService.addResponse(expectedResponse);
376 
377     String parent =
378         "projects/project-3187/locations/location-3187/dataStores/dataStore-3187/branches/branche-3187";
379     Document document = Document.newBuilder().build();
380     String documentId = "documentId-814940266";
381 
382     Document actualResponse = client.createDocument(parent, document, documentId);
383     Assert.assertEquals(expectedResponse, actualResponse);
384 
385     List<String> actualRequests = mockService.getRequestPaths();
386     Assert.assertEquals(1, actualRequests.size());
387 
388     String apiClientHeaderKey =
389         mockService
390             .getRequestHeaders()
391             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
392             .iterator()
393             .next();
394     Assert.assertTrue(
395         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
396             .matcher(apiClientHeaderKey)
397             .matches());
398   }
399 
400   @Test
createDocumentExceptionTest2()401   public void createDocumentExceptionTest2() throws Exception {
402     ApiException exception =
403         ApiExceptionFactory.createException(
404             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
405     mockService.addException(exception);
406 
407     try {
408       String parent =
409           "projects/project-3187/locations/location-3187/dataStores/dataStore-3187/branches/branche-3187";
410       Document document = Document.newBuilder().build();
411       String documentId = "documentId-814940266";
412       client.createDocument(parent, document, documentId);
413       Assert.fail("No exception raised");
414     } catch (InvalidArgumentException e) {
415       // Expected exception.
416     }
417   }
418 
419   @Test
updateDocumentTest()420   public void updateDocumentTest() throws Exception {
421     Document expectedResponse =
422         Document.newBuilder()
423             .setName(
424                 DocumentName.ofProjectLocationDataStoreBranchDocumentName(
425                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]")
426                     .toString())
427             .setId("id3355")
428             .setSchemaId("schemaId-697673060")
429             .setContent(Document.Content.newBuilder().build())
430             .setParentDocumentId("parentDocumentId1990105056")
431             .setDerivedStructData(Struct.newBuilder().build())
432             .build();
433     mockService.addResponse(expectedResponse);
434 
435     UpdateDocumentRequest request =
436         UpdateDocumentRequest.newBuilder()
437             .setDocument(
438                 Document.newBuilder()
439                     .setName(
440                         DocumentName.ofProjectLocationDataStoreBranchDocumentName(
441                                 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]")
442                             .toString())
443                     .setId("id3355")
444                     .setSchemaId("schemaId-697673060")
445                     .setContent(Document.Content.newBuilder().build())
446                     .setParentDocumentId("parentDocumentId1990105056")
447                     .setDerivedStructData(Struct.newBuilder().build())
448                     .build())
449             .setAllowMissing(true)
450             .build();
451 
452     Document actualResponse = client.updateDocument(request);
453     Assert.assertEquals(expectedResponse, actualResponse);
454 
455     List<String> actualRequests = mockService.getRequestPaths();
456     Assert.assertEquals(1, actualRequests.size());
457 
458     String apiClientHeaderKey =
459         mockService
460             .getRequestHeaders()
461             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
462             .iterator()
463             .next();
464     Assert.assertTrue(
465         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
466             .matcher(apiClientHeaderKey)
467             .matches());
468   }
469 
470   @Test
updateDocumentExceptionTest()471   public void updateDocumentExceptionTest() throws Exception {
472     ApiException exception =
473         ApiExceptionFactory.createException(
474             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
475     mockService.addException(exception);
476 
477     try {
478       UpdateDocumentRequest request =
479           UpdateDocumentRequest.newBuilder()
480               .setDocument(
481                   Document.newBuilder()
482                       .setName(
483                           DocumentName.ofProjectLocationDataStoreBranchDocumentName(
484                                   "[PROJECT]",
485                                   "[LOCATION]",
486                                   "[DATA_STORE]",
487                                   "[BRANCH]",
488                                   "[DOCUMENT]")
489                               .toString())
490                       .setId("id3355")
491                       .setSchemaId("schemaId-697673060")
492                       .setContent(Document.Content.newBuilder().build())
493                       .setParentDocumentId("parentDocumentId1990105056")
494                       .setDerivedStructData(Struct.newBuilder().build())
495                       .build())
496               .setAllowMissing(true)
497               .build();
498       client.updateDocument(request);
499       Assert.fail("No exception raised");
500     } catch (InvalidArgumentException e) {
501       // Expected exception.
502     }
503   }
504 
505   @Test
deleteDocumentTest()506   public void deleteDocumentTest() throws Exception {
507     Empty expectedResponse = Empty.newBuilder().build();
508     mockService.addResponse(expectedResponse);
509 
510     DocumentName name =
511         DocumentName.ofProjectLocationDataStoreBranchDocumentName(
512             "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]");
513 
514     client.deleteDocument(name);
515 
516     List<String> actualRequests = mockService.getRequestPaths();
517     Assert.assertEquals(1, actualRequests.size());
518 
519     String apiClientHeaderKey =
520         mockService
521             .getRequestHeaders()
522             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
523             .iterator()
524             .next();
525     Assert.assertTrue(
526         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
527             .matcher(apiClientHeaderKey)
528             .matches());
529   }
530 
531   @Test
deleteDocumentExceptionTest()532   public void deleteDocumentExceptionTest() throws Exception {
533     ApiException exception =
534         ApiExceptionFactory.createException(
535             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
536     mockService.addException(exception);
537 
538     try {
539       DocumentName name =
540           DocumentName.ofProjectLocationDataStoreBranchDocumentName(
541               "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]", "[DOCUMENT]");
542       client.deleteDocument(name);
543       Assert.fail("No exception raised");
544     } catch (InvalidArgumentException e) {
545       // Expected exception.
546     }
547   }
548 
549   @Test
deleteDocumentTest2()550   public void deleteDocumentTest2() throws Exception {
551     Empty expectedResponse = Empty.newBuilder().build();
552     mockService.addResponse(expectedResponse);
553 
554     String name =
555         "projects/project-2446/locations/location-2446/dataStores/dataStore-2446/branches/branche-2446/documents/document-2446";
556 
557     client.deleteDocument(name);
558 
559     List<String> actualRequests = mockService.getRequestPaths();
560     Assert.assertEquals(1, actualRequests.size());
561 
562     String apiClientHeaderKey =
563         mockService
564             .getRequestHeaders()
565             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
566             .iterator()
567             .next();
568     Assert.assertTrue(
569         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
570             .matcher(apiClientHeaderKey)
571             .matches());
572   }
573 
574   @Test
deleteDocumentExceptionTest2()575   public void deleteDocumentExceptionTest2() throws Exception {
576     ApiException exception =
577         ApiExceptionFactory.createException(
578             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
579     mockService.addException(exception);
580 
581     try {
582       String name =
583           "projects/project-2446/locations/location-2446/dataStores/dataStore-2446/branches/branche-2446/documents/document-2446";
584       client.deleteDocument(name);
585       Assert.fail("No exception raised");
586     } catch (InvalidArgumentException e) {
587       // Expected exception.
588     }
589   }
590 
591   @Test
importDocumentsTest()592   public void importDocumentsTest() throws Exception {
593     ImportDocumentsResponse expectedResponse =
594         ImportDocumentsResponse.newBuilder()
595             .addAllErrorSamples(new ArrayList<Status>())
596             .setErrorConfig(ImportErrorConfig.newBuilder().build())
597             .build();
598     Operation resultOperation =
599         Operation.newBuilder()
600             .setName("importDocumentsTest")
601             .setDone(true)
602             .setResponse(Any.pack(expectedResponse))
603             .build();
604     mockService.addResponse(resultOperation);
605 
606     ImportDocumentsRequest request =
607         ImportDocumentsRequest.newBuilder()
608             .setParent(
609                 BranchName.ofProjectLocationDataStoreBranchName(
610                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]")
611                     .toString())
612             .setErrorConfig(ImportErrorConfig.newBuilder().build())
613             .build();
614 
615     ImportDocumentsResponse actualResponse = client.importDocumentsAsync(request).get();
616     Assert.assertEquals(expectedResponse, actualResponse);
617 
618     List<String> actualRequests = mockService.getRequestPaths();
619     Assert.assertEquals(1, actualRequests.size());
620 
621     String apiClientHeaderKey =
622         mockService
623             .getRequestHeaders()
624             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
625             .iterator()
626             .next();
627     Assert.assertTrue(
628         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
629             .matcher(apiClientHeaderKey)
630             .matches());
631   }
632 
633   @Test
importDocumentsExceptionTest()634   public void importDocumentsExceptionTest() throws Exception {
635     ApiException exception =
636         ApiExceptionFactory.createException(
637             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
638     mockService.addException(exception);
639 
640     try {
641       ImportDocumentsRequest request =
642           ImportDocumentsRequest.newBuilder()
643               .setParent(
644                   BranchName.ofProjectLocationDataStoreBranchName(
645                           "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]")
646                       .toString())
647               .setErrorConfig(ImportErrorConfig.newBuilder().build())
648               .build();
649       client.importDocumentsAsync(request).get();
650       Assert.fail("No exception raised");
651     } catch (ExecutionException e) {
652     }
653   }
654 
655   @Test
purgeDocumentsTest()656   public void purgeDocumentsTest() throws Exception {
657     PurgeDocumentsResponse expectedResponse =
658         PurgeDocumentsResponse.newBuilder()
659             .setPurgeCount(575305851)
660             .addAllPurgeSample(new ArrayList<String>())
661             .build();
662     Operation resultOperation =
663         Operation.newBuilder()
664             .setName("purgeDocumentsTest")
665             .setDone(true)
666             .setResponse(Any.pack(expectedResponse))
667             .build();
668     mockService.addResponse(resultOperation);
669 
670     PurgeDocumentsRequest request =
671         PurgeDocumentsRequest.newBuilder()
672             .setParent(
673                 BranchName.ofProjectLocationDataStoreBranchName(
674                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]")
675                     .toString())
676             .setFilter("filter-1274492040")
677             .setForce(true)
678             .build();
679 
680     PurgeDocumentsResponse actualResponse = client.purgeDocumentsAsync(request).get();
681     Assert.assertEquals(expectedResponse, actualResponse);
682 
683     List<String> actualRequests = mockService.getRequestPaths();
684     Assert.assertEquals(1, actualRequests.size());
685 
686     String apiClientHeaderKey =
687         mockService
688             .getRequestHeaders()
689             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
690             .iterator()
691             .next();
692     Assert.assertTrue(
693         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
694             .matcher(apiClientHeaderKey)
695             .matches());
696   }
697 
698   @Test
purgeDocumentsExceptionTest()699   public void purgeDocumentsExceptionTest() throws Exception {
700     ApiException exception =
701         ApiExceptionFactory.createException(
702             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
703     mockService.addException(exception);
704 
705     try {
706       PurgeDocumentsRequest request =
707           PurgeDocumentsRequest.newBuilder()
708               .setParent(
709                   BranchName.ofProjectLocationDataStoreBranchName(
710                           "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]")
711                       .toString())
712               .setFilter("filter-1274492040")
713               .setForce(true)
714               .build();
715       client.purgeDocumentsAsync(request).get();
716       Assert.fail("No exception raised");
717     } catch (ExecutionException e) {
718     }
719   }
720 }
721