• 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.automl.v1beta1;
18 
19 import com.google.api.gax.core.NoCredentialsProvider;
20 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
21 import com.google.api.gax.httpjson.testing.MockHttpService;
22 import com.google.api.gax.rpc.ApiClientHeaderProvider;
23 import com.google.api.gax.rpc.ApiException;
24 import com.google.api.gax.rpc.ApiExceptionFactory;
25 import com.google.api.gax.rpc.InvalidArgumentException;
26 import com.google.api.gax.rpc.StatusCode;
27 import com.google.api.gax.rpc.testing.FakeStatusCode;
28 import com.google.cloud.automl.v1beta1.stub.HttpJsonPredictionServiceStub;
29 import com.google.longrunning.Operation;
30 import com.google.protobuf.Any;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
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 PredictionServiceClientHttpJsonTest {
47   private static MockHttpService mockService;
48   private static PredictionServiceClient client;
49 
50   @BeforeClass
startStaticServer()51   public static void startStaticServer() throws IOException {
52     mockService =
53         new MockHttpService(
54             HttpJsonPredictionServiceStub.getMethodDescriptors(),
55             PredictionServiceSettings.getDefaultEndpoint());
56     PredictionServiceSettings settings =
57         PredictionServiceSettings.newHttpJsonBuilder()
58             .setTransportChannelProvider(
59                 PredictionServiceSettings.defaultHttpJsonTransportProviderBuilder()
60                     .setHttpTransport(mockService)
61                     .build())
62             .setCredentialsProvider(NoCredentialsProvider.create())
63             .build();
64     client = PredictionServiceClient.create(settings);
65   }
66 
67   @AfterClass
stopServer()68   public static void stopServer() {
69     client.close();
70   }
71 
72   @Before
setUp()73   public void setUp() {}
74 
75   @After
tearDown()76   public void tearDown() throws Exception {
77     mockService.reset();
78   }
79 
80   @Test
predictTest()81   public void predictTest() throws Exception {
82     PredictResponse expectedResponse =
83         PredictResponse.newBuilder()
84             .addAllPayload(new ArrayList<AnnotationPayload>())
85             .setPreprocessedInput(ExamplePayload.newBuilder().build())
86             .putAllMetadata(new HashMap<String, String>())
87             .build();
88     mockService.addResponse(expectedResponse);
89 
90     ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
91     ExamplePayload payload = ExamplePayload.newBuilder().build();
92     Map<String, String> params = new HashMap<>();
93 
94     PredictResponse actualResponse = client.predict(name, payload, params);
95     Assert.assertEquals(expectedResponse, actualResponse);
96 
97     List<String> actualRequests = mockService.getRequestPaths();
98     Assert.assertEquals(1, actualRequests.size());
99 
100     String apiClientHeaderKey =
101         mockService
102             .getRequestHeaders()
103             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
104             .iterator()
105             .next();
106     Assert.assertTrue(
107         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
108             .matcher(apiClientHeaderKey)
109             .matches());
110   }
111 
112   @Test
predictExceptionTest()113   public void predictExceptionTest() throws Exception {
114     ApiException exception =
115         ApiExceptionFactory.createException(
116             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
117     mockService.addException(exception);
118 
119     try {
120       ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
121       ExamplePayload payload = ExamplePayload.newBuilder().build();
122       Map<String, String> params = new HashMap<>();
123       client.predict(name, payload, params);
124       Assert.fail("No exception raised");
125     } catch (InvalidArgumentException e) {
126       // Expected exception.
127     }
128   }
129 
130   @Test
predictTest2()131   public void predictTest2() throws Exception {
132     PredictResponse expectedResponse =
133         PredictResponse.newBuilder()
134             .addAllPayload(new ArrayList<AnnotationPayload>())
135             .setPreprocessedInput(ExamplePayload.newBuilder().build())
136             .putAllMetadata(new HashMap<String, String>())
137             .build();
138     mockService.addResponse(expectedResponse);
139 
140     String name = "projects/project-7594/locations/location-7594/models/model-7594";
141     ExamplePayload payload = ExamplePayload.newBuilder().build();
142     Map<String, String> params = new HashMap<>();
143 
144     PredictResponse actualResponse = client.predict(name, payload, params);
145     Assert.assertEquals(expectedResponse, actualResponse);
146 
147     List<String> actualRequests = mockService.getRequestPaths();
148     Assert.assertEquals(1, actualRequests.size());
149 
150     String apiClientHeaderKey =
151         mockService
152             .getRequestHeaders()
153             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
154             .iterator()
155             .next();
156     Assert.assertTrue(
157         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
158             .matcher(apiClientHeaderKey)
159             .matches());
160   }
161 
162   @Test
predictExceptionTest2()163   public void predictExceptionTest2() throws Exception {
164     ApiException exception =
165         ApiExceptionFactory.createException(
166             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
167     mockService.addException(exception);
168 
169     try {
170       String name = "projects/project-7594/locations/location-7594/models/model-7594";
171       ExamplePayload payload = ExamplePayload.newBuilder().build();
172       Map<String, String> params = new HashMap<>();
173       client.predict(name, payload, params);
174       Assert.fail("No exception raised");
175     } catch (InvalidArgumentException e) {
176       // Expected exception.
177     }
178   }
179 
180   @Test
batchPredictTest()181   public void batchPredictTest() throws Exception {
182     BatchPredictResult expectedResponse =
183         BatchPredictResult.newBuilder().putAllMetadata(new HashMap<String, String>()).build();
184     Operation resultOperation =
185         Operation.newBuilder()
186             .setName("batchPredictTest")
187             .setDone(true)
188             .setResponse(Any.pack(expectedResponse))
189             .build();
190     mockService.addResponse(resultOperation);
191 
192     ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
193     BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().build();
194     BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().build();
195     Map<String, String> params = new HashMap<>();
196 
197     BatchPredictResult actualResponse =
198         client.batchPredictAsync(name, inputConfig, outputConfig, params).get();
199     Assert.assertEquals(expectedResponse, actualResponse);
200 
201     List<String> actualRequests = mockService.getRequestPaths();
202     Assert.assertEquals(1, actualRequests.size());
203 
204     String apiClientHeaderKey =
205         mockService
206             .getRequestHeaders()
207             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
208             .iterator()
209             .next();
210     Assert.assertTrue(
211         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
212             .matcher(apiClientHeaderKey)
213             .matches());
214   }
215 
216   @Test
batchPredictExceptionTest()217   public void batchPredictExceptionTest() throws Exception {
218     ApiException exception =
219         ApiExceptionFactory.createException(
220             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
221     mockService.addException(exception);
222 
223     try {
224       ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
225       BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().build();
226       BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().build();
227       Map<String, String> params = new HashMap<>();
228       client.batchPredictAsync(name, inputConfig, outputConfig, params).get();
229       Assert.fail("No exception raised");
230     } catch (ExecutionException e) {
231     }
232   }
233 
234   @Test
batchPredictTest2()235   public void batchPredictTest2() throws Exception {
236     BatchPredictResult expectedResponse =
237         BatchPredictResult.newBuilder().putAllMetadata(new HashMap<String, String>()).build();
238     Operation resultOperation =
239         Operation.newBuilder()
240             .setName("batchPredictTest")
241             .setDone(true)
242             .setResponse(Any.pack(expectedResponse))
243             .build();
244     mockService.addResponse(resultOperation);
245 
246     String name = "projects/project-7594/locations/location-7594/models/model-7594";
247     BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().build();
248     BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().build();
249     Map<String, String> params = new HashMap<>();
250 
251     BatchPredictResult actualResponse =
252         client.batchPredictAsync(name, inputConfig, outputConfig, params).get();
253     Assert.assertEquals(expectedResponse, actualResponse);
254 
255     List<String> actualRequests = mockService.getRequestPaths();
256     Assert.assertEquals(1, actualRequests.size());
257 
258     String apiClientHeaderKey =
259         mockService
260             .getRequestHeaders()
261             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
262             .iterator()
263             .next();
264     Assert.assertTrue(
265         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
266             .matcher(apiClientHeaderKey)
267             .matches());
268   }
269 
270   @Test
batchPredictExceptionTest2()271   public void batchPredictExceptionTest2() throws Exception {
272     ApiException exception =
273         ApiExceptionFactory.createException(
274             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
275     mockService.addException(exception);
276 
277     try {
278       String name = "projects/project-7594/locations/location-7594/models/model-7594";
279       BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().build();
280       BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().build();
281       Map<String, String> params = new HashMap<>();
282       client.batchPredictAsync(name, inputConfig, outputConfig, params).get();
283       Assert.fail("No exception raised");
284     } catch (ExecutionException e) {
285     }
286   }
287 }
288