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.grpc.GaxGrpcProperties; 22 import com.google.api.gax.grpc.testing.LocalChannelProvider; 23 import com.google.api.gax.grpc.testing.MockGrpcService; 24 import com.google.api.gax.grpc.testing.MockServiceHelper; 25 import com.google.api.gax.rpc.ApiClientHeaderProvider; 26 import com.google.api.gax.rpc.InvalidArgumentException; 27 import com.google.api.gax.rpc.StatusCode; 28 import com.google.longrunning.Operation; 29 import com.google.protobuf.AbstractMessage; 30 import com.google.protobuf.Any; 31 import com.google.protobuf.ByteString; 32 import com.google.protobuf.Timestamp; 33 import com.google.rpc.Status; 34 import io.grpc.StatusRuntimeException; 35 import java.io.IOException; 36 import java.util.ArrayList; 37 import java.util.Arrays; 38 import java.util.HashMap; 39 import java.util.List; 40 import java.util.UUID; 41 import java.util.concurrent.ExecutionException; 42 import javax.annotation.Generated; 43 import org.junit.After; 44 import org.junit.AfterClass; 45 import org.junit.Assert; 46 import org.junit.Before; 47 import org.junit.BeforeClass; 48 import org.junit.Test; 49 50 @Generated("by gapic-generator-java") 51 public class UserEventServiceClientTest { 52 private static MockServiceHelper mockServiceHelper; 53 private static MockUserEventService mockUserEventService; 54 private LocalChannelProvider channelProvider; 55 private UserEventServiceClient client; 56 57 @BeforeClass startStaticServer()58 public static void startStaticServer() { 59 mockUserEventService = new MockUserEventService(); 60 mockServiceHelper = 61 new MockServiceHelper( 62 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockUserEventService)); 63 mockServiceHelper.start(); 64 } 65 66 @AfterClass stopServer()67 public static void stopServer() { 68 mockServiceHelper.stop(); 69 } 70 71 @Before setUp()72 public void setUp() throws IOException { 73 mockServiceHelper.reset(); 74 channelProvider = mockServiceHelper.createChannelProvider(); 75 UserEventServiceSettings settings = 76 UserEventServiceSettings.newBuilder() 77 .setTransportChannelProvider(channelProvider) 78 .setCredentialsProvider(NoCredentialsProvider.create()) 79 .build(); 80 client = UserEventServiceClient.create(settings); 81 } 82 83 @After tearDown()84 public void tearDown() throws Exception { 85 client.close(); 86 } 87 88 @Test writeUserEventTest()89 public void writeUserEventTest() throws Exception { 90 UserEvent expectedResponse = 91 UserEvent.newBuilder() 92 .setEventType("eventType31430900") 93 .setUserPseudoId("userPseudoId-1155274652") 94 .setEventTime(Timestamp.newBuilder().build()) 95 .setUserInfo(UserInfo.newBuilder().build()) 96 .setDirectUserRequest(true) 97 .setSessionId("sessionId607796817") 98 .setPageInfo(PageInfo.newBuilder().build()) 99 .setAttributionToken("attributionToken104706234") 100 .setFilter("filter-1274492040") 101 .addAllDocuments(new ArrayList<DocumentInfo>()) 102 .setPanel(PanelInfo.newBuilder().build()) 103 .setSearchInfo(SearchInfo.newBuilder().build()) 104 .setCompletionInfo(CompletionInfo.newBuilder().build()) 105 .setTransactionInfo(TransactionInfo.newBuilder().build()) 106 .addAllTagIds(new ArrayList<String>()) 107 .addAllPromotionIds(new ArrayList<String>()) 108 .putAllAttributes(new HashMap<String, CustomAttribute>()) 109 .setMediaInfo(MediaInfo.newBuilder().build()) 110 .build(); 111 mockUserEventService.addResponse(expectedResponse); 112 113 WriteUserEventRequest request = 114 WriteUserEventRequest.newBuilder() 115 .setParent( 116 DataStoreName.ofProjectLocationDataStoreName( 117 "[PROJECT]", "[LOCATION]", "[DATA_STORE]") 118 .toString()) 119 .setUserEvent(UserEvent.newBuilder().build()) 120 .build(); 121 122 UserEvent actualResponse = client.writeUserEvent(request); 123 Assert.assertEquals(expectedResponse, actualResponse); 124 125 List<AbstractMessage> actualRequests = mockUserEventService.getRequests(); 126 Assert.assertEquals(1, actualRequests.size()); 127 WriteUserEventRequest actualRequest = ((WriteUserEventRequest) actualRequests.get(0)); 128 129 Assert.assertEquals(request.getParent(), actualRequest.getParent()); 130 Assert.assertEquals(request.getUserEvent(), actualRequest.getUserEvent()); 131 Assert.assertTrue( 132 channelProvider.isHeaderSent( 133 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 134 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 135 } 136 137 @Test writeUserEventExceptionTest()138 public void writeUserEventExceptionTest() throws Exception { 139 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 140 mockUserEventService.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 mockUserEventService.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<AbstractMessage> actualRequests = mockUserEventService.getRequests(); 183 Assert.assertEquals(1, actualRequests.size()); 184 CollectUserEventRequest actualRequest = ((CollectUserEventRequest) actualRequests.get(0)); 185 186 Assert.assertEquals(request.getParent(), actualRequest.getParent()); 187 Assert.assertEquals(request.getUserEvent(), actualRequest.getUserEvent()); 188 Assert.assertEquals(request.getUri(), actualRequest.getUri()); 189 Assert.assertEquals(request.getEts(), actualRequest.getEts()); 190 Assert.assertTrue( 191 channelProvider.isHeaderSent( 192 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 193 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 194 } 195 196 @Test collectUserEventExceptionTest()197 public void collectUserEventExceptionTest() throws Exception { 198 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 199 mockUserEventService.addException(exception); 200 201 try { 202 CollectUserEventRequest request = 203 CollectUserEventRequest.newBuilder() 204 .setParent( 205 DataStoreName.ofProjectLocationDataStoreName( 206 "[PROJECT]", "[LOCATION]", "[DATA_STORE]") 207 .toString()) 208 .setUserEvent("userEvent315571599") 209 .setUri("uri116076") 210 .setEts(100772) 211 .build(); 212 client.collectUserEvent(request); 213 Assert.fail("No exception raised"); 214 } catch (InvalidArgumentException e) { 215 // Expected exception. 216 } 217 } 218 219 @Test importUserEventsTest()220 public void importUserEventsTest() throws Exception { 221 ImportUserEventsResponse expectedResponse = 222 ImportUserEventsResponse.newBuilder() 223 .addAllErrorSamples(new ArrayList<Status>()) 224 .setErrorConfig(ImportErrorConfig.newBuilder().build()) 225 .setJoinedEventsCount(-720068705) 226 .setUnjoinedEventsCount(512159846) 227 .build(); 228 Operation resultOperation = 229 Operation.newBuilder() 230 .setName("importUserEventsTest") 231 .setDone(true) 232 .setResponse(Any.pack(expectedResponse)) 233 .build(); 234 mockUserEventService.addResponse(resultOperation); 235 236 ImportUserEventsRequest request = 237 ImportUserEventsRequest.newBuilder() 238 .setParent( 239 DataStoreName.ofProjectLocationDataStoreName( 240 "[PROJECT]", "[LOCATION]", "[DATA_STORE]") 241 .toString()) 242 .setErrorConfig(ImportErrorConfig.newBuilder().build()) 243 .build(); 244 245 ImportUserEventsResponse actualResponse = client.importUserEventsAsync(request).get(); 246 Assert.assertEquals(expectedResponse, actualResponse); 247 248 List<AbstractMessage> actualRequests = mockUserEventService.getRequests(); 249 Assert.assertEquals(1, actualRequests.size()); 250 ImportUserEventsRequest actualRequest = ((ImportUserEventsRequest) actualRequests.get(0)); 251 252 Assert.assertEquals(request.getInlineSource(), actualRequest.getInlineSource()); 253 Assert.assertEquals(request.getGcsSource(), actualRequest.getGcsSource()); 254 Assert.assertEquals(request.getBigquerySource(), actualRequest.getBigquerySource()); 255 Assert.assertEquals(request.getParent(), actualRequest.getParent()); 256 Assert.assertEquals(request.getErrorConfig(), actualRequest.getErrorConfig()); 257 Assert.assertTrue( 258 channelProvider.isHeaderSent( 259 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 260 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 261 } 262 263 @Test importUserEventsExceptionTest()264 public void importUserEventsExceptionTest() throws Exception { 265 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 266 mockUserEventService.addException(exception); 267 268 try { 269 ImportUserEventsRequest request = 270 ImportUserEventsRequest.newBuilder() 271 .setParent( 272 DataStoreName.ofProjectLocationDataStoreName( 273 "[PROJECT]", "[LOCATION]", "[DATA_STORE]") 274 .toString()) 275 .setErrorConfig(ImportErrorConfig.newBuilder().build()) 276 .build(); 277 client.importUserEventsAsync(request).get(); 278 Assert.fail("No exception raised"); 279 } catch (ExecutionException e) { 280 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 281 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 282 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 283 } 284 } 285 } 286