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