• 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.v2;
18 
19 import static com.google.cloud.dialogflow.v2.FulfillmentsClient.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.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.cloud.location.GetLocationRequest;
29 import com.google.cloud.location.ListLocationsRequest;
30 import com.google.cloud.location.ListLocationsResponse;
31 import com.google.cloud.location.Location;
32 import com.google.common.collect.Lists;
33 import com.google.protobuf.AbstractMessage;
34 import com.google.protobuf.Any;
35 import com.google.protobuf.FieldMask;
36 import io.grpc.StatusRuntimeException;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.UUID;
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 FulfillmentsClientTest {
53   private static MockFulfillments mockFulfillments;
54   private static MockLocations mockLocations;
55   private static MockServiceHelper mockServiceHelper;
56   private LocalChannelProvider channelProvider;
57   private FulfillmentsClient client;
58 
59   @BeforeClass
startStaticServer()60   public static void startStaticServer() {
61     mockFulfillments = new MockFulfillments();
62     mockLocations = new MockLocations();
63     mockServiceHelper =
64         new MockServiceHelper(
65             UUID.randomUUID().toString(),
66             Arrays.<MockGrpcService>asList(mockFulfillments, mockLocations));
67     mockServiceHelper.start();
68   }
69 
70   @AfterClass
stopServer()71   public static void stopServer() {
72     mockServiceHelper.stop();
73   }
74 
75   @Before
setUp()76   public void setUp() throws IOException {
77     mockServiceHelper.reset();
78     channelProvider = mockServiceHelper.createChannelProvider();
79     FulfillmentsSettings settings =
80         FulfillmentsSettings.newBuilder()
81             .setTransportChannelProvider(channelProvider)
82             .setCredentialsProvider(NoCredentialsProvider.create())
83             .build();
84     client = FulfillmentsClient.create(settings);
85   }
86 
87   @After
tearDown()88   public void tearDown() throws Exception {
89     client.close();
90   }
91 
92   @Test
getFulfillmentTest()93   public void getFulfillmentTest() throws Exception {
94     Fulfillment expectedResponse =
95         Fulfillment.newBuilder()
96             .setName(FulfillmentName.ofProjectName("[PROJECT]").toString())
97             .setDisplayName("displayName1714148973")
98             .setEnabled(true)
99             .addAllFeatures(new ArrayList<Fulfillment.Feature>())
100             .build();
101     mockFulfillments.addResponse(expectedResponse);
102 
103     FulfillmentName name = FulfillmentName.ofProjectName("[PROJECT]");
104 
105     Fulfillment actualResponse = client.getFulfillment(name);
106     Assert.assertEquals(expectedResponse, actualResponse);
107 
108     List<AbstractMessage> actualRequests = mockFulfillments.getRequests();
109     Assert.assertEquals(1, actualRequests.size());
110     GetFulfillmentRequest actualRequest = ((GetFulfillmentRequest) actualRequests.get(0));
111 
112     Assert.assertEquals(name.toString(), actualRequest.getName());
113     Assert.assertTrue(
114         channelProvider.isHeaderSent(
115             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
116             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
117   }
118 
119   @Test
getFulfillmentExceptionTest()120   public void getFulfillmentExceptionTest() throws Exception {
121     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
122     mockFulfillments.addException(exception);
123 
124     try {
125       FulfillmentName name = FulfillmentName.ofProjectName("[PROJECT]");
126       client.getFulfillment(name);
127       Assert.fail("No exception raised");
128     } catch (InvalidArgumentException e) {
129       // Expected exception.
130     }
131   }
132 
133   @Test
getFulfillmentTest2()134   public void getFulfillmentTest2() throws Exception {
135     Fulfillment expectedResponse =
136         Fulfillment.newBuilder()
137             .setName(FulfillmentName.ofProjectName("[PROJECT]").toString())
138             .setDisplayName("displayName1714148973")
139             .setEnabled(true)
140             .addAllFeatures(new ArrayList<Fulfillment.Feature>())
141             .build();
142     mockFulfillments.addResponse(expectedResponse);
143 
144     String name = "name3373707";
145 
146     Fulfillment actualResponse = client.getFulfillment(name);
147     Assert.assertEquals(expectedResponse, actualResponse);
148 
149     List<AbstractMessage> actualRequests = mockFulfillments.getRequests();
150     Assert.assertEquals(1, actualRequests.size());
151     GetFulfillmentRequest actualRequest = ((GetFulfillmentRequest) actualRequests.get(0));
152 
153     Assert.assertEquals(name, actualRequest.getName());
154     Assert.assertTrue(
155         channelProvider.isHeaderSent(
156             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
157             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
158   }
159 
160   @Test
getFulfillmentExceptionTest2()161   public void getFulfillmentExceptionTest2() throws Exception {
162     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
163     mockFulfillments.addException(exception);
164 
165     try {
166       String name = "name3373707";
167       client.getFulfillment(name);
168       Assert.fail("No exception raised");
169     } catch (InvalidArgumentException e) {
170       // Expected exception.
171     }
172   }
173 
174   @Test
updateFulfillmentTest()175   public void updateFulfillmentTest() throws Exception {
176     Fulfillment expectedResponse =
177         Fulfillment.newBuilder()
178             .setName(FulfillmentName.ofProjectName("[PROJECT]").toString())
179             .setDisplayName("displayName1714148973")
180             .setEnabled(true)
181             .addAllFeatures(new ArrayList<Fulfillment.Feature>())
182             .build();
183     mockFulfillments.addResponse(expectedResponse);
184 
185     Fulfillment fulfillment = Fulfillment.newBuilder().build();
186     FieldMask updateMask = FieldMask.newBuilder().build();
187 
188     Fulfillment actualResponse = client.updateFulfillment(fulfillment, updateMask);
189     Assert.assertEquals(expectedResponse, actualResponse);
190 
191     List<AbstractMessage> actualRequests = mockFulfillments.getRequests();
192     Assert.assertEquals(1, actualRequests.size());
193     UpdateFulfillmentRequest actualRequest = ((UpdateFulfillmentRequest) actualRequests.get(0));
194 
195     Assert.assertEquals(fulfillment, actualRequest.getFulfillment());
196     Assert.assertEquals(updateMask, actualRequest.getUpdateMask());
197     Assert.assertTrue(
198         channelProvider.isHeaderSent(
199             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
200             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
201   }
202 
203   @Test
updateFulfillmentExceptionTest()204   public void updateFulfillmentExceptionTest() throws Exception {
205     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
206     mockFulfillments.addException(exception);
207 
208     try {
209       Fulfillment fulfillment = Fulfillment.newBuilder().build();
210       FieldMask updateMask = FieldMask.newBuilder().build();
211       client.updateFulfillment(fulfillment, updateMask);
212       Assert.fail("No exception raised");
213     } catch (InvalidArgumentException e) {
214       // Expected exception.
215     }
216   }
217 
218   @Test
listLocationsTest()219   public void listLocationsTest() throws Exception {
220     Location responsesElement = Location.newBuilder().build();
221     ListLocationsResponse expectedResponse =
222         ListLocationsResponse.newBuilder()
223             .setNextPageToken("")
224             .addAllLocations(Arrays.asList(responsesElement))
225             .build();
226     mockLocations.addResponse(expectedResponse);
227 
228     ListLocationsRequest request =
229         ListLocationsRequest.newBuilder()
230             .setName("name3373707")
231             .setFilter("filter-1274492040")
232             .setPageSize(883849137)
233             .setPageToken("pageToken873572522")
234             .build();
235 
236     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
237 
238     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
239 
240     Assert.assertEquals(1, resources.size());
241     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
242 
243     List<AbstractMessage> actualRequests = mockLocations.getRequests();
244     Assert.assertEquals(1, actualRequests.size());
245     ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0));
246 
247     Assert.assertEquals(request.getName(), actualRequest.getName());
248     Assert.assertEquals(request.getFilter(), actualRequest.getFilter());
249     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
250     Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
251     Assert.assertTrue(
252         channelProvider.isHeaderSent(
253             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
254             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
255   }
256 
257   @Test
listLocationsExceptionTest()258   public void listLocationsExceptionTest() throws Exception {
259     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
260     mockLocations.addException(exception);
261 
262     try {
263       ListLocationsRequest request =
264           ListLocationsRequest.newBuilder()
265               .setName("name3373707")
266               .setFilter("filter-1274492040")
267               .setPageSize(883849137)
268               .setPageToken("pageToken873572522")
269               .build();
270       client.listLocations(request);
271       Assert.fail("No exception raised");
272     } catch (InvalidArgumentException e) {
273       // Expected exception.
274     }
275   }
276 
277   @Test
getLocationTest()278   public void getLocationTest() throws Exception {
279     Location expectedResponse =
280         Location.newBuilder()
281             .setName("name3373707")
282             .setLocationId("locationId1541836720")
283             .setDisplayName("displayName1714148973")
284             .putAllLabels(new HashMap<String, String>())
285             .setMetadata(Any.newBuilder().build())
286             .build();
287     mockLocations.addResponse(expectedResponse);
288 
289     GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
290 
291     Location actualResponse = client.getLocation(request);
292     Assert.assertEquals(expectedResponse, actualResponse);
293 
294     List<AbstractMessage> actualRequests = mockLocations.getRequests();
295     Assert.assertEquals(1, actualRequests.size());
296     GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0));
297 
298     Assert.assertEquals(request.getName(), actualRequest.getName());
299     Assert.assertTrue(
300         channelProvider.isHeaderSent(
301             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
302             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
303   }
304 
305   @Test
getLocationExceptionTest()306   public void getLocationExceptionTest() throws Exception {
307     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
308     mockLocations.addException(exception);
309 
310     try {
311       GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
312       client.getLocation(request);
313       Assert.fail("No exception raised");
314     } catch (InvalidArgumentException e) {
315       // Expected exception.
316     }
317   }
318 }
319