• 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.AnswerRecordsClient.ListAnswerRecordsPagedResponse;
20 import static com.google.cloud.dialogflow.v2.AnswerRecordsClient.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.FieldMask;
37 import io.grpc.StatusRuntimeException;
38 import java.io.IOException;
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 AnswerRecordsClientTest {
53   private static MockAnswerRecords mockAnswerRecords;
54   private static MockLocations mockLocations;
55   private static MockServiceHelper mockServiceHelper;
56   private LocalChannelProvider channelProvider;
57   private AnswerRecordsClient client;
58 
59   @BeforeClass
startStaticServer()60   public static void startStaticServer() {
61     mockAnswerRecords = new MockAnswerRecords();
62     mockLocations = new MockLocations();
63     mockServiceHelper =
64         new MockServiceHelper(
65             UUID.randomUUID().toString(),
66             Arrays.<MockGrpcService>asList(mockAnswerRecords, 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     AnswerRecordsSettings settings =
80         AnswerRecordsSettings.newBuilder()
81             .setTransportChannelProvider(channelProvider)
82             .setCredentialsProvider(NoCredentialsProvider.create())
83             .build();
84     client = AnswerRecordsClient.create(settings);
85   }
86 
87   @After
tearDown()88   public void tearDown() throws Exception {
89     client.close();
90   }
91 
92   @Test
listAnswerRecordsTest()93   public void listAnswerRecordsTest() throws Exception {
94     AnswerRecord responsesElement = AnswerRecord.newBuilder().build();
95     ListAnswerRecordsResponse expectedResponse =
96         ListAnswerRecordsResponse.newBuilder()
97             .setNextPageToken("")
98             .addAllAnswerRecords(Arrays.asList(responsesElement))
99             .build();
100     mockAnswerRecords.addResponse(expectedResponse);
101 
102     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
103 
104     ListAnswerRecordsPagedResponse pagedListResponse = client.listAnswerRecords(parent);
105 
106     List<AnswerRecord> resources = Lists.newArrayList(pagedListResponse.iterateAll());
107 
108     Assert.assertEquals(1, resources.size());
109     Assert.assertEquals(expectedResponse.getAnswerRecordsList().get(0), resources.get(0));
110 
111     List<AbstractMessage> actualRequests = mockAnswerRecords.getRequests();
112     Assert.assertEquals(1, actualRequests.size());
113     ListAnswerRecordsRequest actualRequest = ((ListAnswerRecordsRequest) actualRequests.get(0));
114 
115     Assert.assertEquals(parent.toString(), actualRequest.getParent());
116     Assert.assertTrue(
117         channelProvider.isHeaderSent(
118             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
119             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
120   }
121 
122   @Test
listAnswerRecordsExceptionTest()123   public void listAnswerRecordsExceptionTest() throws Exception {
124     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
125     mockAnswerRecords.addException(exception);
126 
127     try {
128       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
129       client.listAnswerRecords(parent);
130       Assert.fail("No exception raised");
131     } catch (InvalidArgumentException e) {
132       // Expected exception.
133     }
134   }
135 
136   @Test
listAnswerRecordsTest2()137   public void listAnswerRecordsTest2() throws Exception {
138     AnswerRecord responsesElement = AnswerRecord.newBuilder().build();
139     ListAnswerRecordsResponse expectedResponse =
140         ListAnswerRecordsResponse.newBuilder()
141             .setNextPageToken("")
142             .addAllAnswerRecords(Arrays.asList(responsesElement))
143             .build();
144     mockAnswerRecords.addResponse(expectedResponse);
145 
146     ProjectName parent = ProjectName.of("[PROJECT]");
147 
148     ListAnswerRecordsPagedResponse pagedListResponse = client.listAnswerRecords(parent);
149 
150     List<AnswerRecord> resources = Lists.newArrayList(pagedListResponse.iterateAll());
151 
152     Assert.assertEquals(1, resources.size());
153     Assert.assertEquals(expectedResponse.getAnswerRecordsList().get(0), resources.get(0));
154 
155     List<AbstractMessage> actualRequests = mockAnswerRecords.getRequests();
156     Assert.assertEquals(1, actualRequests.size());
157     ListAnswerRecordsRequest actualRequest = ((ListAnswerRecordsRequest) actualRequests.get(0));
158 
159     Assert.assertEquals(parent.toString(), actualRequest.getParent());
160     Assert.assertTrue(
161         channelProvider.isHeaderSent(
162             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
163             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
164   }
165 
166   @Test
listAnswerRecordsExceptionTest2()167   public void listAnswerRecordsExceptionTest2() throws Exception {
168     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
169     mockAnswerRecords.addException(exception);
170 
171     try {
172       ProjectName parent = ProjectName.of("[PROJECT]");
173       client.listAnswerRecords(parent);
174       Assert.fail("No exception raised");
175     } catch (InvalidArgumentException e) {
176       // Expected exception.
177     }
178   }
179 
180   @Test
listAnswerRecordsTest3()181   public void listAnswerRecordsTest3() throws Exception {
182     AnswerRecord responsesElement = AnswerRecord.newBuilder().build();
183     ListAnswerRecordsResponse expectedResponse =
184         ListAnswerRecordsResponse.newBuilder()
185             .setNextPageToken("")
186             .addAllAnswerRecords(Arrays.asList(responsesElement))
187             .build();
188     mockAnswerRecords.addResponse(expectedResponse);
189 
190     String parent = "parent-995424086";
191 
192     ListAnswerRecordsPagedResponse pagedListResponse = client.listAnswerRecords(parent);
193 
194     List<AnswerRecord> resources = Lists.newArrayList(pagedListResponse.iterateAll());
195 
196     Assert.assertEquals(1, resources.size());
197     Assert.assertEquals(expectedResponse.getAnswerRecordsList().get(0), resources.get(0));
198 
199     List<AbstractMessage> actualRequests = mockAnswerRecords.getRequests();
200     Assert.assertEquals(1, actualRequests.size());
201     ListAnswerRecordsRequest actualRequest = ((ListAnswerRecordsRequest) actualRequests.get(0));
202 
203     Assert.assertEquals(parent, actualRequest.getParent());
204     Assert.assertTrue(
205         channelProvider.isHeaderSent(
206             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
207             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
208   }
209 
210   @Test
listAnswerRecordsExceptionTest3()211   public void listAnswerRecordsExceptionTest3() throws Exception {
212     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
213     mockAnswerRecords.addException(exception);
214 
215     try {
216       String parent = "parent-995424086";
217       client.listAnswerRecords(parent);
218       Assert.fail("No exception raised");
219     } catch (InvalidArgumentException e) {
220       // Expected exception.
221     }
222   }
223 
224   @Test
updateAnswerRecordTest()225   public void updateAnswerRecordTest() throws Exception {
226     AnswerRecord expectedResponse =
227         AnswerRecord.newBuilder()
228             .setName(
229                 AnswerRecordName.ofProjectAnswerRecordName("[PROJECT]", "[ANSWER_RECORD]")
230                     .toString())
231             .setAnswerFeedback(AnswerFeedback.newBuilder().build())
232             .build();
233     mockAnswerRecords.addResponse(expectedResponse);
234 
235     AnswerRecord answerRecord = AnswerRecord.newBuilder().build();
236     FieldMask updateMask = FieldMask.newBuilder().build();
237 
238     AnswerRecord actualResponse = client.updateAnswerRecord(answerRecord, updateMask);
239     Assert.assertEquals(expectedResponse, actualResponse);
240 
241     List<AbstractMessage> actualRequests = mockAnswerRecords.getRequests();
242     Assert.assertEquals(1, actualRequests.size());
243     UpdateAnswerRecordRequest actualRequest = ((UpdateAnswerRecordRequest) actualRequests.get(0));
244 
245     Assert.assertEquals(answerRecord, actualRequest.getAnswerRecord());
246     Assert.assertEquals(updateMask, actualRequest.getUpdateMask());
247     Assert.assertTrue(
248         channelProvider.isHeaderSent(
249             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
250             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
251   }
252 
253   @Test
updateAnswerRecordExceptionTest()254   public void updateAnswerRecordExceptionTest() throws Exception {
255     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
256     mockAnswerRecords.addException(exception);
257 
258     try {
259       AnswerRecord answerRecord = AnswerRecord.newBuilder().build();
260       FieldMask updateMask = FieldMask.newBuilder().build();
261       client.updateAnswerRecord(answerRecord, updateMask);
262       Assert.fail("No exception raised");
263     } catch (InvalidArgumentException e) {
264       // Expected exception.
265     }
266   }
267 
268   @Test
listLocationsTest()269   public void listLocationsTest() throws Exception {
270     Location responsesElement = Location.newBuilder().build();
271     ListLocationsResponse expectedResponse =
272         ListLocationsResponse.newBuilder()
273             .setNextPageToken("")
274             .addAllLocations(Arrays.asList(responsesElement))
275             .build();
276     mockLocations.addResponse(expectedResponse);
277 
278     ListLocationsRequest request =
279         ListLocationsRequest.newBuilder()
280             .setName("name3373707")
281             .setFilter("filter-1274492040")
282             .setPageSize(883849137)
283             .setPageToken("pageToken873572522")
284             .build();
285 
286     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
287 
288     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
289 
290     Assert.assertEquals(1, resources.size());
291     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
292 
293     List<AbstractMessage> actualRequests = mockLocations.getRequests();
294     Assert.assertEquals(1, actualRequests.size());
295     ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0));
296 
297     Assert.assertEquals(request.getName(), actualRequest.getName());
298     Assert.assertEquals(request.getFilter(), actualRequest.getFilter());
299     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
300     Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
301     Assert.assertTrue(
302         channelProvider.isHeaderSent(
303             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
304             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
305   }
306 
307   @Test
listLocationsExceptionTest()308   public void listLocationsExceptionTest() throws Exception {
309     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
310     mockLocations.addException(exception);
311 
312     try {
313       ListLocationsRequest request =
314           ListLocationsRequest.newBuilder()
315               .setName("name3373707")
316               .setFilter("filter-1274492040")
317               .setPageSize(883849137)
318               .setPageToken("pageToken873572522")
319               .build();
320       client.listLocations(request);
321       Assert.fail("No exception raised");
322     } catch (InvalidArgumentException e) {
323       // Expected exception.
324     }
325   }
326 
327   @Test
getLocationTest()328   public void getLocationTest() throws Exception {
329     Location expectedResponse =
330         Location.newBuilder()
331             .setName("name3373707")
332             .setLocationId("locationId1541836720")
333             .setDisplayName("displayName1714148973")
334             .putAllLabels(new HashMap<String, String>())
335             .setMetadata(Any.newBuilder().build())
336             .build();
337     mockLocations.addResponse(expectedResponse);
338 
339     GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
340 
341     Location actualResponse = client.getLocation(request);
342     Assert.assertEquals(expectedResponse, actualResponse);
343 
344     List<AbstractMessage> actualRequests = mockLocations.getRequests();
345     Assert.assertEquals(1, actualRequests.size());
346     GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0));
347 
348     Assert.assertEquals(request.getName(), actualRequest.getName());
349     Assert.assertTrue(
350         channelProvider.isHeaderSent(
351             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
352             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
353   }
354 
355   @Test
getLocationExceptionTest()356   public void getLocationExceptionTest() throws Exception {
357     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
358     mockLocations.addException(exception);
359 
360     try {
361       GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
362       client.getLocation(request);
363       Assert.fail("No exception raised");
364     } catch (InvalidArgumentException e) {
365       // Expected exception.
366     }
367   }
368 }
369