• 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.recommendationengine.v1beta1;
18 
19 import static com.google.cloud.recommendationengine.v1beta1.PredictionApiKeyRegistryClient.ListPredictionApiKeyRegistrationsPagedResponse;
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.recommendationengine.v1beta1.stub.HttpJsonPredictionApiKeyRegistryStub;
31 import com.google.common.collect.Lists;
32 import com.google.protobuf.Empty;
33 import java.io.IOException;
34 import java.util.Arrays;
35 import java.util.List;
36 import javax.annotation.Generated;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Assert;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 
44 @Generated("by gapic-generator-java")
45 public class PredictionApiKeyRegistryClientHttpJsonTest {
46   private static MockHttpService mockService;
47   private static PredictionApiKeyRegistryClient client;
48 
49   @BeforeClass
startStaticServer()50   public static void startStaticServer() throws IOException {
51     mockService =
52         new MockHttpService(
53             HttpJsonPredictionApiKeyRegistryStub.getMethodDescriptors(),
54             PredictionApiKeyRegistrySettings.getDefaultEndpoint());
55     PredictionApiKeyRegistrySettings settings =
56         PredictionApiKeyRegistrySettings.newHttpJsonBuilder()
57             .setTransportChannelProvider(
58                 PredictionApiKeyRegistrySettings.defaultHttpJsonTransportProviderBuilder()
59                     .setHttpTransport(mockService)
60                     .build())
61             .setCredentialsProvider(NoCredentialsProvider.create())
62             .build();
63     client = PredictionApiKeyRegistryClient.create(settings);
64   }
65 
66   @AfterClass
stopServer()67   public static void stopServer() {
68     client.close();
69   }
70 
71   @Before
setUp()72   public void setUp() {}
73 
74   @After
tearDown()75   public void tearDown() throws Exception {
76     mockService.reset();
77   }
78 
79   @Test
createPredictionApiKeyRegistrationTest()80   public void createPredictionApiKeyRegistrationTest() throws Exception {
81     PredictionApiKeyRegistration expectedResponse =
82         PredictionApiKeyRegistration.newBuilder().setApiKey("apiKey-1411301915").build();
83     mockService.addResponse(expectedResponse);
84 
85     EventStoreName parent =
86         EventStoreName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]");
87     PredictionApiKeyRegistration predictionApiKeyRegistration =
88         PredictionApiKeyRegistration.newBuilder().build();
89 
90     PredictionApiKeyRegistration actualResponse =
91         client.createPredictionApiKeyRegistration(parent, predictionApiKeyRegistration);
92     Assert.assertEquals(expectedResponse, actualResponse);
93 
94     List<String> actualRequests = mockService.getRequestPaths();
95     Assert.assertEquals(1, actualRequests.size());
96 
97     String apiClientHeaderKey =
98         mockService
99             .getRequestHeaders()
100             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
101             .iterator()
102             .next();
103     Assert.assertTrue(
104         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
105             .matcher(apiClientHeaderKey)
106             .matches());
107   }
108 
109   @Test
createPredictionApiKeyRegistrationExceptionTest()110   public void createPredictionApiKeyRegistrationExceptionTest() throws Exception {
111     ApiException exception =
112         ApiExceptionFactory.createException(
113             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
114     mockService.addException(exception);
115 
116     try {
117       EventStoreName parent =
118           EventStoreName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]");
119       PredictionApiKeyRegistration predictionApiKeyRegistration =
120           PredictionApiKeyRegistration.newBuilder().build();
121       client.createPredictionApiKeyRegistration(parent, predictionApiKeyRegistration);
122       Assert.fail("No exception raised");
123     } catch (InvalidArgumentException e) {
124       // Expected exception.
125     }
126   }
127 
128   @Test
createPredictionApiKeyRegistrationTest2()129   public void createPredictionApiKeyRegistrationTest2() throws Exception {
130     PredictionApiKeyRegistration expectedResponse =
131         PredictionApiKeyRegistration.newBuilder().setApiKey("apiKey-1411301915").build();
132     mockService.addResponse(expectedResponse);
133 
134     String parent =
135         "projects/project-765/locations/location-765/catalogs/catalog-765/eventStores/eventStore-765";
136     PredictionApiKeyRegistration predictionApiKeyRegistration =
137         PredictionApiKeyRegistration.newBuilder().build();
138 
139     PredictionApiKeyRegistration actualResponse =
140         client.createPredictionApiKeyRegistration(parent, predictionApiKeyRegistration);
141     Assert.assertEquals(expectedResponse, actualResponse);
142 
143     List<String> actualRequests = mockService.getRequestPaths();
144     Assert.assertEquals(1, actualRequests.size());
145 
146     String apiClientHeaderKey =
147         mockService
148             .getRequestHeaders()
149             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
150             .iterator()
151             .next();
152     Assert.assertTrue(
153         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
154             .matcher(apiClientHeaderKey)
155             .matches());
156   }
157 
158   @Test
createPredictionApiKeyRegistrationExceptionTest2()159   public void createPredictionApiKeyRegistrationExceptionTest2() throws Exception {
160     ApiException exception =
161         ApiExceptionFactory.createException(
162             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
163     mockService.addException(exception);
164 
165     try {
166       String parent =
167           "projects/project-765/locations/location-765/catalogs/catalog-765/eventStores/eventStore-765";
168       PredictionApiKeyRegistration predictionApiKeyRegistration =
169           PredictionApiKeyRegistration.newBuilder().build();
170       client.createPredictionApiKeyRegistration(parent, predictionApiKeyRegistration);
171       Assert.fail("No exception raised");
172     } catch (InvalidArgumentException e) {
173       // Expected exception.
174     }
175   }
176 
177   @Test
listPredictionApiKeyRegistrationsTest()178   public void listPredictionApiKeyRegistrationsTest() throws Exception {
179     PredictionApiKeyRegistration responsesElement =
180         PredictionApiKeyRegistration.newBuilder().build();
181     ListPredictionApiKeyRegistrationsResponse expectedResponse =
182         ListPredictionApiKeyRegistrationsResponse.newBuilder()
183             .setNextPageToken("")
184             .addAllPredictionApiKeyRegistrations(Arrays.asList(responsesElement))
185             .build();
186     mockService.addResponse(expectedResponse);
187 
188     EventStoreName parent =
189         EventStoreName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]");
190 
191     ListPredictionApiKeyRegistrationsPagedResponse pagedListResponse =
192         client.listPredictionApiKeyRegistrations(parent);
193 
194     List<PredictionApiKeyRegistration> resources =
195         Lists.newArrayList(pagedListResponse.iterateAll());
196 
197     Assert.assertEquals(1, resources.size());
198     Assert.assertEquals(
199         expectedResponse.getPredictionApiKeyRegistrationsList().get(0), resources.get(0));
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
listPredictionApiKeyRegistrationsExceptionTest()217   public void listPredictionApiKeyRegistrationsExceptionTest() 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       EventStoreName parent =
225           EventStoreName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]");
226       client.listPredictionApiKeyRegistrations(parent);
227       Assert.fail("No exception raised");
228     } catch (InvalidArgumentException e) {
229       // Expected exception.
230     }
231   }
232 
233   @Test
listPredictionApiKeyRegistrationsTest2()234   public void listPredictionApiKeyRegistrationsTest2() throws Exception {
235     PredictionApiKeyRegistration responsesElement =
236         PredictionApiKeyRegistration.newBuilder().build();
237     ListPredictionApiKeyRegistrationsResponse expectedResponse =
238         ListPredictionApiKeyRegistrationsResponse.newBuilder()
239             .setNextPageToken("")
240             .addAllPredictionApiKeyRegistrations(Arrays.asList(responsesElement))
241             .build();
242     mockService.addResponse(expectedResponse);
243 
244     String parent =
245         "projects/project-765/locations/location-765/catalogs/catalog-765/eventStores/eventStore-765";
246 
247     ListPredictionApiKeyRegistrationsPagedResponse pagedListResponse =
248         client.listPredictionApiKeyRegistrations(parent);
249 
250     List<PredictionApiKeyRegistration> resources =
251         Lists.newArrayList(pagedListResponse.iterateAll());
252 
253     Assert.assertEquals(1, resources.size());
254     Assert.assertEquals(
255         expectedResponse.getPredictionApiKeyRegistrationsList().get(0), resources.get(0));
256 
257     List<String> actualRequests = mockService.getRequestPaths();
258     Assert.assertEquals(1, actualRequests.size());
259 
260     String apiClientHeaderKey =
261         mockService
262             .getRequestHeaders()
263             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
264             .iterator()
265             .next();
266     Assert.assertTrue(
267         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
268             .matcher(apiClientHeaderKey)
269             .matches());
270   }
271 
272   @Test
listPredictionApiKeyRegistrationsExceptionTest2()273   public void listPredictionApiKeyRegistrationsExceptionTest2() throws Exception {
274     ApiException exception =
275         ApiExceptionFactory.createException(
276             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
277     mockService.addException(exception);
278 
279     try {
280       String parent =
281           "projects/project-765/locations/location-765/catalogs/catalog-765/eventStores/eventStore-765";
282       client.listPredictionApiKeyRegistrations(parent);
283       Assert.fail("No exception raised");
284     } catch (InvalidArgumentException e) {
285       // Expected exception.
286     }
287   }
288 
289   @Test
deletePredictionApiKeyRegistrationTest()290   public void deletePredictionApiKeyRegistrationTest() throws Exception {
291     Empty expectedResponse = Empty.newBuilder().build();
292     mockService.addResponse(expectedResponse);
293 
294     PredictionApiKeyRegistrationName name =
295         PredictionApiKeyRegistrationName.of(
296             "[PROJECT]",
297             "[LOCATION]",
298             "[CATALOG]",
299             "[EVENT_STORE]",
300             "[PREDICTION_API_KEY_REGISTRATION]");
301 
302     client.deletePredictionApiKeyRegistration(name);
303 
304     List<String> actualRequests = mockService.getRequestPaths();
305     Assert.assertEquals(1, actualRequests.size());
306 
307     String apiClientHeaderKey =
308         mockService
309             .getRequestHeaders()
310             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
311             .iterator()
312             .next();
313     Assert.assertTrue(
314         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
315             .matcher(apiClientHeaderKey)
316             .matches());
317   }
318 
319   @Test
deletePredictionApiKeyRegistrationExceptionTest()320   public void deletePredictionApiKeyRegistrationExceptionTest() throws Exception {
321     ApiException exception =
322         ApiExceptionFactory.createException(
323             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
324     mockService.addException(exception);
325 
326     try {
327       PredictionApiKeyRegistrationName name =
328           PredictionApiKeyRegistrationName.of(
329               "[PROJECT]",
330               "[LOCATION]",
331               "[CATALOG]",
332               "[EVENT_STORE]",
333               "[PREDICTION_API_KEY_REGISTRATION]");
334       client.deletePredictionApiKeyRegistration(name);
335       Assert.fail("No exception raised");
336     } catch (InvalidArgumentException e) {
337       // Expected exception.
338     }
339   }
340 
341   @Test
deletePredictionApiKeyRegistrationTest2()342   public void deletePredictionApiKeyRegistrationTest2() throws Exception {
343     Empty expectedResponse = Empty.newBuilder().build();
344     mockService.addResponse(expectedResponse);
345 
346     String name =
347         "projects/project-76/locations/location-76/catalogs/catalog-76/eventStores/eventStore-76/predictionApiKeyRegistrations/predictionApiKeyRegistration-76";
348 
349     client.deletePredictionApiKeyRegistration(name);
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
deletePredictionApiKeyRegistrationExceptionTest2()367   public void deletePredictionApiKeyRegistrationExceptionTest2() 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 name =
375           "projects/project-76/locations/location-76/catalogs/catalog-76/eventStores/eventStore-76/predictionApiKeyRegistrations/predictionApiKeyRegistration-76";
376       client.deletePredictionApiKeyRegistration(name);
377       Assert.fail("No exception raised");
378     } catch (InvalidArgumentException e) {
379       // Expected exception.
380     }
381   }
382 }
383