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.dialogflow.v2; 18 19 import static com.google.cloud.dialogflow.v2.SessionsClient.ListLocationsPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.grpc.GaxGrpcProperties; 23 import com.google.api.gax.grpc.testing.LocalChannelProvider; 24 import com.google.api.gax.grpc.testing.MockGrpcService; 25 import com.google.api.gax.grpc.testing.MockServiceHelper; 26 import com.google.api.gax.grpc.testing.MockStreamObserver; 27 import com.google.api.gax.rpc.ApiClientHeaderProvider; 28 import com.google.api.gax.rpc.ApiStreamObserver; 29 import com.google.api.gax.rpc.BidiStreamingCallable; 30 import com.google.api.gax.rpc.InvalidArgumentException; 31 import com.google.api.gax.rpc.StatusCode; 32 import com.google.cloud.location.GetLocationRequest; 33 import com.google.cloud.location.ListLocationsRequest; 34 import com.google.cloud.location.ListLocationsResponse; 35 import com.google.cloud.location.Location; 36 import com.google.common.collect.Lists; 37 import com.google.protobuf.AbstractMessage; 38 import com.google.protobuf.Any; 39 import com.google.protobuf.ByteString; 40 import com.google.protobuf.FieldMask; 41 import com.google.rpc.Status; 42 import io.grpc.StatusRuntimeException; 43 import java.io.IOException; 44 import java.util.Arrays; 45 import java.util.HashMap; 46 import java.util.List; 47 import java.util.UUID; 48 import java.util.concurrent.ExecutionException; 49 import javax.annotation.Generated; 50 import org.junit.After; 51 import org.junit.AfterClass; 52 import org.junit.Assert; 53 import org.junit.Before; 54 import org.junit.BeforeClass; 55 import org.junit.Test; 56 57 @Generated("by gapic-generator-java") 58 public class SessionsClientTest { 59 private static MockLocations mockLocations; 60 private static MockServiceHelper mockServiceHelper; 61 private static MockSessions mockSessions; 62 private LocalChannelProvider channelProvider; 63 private SessionsClient client; 64 65 @BeforeClass startStaticServer()66 public static void startStaticServer() { 67 mockSessions = new MockSessions(); 68 mockLocations = new MockLocations(); 69 mockServiceHelper = 70 new MockServiceHelper( 71 UUID.randomUUID().toString(), 72 Arrays.<MockGrpcService>asList(mockSessions, mockLocations)); 73 mockServiceHelper.start(); 74 } 75 76 @AfterClass stopServer()77 public static void stopServer() { 78 mockServiceHelper.stop(); 79 } 80 81 @Before setUp()82 public void setUp() throws IOException { 83 mockServiceHelper.reset(); 84 channelProvider = mockServiceHelper.createChannelProvider(); 85 SessionsSettings settings = 86 SessionsSettings.newBuilder() 87 .setTransportChannelProvider(channelProvider) 88 .setCredentialsProvider(NoCredentialsProvider.create()) 89 .build(); 90 client = SessionsClient.create(settings); 91 } 92 93 @After tearDown()94 public void tearDown() throws Exception { 95 client.close(); 96 } 97 98 @Test detectIntentTest()99 public void detectIntentTest() throws Exception { 100 DetectIntentResponse expectedResponse = 101 DetectIntentResponse.newBuilder() 102 .setResponseId("responseId-633138884") 103 .setQueryResult(QueryResult.newBuilder().build()) 104 .setWebhookStatus(Status.newBuilder().build()) 105 .setOutputAudio(ByteString.EMPTY) 106 .setOutputAudioConfig(OutputAudioConfig.newBuilder().build()) 107 .build(); 108 mockSessions.addResponse(expectedResponse); 109 110 SessionName session = SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]"); 111 QueryInput queryInput = QueryInput.newBuilder().build(); 112 113 DetectIntentResponse actualResponse = client.detectIntent(session, queryInput); 114 Assert.assertEquals(expectedResponse, actualResponse); 115 116 List<AbstractMessage> actualRequests = mockSessions.getRequests(); 117 Assert.assertEquals(1, actualRequests.size()); 118 DetectIntentRequest actualRequest = ((DetectIntentRequest) actualRequests.get(0)); 119 120 Assert.assertEquals(session.toString(), actualRequest.getSession()); 121 Assert.assertEquals(queryInput, actualRequest.getQueryInput()); 122 Assert.assertTrue( 123 channelProvider.isHeaderSent( 124 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 125 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 126 } 127 128 @Test detectIntentExceptionTest()129 public void detectIntentExceptionTest() throws Exception { 130 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 131 mockSessions.addException(exception); 132 133 try { 134 SessionName session = SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]"); 135 QueryInput queryInput = QueryInput.newBuilder().build(); 136 client.detectIntent(session, queryInput); 137 Assert.fail("No exception raised"); 138 } catch (InvalidArgumentException e) { 139 // Expected exception. 140 } 141 } 142 143 @Test detectIntentTest2()144 public void detectIntentTest2() throws Exception { 145 DetectIntentResponse expectedResponse = 146 DetectIntentResponse.newBuilder() 147 .setResponseId("responseId-633138884") 148 .setQueryResult(QueryResult.newBuilder().build()) 149 .setWebhookStatus(Status.newBuilder().build()) 150 .setOutputAudio(ByteString.EMPTY) 151 .setOutputAudioConfig(OutputAudioConfig.newBuilder().build()) 152 .build(); 153 mockSessions.addResponse(expectedResponse); 154 155 String session = "session1984987798"; 156 QueryInput queryInput = QueryInput.newBuilder().build(); 157 158 DetectIntentResponse actualResponse = client.detectIntent(session, queryInput); 159 Assert.assertEquals(expectedResponse, actualResponse); 160 161 List<AbstractMessage> actualRequests = mockSessions.getRequests(); 162 Assert.assertEquals(1, actualRequests.size()); 163 DetectIntentRequest actualRequest = ((DetectIntentRequest) actualRequests.get(0)); 164 165 Assert.assertEquals(session, actualRequest.getSession()); 166 Assert.assertEquals(queryInput, actualRequest.getQueryInput()); 167 Assert.assertTrue( 168 channelProvider.isHeaderSent( 169 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 170 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 171 } 172 173 @Test detectIntentExceptionTest2()174 public void detectIntentExceptionTest2() throws Exception { 175 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 176 mockSessions.addException(exception); 177 178 try { 179 String session = "session1984987798"; 180 QueryInput queryInput = QueryInput.newBuilder().build(); 181 client.detectIntent(session, queryInput); 182 Assert.fail("No exception raised"); 183 } catch (InvalidArgumentException e) { 184 // Expected exception. 185 } 186 } 187 188 @Test streamingDetectIntentTest()189 public void streamingDetectIntentTest() throws Exception { 190 StreamingDetectIntentResponse expectedResponse = 191 StreamingDetectIntentResponse.newBuilder() 192 .setResponseId("responseId-633138884") 193 .setRecognitionResult(StreamingRecognitionResult.newBuilder().build()) 194 .setQueryResult(QueryResult.newBuilder().build()) 195 .setWebhookStatus(Status.newBuilder().build()) 196 .setOutputAudio(ByteString.EMPTY) 197 .setOutputAudioConfig(OutputAudioConfig.newBuilder().build()) 198 .setDebuggingInfo(CloudConversationDebuggingInfo.newBuilder().build()) 199 .build(); 200 mockSessions.addResponse(expectedResponse); 201 StreamingDetectIntentRequest request = 202 StreamingDetectIntentRequest.newBuilder() 203 .setSession(SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]").toString()) 204 .setQueryParams(QueryParameters.newBuilder().build()) 205 .setQueryInput(QueryInput.newBuilder().build()) 206 .setSingleUtterance(true) 207 .setOutputAudioConfig(OutputAudioConfig.newBuilder().build()) 208 .setOutputAudioConfigMask(FieldMask.newBuilder().build()) 209 .setInputAudio(ByteString.EMPTY) 210 .setEnableDebuggingInfo(true) 211 .build(); 212 213 MockStreamObserver<StreamingDetectIntentResponse> responseObserver = new MockStreamObserver<>(); 214 215 BidiStreamingCallable<StreamingDetectIntentRequest, StreamingDetectIntentResponse> callable = 216 client.streamingDetectIntentCallable(); 217 ApiStreamObserver<StreamingDetectIntentRequest> requestObserver = 218 callable.bidiStreamingCall(responseObserver); 219 220 requestObserver.onNext(request); 221 requestObserver.onCompleted(); 222 223 List<StreamingDetectIntentResponse> actualResponses = responseObserver.future().get(); 224 Assert.assertEquals(1, actualResponses.size()); 225 Assert.assertEquals(expectedResponse, actualResponses.get(0)); 226 } 227 228 @Test streamingDetectIntentExceptionTest()229 public void streamingDetectIntentExceptionTest() throws Exception { 230 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 231 mockSessions.addException(exception); 232 StreamingDetectIntentRequest request = 233 StreamingDetectIntentRequest.newBuilder() 234 .setSession(SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]").toString()) 235 .setQueryParams(QueryParameters.newBuilder().build()) 236 .setQueryInput(QueryInput.newBuilder().build()) 237 .setSingleUtterance(true) 238 .setOutputAudioConfig(OutputAudioConfig.newBuilder().build()) 239 .setOutputAudioConfigMask(FieldMask.newBuilder().build()) 240 .setInputAudio(ByteString.EMPTY) 241 .setEnableDebuggingInfo(true) 242 .build(); 243 244 MockStreamObserver<StreamingDetectIntentResponse> responseObserver = new MockStreamObserver<>(); 245 246 BidiStreamingCallable<StreamingDetectIntentRequest, StreamingDetectIntentResponse> callable = 247 client.streamingDetectIntentCallable(); 248 ApiStreamObserver<StreamingDetectIntentRequest> requestObserver = 249 callable.bidiStreamingCall(responseObserver); 250 251 requestObserver.onNext(request); 252 253 try { 254 List<StreamingDetectIntentResponse> actualResponses = responseObserver.future().get(); 255 Assert.fail("No exception thrown"); 256 } catch (ExecutionException e) { 257 Assert.assertTrue(e.getCause() instanceof InvalidArgumentException); 258 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 259 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 260 } 261 } 262 263 @Test listLocationsTest()264 public void listLocationsTest() throws Exception { 265 Location responsesElement = Location.newBuilder().build(); 266 ListLocationsResponse expectedResponse = 267 ListLocationsResponse.newBuilder() 268 .setNextPageToken("") 269 .addAllLocations(Arrays.asList(responsesElement)) 270 .build(); 271 mockLocations.addResponse(expectedResponse); 272 273 ListLocationsRequest request = 274 ListLocationsRequest.newBuilder() 275 .setName("name3373707") 276 .setFilter("filter-1274492040") 277 .setPageSize(883849137) 278 .setPageToken("pageToken873572522") 279 .build(); 280 281 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 282 283 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 284 285 Assert.assertEquals(1, resources.size()); 286 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 287 288 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 289 Assert.assertEquals(1, actualRequests.size()); 290 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 291 292 Assert.assertEquals(request.getName(), actualRequest.getName()); 293 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 294 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 295 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 296 Assert.assertTrue( 297 channelProvider.isHeaderSent( 298 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 299 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 300 } 301 302 @Test listLocationsExceptionTest()303 public void listLocationsExceptionTest() throws Exception { 304 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 305 mockLocations.addException(exception); 306 307 try { 308 ListLocationsRequest request = 309 ListLocationsRequest.newBuilder() 310 .setName("name3373707") 311 .setFilter("filter-1274492040") 312 .setPageSize(883849137) 313 .setPageToken("pageToken873572522") 314 .build(); 315 client.listLocations(request); 316 Assert.fail("No exception raised"); 317 } catch (InvalidArgumentException e) { 318 // Expected exception. 319 } 320 } 321 322 @Test getLocationTest()323 public void getLocationTest() throws Exception { 324 Location expectedResponse = 325 Location.newBuilder() 326 .setName("name3373707") 327 .setLocationId("locationId1541836720") 328 .setDisplayName("displayName1714148973") 329 .putAllLabels(new HashMap<String, String>()) 330 .setMetadata(Any.newBuilder().build()) 331 .build(); 332 mockLocations.addResponse(expectedResponse); 333 334 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 335 336 Location actualResponse = client.getLocation(request); 337 Assert.assertEquals(expectedResponse, actualResponse); 338 339 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 340 Assert.assertEquals(1, actualRequests.size()); 341 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 342 343 Assert.assertEquals(request.getName(), actualRequest.getName()); 344 Assert.assertTrue( 345 channelProvider.isHeaderSent( 346 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 347 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 348 } 349 350 @Test getLocationExceptionTest()351 public void getLocationExceptionTest() throws Exception { 352 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 353 mockLocations.addException(exception); 354 355 try { 356 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 357 client.getLocation(request); 358 Assert.fail("No exception raised"); 359 } catch (InvalidArgumentException e) { 360 // Expected exception. 361 } 362 } 363 } 364