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