• 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.grpc.GaxGrpcProperties;
25 import com.google.api.gax.grpc.testing.LocalChannelProvider;
26 import com.google.api.gax.grpc.testing.MockGrpcService;
27 import com.google.api.gax.grpc.testing.MockServiceHelper;
28 import com.google.api.gax.rpc.ApiClientHeaderProvider;
29 import com.google.api.gax.rpc.InvalidArgumentException;
30 import com.google.cloud.location.GetLocationRequest;
31 import com.google.cloud.location.ListLocationsRequest;
32 import com.google.cloud.location.ListLocationsResponse;
33 import com.google.cloud.location.Location;
34 import com.google.common.collect.Lists;
35 import com.google.protobuf.AbstractMessage;
36 import com.google.protobuf.Any;
37 import com.google.protobuf.Empty;
38 import com.google.protobuf.FieldMask;
39 import com.google.protobuf.Timestamp;
40 import io.grpc.StatusRuntimeException;
41 import java.io.IOException;
42 import java.util.Arrays;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.UUID;
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 EnvironmentsClientTest {
56   private static MockEnvironments mockEnvironments;
57   private static MockLocations mockLocations;
58   private static MockServiceHelper mockServiceHelper;
59   private LocalChannelProvider channelProvider;
60   private EnvironmentsClient client;
61 
62   @BeforeClass
startStaticServer()63   public static void startStaticServer() {
64     mockEnvironments = new MockEnvironments();
65     mockLocations = new MockLocations();
66     mockServiceHelper =
67         new MockServiceHelper(
68             UUID.randomUUID().toString(),
69             Arrays.<MockGrpcService>asList(mockEnvironments, mockLocations));
70     mockServiceHelper.start();
71   }
72 
73   @AfterClass
stopServer()74   public static void stopServer() {
75     mockServiceHelper.stop();
76   }
77 
78   @Before
setUp()79   public void setUp() throws IOException {
80     mockServiceHelper.reset();
81     channelProvider = mockServiceHelper.createChannelProvider();
82     EnvironmentsSettings settings =
83         EnvironmentsSettings.newBuilder()
84             .setTransportChannelProvider(channelProvider)
85             .setCredentialsProvider(NoCredentialsProvider.create())
86             .build();
87     client = EnvironmentsClient.create(settings);
88   }
89 
90   @After
tearDown()91   public void tearDown() throws Exception {
92     client.close();
93   }
94 
95   @Test
listEnvironmentsTest()96   public void listEnvironmentsTest() throws Exception {
97     Environment responsesElement = Environment.newBuilder().build();
98     ListEnvironmentsResponse expectedResponse =
99         ListEnvironmentsResponse.newBuilder()
100             .setNextPageToken("")
101             .addAllEnvironments(Arrays.asList(responsesElement))
102             .build();
103     mockEnvironments.addResponse(expectedResponse);
104 
105     AgentName parent = AgentName.ofProjectName("[PROJECT]");
106 
107     ListEnvironmentsPagedResponse pagedListResponse = client.listEnvironments(parent);
108 
109     List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll());
110 
111     Assert.assertEquals(1, resources.size());
112     Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0));
113 
114     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
115     Assert.assertEquals(1, actualRequests.size());
116     ListEnvironmentsRequest actualRequest = ((ListEnvironmentsRequest) actualRequests.get(0));
117 
118     Assert.assertEquals(parent.toString(), actualRequest.getParent());
119     Assert.assertTrue(
120         channelProvider.isHeaderSent(
121             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
122             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
123   }
124 
125   @Test
listEnvironmentsExceptionTest()126   public void listEnvironmentsExceptionTest() throws Exception {
127     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
128     mockEnvironments.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     mockEnvironments.addResponse(expectedResponse);
148 
149     String parent = "parent-995424086";
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<AbstractMessage> actualRequests = mockEnvironments.getRequests();
159     Assert.assertEquals(1, actualRequests.size());
160     ListEnvironmentsRequest actualRequest = ((ListEnvironmentsRequest) actualRequests.get(0));
161 
162     Assert.assertEquals(parent, actualRequest.getParent());
163     Assert.assertTrue(
164         channelProvider.isHeaderSent(
165             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
166             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
167   }
168 
169   @Test
listEnvironmentsExceptionTest2()170   public void listEnvironmentsExceptionTest2() throws Exception {
171     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
172     mockEnvironments.addException(exception);
173 
174     try {
175       String parent = "parent-995424086";
176       client.listEnvironments(parent);
177       Assert.fail("No exception raised");
178     } catch (InvalidArgumentException e) {
179       // Expected exception.
180     }
181   }
182 
183   @Test
getEnvironmentTest()184   public void getEnvironmentTest() throws Exception {
185     Environment expectedResponse =
186         Environment.newBuilder()
187             .setName(
188                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
189             .setDescription("description-1724546052")
190             .setAgentVersion(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString())
191             .setUpdateTime(Timestamp.newBuilder().build())
192             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
193             .setFulfillment(Fulfillment.newBuilder().build())
194             .build();
195     mockEnvironments.addResponse(expectedResponse);
196 
197     GetEnvironmentRequest request =
198         GetEnvironmentRequest.newBuilder()
199             .setName(
200                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
201             .build();
202 
203     Environment actualResponse = client.getEnvironment(request);
204     Assert.assertEquals(expectedResponse, actualResponse);
205 
206     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
207     Assert.assertEquals(1, actualRequests.size());
208     GetEnvironmentRequest actualRequest = ((GetEnvironmentRequest) actualRequests.get(0));
209 
210     Assert.assertEquals(request.getName(), actualRequest.getName());
211     Assert.assertTrue(
212         channelProvider.isHeaderSent(
213             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
214             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
215   }
216 
217   @Test
getEnvironmentExceptionTest()218   public void getEnvironmentExceptionTest() throws Exception {
219     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
220     mockEnvironments.addException(exception);
221 
222     try {
223       GetEnvironmentRequest request =
224           GetEnvironmentRequest.newBuilder()
225               .setName(
226                   EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
227               .build();
228       client.getEnvironment(request);
229       Assert.fail("No exception raised");
230     } catch (InvalidArgumentException e) {
231       // Expected exception.
232     }
233   }
234 
235   @Test
createEnvironmentTest()236   public void createEnvironmentTest() throws Exception {
237     Environment expectedResponse =
238         Environment.newBuilder()
239             .setName(
240                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
241             .setDescription("description-1724546052")
242             .setAgentVersion(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString())
243             .setUpdateTime(Timestamp.newBuilder().build())
244             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
245             .setFulfillment(Fulfillment.newBuilder().build())
246             .build();
247     mockEnvironments.addResponse(expectedResponse);
248 
249     CreateEnvironmentRequest request =
250         CreateEnvironmentRequest.newBuilder()
251             .setParent(AgentName.ofProjectName("[PROJECT]").toString())
252             .setEnvironment(Environment.newBuilder().build())
253             .setEnvironmentId("environmentId-950205810")
254             .build();
255 
256     Environment actualResponse = client.createEnvironment(request);
257     Assert.assertEquals(expectedResponse, actualResponse);
258 
259     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
260     Assert.assertEquals(1, actualRequests.size());
261     CreateEnvironmentRequest actualRequest = ((CreateEnvironmentRequest) actualRequests.get(0));
262 
263     Assert.assertEquals(request.getParent(), actualRequest.getParent());
264     Assert.assertEquals(request.getEnvironment(), actualRequest.getEnvironment());
265     Assert.assertEquals(request.getEnvironmentId(), actualRequest.getEnvironmentId());
266     Assert.assertTrue(
267         channelProvider.isHeaderSent(
268             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
269             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
270   }
271 
272   @Test
createEnvironmentExceptionTest()273   public void createEnvironmentExceptionTest() throws Exception {
274     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
275     mockEnvironments.addException(exception);
276 
277     try {
278       CreateEnvironmentRequest request =
279           CreateEnvironmentRequest.newBuilder()
280               .setParent(AgentName.ofProjectName("[PROJECT]").toString())
281               .setEnvironment(Environment.newBuilder().build())
282               .setEnvironmentId("environmentId-950205810")
283               .build();
284       client.createEnvironment(request);
285       Assert.fail("No exception raised");
286     } catch (InvalidArgumentException e) {
287       // Expected exception.
288     }
289   }
290 
291   @Test
updateEnvironmentTest()292   public void updateEnvironmentTest() throws Exception {
293     Environment expectedResponse =
294         Environment.newBuilder()
295             .setName(
296                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
297             .setDescription("description-1724546052")
298             .setAgentVersion(VersionName.ofProjectVersionName("[PROJECT]", "[VERSION]").toString())
299             .setUpdateTime(Timestamp.newBuilder().build())
300             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
301             .setFulfillment(Fulfillment.newBuilder().build())
302             .build();
303     mockEnvironments.addResponse(expectedResponse);
304 
305     UpdateEnvironmentRequest request =
306         UpdateEnvironmentRequest.newBuilder()
307             .setEnvironment(Environment.newBuilder().build())
308             .setUpdateMask(FieldMask.newBuilder().build())
309             .setAllowLoadToDraftAndDiscardChanges(true)
310             .build();
311 
312     Environment actualResponse = client.updateEnvironment(request);
313     Assert.assertEquals(expectedResponse, actualResponse);
314 
315     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
316     Assert.assertEquals(1, actualRequests.size());
317     UpdateEnvironmentRequest actualRequest = ((UpdateEnvironmentRequest) actualRequests.get(0));
318 
319     Assert.assertEquals(request.getEnvironment(), actualRequest.getEnvironment());
320     Assert.assertEquals(request.getUpdateMask(), actualRequest.getUpdateMask());
321     Assert.assertEquals(
322         request.getAllowLoadToDraftAndDiscardChanges(),
323         actualRequest.getAllowLoadToDraftAndDiscardChanges());
324     Assert.assertTrue(
325         channelProvider.isHeaderSent(
326             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
327             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
328   }
329 
330   @Test
updateEnvironmentExceptionTest()331   public void updateEnvironmentExceptionTest() throws Exception {
332     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
333     mockEnvironments.addException(exception);
334 
335     try {
336       UpdateEnvironmentRequest request =
337           UpdateEnvironmentRequest.newBuilder()
338               .setEnvironment(Environment.newBuilder().build())
339               .setUpdateMask(FieldMask.newBuilder().build())
340               .setAllowLoadToDraftAndDiscardChanges(true)
341               .build();
342       client.updateEnvironment(request);
343       Assert.fail("No exception raised");
344     } catch (InvalidArgumentException e) {
345       // Expected exception.
346     }
347   }
348 
349   @Test
deleteEnvironmentTest()350   public void deleteEnvironmentTest() throws Exception {
351     Empty expectedResponse = Empty.newBuilder().build();
352     mockEnvironments.addResponse(expectedResponse);
353 
354     DeleteEnvironmentRequest request =
355         DeleteEnvironmentRequest.newBuilder()
356             .setName(
357                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
358             .build();
359 
360     client.deleteEnvironment(request);
361 
362     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
363     Assert.assertEquals(1, actualRequests.size());
364     DeleteEnvironmentRequest actualRequest = ((DeleteEnvironmentRequest) actualRequests.get(0));
365 
366     Assert.assertEquals(request.getName(), actualRequest.getName());
367     Assert.assertTrue(
368         channelProvider.isHeaderSent(
369             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
370             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
371   }
372 
373   @Test
deleteEnvironmentExceptionTest()374   public void deleteEnvironmentExceptionTest() throws Exception {
375     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
376     mockEnvironments.addException(exception);
377 
378     try {
379       DeleteEnvironmentRequest request =
380           DeleteEnvironmentRequest.newBuilder()
381               .setName(
382                   EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
383               .build();
384       client.deleteEnvironment(request);
385       Assert.fail("No exception raised");
386     } catch (InvalidArgumentException e) {
387       // Expected exception.
388     }
389   }
390 
391   @Test
getEnvironmentHistoryTest()392   public void getEnvironmentHistoryTest() throws Exception {
393     EnvironmentHistory.Entry responsesElement = EnvironmentHistory.Entry.newBuilder().build();
394     EnvironmentHistory expectedResponse =
395         EnvironmentHistory.newBuilder()
396             .setNextPageToken("")
397             .addAllEntries(Arrays.asList(responsesElement))
398             .build();
399     mockEnvironments.addResponse(expectedResponse);
400 
401     GetEnvironmentHistoryRequest request =
402         GetEnvironmentHistoryRequest.newBuilder()
403             .setParent(
404                 EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
405             .setPageSize(883849137)
406             .setPageToken("pageToken873572522")
407             .build();
408 
409     GetEnvironmentHistoryPagedResponse pagedListResponse = client.getEnvironmentHistory(request);
410 
411     List<EnvironmentHistory.Entry> resources = Lists.newArrayList(pagedListResponse.iterateAll());
412 
413     Assert.assertEquals(1, resources.size());
414     Assert.assertEquals(expectedResponse.getEntriesList().get(0), resources.get(0));
415 
416     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
417     Assert.assertEquals(1, actualRequests.size());
418     GetEnvironmentHistoryRequest actualRequest =
419         ((GetEnvironmentHistoryRequest) actualRequests.get(0));
420 
421     Assert.assertEquals(request.getParent(), actualRequest.getParent());
422     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
423     Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
424     Assert.assertTrue(
425         channelProvider.isHeaderSent(
426             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
427             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
428   }
429 
430   @Test
getEnvironmentHistoryExceptionTest()431   public void getEnvironmentHistoryExceptionTest() throws Exception {
432     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
433     mockEnvironments.addException(exception);
434 
435     try {
436       GetEnvironmentHistoryRequest request =
437           GetEnvironmentHistoryRequest.newBuilder()
438               .setParent(
439                   EnvironmentName.ofProjectEnvironmentName("[PROJECT]", "[ENVIRONMENT]").toString())
440               .setPageSize(883849137)
441               .setPageToken("pageToken873572522")
442               .build();
443       client.getEnvironmentHistory(request);
444       Assert.fail("No exception raised");
445     } catch (InvalidArgumentException e) {
446       // Expected exception.
447     }
448   }
449 
450   @Test
listLocationsTest()451   public void listLocationsTest() throws Exception {
452     Location responsesElement = Location.newBuilder().build();
453     ListLocationsResponse expectedResponse =
454         ListLocationsResponse.newBuilder()
455             .setNextPageToken("")
456             .addAllLocations(Arrays.asList(responsesElement))
457             .build();
458     mockLocations.addResponse(expectedResponse);
459 
460     ListLocationsRequest request =
461         ListLocationsRequest.newBuilder()
462             .setName("name3373707")
463             .setFilter("filter-1274492040")
464             .setPageSize(883849137)
465             .setPageToken("pageToken873572522")
466             .build();
467 
468     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
469 
470     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
471 
472     Assert.assertEquals(1, resources.size());
473     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
474 
475     List<AbstractMessage> actualRequests = mockLocations.getRequests();
476     Assert.assertEquals(1, actualRequests.size());
477     ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0));
478 
479     Assert.assertEquals(request.getName(), actualRequest.getName());
480     Assert.assertEquals(request.getFilter(), actualRequest.getFilter());
481     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
482     Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
483     Assert.assertTrue(
484         channelProvider.isHeaderSent(
485             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
486             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
487   }
488 
489   @Test
listLocationsExceptionTest()490   public void listLocationsExceptionTest() throws Exception {
491     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
492     mockLocations.addException(exception);
493 
494     try {
495       ListLocationsRequest request =
496           ListLocationsRequest.newBuilder()
497               .setName("name3373707")
498               .setFilter("filter-1274492040")
499               .setPageSize(883849137)
500               .setPageToken("pageToken873572522")
501               .build();
502       client.listLocations(request);
503       Assert.fail("No exception raised");
504     } catch (InvalidArgumentException e) {
505       // Expected exception.
506     }
507   }
508 
509   @Test
getLocationTest()510   public void getLocationTest() throws Exception {
511     Location expectedResponse =
512         Location.newBuilder()
513             .setName("name3373707")
514             .setLocationId("locationId1541836720")
515             .setDisplayName("displayName1714148973")
516             .putAllLabels(new HashMap<String, String>())
517             .setMetadata(Any.newBuilder().build())
518             .build();
519     mockLocations.addResponse(expectedResponse);
520 
521     GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
522 
523     Location actualResponse = client.getLocation(request);
524     Assert.assertEquals(expectedResponse, actualResponse);
525 
526     List<AbstractMessage> actualRequests = mockLocations.getRequests();
527     Assert.assertEquals(1, actualRequests.size());
528     GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0));
529 
530     Assert.assertEquals(request.getName(), actualRequest.getName());
531     Assert.assertTrue(
532         channelProvider.isHeaderSent(
533             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
534             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
535   }
536 
537   @Test
getLocationExceptionTest()538   public void getLocationExceptionTest() throws Exception {
539     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
540     mockLocations.addException(exception);
541 
542     try {
543       GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
544       client.getLocation(request);
545       Assert.fail("No exception raised");
546     } catch (InvalidArgumentException e) {
547       // Expected exception.
548     }
549   }
550 }
551