• 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.httpjson.GaxHttpJsonProperties;
24 import com.google.api.gax.httpjson.testing.MockHttpService;
25 import com.google.api.gax.rpc.ApiClientHeaderProvider;
26 import com.google.api.gax.rpc.ApiException;
27 import com.google.api.gax.rpc.ApiExceptionFactory;
28 import com.google.api.gax.rpc.InvalidArgumentException;
29 import com.google.api.gax.rpc.StatusCode;
30 import com.google.api.gax.rpc.testing.FakeStatusCode;
31 import com.google.cloud.dialogflow.v2.stub.HttpJsonAnswerRecordsStub;
32 import com.google.cloud.location.GetLocationRequest;
33 import com.google.cloud.location.ListLocationsRequest;
34 import com.google.cloud.location.ListLocationsResponse;
35 import com.google.cloud.location.Location;
36 import com.google.common.collect.Lists;
37 import com.google.protobuf.Any;
38 import com.google.protobuf.FieldMask;
39 import java.io.IOException;
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 AnswerRecordsClientHttpJsonTest {
53   private static MockHttpService mockService;
54   private static AnswerRecordsClient client;
55 
56   @BeforeClass
startStaticServer()57   public static void startStaticServer() throws IOException {
58     mockService =
59         new MockHttpService(
60             HttpJsonAnswerRecordsStub.getMethodDescriptors(),
61             AnswerRecordsSettings.getDefaultEndpoint());
62     AnswerRecordsSettings settings =
63         AnswerRecordsSettings.newHttpJsonBuilder()
64             .setTransportChannelProvider(
65                 AnswerRecordsSettings.defaultHttpJsonTransportProviderBuilder()
66                     .setHttpTransport(mockService)
67                     .build())
68             .setCredentialsProvider(NoCredentialsProvider.create())
69             .build();
70     client = AnswerRecordsClient.create(settings);
71   }
72 
73   @AfterClass
stopServer()74   public static void stopServer() {
75     client.close();
76   }
77 
78   @Before
setUp()79   public void setUp() {}
80 
81   @After
tearDown()82   public void tearDown() throws Exception {
83     mockService.reset();
84   }
85 
86   @Test
listAnswerRecordsTest()87   public void listAnswerRecordsTest() throws Exception {
88     AnswerRecord responsesElement = AnswerRecord.newBuilder().build();
89     ListAnswerRecordsResponse expectedResponse =
90         ListAnswerRecordsResponse.newBuilder()
91             .setNextPageToken("")
92             .addAllAnswerRecords(Arrays.asList(responsesElement))
93             .build();
94     mockService.addResponse(expectedResponse);
95 
96     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
97 
98     ListAnswerRecordsPagedResponse pagedListResponse = client.listAnswerRecords(parent);
99 
100     List<AnswerRecord> resources = Lists.newArrayList(pagedListResponse.iterateAll());
101 
102     Assert.assertEquals(1, resources.size());
103     Assert.assertEquals(expectedResponse.getAnswerRecordsList().get(0), resources.get(0));
104 
105     List<String> actualRequests = mockService.getRequestPaths();
106     Assert.assertEquals(1, actualRequests.size());
107 
108     String apiClientHeaderKey =
109         mockService
110             .getRequestHeaders()
111             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
112             .iterator()
113             .next();
114     Assert.assertTrue(
115         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
116             .matcher(apiClientHeaderKey)
117             .matches());
118   }
119 
120   @Test
listAnswerRecordsExceptionTest()121   public void listAnswerRecordsExceptionTest() throws Exception {
122     ApiException exception =
123         ApiExceptionFactory.createException(
124             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
125     mockService.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     mockService.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<String> actualRequests = mockService.getRequestPaths();
156     Assert.assertEquals(1, actualRequests.size());
157 
158     String apiClientHeaderKey =
159         mockService
160             .getRequestHeaders()
161             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
162             .iterator()
163             .next();
164     Assert.assertTrue(
165         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
166             .matcher(apiClientHeaderKey)
167             .matches());
168   }
169 
170   @Test
listAnswerRecordsExceptionTest2()171   public void listAnswerRecordsExceptionTest2() throws Exception {
172     ApiException exception =
173         ApiExceptionFactory.createException(
174             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
175     mockService.addException(exception);
176 
177     try {
178       ProjectName parent = ProjectName.of("[PROJECT]");
179       client.listAnswerRecords(parent);
180       Assert.fail("No exception raised");
181     } catch (InvalidArgumentException e) {
182       // Expected exception.
183     }
184   }
185 
186   @Test
listAnswerRecordsTest3()187   public void listAnswerRecordsTest3() throws Exception {
188     AnswerRecord responsesElement = AnswerRecord.newBuilder().build();
189     ListAnswerRecordsResponse expectedResponse =
190         ListAnswerRecordsResponse.newBuilder()
191             .setNextPageToken("")
192             .addAllAnswerRecords(Arrays.asList(responsesElement))
193             .build();
194     mockService.addResponse(expectedResponse);
195 
196     String parent = "projects/project-2353";
197 
198     ListAnswerRecordsPagedResponse pagedListResponse = client.listAnswerRecords(parent);
199 
200     List<AnswerRecord> resources = Lists.newArrayList(pagedListResponse.iterateAll());
201 
202     Assert.assertEquals(1, resources.size());
203     Assert.assertEquals(expectedResponse.getAnswerRecordsList().get(0), resources.get(0));
204 
205     List<String> actualRequests = mockService.getRequestPaths();
206     Assert.assertEquals(1, actualRequests.size());
207 
208     String apiClientHeaderKey =
209         mockService
210             .getRequestHeaders()
211             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
212             .iterator()
213             .next();
214     Assert.assertTrue(
215         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
216             .matcher(apiClientHeaderKey)
217             .matches());
218   }
219 
220   @Test
listAnswerRecordsExceptionTest3()221   public void listAnswerRecordsExceptionTest3() throws Exception {
222     ApiException exception =
223         ApiExceptionFactory.createException(
224             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
225     mockService.addException(exception);
226 
227     try {
228       String parent = "projects/project-2353";
229       client.listAnswerRecords(parent);
230       Assert.fail("No exception raised");
231     } catch (InvalidArgumentException e) {
232       // Expected exception.
233     }
234   }
235 
236   @Test
updateAnswerRecordTest()237   public void updateAnswerRecordTest() throws Exception {
238     AnswerRecord expectedResponse =
239         AnswerRecord.newBuilder()
240             .setName(
241                 AnswerRecordName.ofProjectAnswerRecordName("[PROJECT]", "[ANSWER_RECORD]")
242                     .toString())
243             .setAnswerFeedback(AnswerFeedback.newBuilder().build())
244             .build();
245     mockService.addResponse(expectedResponse);
246 
247     AnswerRecord answerRecord =
248         AnswerRecord.newBuilder()
249             .setName(
250                 AnswerRecordName.ofProjectAnswerRecordName("[PROJECT]", "[ANSWER_RECORD]")
251                     .toString())
252             .setAnswerFeedback(AnswerFeedback.newBuilder().build())
253             .build();
254     FieldMask updateMask = FieldMask.newBuilder().build();
255 
256     AnswerRecord actualResponse = client.updateAnswerRecord(answerRecord, updateMask);
257     Assert.assertEquals(expectedResponse, actualResponse);
258 
259     List<String> actualRequests = mockService.getRequestPaths();
260     Assert.assertEquals(1, actualRequests.size());
261 
262     String apiClientHeaderKey =
263         mockService
264             .getRequestHeaders()
265             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
266             .iterator()
267             .next();
268     Assert.assertTrue(
269         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
270             .matcher(apiClientHeaderKey)
271             .matches());
272   }
273 
274   @Test
updateAnswerRecordExceptionTest()275   public void updateAnswerRecordExceptionTest() throws Exception {
276     ApiException exception =
277         ApiExceptionFactory.createException(
278             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
279     mockService.addException(exception);
280 
281     try {
282       AnswerRecord answerRecord =
283           AnswerRecord.newBuilder()
284               .setName(
285                   AnswerRecordName.ofProjectAnswerRecordName("[PROJECT]", "[ANSWER_RECORD]")
286                       .toString())
287               .setAnswerFeedback(AnswerFeedback.newBuilder().build())
288               .build();
289       FieldMask updateMask = FieldMask.newBuilder().build();
290       client.updateAnswerRecord(answerRecord, updateMask);
291       Assert.fail("No exception raised");
292     } catch (InvalidArgumentException e) {
293       // Expected exception.
294     }
295   }
296 
297   @Test
listLocationsTest()298   public void listLocationsTest() throws Exception {
299     Location responsesElement = Location.newBuilder().build();
300     ListLocationsResponse expectedResponse =
301         ListLocationsResponse.newBuilder()
302             .setNextPageToken("")
303             .addAllLocations(Arrays.asList(responsesElement))
304             .build();
305     mockService.addResponse(expectedResponse);
306 
307     ListLocationsRequest request =
308         ListLocationsRequest.newBuilder()
309             .setName("projects/project-3664")
310             .setFilter("filter-1274492040")
311             .setPageSize(883849137)
312             .setPageToken("pageToken873572522")
313             .build();
314 
315     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
316 
317     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
318 
319     Assert.assertEquals(1, resources.size());
320     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
321 
322     List<String> actualRequests = mockService.getRequestPaths();
323     Assert.assertEquals(1, actualRequests.size());
324 
325     String apiClientHeaderKey =
326         mockService
327             .getRequestHeaders()
328             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
329             .iterator()
330             .next();
331     Assert.assertTrue(
332         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
333             .matcher(apiClientHeaderKey)
334             .matches());
335   }
336 
337   @Test
listLocationsExceptionTest()338   public void listLocationsExceptionTest() throws Exception {
339     ApiException exception =
340         ApiExceptionFactory.createException(
341             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
342     mockService.addException(exception);
343 
344     try {
345       ListLocationsRequest request =
346           ListLocationsRequest.newBuilder()
347               .setName("projects/project-3664")
348               .setFilter("filter-1274492040")
349               .setPageSize(883849137)
350               .setPageToken("pageToken873572522")
351               .build();
352       client.listLocations(request);
353       Assert.fail("No exception raised");
354     } catch (InvalidArgumentException e) {
355       // Expected exception.
356     }
357   }
358 
359   @Test
getLocationTest()360   public void getLocationTest() throws Exception {
361     Location expectedResponse =
362         Location.newBuilder()
363             .setName("name3373707")
364             .setLocationId("locationId1541836720")
365             .setDisplayName("displayName1714148973")
366             .putAllLabels(new HashMap<String, String>())
367             .setMetadata(Any.newBuilder().build())
368             .build();
369     mockService.addResponse(expectedResponse);
370 
371     GetLocationRequest request =
372         GetLocationRequest.newBuilder()
373             .setName("projects/project-9062/locations/location-9062")
374             .build();
375 
376     Location actualResponse = client.getLocation(request);
377     Assert.assertEquals(expectedResponse, actualResponse);
378 
379     List<String> actualRequests = mockService.getRequestPaths();
380     Assert.assertEquals(1, actualRequests.size());
381 
382     String apiClientHeaderKey =
383         mockService
384             .getRequestHeaders()
385             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
386             .iterator()
387             .next();
388     Assert.assertTrue(
389         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
390             .matcher(apiClientHeaderKey)
391             .matches());
392   }
393 
394   @Test
getLocationExceptionTest()395   public void getLocationExceptionTest() throws Exception {
396     ApiException exception =
397         ApiExceptionFactory.createException(
398             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
399     mockService.addException(exception);
400 
401     try {
402       GetLocationRequest request =
403           GetLocationRequest.newBuilder()
404               .setName("projects/project-9062/locations/location-9062")
405               .build();
406       client.getLocation(request);
407       Assert.fail("No exception raised");
408     } catch (InvalidArgumentException e) {
409       // Expected exception.
410     }
411   }
412 }
413