• 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.httpjson.GaxHttpJsonProperties;
23 import com.google.api.gax.httpjson.testing.MockHttpService;
24 import com.google.api.gax.rpc.ApiClientHeaderProvider;
25 import com.google.api.gax.rpc.ApiException;
26 import com.google.api.gax.rpc.ApiExceptionFactory;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.api.gax.rpc.StatusCode;
29 import com.google.api.gax.rpc.testing.FakeStatusCode;
30 import com.google.cloud.dialogflow.cx.v3.stub.HttpJsonSessionsStub;
31 import com.google.cloud.location.GetLocationRequest;
32 import com.google.cloud.location.ListLocationsRequest;
33 import com.google.cloud.location.ListLocationsResponse;
34 import com.google.cloud.location.Location;
35 import com.google.common.collect.Lists;
36 import com.google.protobuf.Any;
37 import com.google.protobuf.ByteString;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import java.util.List;
43 import javax.annotation.Generated;
44 import org.junit.After;
45 import org.junit.AfterClass;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 
51 @Generated("by gapic-generator-java")
52 public class SessionsClientHttpJsonTest {
53   private static MockHttpService mockService;
54   private static SessionsClient client;
55 
56   @BeforeClass
startStaticServer()57   public static void startStaticServer() throws IOException {
58     mockService =
59         new MockHttpService(
60             HttpJsonSessionsStub.getMethodDescriptors(), SessionsSettings.getDefaultEndpoint());
61     SessionsSettings settings =
62         SessionsSettings.newHttpJsonBuilder()
63             .setTransportChannelProvider(
64                 SessionsSettings.defaultHttpJsonTransportProviderBuilder()
65                     .setHttpTransport(mockService)
66                     .build())
67             .setCredentialsProvider(NoCredentialsProvider.create())
68             .build();
69     client = SessionsClient.create(settings);
70   }
71 
72   @AfterClass
stopServer()73   public static void stopServer() {
74     client.close();
75   }
76 
77   @Before
setUp()78   public void setUp() {}
79 
80   @After
tearDown()81   public void tearDown() throws Exception {
82     mockService.reset();
83   }
84 
85   @Test
detectIntentTest()86   public void detectIntentTest() throws Exception {
87     DetectIntentResponse expectedResponse =
88         DetectIntentResponse.newBuilder()
89             .setResponseId("responseId-633138884")
90             .setQueryResult(QueryResult.newBuilder().build())
91             .setOutputAudio(ByteString.EMPTY)
92             .setOutputAudioConfig(OutputAudioConfig.newBuilder().build())
93             .setAllowCancellation(true)
94             .build();
95     mockService.addResponse(expectedResponse);
96 
97     DetectIntentRequest request =
98         DetectIntentRequest.newBuilder()
99             .setSession(
100                 SessionName.ofProjectLocationAgentSessionName(
101                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[SESSION]")
102                     .toString())
103             .setQueryParams(QueryParameters.newBuilder().build())
104             .setQueryInput(QueryInput.newBuilder().build())
105             .setOutputAudioConfig(OutputAudioConfig.newBuilder().build())
106             .build();
107 
108     DetectIntentResponse actualResponse = client.detectIntent(request);
109     Assert.assertEquals(expectedResponse, actualResponse);
110 
111     List<String> actualRequests = mockService.getRequestPaths();
112     Assert.assertEquals(1, actualRequests.size());
113 
114     String apiClientHeaderKey =
115         mockService
116             .getRequestHeaders()
117             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
118             .iterator()
119             .next();
120     Assert.assertTrue(
121         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
122             .matcher(apiClientHeaderKey)
123             .matches());
124   }
125 
126   @Test
detectIntentExceptionTest()127   public void detectIntentExceptionTest() throws Exception {
128     ApiException exception =
129         ApiExceptionFactory.createException(
130             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
131     mockService.addException(exception);
132 
133     try {
134       DetectIntentRequest request =
135           DetectIntentRequest.newBuilder()
136               .setSession(
137                   SessionName.ofProjectLocationAgentSessionName(
138                           "[PROJECT]", "[LOCATION]", "[AGENT]", "[SESSION]")
139                       .toString())
140               .setQueryParams(QueryParameters.newBuilder().build())
141               .setQueryInput(QueryInput.newBuilder().build())
142               .setOutputAudioConfig(OutputAudioConfig.newBuilder().build())
143               .build();
144       client.detectIntent(request);
145       Assert.fail("No exception raised");
146     } catch (InvalidArgumentException e) {
147       // Expected exception.
148     }
149   }
150 
151   @Test
streamingDetectIntentUnsupportedMethodTest()152   public void streamingDetectIntentUnsupportedMethodTest() throws Exception {
153     // The streamingDetectIntent() method is not supported in REST transport.
154     // This empty test is generated for technical reasons.
155   }
156 
157   @Test
matchIntentTest()158   public void matchIntentTest() throws Exception {
159     MatchIntentResponse expectedResponse =
160         MatchIntentResponse.newBuilder()
161             .addAllMatches(new ArrayList<Match>())
162             .setCurrentPage(Page.newBuilder().build())
163             .build();
164     mockService.addResponse(expectedResponse);
165 
166     MatchIntentRequest request =
167         MatchIntentRequest.newBuilder()
168             .setSession(
169                 SessionName.ofProjectLocationAgentSessionName(
170                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[SESSION]")
171                     .toString())
172             .setQueryParams(QueryParameters.newBuilder().build())
173             .setQueryInput(QueryInput.newBuilder().build())
174             .setPersistParameterChanges(true)
175             .build();
176 
177     MatchIntentResponse actualResponse = client.matchIntent(request);
178     Assert.assertEquals(expectedResponse, actualResponse);
179 
180     List<String> actualRequests = mockService.getRequestPaths();
181     Assert.assertEquals(1, actualRequests.size());
182 
183     String apiClientHeaderKey =
184         mockService
185             .getRequestHeaders()
186             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
187             .iterator()
188             .next();
189     Assert.assertTrue(
190         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
191             .matcher(apiClientHeaderKey)
192             .matches());
193   }
194 
195   @Test
matchIntentExceptionTest()196   public void matchIntentExceptionTest() throws Exception {
197     ApiException exception =
198         ApiExceptionFactory.createException(
199             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
200     mockService.addException(exception);
201 
202     try {
203       MatchIntentRequest request =
204           MatchIntentRequest.newBuilder()
205               .setSession(
206                   SessionName.ofProjectLocationAgentSessionName(
207                           "[PROJECT]", "[LOCATION]", "[AGENT]", "[SESSION]")
208                       .toString())
209               .setQueryParams(QueryParameters.newBuilder().build())
210               .setQueryInput(QueryInput.newBuilder().build())
211               .setPersistParameterChanges(true)
212               .build();
213       client.matchIntent(request);
214       Assert.fail("No exception raised");
215     } catch (InvalidArgumentException e) {
216       // Expected exception.
217     }
218   }
219 
220   @Test
fulfillIntentTest()221   public void fulfillIntentTest() throws Exception {
222     FulfillIntentResponse expectedResponse =
223         FulfillIntentResponse.newBuilder()
224             .setResponseId("responseId-633138884")
225             .setQueryResult(QueryResult.newBuilder().build())
226             .setOutputAudio(ByteString.EMPTY)
227             .setOutputAudioConfig(OutputAudioConfig.newBuilder().build())
228             .build();
229     mockService.addResponse(expectedResponse);
230 
231     FulfillIntentRequest request =
232         FulfillIntentRequest.newBuilder()
233             .setMatchIntentRequest(
234                 MatchIntentRequest.newBuilder()
235                     .setSession(
236                         SessionName.ofProjectLocationAgentSessionName(
237                                 "[PROJECT]", "[LOCATION]", "[AGENT]", "[SESSION]")
238                             .toString())
239                     .setQueryParams(QueryParameters.newBuilder().build())
240                     .setQueryInput(QueryInput.newBuilder().build())
241                     .setPersistParameterChanges(true)
242                     .build())
243             .setMatch(Match.newBuilder().build())
244             .setOutputAudioConfig(OutputAudioConfig.newBuilder().build())
245             .build();
246 
247     FulfillIntentResponse actualResponse = client.fulfillIntent(request);
248     Assert.assertEquals(expectedResponse, actualResponse);
249 
250     List<String> actualRequests = mockService.getRequestPaths();
251     Assert.assertEquals(1, actualRequests.size());
252 
253     String apiClientHeaderKey =
254         mockService
255             .getRequestHeaders()
256             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
257             .iterator()
258             .next();
259     Assert.assertTrue(
260         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
261             .matcher(apiClientHeaderKey)
262             .matches());
263   }
264 
265   @Test
fulfillIntentExceptionTest()266   public void fulfillIntentExceptionTest() throws Exception {
267     ApiException exception =
268         ApiExceptionFactory.createException(
269             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
270     mockService.addException(exception);
271 
272     try {
273       FulfillIntentRequest request =
274           FulfillIntentRequest.newBuilder()
275               .setMatchIntentRequest(
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               .setMatch(Match.newBuilder().build())
286               .setOutputAudioConfig(OutputAudioConfig.newBuilder().build())
287               .build();
288       client.fulfillIntent(request);
289       Assert.fail("No exception raised");
290     } catch (InvalidArgumentException e) {
291       // Expected exception.
292     }
293   }
294 
295   @Test
listLocationsTest()296   public void listLocationsTest() throws Exception {
297     Location responsesElement = Location.newBuilder().build();
298     ListLocationsResponse expectedResponse =
299         ListLocationsResponse.newBuilder()
300             .setNextPageToken("")
301             .addAllLocations(Arrays.asList(responsesElement))
302             .build();
303     mockService.addResponse(expectedResponse);
304 
305     ListLocationsRequest request =
306         ListLocationsRequest.newBuilder()
307             .setName("projects/project-3664")
308             .setFilter("filter-1274492040")
309             .setPageSize(883849137)
310             .setPageToken("pageToken873572522")
311             .build();
312 
313     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
314 
315     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
316 
317     Assert.assertEquals(1, resources.size());
318     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
319 
320     List<String> actualRequests = mockService.getRequestPaths();
321     Assert.assertEquals(1, actualRequests.size());
322 
323     String apiClientHeaderKey =
324         mockService
325             .getRequestHeaders()
326             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
327             .iterator()
328             .next();
329     Assert.assertTrue(
330         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
331             .matcher(apiClientHeaderKey)
332             .matches());
333   }
334 
335   @Test
listLocationsExceptionTest()336   public void listLocationsExceptionTest() throws Exception {
337     ApiException exception =
338         ApiExceptionFactory.createException(
339             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
340     mockService.addException(exception);
341 
342     try {
343       ListLocationsRequest request =
344           ListLocationsRequest.newBuilder()
345               .setName("projects/project-3664")
346               .setFilter("filter-1274492040")
347               .setPageSize(883849137)
348               .setPageToken("pageToken873572522")
349               .build();
350       client.listLocations(request);
351       Assert.fail("No exception raised");
352     } catch (InvalidArgumentException e) {
353       // Expected exception.
354     }
355   }
356 
357   @Test
getLocationTest()358   public void getLocationTest() throws Exception {
359     Location expectedResponse =
360         Location.newBuilder()
361             .setName("name3373707")
362             .setLocationId("locationId1541836720")
363             .setDisplayName("displayName1714148973")
364             .putAllLabels(new HashMap<String, String>())
365             .setMetadata(Any.newBuilder().build())
366             .build();
367     mockService.addResponse(expectedResponse);
368 
369     GetLocationRequest request =
370         GetLocationRequest.newBuilder()
371             .setName("projects/project-9062/locations/location-9062")
372             .build();
373 
374     Location actualResponse = client.getLocation(request);
375     Assert.assertEquals(expectedResponse, actualResponse);
376 
377     List<String> actualRequests = mockService.getRequestPaths();
378     Assert.assertEquals(1, actualRequests.size());
379 
380     String apiClientHeaderKey =
381         mockService
382             .getRequestHeaders()
383             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
384             .iterator()
385             .next();
386     Assert.assertTrue(
387         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
388             .matcher(apiClientHeaderKey)
389             .matches());
390   }
391 
392   @Test
getLocationExceptionTest()393   public void getLocationExceptionTest() throws Exception {
394     ApiException exception =
395         ApiExceptionFactory.createException(
396             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
397     mockService.addException(exception);
398 
399     try {
400       GetLocationRequest request =
401           GetLocationRequest.newBuilder()
402               .setName("projects/project-9062/locations/location-9062")
403               .build();
404       client.getLocation(request);
405       Assert.fail("No exception raised");
406     } catch (InvalidArgumentException e) {
407       // Expected exception.
408     }
409   }
410 }
411