• 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.IntentsClient.ListIntentsPagedResponse;
20 import static com.google.cloud.dialogflow.cx.v3.IntentsClient.ListLocationsPagedResponse;
21 
22 import com.google.api.gax.core.NoCredentialsProvider;
23 import com.google.api.gax.grpc.GaxGrpcProperties;
24 import com.google.api.gax.grpc.testing.LocalChannelProvider;
25 import com.google.api.gax.grpc.testing.MockGrpcService;
26 import com.google.api.gax.grpc.testing.MockServiceHelper;
27 import com.google.api.gax.rpc.ApiClientHeaderProvider;
28 import com.google.api.gax.rpc.InvalidArgumentException;
29 import com.google.cloud.location.GetLocationRequest;
30 import com.google.cloud.location.ListLocationsRequest;
31 import com.google.cloud.location.ListLocationsResponse;
32 import com.google.cloud.location.Location;
33 import com.google.common.collect.Lists;
34 import com.google.protobuf.AbstractMessage;
35 import com.google.protobuf.Any;
36 import com.google.protobuf.Empty;
37 import com.google.protobuf.FieldMask;
38 import io.grpc.StatusRuntimeException;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.UUID;
45 import javax.annotation.Generated;
46 import org.junit.After;
47 import org.junit.AfterClass;
48 import org.junit.Assert;
49 import org.junit.Before;
50 import org.junit.BeforeClass;
51 import org.junit.Test;
52 
53 @Generated("by gapic-generator-java")
54 public class IntentsClientTest {
55   private static MockIntents mockIntents;
56   private static MockLocations mockLocations;
57   private static MockServiceHelper mockServiceHelper;
58   private LocalChannelProvider channelProvider;
59   private IntentsClient client;
60 
61   @BeforeClass
startStaticServer()62   public static void startStaticServer() {
63     mockIntents = new MockIntents();
64     mockLocations = new MockLocations();
65     mockServiceHelper =
66         new MockServiceHelper(
67             UUID.randomUUID().toString(),
68             Arrays.<MockGrpcService>asList(mockIntents, mockLocations));
69     mockServiceHelper.start();
70   }
71 
72   @AfterClass
stopServer()73   public static void stopServer() {
74     mockServiceHelper.stop();
75   }
76 
77   @Before
setUp()78   public void setUp() throws IOException {
79     mockServiceHelper.reset();
80     channelProvider = mockServiceHelper.createChannelProvider();
81     IntentsSettings settings =
82         IntentsSettings.newBuilder()
83             .setTransportChannelProvider(channelProvider)
84             .setCredentialsProvider(NoCredentialsProvider.create())
85             .build();
86     client = IntentsClient.create(settings);
87   }
88 
89   @After
tearDown()90   public void tearDown() throws Exception {
91     client.close();
92   }
93 
94   @Test
listIntentsTest()95   public void listIntentsTest() throws Exception {
96     Intent responsesElement = Intent.newBuilder().build();
97     ListIntentsResponse expectedResponse =
98         ListIntentsResponse.newBuilder()
99             .setNextPageToken("")
100             .addAllIntents(Arrays.asList(responsesElement))
101             .build();
102     mockIntents.addResponse(expectedResponse);
103 
104     AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
105 
106     ListIntentsPagedResponse pagedListResponse = client.listIntents(parent);
107 
108     List<Intent> resources = Lists.newArrayList(pagedListResponse.iterateAll());
109 
110     Assert.assertEquals(1, resources.size());
111     Assert.assertEquals(expectedResponse.getIntentsList().get(0), resources.get(0));
112 
113     List<AbstractMessage> actualRequests = mockIntents.getRequests();
114     Assert.assertEquals(1, actualRequests.size());
115     ListIntentsRequest actualRequest = ((ListIntentsRequest) actualRequests.get(0));
116 
117     Assert.assertEquals(parent.toString(), actualRequest.getParent());
118     Assert.assertTrue(
119         channelProvider.isHeaderSent(
120             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
121             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
122   }
123 
124   @Test
listIntentsExceptionTest()125   public void listIntentsExceptionTest() throws Exception {
126     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
127     mockIntents.addException(exception);
128 
129     try {
130       AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
131       client.listIntents(parent);
132       Assert.fail("No exception raised");
133     } catch (InvalidArgumentException e) {
134       // Expected exception.
135     }
136   }
137 
138   @Test
listIntentsTest2()139   public void listIntentsTest2() throws Exception {
140     Intent responsesElement = Intent.newBuilder().build();
141     ListIntentsResponse expectedResponse =
142         ListIntentsResponse.newBuilder()
143             .setNextPageToken("")
144             .addAllIntents(Arrays.asList(responsesElement))
145             .build();
146     mockIntents.addResponse(expectedResponse);
147 
148     String parent = "parent-995424086";
149 
150     ListIntentsPagedResponse pagedListResponse = client.listIntents(parent);
151 
152     List<Intent> resources = Lists.newArrayList(pagedListResponse.iterateAll());
153 
154     Assert.assertEquals(1, resources.size());
155     Assert.assertEquals(expectedResponse.getIntentsList().get(0), resources.get(0));
156 
157     List<AbstractMessage> actualRequests = mockIntents.getRequests();
158     Assert.assertEquals(1, actualRequests.size());
159     ListIntentsRequest actualRequest = ((ListIntentsRequest) actualRequests.get(0));
160 
161     Assert.assertEquals(parent, actualRequest.getParent());
162     Assert.assertTrue(
163         channelProvider.isHeaderSent(
164             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
165             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
166   }
167 
168   @Test
listIntentsExceptionTest2()169   public void listIntentsExceptionTest2() throws Exception {
170     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
171     mockIntents.addException(exception);
172 
173     try {
174       String parent = "parent-995424086";
175       client.listIntents(parent);
176       Assert.fail("No exception raised");
177     } catch (InvalidArgumentException e) {
178       // Expected exception.
179     }
180   }
181 
182   @Test
getIntentTest()183   public void getIntentTest() throws Exception {
184     Intent expectedResponse =
185         Intent.newBuilder()
186             .setName(IntentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[INTENT]").toString())
187             .setDisplayName("displayName1714148973")
188             .addAllTrainingPhrases(new ArrayList<Intent.TrainingPhrase>())
189             .addAllParameters(new ArrayList<Intent.Parameter>())
190             .setPriority(-1165461084)
191             .setIsFallback(true)
192             .putAllLabels(new HashMap<String, String>())
193             .setDescription("description-1724546052")
194             .build();
195     mockIntents.addResponse(expectedResponse);
196 
197     IntentName name = IntentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[INTENT]");
198 
199     Intent actualResponse = client.getIntent(name);
200     Assert.assertEquals(expectedResponse, actualResponse);
201 
202     List<AbstractMessage> actualRequests = mockIntents.getRequests();
203     Assert.assertEquals(1, actualRequests.size());
204     GetIntentRequest actualRequest = ((GetIntentRequest) actualRequests.get(0));
205 
206     Assert.assertEquals(name.toString(), actualRequest.getName());
207     Assert.assertTrue(
208         channelProvider.isHeaderSent(
209             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
210             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
211   }
212 
213   @Test
getIntentExceptionTest()214   public void getIntentExceptionTest() throws Exception {
215     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
216     mockIntents.addException(exception);
217 
218     try {
219       IntentName name = IntentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[INTENT]");
220       client.getIntent(name);
221       Assert.fail("No exception raised");
222     } catch (InvalidArgumentException e) {
223       // Expected exception.
224     }
225   }
226 
227   @Test
getIntentTest2()228   public void getIntentTest2() throws Exception {
229     Intent expectedResponse =
230         Intent.newBuilder()
231             .setName(IntentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[INTENT]").toString())
232             .setDisplayName("displayName1714148973")
233             .addAllTrainingPhrases(new ArrayList<Intent.TrainingPhrase>())
234             .addAllParameters(new ArrayList<Intent.Parameter>())
235             .setPriority(-1165461084)
236             .setIsFallback(true)
237             .putAllLabels(new HashMap<String, String>())
238             .setDescription("description-1724546052")
239             .build();
240     mockIntents.addResponse(expectedResponse);
241 
242     String name = "name3373707";
243 
244     Intent actualResponse = client.getIntent(name);
245     Assert.assertEquals(expectedResponse, actualResponse);
246 
247     List<AbstractMessage> actualRequests = mockIntents.getRequests();
248     Assert.assertEquals(1, actualRequests.size());
249     GetIntentRequest actualRequest = ((GetIntentRequest) actualRequests.get(0));
250 
251     Assert.assertEquals(name, actualRequest.getName());
252     Assert.assertTrue(
253         channelProvider.isHeaderSent(
254             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
255             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
256   }
257 
258   @Test
getIntentExceptionTest2()259   public void getIntentExceptionTest2() throws Exception {
260     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
261     mockIntents.addException(exception);
262 
263     try {
264       String name = "name3373707";
265       client.getIntent(name);
266       Assert.fail("No exception raised");
267     } catch (InvalidArgumentException e) {
268       // Expected exception.
269     }
270   }
271 
272   @Test
createIntentTest()273   public void createIntentTest() throws Exception {
274     Intent expectedResponse =
275         Intent.newBuilder()
276             .setName(IntentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[INTENT]").toString())
277             .setDisplayName("displayName1714148973")
278             .addAllTrainingPhrases(new ArrayList<Intent.TrainingPhrase>())
279             .addAllParameters(new ArrayList<Intent.Parameter>())
280             .setPriority(-1165461084)
281             .setIsFallback(true)
282             .putAllLabels(new HashMap<String, String>())
283             .setDescription("description-1724546052")
284             .build();
285     mockIntents.addResponse(expectedResponse);
286 
287     AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
288     Intent intent = Intent.newBuilder().build();
289 
290     Intent actualResponse = client.createIntent(parent, intent);
291     Assert.assertEquals(expectedResponse, actualResponse);
292 
293     List<AbstractMessage> actualRequests = mockIntents.getRequests();
294     Assert.assertEquals(1, actualRequests.size());
295     CreateIntentRequest actualRequest = ((CreateIntentRequest) actualRequests.get(0));
296 
297     Assert.assertEquals(parent.toString(), actualRequest.getParent());
298     Assert.assertEquals(intent, actualRequest.getIntent());
299     Assert.assertTrue(
300         channelProvider.isHeaderSent(
301             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
302             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
303   }
304 
305   @Test
createIntentExceptionTest()306   public void createIntentExceptionTest() throws Exception {
307     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
308     mockIntents.addException(exception);
309 
310     try {
311       AgentName parent = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
312       Intent intent = Intent.newBuilder().build();
313       client.createIntent(parent, intent);
314       Assert.fail("No exception raised");
315     } catch (InvalidArgumentException e) {
316       // Expected exception.
317     }
318   }
319 
320   @Test
createIntentTest2()321   public void createIntentTest2() throws Exception {
322     Intent expectedResponse =
323         Intent.newBuilder()
324             .setName(IntentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[INTENT]").toString())
325             .setDisplayName("displayName1714148973")
326             .addAllTrainingPhrases(new ArrayList<Intent.TrainingPhrase>())
327             .addAllParameters(new ArrayList<Intent.Parameter>())
328             .setPriority(-1165461084)
329             .setIsFallback(true)
330             .putAllLabels(new HashMap<String, String>())
331             .setDescription("description-1724546052")
332             .build();
333     mockIntents.addResponse(expectedResponse);
334 
335     String parent = "parent-995424086";
336     Intent intent = Intent.newBuilder().build();
337 
338     Intent actualResponse = client.createIntent(parent, intent);
339     Assert.assertEquals(expectedResponse, actualResponse);
340 
341     List<AbstractMessage> actualRequests = mockIntents.getRequests();
342     Assert.assertEquals(1, actualRequests.size());
343     CreateIntentRequest actualRequest = ((CreateIntentRequest) actualRequests.get(0));
344 
345     Assert.assertEquals(parent, actualRequest.getParent());
346     Assert.assertEquals(intent, actualRequest.getIntent());
347     Assert.assertTrue(
348         channelProvider.isHeaderSent(
349             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
350             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
351   }
352 
353   @Test
createIntentExceptionTest2()354   public void createIntentExceptionTest2() throws Exception {
355     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
356     mockIntents.addException(exception);
357 
358     try {
359       String parent = "parent-995424086";
360       Intent intent = Intent.newBuilder().build();
361       client.createIntent(parent, intent);
362       Assert.fail("No exception raised");
363     } catch (InvalidArgumentException e) {
364       // Expected exception.
365     }
366   }
367 
368   @Test
updateIntentTest()369   public void updateIntentTest() throws Exception {
370     Intent expectedResponse =
371         Intent.newBuilder()
372             .setName(IntentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[INTENT]").toString())
373             .setDisplayName("displayName1714148973")
374             .addAllTrainingPhrases(new ArrayList<Intent.TrainingPhrase>())
375             .addAllParameters(new ArrayList<Intent.Parameter>())
376             .setPriority(-1165461084)
377             .setIsFallback(true)
378             .putAllLabels(new HashMap<String, String>())
379             .setDescription("description-1724546052")
380             .build();
381     mockIntents.addResponse(expectedResponse);
382 
383     Intent intent = Intent.newBuilder().build();
384     FieldMask updateMask = FieldMask.newBuilder().build();
385 
386     Intent actualResponse = client.updateIntent(intent, updateMask);
387     Assert.assertEquals(expectedResponse, actualResponse);
388 
389     List<AbstractMessage> actualRequests = mockIntents.getRequests();
390     Assert.assertEquals(1, actualRequests.size());
391     UpdateIntentRequest actualRequest = ((UpdateIntentRequest) actualRequests.get(0));
392 
393     Assert.assertEquals(intent, actualRequest.getIntent());
394     Assert.assertEquals(updateMask, actualRequest.getUpdateMask());
395     Assert.assertTrue(
396         channelProvider.isHeaderSent(
397             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
398             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
399   }
400 
401   @Test
updateIntentExceptionTest()402   public void updateIntentExceptionTest() throws Exception {
403     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
404     mockIntents.addException(exception);
405 
406     try {
407       Intent intent = Intent.newBuilder().build();
408       FieldMask updateMask = FieldMask.newBuilder().build();
409       client.updateIntent(intent, updateMask);
410       Assert.fail("No exception raised");
411     } catch (InvalidArgumentException e) {
412       // Expected exception.
413     }
414   }
415 
416   @Test
deleteIntentTest()417   public void deleteIntentTest() throws Exception {
418     Empty expectedResponse = Empty.newBuilder().build();
419     mockIntents.addResponse(expectedResponse);
420 
421     IntentName name = IntentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[INTENT]");
422 
423     client.deleteIntent(name);
424 
425     List<AbstractMessage> actualRequests = mockIntents.getRequests();
426     Assert.assertEquals(1, actualRequests.size());
427     DeleteIntentRequest actualRequest = ((DeleteIntentRequest) actualRequests.get(0));
428 
429     Assert.assertEquals(name.toString(), actualRequest.getName());
430     Assert.assertTrue(
431         channelProvider.isHeaderSent(
432             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
433             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
434   }
435 
436   @Test
deleteIntentExceptionTest()437   public void deleteIntentExceptionTest() throws Exception {
438     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
439     mockIntents.addException(exception);
440 
441     try {
442       IntentName name = IntentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[INTENT]");
443       client.deleteIntent(name);
444       Assert.fail("No exception raised");
445     } catch (InvalidArgumentException e) {
446       // Expected exception.
447     }
448   }
449 
450   @Test
deleteIntentTest2()451   public void deleteIntentTest2() throws Exception {
452     Empty expectedResponse = Empty.newBuilder().build();
453     mockIntents.addResponse(expectedResponse);
454 
455     String name = "name3373707";
456 
457     client.deleteIntent(name);
458 
459     List<AbstractMessage> actualRequests = mockIntents.getRequests();
460     Assert.assertEquals(1, actualRequests.size());
461     DeleteIntentRequest actualRequest = ((DeleteIntentRequest) actualRequests.get(0));
462 
463     Assert.assertEquals(name, actualRequest.getName());
464     Assert.assertTrue(
465         channelProvider.isHeaderSent(
466             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
467             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
468   }
469 
470   @Test
deleteIntentExceptionTest2()471   public void deleteIntentExceptionTest2() throws Exception {
472     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
473     mockIntents.addException(exception);
474 
475     try {
476       String name = "name3373707";
477       client.deleteIntent(name);
478       Assert.fail("No exception raised");
479     } catch (InvalidArgumentException e) {
480       // Expected exception.
481     }
482   }
483 
484   @Test
listLocationsTest()485   public void listLocationsTest() throws Exception {
486     Location responsesElement = Location.newBuilder().build();
487     ListLocationsResponse expectedResponse =
488         ListLocationsResponse.newBuilder()
489             .setNextPageToken("")
490             .addAllLocations(Arrays.asList(responsesElement))
491             .build();
492     mockLocations.addResponse(expectedResponse);
493 
494     ListLocationsRequest request =
495         ListLocationsRequest.newBuilder()
496             .setName("name3373707")
497             .setFilter("filter-1274492040")
498             .setPageSize(883849137)
499             .setPageToken("pageToken873572522")
500             .build();
501 
502     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
503 
504     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
505 
506     Assert.assertEquals(1, resources.size());
507     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
508 
509     List<AbstractMessage> actualRequests = mockLocations.getRequests();
510     Assert.assertEquals(1, actualRequests.size());
511     ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0));
512 
513     Assert.assertEquals(request.getName(), actualRequest.getName());
514     Assert.assertEquals(request.getFilter(), actualRequest.getFilter());
515     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
516     Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
517     Assert.assertTrue(
518         channelProvider.isHeaderSent(
519             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
520             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
521   }
522 
523   @Test
listLocationsExceptionTest()524   public void listLocationsExceptionTest() throws Exception {
525     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
526     mockLocations.addException(exception);
527 
528     try {
529       ListLocationsRequest request =
530           ListLocationsRequest.newBuilder()
531               .setName("name3373707")
532               .setFilter("filter-1274492040")
533               .setPageSize(883849137)
534               .setPageToken("pageToken873572522")
535               .build();
536       client.listLocations(request);
537       Assert.fail("No exception raised");
538     } catch (InvalidArgumentException e) {
539       // Expected exception.
540     }
541   }
542 
543   @Test
getLocationTest()544   public void getLocationTest() throws Exception {
545     Location expectedResponse =
546         Location.newBuilder()
547             .setName("name3373707")
548             .setLocationId("locationId1541836720")
549             .setDisplayName("displayName1714148973")
550             .putAllLabels(new HashMap<String, String>())
551             .setMetadata(Any.newBuilder().build())
552             .build();
553     mockLocations.addResponse(expectedResponse);
554 
555     GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
556 
557     Location actualResponse = client.getLocation(request);
558     Assert.assertEquals(expectedResponse, actualResponse);
559 
560     List<AbstractMessage> actualRequests = mockLocations.getRequests();
561     Assert.assertEquals(1, actualRequests.size());
562     GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0));
563 
564     Assert.assertEquals(request.getName(), actualRequest.getName());
565     Assert.assertTrue(
566         channelProvider.isHeaderSent(
567             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
568             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
569   }
570 
571   @Test
getLocationExceptionTest()572   public void getLocationExceptionTest() throws Exception {
573     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
574     mockLocations.addException(exception);
575 
576     try {
577       GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
578       client.getLocation(request);
579       Assert.fail("No exception raised");
580     } catch (InvalidArgumentException e) {
581       // Expected exception.
582     }
583   }
584 }
585