• 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 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.discoveryengine.v1beta.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             .setUserPseudoId("userPseudoId-1155274652")
89             .setEventTime(Timestamp.newBuilder().build())
90             .setUserInfo(UserInfo.newBuilder().build())
91             .setDirectUserRequest(true)
92             .setSessionId("sessionId607796817")
93             .setPageInfo(PageInfo.newBuilder().build())
94             .setAttributionToken("attributionToken104706234")
95             .setFilter("filter-1274492040")
96             .addAllDocuments(new ArrayList<DocumentInfo>())
97             .setPanel(PanelInfo.newBuilder().build())
98             .setSearchInfo(SearchInfo.newBuilder().build())
99             .setCompletionInfo(CompletionInfo.newBuilder().build())
100             .setTransactionInfo(TransactionInfo.newBuilder().build())
101             .addAllTagIds(new ArrayList<String>())
102             .addAllPromotionIds(new ArrayList<String>())
103             .putAllAttributes(new HashMap<String, CustomAttribute>())
104             .setMediaInfo(MediaInfo.newBuilder().build())
105             .build();
106     mockService.addResponse(expectedResponse);
107 
108     WriteUserEventRequest request =
109         WriteUserEventRequest.newBuilder()
110             .setParent(
111                 DataStoreName.ofProjectLocationDataStoreName(
112                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]")
113                     .toString())
114             .setUserEvent(UserEvent.newBuilder().build())
115             .build();
116 
117     UserEvent actualResponse = client.writeUserEvent(request);
118     Assert.assertEquals(expectedResponse, actualResponse);
119 
120     List<String> actualRequests = mockService.getRequestPaths();
121     Assert.assertEquals(1, actualRequests.size());
122 
123     String apiClientHeaderKey =
124         mockService
125             .getRequestHeaders()
126             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
127             .iterator()
128             .next();
129     Assert.assertTrue(
130         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
131             .matcher(apiClientHeaderKey)
132             .matches());
133   }
134 
135   @Test
writeUserEventExceptionTest()136   public void writeUserEventExceptionTest() throws Exception {
137     ApiException exception =
138         ApiExceptionFactory.createException(
139             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
140     mockService.addException(exception);
141 
142     try {
143       WriteUserEventRequest request =
144           WriteUserEventRequest.newBuilder()
145               .setParent(
146                   DataStoreName.ofProjectLocationDataStoreName(
147                           "[PROJECT]", "[LOCATION]", "[DATA_STORE]")
148                       .toString())
149               .setUserEvent(UserEvent.newBuilder().build())
150               .build();
151       client.writeUserEvent(request);
152       Assert.fail("No exception raised");
153     } catch (InvalidArgumentException e) {
154       // Expected exception.
155     }
156   }
157 
158   @Test
collectUserEventTest()159   public void collectUserEventTest() throws Exception {
160     HttpBody expectedResponse =
161         HttpBody.newBuilder()
162             .setContentType("contentType-389131437")
163             .setData(ByteString.EMPTY)
164             .addAllExtensions(new ArrayList<Any>())
165             .build();
166     mockService.addResponse(expectedResponse);
167 
168     CollectUserEventRequest request =
169         CollectUserEventRequest.newBuilder()
170             .setParent(
171                 DataStoreName.ofProjectLocationDataStoreName(
172                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]")
173                     .toString())
174             .setUserEvent("userEvent315571599")
175             .setUri("uri116076")
176             .setEts(100772)
177             .build();
178 
179     HttpBody actualResponse = client.collectUserEvent(request);
180     Assert.assertEquals(expectedResponse, actualResponse);
181 
182     List<String> actualRequests = mockService.getRequestPaths();
183     Assert.assertEquals(1, actualRequests.size());
184 
185     String apiClientHeaderKey =
186         mockService
187             .getRequestHeaders()
188             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
189             .iterator()
190             .next();
191     Assert.assertTrue(
192         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
193             .matcher(apiClientHeaderKey)
194             .matches());
195   }
196 
197   @Test
collectUserEventExceptionTest()198   public void collectUserEventExceptionTest() throws Exception {
199     ApiException exception =
200         ApiExceptionFactory.createException(
201             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
202     mockService.addException(exception);
203 
204     try {
205       CollectUserEventRequest request =
206           CollectUserEventRequest.newBuilder()
207               .setParent(
208                   DataStoreName.ofProjectLocationDataStoreName(
209                           "[PROJECT]", "[LOCATION]", "[DATA_STORE]")
210                       .toString())
211               .setUserEvent("userEvent315571599")
212               .setUri("uri116076")
213               .setEts(100772)
214               .build();
215       client.collectUserEvent(request);
216       Assert.fail("No exception raised");
217     } catch (InvalidArgumentException e) {
218       // Expected exception.
219     }
220   }
221 
222   @Test
importUserEventsTest()223   public void importUserEventsTest() throws Exception {
224     ImportUserEventsResponse expectedResponse =
225         ImportUserEventsResponse.newBuilder()
226             .addAllErrorSamples(new ArrayList<Status>())
227             .setErrorConfig(ImportErrorConfig.newBuilder().build())
228             .setJoinedEventsCount(-720068705)
229             .setUnjoinedEventsCount(512159846)
230             .build();
231     Operation resultOperation =
232         Operation.newBuilder()
233             .setName("importUserEventsTest")
234             .setDone(true)
235             .setResponse(Any.pack(expectedResponse))
236             .build();
237     mockService.addResponse(resultOperation);
238 
239     ImportUserEventsRequest request =
240         ImportUserEventsRequest.newBuilder()
241             .setParent(
242                 DataStoreName.ofProjectLocationDataStoreName(
243                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]")
244                     .toString())
245             .setErrorConfig(ImportErrorConfig.newBuilder().build())
246             .build();
247 
248     ImportUserEventsResponse actualResponse = client.importUserEventsAsync(request).get();
249     Assert.assertEquals(expectedResponse, actualResponse);
250 
251     List<String> actualRequests = mockService.getRequestPaths();
252     Assert.assertEquals(1, actualRequests.size());
253 
254     String apiClientHeaderKey =
255         mockService
256             .getRequestHeaders()
257             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
258             .iterator()
259             .next();
260     Assert.assertTrue(
261         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
262             .matcher(apiClientHeaderKey)
263             .matches());
264   }
265 
266   @Test
importUserEventsExceptionTest()267   public void importUserEventsExceptionTest() throws Exception {
268     ApiException exception =
269         ApiExceptionFactory.createException(
270             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
271     mockService.addException(exception);
272 
273     try {
274       ImportUserEventsRequest request =
275           ImportUserEventsRequest.newBuilder()
276               .setParent(
277                   DataStoreName.ofProjectLocationDataStoreName(
278                           "[PROJECT]", "[LOCATION]", "[DATA_STORE]")
279                       .toString())
280               .setErrorConfig(ImportErrorConfig.newBuilder().build())
281               .build();
282       client.importUserEventsAsync(request).get();
283       Assert.fail("No exception raised");
284     } catch (ExecutionException e) {
285     }
286   }
287 }
288