• 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.retail.v2beta;
18 
19 import com.google.api.HttpBody;
20 import com.google.api.gax.core.NoCredentialsProvider;
21 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
22 import com.google.api.gax.httpjson.testing.MockHttpService;
23 import com.google.api.gax.rpc.ApiClientHeaderProvider;
24 import com.google.api.gax.rpc.ApiException;
25 import com.google.api.gax.rpc.ApiExceptionFactory;
26 import com.google.api.gax.rpc.InvalidArgumentException;
27 import com.google.api.gax.rpc.StatusCode;
28 import com.google.api.gax.rpc.testing.FakeStatusCode;
29 import com.google.cloud.retail.v2beta.stub.HttpJsonUserEventServiceStub;
30 import com.google.longrunning.Operation;
31 import com.google.protobuf.Any;
32 import com.google.protobuf.ByteString;
33 import com.google.protobuf.Timestamp;
34 import com.google.rpc.Status;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.concurrent.ExecutionException;
40 import javax.annotation.Generated;
41 import org.junit.After;
42 import org.junit.AfterClass;
43 import org.junit.Assert;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 
48 @Generated("by gapic-generator-java")
49 public class UserEventServiceClientHttpJsonTest {
50   private static MockHttpService mockService;
51   private static UserEventServiceClient client;
52 
53   @BeforeClass
startStaticServer()54   public static void startStaticServer() throws IOException {
55     mockService =
56         new MockHttpService(
57             HttpJsonUserEventServiceStub.getMethodDescriptors(),
58             UserEventServiceSettings.getDefaultEndpoint());
59     UserEventServiceSettings settings =
60         UserEventServiceSettings.newHttpJsonBuilder()
61             .setTransportChannelProvider(
62                 UserEventServiceSettings.defaultHttpJsonTransportProviderBuilder()
63                     .setHttpTransport(mockService)
64                     .build())
65             .setCredentialsProvider(NoCredentialsProvider.create())
66             .build();
67     client = UserEventServiceClient.create(settings);
68   }
69 
70   @AfterClass
stopServer()71   public static void stopServer() {
72     client.close();
73   }
74 
75   @Before
setUp()76   public void setUp() {}
77 
78   @After
tearDown()79   public void tearDown() throws Exception {
80     mockService.reset();
81   }
82 
83   @Test
writeUserEventTest()84   public void writeUserEventTest() throws Exception {
85     UserEvent expectedResponse =
86         UserEvent.newBuilder()
87             .setEventType("eventType31430900")
88             .setVisitorId("visitorId1880545833")
89             .setSessionId("sessionId607796817")
90             .setEventTime(Timestamp.newBuilder().build())
91             .addAllExperimentIds(new ArrayList<String>())
92             .setAttributionToken("attributionToken104706234")
93             .addAllProductDetails(new ArrayList<ProductDetail>())
94             .setCompletionDetail(CompletionDetail.newBuilder().build())
95             .putAllAttributes(new HashMap<String, CustomAttribute>())
96             .setCartId("cartId-1367589797")
97             .setPurchaseTransaction(PurchaseTransaction.newBuilder().build())
98             .setSearchQuery("searchQuery-552137728")
99             .setFilter("filter-1274492040")
100             .setOrderBy("orderBy-1207110587")
101             .setOffset(-1019779949)
102             .addAllPageCategories(new ArrayList<String>())
103             .setUserInfo(UserInfo.newBuilder().build())
104             .setUri("uri116076")
105             .setReferrerUri("referrerUri348088525")
106             .setPageViewId("pageViewId1362499087")
107             .setEntity("entity-1298275357")
108             .build();
109     mockService.addResponse(expectedResponse);
110 
111     WriteUserEventRequest request =
112         WriteUserEventRequest.newBuilder()
113             .setParent("projects/project-6267/locations/location-6267/catalogs/catalog-6267")
114             .setUserEvent(UserEvent.newBuilder().build())
115             .setWriteAsync(true)
116             .build();
117 
118     UserEvent actualResponse = client.writeUserEvent(request);
119     Assert.assertEquals(expectedResponse, actualResponse);
120 
121     List<String> actualRequests = mockService.getRequestPaths();
122     Assert.assertEquals(1, actualRequests.size());
123 
124     String apiClientHeaderKey =
125         mockService
126             .getRequestHeaders()
127             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
128             .iterator()
129             .next();
130     Assert.assertTrue(
131         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
132             .matcher(apiClientHeaderKey)
133             .matches());
134   }
135 
136   @Test
writeUserEventExceptionTest()137   public void writeUserEventExceptionTest() throws Exception {
138     ApiException exception =
139         ApiExceptionFactory.createException(
140             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
141     mockService.addException(exception);
142 
143     try {
144       WriteUserEventRequest request =
145           WriteUserEventRequest.newBuilder()
146               .setParent("projects/project-6267/locations/location-6267/catalogs/catalog-6267")
147               .setUserEvent(UserEvent.newBuilder().build())
148               .setWriteAsync(true)
149               .build();
150       client.writeUserEvent(request);
151       Assert.fail("No exception raised");
152     } catch (InvalidArgumentException e) {
153       // Expected exception.
154     }
155   }
156 
157   @Test
collectUserEventTest()158   public void collectUserEventTest() throws Exception {
159     HttpBody expectedResponse =
160         HttpBody.newBuilder()
161             .setContentType("contentType-389131437")
162             .setData(ByteString.EMPTY)
163             .addAllExtensions(new ArrayList<Any>())
164             .build();
165     mockService.addResponse(expectedResponse);
166 
167     CollectUserEventRequest request =
168         CollectUserEventRequest.newBuilder()
169             .setParent("projects/project-6267/locations/location-6267/catalogs/catalog-6267")
170             .setUserEvent("userEvent315571599")
171             .setUri("uri116076")
172             .setEts(100772)
173             .setRawJson("rawJson985449776")
174             .build();
175 
176     HttpBody actualResponse = client.collectUserEvent(request);
177     Assert.assertEquals(expectedResponse, actualResponse);
178 
179     List<String> actualRequests = mockService.getRequestPaths();
180     Assert.assertEquals(1, actualRequests.size());
181 
182     String apiClientHeaderKey =
183         mockService
184             .getRequestHeaders()
185             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
186             .iterator()
187             .next();
188     Assert.assertTrue(
189         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
190             .matcher(apiClientHeaderKey)
191             .matches());
192   }
193 
194   @Test
collectUserEventExceptionTest()195   public void collectUserEventExceptionTest() throws Exception {
196     ApiException exception =
197         ApiExceptionFactory.createException(
198             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
199     mockService.addException(exception);
200 
201     try {
202       CollectUserEventRequest request =
203           CollectUserEventRequest.newBuilder()
204               .setParent("projects/project-6267/locations/location-6267/catalogs/catalog-6267")
205               .setUserEvent("userEvent315571599")
206               .setUri("uri116076")
207               .setEts(100772)
208               .setRawJson("rawJson985449776")
209               .build();
210       client.collectUserEvent(request);
211       Assert.fail("No exception raised");
212     } catch (InvalidArgumentException e) {
213       // Expected exception.
214     }
215   }
216 
217   @Test
purgeUserEventsTest()218   public void purgeUserEventsTest() throws Exception {
219     PurgeUserEventsResponse expectedResponse =
220         PurgeUserEventsResponse.newBuilder().setPurgedEventsCount(-310774833).build();
221     Operation resultOperation =
222         Operation.newBuilder()
223             .setName("purgeUserEventsTest")
224             .setDone(true)
225             .setResponse(Any.pack(expectedResponse))
226             .build();
227     mockService.addResponse(resultOperation);
228 
229     PurgeUserEventsRequest request =
230         PurgeUserEventsRequest.newBuilder()
231             .setParent(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString())
232             .setFilter("filter-1274492040")
233             .setForce(true)
234             .build();
235 
236     PurgeUserEventsResponse actualResponse = client.purgeUserEventsAsync(request).get();
237     Assert.assertEquals(expectedResponse, actualResponse);
238 
239     List<String> actualRequests = mockService.getRequestPaths();
240     Assert.assertEquals(1, actualRequests.size());
241 
242     String apiClientHeaderKey =
243         mockService
244             .getRequestHeaders()
245             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
246             .iterator()
247             .next();
248     Assert.assertTrue(
249         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
250             .matcher(apiClientHeaderKey)
251             .matches());
252   }
253 
254   @Test
purgeUserEventsExceptionTest()255   public void purgeUserEventsExceptionTest() throws Exception {
256     ApiException exception =
257         ApiExceptionFactory.createException(
258             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
259     mockService.addException(exception);
260 
261     try {
262       PurgeUserEventsRequest request =
263           PurgeUserEventsRequest.newBuilder()
264               .setParent(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString())
265               .setFilter("filter-1274492040")
266               .setForce(true)
267               .build();
268       client.purgeUserEventsAsync(request).get();
269       Assert.fail("No exception raised");
270     } catch (ExecutionException e) {
271     }
272   }
273 
274   @Test
importUserEventsTest()275   public void importUserEventsTest() throws Exception {
276     ImportUserEventsResponse expectedResponse =
277         ImportUserEventsResponse.newBuilder()
278             .addAllErrorSamples(new ArrayList<Status>())
279             .setErrorsConfig(ImportErrorsConfig.newBuilder().build())
280             .setImportSummary(UserEventImportSummary.newBuilder().build())
281             .build();
282     Operation resultOperation =
283         Operation.newBuilder()
284             .setName("importUserEventsTest")
285             .setDone(true)
286             .setResponse(Any.pack(expectedResponse))
287             .build();
288     mockService.addResponse(resultOperation);
289 
290     ImportUserEventsRequest request =
291         ImportUserEventsRequest.newBuilder()
292             .setParent(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString())
293             .setInputConfig(UserEventInputConfig.newBuilder().build())
294             .setErrorsConfig(ImportErrorsConfig.newBuilder().build())
295             .build();
296 
297     ImportUserEventsResponse actualResponse = client.importUserEventsAsync(request).get();
298     Assert.assertEquals(expectedResponse, actualResponse);
299 
300     List<String> actualRequests = mockService.getRequestPaths();
301     Assert.assertEquals(1, actualRequests.size());
302 
303     String apiClientHeaderKey =
304         mockService
305             .getRequestHeaders()
306             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
307             .iterator()
308             .next();
309     Assert.assertTrue(
310         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
311             .matcher(apiClientHeaderKey)
312             .matches());
313   }
314 
315   @Test
importUserEventsExceptionTest()316   public void importUserEventsExceptionTest() throws Exception {
317     ApiException exception =
318         ApiExceptionFactory.createException(
319             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
320     mockService.addException(exception);
321 
322     try {
323       ImportUserEventsRequest request =
324           ImportUserEventsRequest.newBuilder()
325               .setParent(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString())
326               .setInputConfig(UserEventInputConfig.newBuilder().build())
327               .setErrorsConfig(ImportErrorsConfig.newBuilder().build())
328               .build();
329       client.importUserEventsAsync(request).get();
330       Assert.fail("No exception raised");
331     } catch (ExecutionException e) {
332     }
333   }
334 
335   @Test
rejoinUserEventsTest()336   public void rejoinUserEventsTest() throws Exception {
337     RejoinUserEventsResponse expectedResponse =
338         RejoinUserEventsResponse.newBuilder().setRejoinedUserEventsCount(-1152281574).build();
339     Operation resultOperation =
340         Operation.newBuilder()
341             .setName("rejoinUserEventsTest")
342             .setDone(true)
343             .setResponse(Any.pack(expectedResponse))
344             .build();
345     mockService.addResponse(resultOperation);
346 
347     RejoinUserEventsRequest request =
348         RejoinUserEventsRequest.newBuilder()
349             .setParent("projects/project-6267/locations/location-6267/catalogs/catalog-6267")
350             .build();
351 
352     RejoinUserEventsResponse actualResponse = client.rejoinUserEventsAsync(request).get();
353     Assert.assertEquals(expectedResponse, actualResponse);
354 
355     List<String> actualRequests = mockService.getRequestPaths();
356     Assert.assertEquals(1, actualRequests.size());
357 
358     String apiClientHeaderKey =
359         mockService
360             .getRequestHeaders()
361             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
362             .iterator()
363             .next();
364     Assert.assertTrue(
365         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
366             .matcher(apiClientHeaderKey)
367             .matches());
368   }
369 
370   @Test
rejoinUserEventsExceptionTest()371   public void rejoinUserEventsExceptionTest() throws Exception {
372     ApiException exception =
373         ApiExceptionFactory.createException(
374             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
375     mockService.addException(exception);
376 
377     try {
378       RejoinUserEventsRequest request =
379           RejoinUserEventsRequest.newBuilder()
380               .setParent("projects/project-6267/locations/location-6267/catalogs/catalog-6267")
381               .build();
382       client.rejoinUserEventsAsync(request).get();
383       Assert.fail("No exception raised");
384     } catch (ExecutionException e) {
385     }
386   }
387 }
388