• 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.EnvironmentsClient.GetEnvironmentHistoryPagedResponse;
20 import static com.google.cloud.dialogflow.v2.EnvironmentsClient.ListEnvironmentsPagedResponse;
21 import static com.google.cloud.dialogflow.v2.EnvironmentsClient.ListLocationsPagedResponse;
22 
23 import com.google.api.gax.core.NoCredentialsProvider;
24 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
25 import com.google.api.gax.httpjson.testing.MockHttpService;
26 import com.google.api.gax.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.ApiException;
28 import com.google.api.gax.rpc.ApiExceptionFactory;
29 import com.google.api.gax.rpc.InvalidArgumentException;
30 import com.google.api.gax.rpc.StatusCode;
31 import com.google.api.gax.rpc.testing.FakeStatusCode;
32 import com.google.cloud.dialogflow.v2.stub.HttpJsonEnvironmentsStub;
33 import com.google.cloud.location.GetLocationRequest;
34 import com.google.cloud.location.ListLocationsRequest;
35 import com.google.cloud.location.ListLocationsResponse;
36 import com.google.cloud.location.Location;
37 import com.google.common.collect.Lists;
38 import com.google.protobuf.Any;
39 import com.google.protobuf.Empty;
40 import com.google.protobuf.FieldMask;
41 import com.google.protobuf.Timestamp;
42 import java.io.IOException;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.List;
46 import javax.annotation.Generated;
47 import org.junit.After;
48 import org.junit.AfterClass;
49 import org.junit.Assert;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 
54 @Generated("by gapic-generator-java")
55 public class EnvironmentsClientHttpJsonTest {
56   private static MockHttpService mockService;
57   private static EnvironmentsClient client;
58 
59   @BeforeClass
startStaticServer()60   public static void startStaticServer() throws IOException {
61     mockService =
62         new MockHttpService(
63             HttpJsonEnvironmentsStub.getMethodDescriptors(),
64             EnvironmentsSettings.getDefaultEndpoint());
65     EnvironmentsSettings settings =
66         EnvironmentsSettings.newHttpJsonBuilder()
67             .setTransportChannelProvider(
68                 EnvironmentsSettings.defaultHttpJsonTransportProviderBuilder()
69                     .setHttpTransport(mockService)
70                     .build())
71             .setCredentialsProvider(NoCredentialsProvider.create())
72             .build();
73     client = EnvironmentsClient.create(settings);
74   }
75 
76   @AfterClass
stopServer()77   public static void stopServer() {
78     client.close();
79   }
80 
81   @Before
setUp()82   public void setUp() {}
83 
84   @After
tearDown()85   public void tearDown() throws Exception {
86     mockService.reset();
87   }
88 
89   @Test
listEnvironmentsTest()90   public void listEnvironmentsTest() throws Exception {
91     Environment responsesElement = Environment.newBuilder().build();
92     ListEnvironmentsResponse expectedResponse =
93         ListEnvironmentsResponse.newBuilder()
94             .setNextPageToken("")
95             .addAllEnvironments(Arrays.asList(responsesElement))
96             .build();
97     mockService.addResponse(expectedResponse);
98 
99     AgentName parent = AgentName.ofProjectName("[PROJECT]");
100 
101     ListEnvironmentsPagedResponse pagedListResponse = client.listEnvironments(parent);
102 
103     List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll());
104 
105     Assert.assertEquals(1, resources.size());
106     Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0));
107 
108     List<String> actualRequests = mockService.getRequestPaths();
109     Assert.assertEquals(1, actualRequests.size());
110 
111     String apiClientHeaderKey =
112         mockService
113             .getRequestHeaders()
114             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
115             .iterator()
116             .next();
117     Assert.assertTrue(
118         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
119             .matcher(apiClientHeaderKey)
120             .matches());
121   }
122 
123   @Test
listEnvironmentsExceptionTest()124   public void listEnvironmentsExceptionTest() throws Exception {
125     ApiException exception =
126         ApiExceptionFactory.createException(
127             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
128     mockService.addException(exception);
129 
130     try {
131       AgentName parent = AgentName.ofProjectName("[PROJECT]");
132       client.listEnvironments(parent);
133       Assert.fail("No exception raised");
134     } catch (InvalidArgumentException e) {
135       // Expected exception.
136     }
137   }
138 
139   @Test
listEnvironmentsTest2()140   public void listEnvironmentsTest2() throws Exception {
141     Environment responsesElement = Environment.newBuilder().build();
142     ListEnvironmentsResponse expectedResponse =
143         ListEnvironmentsResponse.newBuilder()
144             .setNextPageToken("")
145             .addAllEnvironments(Arrays.asList(responsesElement))
146             .build();
147     mockService.addResponse(expectedResponse);
148 
149     String parent = "projects/project-2469/agent";
150 
151     ListEnvironmentsPagedResponse pagedListResponse = client.listEnvironments(parent);
152 
153     List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll());
154 
155     Assert.assertEquals(1, resources.size());
156     Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0));
157 
158     List<String> actualRequests = mockService.getRequestPaths();
159     Assert.assertEquals(1, actualRequests.size());
160 
161     String apiClientHeaderKey =
162         mockService
163             .getRequestHeaders()
164             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
165             .iterator()
166             .next();
167     Assert.assertTrue(
168         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
169             .matcher(apiClientHeaderKey)
170             .matches());
171   }
172 
173   @Test
listEnvironmentsExceptionTest2()174   public void listEnvironmentsExceptionTest2() throws Exception {
175     ApiException exception =
176         ApiExceptionFactory.createException(
177             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
178     mockService.addException(exception);
179 
180     try {
181       String parent = "projects/project-2469/agent";
182       client.listEnvironments(parent);
183       Assert.fail("No exception raised");
184     } catch (InvalidArgumentException e) {
185       // Expected exception.
186     }
187   }
188 
189   @Test
getEnvironmentTest()190   public void getEnvironmentTest() throws Exception {
191     Environment expectedResponse =
192         Environment.newBuilder()
193             .setName(
194                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
195             .setDescription("description-1724546052")
196             .setAgentVersion(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString())
197             .setUpdateTime(Timestamp.newBuilder().build())
198             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
199             .setFulfillment(Fulfillment.newBuilder().build())
200             .build();
201     mockService.addResponse(expectedResponse);
202 
203     GetEnvironmentRequest request =
204         GetEnvironmentRequest.newBuilder()
205             .setName(
206                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
207             .build();
208 
209     Environment actualResponse = client.getEnvironment(request);
210     Assert.assertEquals(expectedResponse, actualResponse);
211 
212     List<String> actualRequests = mockService.getRequestPaths();
213     Assert.assertEquals(1, actualRequests.size());
214 
215     String apiClientHeaderKey =
216         mockService
217             .getRequestHeaders()
218             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
219             .iterator()
220             .next();
221     Assert.assertTrue(
222         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
223             .matcher(apiClientHeaderKey)
224             .matches());
225   }
226 
227   @Test
getEnvironmentExceptionTest()228   public void getEnvironmentExceptionTest() throws Exception {
229     ApiException exception =
230         ApiExceptionFactory.createException(
231             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
232     mockService.addException(exception);
233 
234     try {
235       GetEnvironmentRequest request =
236           GetEnvironmentRequest.newBuilder()
237               .setName(
238                   EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
239               .build();
240       client.getEnvironment(request);
241       Assert.fail("No exception raised");
242     } catch (InvalidArgumentException e) {
243       // Expected exception.
244     }
245   }
246 
247   @Test
createEnvironmentTest()248   public void createEnvironmentTest() throws Exception {
249     Environment expectedResponse =
250         Environment.newBuilder()
251             .setName(
252                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
253             .setDescription("description-1724546052")
254             .setAgentVersion(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString())
255             .setUpdateTime(Timestamp.newBuilder().build())
256             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
257             .setFulfillment(Fulfillment.newBuilder().build())
258             .build();
259     mockService.addResponse(expectedResponse);
260 
261     CreateEnvironmentRequest request =
262         CreateEnvironmentRequest.newBuilder()
263             .setParent(AgentName.ofProjectName("[PROJECT]").toString())
264             .setEnvironment(Environment.newBuilder().build())
265             .setEnvironmentId("environmentId-950205810")
266             .build();
267 
268     Environment actualResponse = client.createEnvironment(request);
269     Assert.assertEquals(expectedResponse, actualResponse);
270 
271     List<String> actualRequests = mockService.getRequestPaths();
272     Assert.assertEquals(1, actualRequests.size());
273 
274     String apiClientHeaderKey =
275         mockService
276             .getRequestHeaders()
277             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
278             .iterator()
279             .next();
280     Assert.assertTrue(
281         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
282             .matcher(apiClientHeaderKey)
283             .matches());
284   }
285 
286   @Test
createEnvironmentExceptionTest()287   public void createEnvironmentExceptionTest() throws Exception {
288     ApiException exception =
289         ApiExceptionFactory.createException(
290             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
291     mockService.addException(exception);
292 
293     try {
294       CreateEnvironmentRequest request =
295           CreateEnvironmentRequest.newBuilder()
296               .setParent(AgentName.ofProjectName("[PROJECT]").toString())
297               .setEnvironment(Environment.newBuilder().build())
298               .setEnvironmentId("environmentId-950205810")
299               .build();
300       client.createEnvironment(request);
301       Assert.fail("No exception raised");
302     } catch (InvalidArgumentException e) {
303       // Expected exception.
304     }
305   }
306 
307   @Test
updateEnvironmentTest()308   public void updateEnvironmentTest() throws Exception {
309     Environment expectedResponse =
310         Environment.newBuilder()
311             .setName(
312                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
313             .setDescription("description-1724546052")
314             .setAgentVersion(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString())
315             .setUpdateTime(Timestamp.newBuilder().build())
316             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
317             .setFulfillment(Fulfillment.newBuilder().build())
318             .build();
319     mockService.addResponse(expectedResponse);
320 
321     UpdateEnvironmentRequest request =
322         UpdateEnvironmentRequest.newBuilder()
323             .setEnvironment(
324                 Environment.newBuilder()
325                     .setName(
326                         EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]")
327                             .toString())
328                     .setDescription("description-1724546052")
329                     .setAgentVersion(
330                         VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString())
331                     .setUpdateTime(Timestamp.newBuilder().build())
332                     .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
333                     .setFulfillment(Fulfillment.newBuilder().build())
334                     .build())
335             .setUpdateMask(FieldMask.newBuilder().build())
336             .setAllowLoadToDraftAndDiscardChanges(true)
337             .build();
338 
339     Environment actualResponse = client.updateEnvironment(request);
340     Assert.assertEquals(expectedResponse, actualResponse);
341 
342     List<String> actualRequests = mockService.getRequestPaths();
343     Assert.assertEquals(1, actualRequests.size());
344 
345     String apiClientHeaderKey =
346         mockService
347             .getRequestHeaders()
348             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
349             .iterator()
350             .next();
351     Assert.assertTrue(
352         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
353             .matcher(apiClientHeaderKey)
354             .matches());
355   }
356 
357   @Test
updateEnvironmentExceptionTest()358   public void updateEnvironmentExceptionTest() throws Exception {
359     ApiException exception =
360         ApiExceptionFactory.createException(
361             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
362     mockService.addException(exception);
363 
364     try {
365       UpdateEnvironmentRequest request =
366           UpdateEnvironmentRequest.newBuilder()
367               .setEnvironment(
368                   Environment.newBuilder()
369                       .setName(
370                           EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]")
371                               .toString())
372                       .setDescription("description-1724546052")
373                       .setAgentVersion(
374                           VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString())
375                       .setUpdateTime(Timestamp.newBuilder().build())
376                       .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
377                       .setFulfillment(Fulfillment.newBuilder().build())
378                       .build())
379               .setUpdateMask(FieldMask.newBuilder().build())
380               .setAllowLoadToDraftAndDiscardChanges(true)
381               .build();
382       client.updateEnvironment(request);
383       Assert.fail("No exception raised");
384     } catch (InvalidArgumentException e) {
385       // Expected exception.
386     }
387   }
388 
389   @Test
deleteEnvironmentTest()390   public void deleteEnvironmentTest() throws Exception {
391     Empty expectedResponse = Empty.newBuilder().build();
392     mockService.addResponse(expectedResponse);
393 
394     DeleteEnvironmentRequest request =
395         DeleteEnvironmentRequest.newBuilder()
396             .setName(
397                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
398             .build();
399 
400     client.deleteEnvironment(request);
401 
402     List<String> actualRequests = mockService.getRequestPaths();
403     Assert.assertEquals(1, actualRequests.size());
404 
405     String apiClientHeaderKey =
406         mockService
407             .getRequestHeaders()
408             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
409             .iterator()
410             .next();
411     Assert.assertTrue(
412         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
413             .matcher(apiClientHeaderKey)
414             .matches());
415   }
416 
417   @Test
deleteEnvironmentExceptionTest()418   public void deleteEnvironmentExceptionTest() throws Exception {
419     ApiException exception =
420         ApiExceptionFactory.createException(
421             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
422     mockService.addException(exception);
423 
424     try {
425       DeleteEnvironmentRequest request =
426           DeleteEnvironmentRequest.newBuilder()
427               .setName(
428                   EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
429               .build();
430       client.deleteEnvironment(request);
431       Assert.fail("No exception raised");
432     } catch (InvalidArgumentException e) {
433       // Expected exception.
434     }
435   }
436 
437   @Test
getEnvironmentHistoryTest()438   public void getEnvironmentHistoryTest() throws Exception {
439     EnvironmentHistory.Entry responsesElement = EnvironmentHistory.Entry.newBuilder().build();
440     EnvironmentHistory expectedResponse =
441         EnvironmentHistory.newBuilder()
442             .setNextPageToken("")
443             .addAllEntries(Arrays.asList(responsesElement))
444             .build();
445     mockService.addResponse(expectedResponse);
446 
447     GetEnvironmentHistoryRequest request =
448         GetEnvironmentHistoryRequest.newBuilder()
449             .setParent(
450                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
451             .setPageSize(883849137)
452             .setPageToken("pageToken873572522")
453             .build();
454 
455     GetEnvironmentHistoryPagedResponse pagedListResponse = client.getEnvironmentHistory(request);
456 
457     List<EnvironmentHistory.Entry> resources = Lists.newArrayList(pagedListResponse.iterateAll());
458 
459     Assert.assertEquals(1, resources.size());
460     Assert.assertEquals(expectedResponse.getEntriesList().get(0), resources.get(0));
461 
462     List<String> actualRequests = mockService.getRequestPaths();
463     Assert.assertEquals(1, actualRequests.size());
464 
465     String apiClientHeaderKey =
466         mockService
467             .getRequestHeaders()
468             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
469             .iterator()
470             .next();
471     Assert.assertTrue(
472         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
473             .matcher(apiClientHeaderKey)
474             .matches());
475   }
476 
477   @Test
getEnvironmentHistoryExceptionTest()478   public void getEnvironmentHistoryExceptionTest() throws Exception {
479     ApiException exception =
480         ApiExceptionFactory.createException(
481             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
482     mockService.addException(exception);
483 
484     try {
485       GetEnvironmentHistoryRequest request =
486           GetEnvironmentHistoryRequest.newBuilder()
487               .setParent(
488                   EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
489               .setPageSize(883849137)
490               .setPageToken("pageToken873572522")
491               .build();
492       client.getEnvironmentHistory(request);
493       Assert.fail("No exception raised");
494     } catch (InvalidArgumentException e) {
495       // Expected exception.
496     }
497   }
498 
499   @Test
listLocationsTest()500   public void listLocationsTest() throws Exception {
501     Location responsesElement = Location.newBuilder().build();
502     ListLocationsResponse expectedResponse =
503         ListLocationsResponse.newBuilder()
504             .setNextPageToken("")
505             .addAllLocations(Arrays.asList(responsesElement))
506             .build();
507     mockService.addResponse(expectedResponse);
508 
509     ListLocationsRequest request =
510         ListLocationsRequest.newBuilder()
511             .setName("projects/project-3664")
512             .setFilter("filter-1274492040")
513             .setPageSize(883849137)
514             .setPageToken("pageToken873572522")
515             .build();
516 
517     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
518 
519     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
520 
521     Assert.assertEquals(1, resources.size());
522     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
523 
524     List<String> actualRequests = mockService.getRequestPaths();
525     Assert.assertEquals(1, actualRequests.size());
526 
527     String apiClientHeaderKey =
528         mockService
529             .getRequestHeaders()
530             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
531             .iterator()
532             .next();
533     Assert.assertTrue(
534         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
535             .matcher(apiClientHeaderKey)
536             .matches());
537   }
538 
539   @Test
listLocationsExceptionTest()540   public void listLocationsExceptionTest() throws Exception {
541     ApiException exception =
542         ApiExceptionFactory.createException(
543             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
544     mockService.addException(exception);
545 
546     try {
547       ListLocationsRequest request =
548           ListLocationsRequest.newBuilder()
549               .setName("projects/project-3664")
550               .setFilter("filter-1274492040")
551               .setPageSize(883849137)
552               .setPageToken("pageToken873572522")
553               .build();
554       client.listLocations(request);
555       Assert.fail("No exception raised");
556     } catch (InvalidArgumentException e) {
557       // Expected exception.
558     }
559   }
560 
561   @Test
getLocationTest()562   public void getLocationTest() throws Exception {
563     Location expectedResponse =
564         Location.newBuilder()
565             .setName("name3373707")
566             .setLocationId("locationId1541836720")
567             .setDisplayName("displayName1714148973")
568             .putAllLabels(new HashMap<String, String>())
569             .setMetadata(Any.newBuilder().build())
570             .build();
571     mockService.addResponse(expectedResponse);
572 
573     GetLocationRequest request =
574         GetLocationRequest.newBuilder()
575             .setName("projects/project-9062/locations/location-9062")
576             .build();
577 
578     Location actualResponse = client.getLocation(request);
579     Assert.assertEquals(expectedResponse, actualResponse);
580 
581     List<String> actualRequests = mockService.getRequestPaths();
582     Assert.assertEquals(1, actualRequests.size());
583 
584     String apiClientHeaderKey =
585         mockService
586             .getRequestHeaders()
587             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
588             .iterator()
589             .next();
590     Assert.assertTrue(
591         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
592             .matcher(apiClientHeaderKey)
593             .matches());
594   }
595 
596   @Test
getLocationExceptionTest()597   public void getLocationExceptionTest() throws Exception {
598     ApiException exception =
599         ApiExceptionFactory.createException(
600             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
601     mockService.addException(exception);
602 
603     try {
604       GetLocationRequest request =
605           GetLocationRequest.newBuilder()
606               .setName("projects/project-9062/locations/location-9062")
607               .build();
608       client.getLocation(request);
609       Assert.fail("No exception raised");
610     } catch (InvalidArgumentException e) {
611       // Expected exception.
612     }
613   }
614 }
615