• 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.documentai.v1beta1;
18 
19 import com.google.api.gax.core.NoCredentialsProvider;
20 import com.google.api.gax.grpc.GaxGrpcProperties;
21 import com.google.api.gax.grpc.testing.LocalChannelProvider;
22 import com.google.api.gax.grpc.testing.MockGrpcService;
23 import com.google.api.gax.grpc.testing.MockServiceHelper;
24 import com.google.api.gax.rpc.ApiClientHeaderProvider;
25 import com.google.api.gax.rpc.InvalidArgumentException;
26 import com.google.api.gax.rpc.StatusCode;
27 import com.google.longrunning.Operation;
28 import com.google.protobuf.AbstractMessage;
29 import com.google.protobuf.Any;
30 import io.grpc.StatusRuntimeException;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.UUID;
36 import java.util.concurrent.ExecutionException;
37 import javax.annotation.Generated;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Assert;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 
45 @Generated("by gapic-generator-java")
46 public class DocumentUnderstandingServiceClientTest {
47   private static MockDocumentUnderstandingService mockDocumentUnderstandingService;
48   private static MockServiceHelper mockServiceHelper;
49   private LocalChannelProvider channelProvider;
50   private DocumentUnderstandingServiceClient client;
51 
52   @BeforeClass
startStaticServer()53   public static void startStaticServer() {
54     mockDocumentUnderstandingService = new MockDocumentUnderstandingService();
55     mockServiceHelper =
56         new MockServiceHelper(
57             UUID.randomUUID().toString(),
58             Arrays.<MockGrpcService>asList(mockDocumentUnderstandingService));
59     mockServiceHelper.start();
60   }
61 
62   @AfterClass
stopServer()63   public static void stopServer() {
64     mockServiceHelper.stop();
65   }
66 
67   @Before
setUp()68   public void setUp() throws IOException {
69     mockServiceHelper.reset();
70     channelProvider = mockServiceHelper.createChannelProvider();
71     DocumentUnderstandingServiceSettings settings =
72         DocumentUnderstandingServiceSettings.newBuilder()
73             .setTransportChannelProvider(channelProvider)
74             .setCredentialsProvider(NoCredentialsProvider.create())
75             .build();
76     client = DocumentUnderstandingServiceClient.create(settings);
77   }
78 
79   @After
tearDown()80   public void tearDown() throws Exception {
81     client.close();
82   }
83 
84   @Test
batchProcessDocumentsTest()85   public void batchProcessDocumentsTest() throws Exception {
86     BatchProcessDocumentsResponse expectedResponse =
87         BatchProcessDocumentsResponse.newBuilder()
88             .addAllResponses(new ArrayList<ProcessDocumentResponse>())
89             .build();
90     Operation resultOperation =
91         Operation.newBuilder()
92             .setName("batchProcessDocumentsTest")
93             .setDone(true)
94             .setResponse(Any.pack(expectedResponse))
95             .build();
96     mockDocumentUnderstandingService.addResponse(resultOperation);
97 
98     List<ProcessDocumentRequest> requests = new ArrayList<>();
99 
100     BatchProcessDocumentsResponse actualResponse =
101         client.batchProcessDocumentsAsync(requests).get();
102     Assert.assertEquals(expectedResponse, actualResponse);
103 
104     List<AbstractMessage> actualRequests = mockDocumentUnderstandingService.getRequests();
105     Assert.assertEquals(1, actualRequests.size());
106     BatchProcessDocumentsRequest actualRequest =
107         ((BatchProcessDocumentsRequest) actualRequests.get(0));
108 
109     Assert.assertEquals(requests, actualRequest.getRequestsList());
110     Assert.assertTrue(
111         channelProvider.isHeaderSent(
112             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
113             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
114   }
115 
116   @Test
batchProcessDocumentsExceptionTest()117   public void batchProcessDocumentsExceptionTest() throws Exception {
118     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
119     mockDocumentUnderstandingService.addException(exception);
120 
121     try {
122       List<ProcessDocumentRequest> requests = new ArrayList<>();
123       client.batchProcessDocumentsAsync(requests).get();
124       Assert.fail("No exception raised");
125     } catch (ExecutionException e) {
126       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
127       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
128       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
129     }
130   }
131 }
132