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