• 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.v2beta1;
18 
19 import static com.google.cloud.dialogflow.v2beta1.AgentsClient.ListLocationsPagedResponse;
20 import static com.google.cloud.dialogflow.v2beta1.AgentsClient.SearchAgentsPagedResponse;
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.api.gax.rpc.StatusCode;
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.longrunning.Operation;
36 import com.google.protobuf.AbstractMessage;
37 import com.google.protobuf.Any;
38 import com.google.protobuf.Empty;
39 import io.grpc.StatusRuntimeException;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.UUID;
46 import java.util.concurrent.ExecutionException;
47 import javax.annotation.Generated;
48 import org.junit.After;
49 import org.junit.AfterClass;
50 import org.junit.Assert;
51 import org.junit.Before;
52 import org.junit.BeforeClass;
53 import org.junit.Test;
54 
55 @Generated("by gapic-generator-java")
56 public class AgentsClientTest {
57   private static MockAgents mockAgents;
58   private static MockLocations mockLocations;
59   private static MockServiceHelper mockServiceHelper;
60   private LocalChannelProvider channelProvider;
61   private AgentsClient client;
62 
63   @BeforeClass
startStaticServer()64   public static void startStaticServer() {
65     mockAgents = new MockAgents();
66     mockLocations = new MockLocations();
67     mockServiceHelper =
68         new MockServiceHelper(
69             UUID.randomUUID().toString(),
70             Arrays.<MockGrpcService>asList(mockAgents, mockLocations));
71     mockServiceHelper.start();
72   }
73 
74   @AfterClass
stopServer()75   public static void stopServer() {
76     mockServiceHelper.stop();
77   }
78 
79   @Before
setUp()80   public void setUp() throws IOException {
81     mockServiceHelper.reset();
82     channelProvider = mockServiceHelper.createChannelProvider();
83     AgentsSettings settings =
84         AgentsSettings.newBuilder()
85             .setTransportChannelProvider(channelProvider)
86             .setCredentialsProvider(NoCredentialsProvider.create())
87             .build();
88     client = AgentsClient.create(settings);
89   }
90 
91   @After
tearDown()92   public void tearDown() throws Exception {
93     client.close();
94   }
95 
96   @Test
getAgentTest()97   public void getAgentTest() throws Exception {
98     Agent expectedResponse =
99         Agent.newBuilder()
100             .setParent(ProjectName.of("[PROJECT]").toString())
101             .setDisplayName("displayName1714148973")
102             .setDefaultLanguageCode("defaultLanguageCode2069633606")
103             .addAllSupportedLanguageCodes(new ArrayList<String>())
104             .setTimeZone("timeZone-2077180903")
105             .setDescription("description-1724546052")
106             .setAvatarUri("avatarUri-428646061")
107             .setEnableLogging(true)
108             .setClassificationThreshold(1115810642)
109             .build();
110     mockAgents.addResponse(expectedResponse);
111 
112     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
113 
114     Agent actualResponse = client.getAgent(parent);
115     Assert.assertEquals(expectedResponse, actualResponse);
116 
117     List<AbstractMessage> actualRequests = mockAgents.getRequests();
118     Assert.assertEquals(1, actualRequests.size());
119     GetAgentRequest actualRequest = ((GetAgentRequest) actualRequests.get(0));
120 
121     Assert.assertEquals(parent.toString(), actualRequest.getParent());
122     Assert.assertTrue(
123         channelProvider.isHeaderSent(
124             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
125             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
126   }
127 
128   @Test
getAgentExceptionTest()129   public void getAgentExceptionTest() throws Exception {
130     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
131     mockAgents.addException(exception);
132 
133     try {
134       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
135       client.getAgent(parent);
136       Assert.fail("No exception raised");
137     } catch (InvalidArgumentException e) {
138       // Expected exception.
139     }
140   }
141 
142   @Test
getAgentTest2()143   public void getAgentTest2() throws Exception {
144     Agent expectedResponse =
145         Agent.newBuilder()
146             .setParent(ProjectName.of("[PROJECT]").toString())
147             .setDisplayName("displayName1714148973")
148             .setDefaultLanguageCode("defaultLanguageCode2069633606")
149             .addAllSupportedLanguageCodes(new ArrayList<String>())
150             .setTimeZone("timeZone-2077180903")
151             .setDescription("description-1724546052")
152             .setAvatarUri("avatarUri-428646061")
153             .setEnableLogging(true)
154             .setClassificationThreshold(1115810642)
155             .build();
156     mockAgents.addResponse(expectedResponse);
157 
158     ProjectName parent = ProjectName.of("[PROJECT]");
159 
160     Agent actualResponse = client.getAgent(parent);
161     Assert.assertEquals(expectedResponse, actualResponse);
162 
163     List<AbstractMessage> actualRequests = mockAgents.getRequests();
164     Assert.assertEquals(1, actualRequests.size());
165     GetAgentRequest actualRequest = ((GetAgentRequest) actualRequests.get(0));
166 
167     Assert.assertEquals(parent.toString(), actualRequest.getParent());
168     Assert.assertTrue(
169         channelProvider.isHeaderSent(
170             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
171             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
172   }
173 
174   @Test
getAgentExceptionTest2()175   public void getAgentExceptionTest2() throws Exception {
176     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
177     mockAgents.addException(exception);
178 
179     try {
180       ProjectName parent = ProjectName.of("[PROJECT]");
181       client.getAgent(parent);
182       Assert.fail("No exception raised");
183     } catch (InvalidArgumentException e) {
184       // Expected exception.
185     }
186   }
187 
188   @Test
getAgentTest3()189   public void getAgentTest3() throws Exception {
190     Agent expectedResponse =
191         Agent.newBuilder()
192             .setParent(ProjectName.of("[PROJECT]").toString())
193             .setDisplayName("displayName1714148973")
194             .setDefaultLanguageCode("defaultLanguageCode2069633606")
195             .addAllSupportedLanguageCodes(new ArrayList<String>())
196             .setTimeZone("timeZone-2077180903")
197             .setDescription("description-1724546052")
198             .setAvatarUri("avatarUri-428646061")
199             .setEnableLogging(true)
200             .setClassificationThreshold(1115810642)
201             .build();
202     mockAgents.addResponse(expectedResponse);
203 
204     String parent = "parent-995424086";
205 
206     Agent actualResponse = client.getAgent(parent);
207     Assert.assertEquals(expectedResponse, actualResponse);
208 
209     List<AbstractMessage> actualRequests = mockAgents.getRequests();
210     Assert.assertEquals(1, actualRequests.size());
211     GetAgentRequest actualRequest = ((GetAgentRequest) actualRequests.get(0));
212 
213     Assert.assertEquals(parent, actualRequest.getParent());
214     Assert.assertTrue(
215         channelProvider.isHeaderSent(
216             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
217             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
218   }
219 
220   @Test
getAgentExceptionTest3()221   public void getAgentExceptionTest3() throws Exception {
222     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
223     mockAgents.addException(exception);
224 
225     try {
226       String parent = "parent-995424086";
227       client.getAgent(parent);
228       Assert.fail("No exception raised");
229     } catch (InvalidArgumentException e) {
230       // Expected exception.
231     }
232   }
233 
234   @Test
setAgentTest()235   public void setAgentTest() throws Exception {
236     Agent expectedResponse =
237         Agent.newBuilder()
238             .setParent(ProjectName.of("[PROJECT]").toString())
239             .setDisplayName("displayName1714148973")
240             .setDefaultLanguageCode("defaultLanguageCode2069633606")
241             .addAllSupportedLanguageCodes(new ArrayList<String>())
242             .setTimeZone("timeZone-2077180903")
243             .setDescription("description-1724546052")
244             .setAvatarUri("avatarUri-428646061")
245             .setEnableLogging(true)
246             .setClassificationThreshold(1115810642)
247             .build();
248     mockAgents.addResponse(expectedResponse);
249 
250     Agent agent = Agent.newBuilder().build();
251 
252     Agent actualResponse = client.setAgent(agent);
253     Assert.assertEquals(expectedResponse, actualResponse);
254 
255     List<AbstractMessage> actualRequests = mockAgents.getRequests();
256     Assert.assertEquals(1, actualRequests.size());
257     SetAgentRequest actualRequest = ((SetAgentRequest) actualRequests.get(0));
258 
259     Assert.assertEquals(agent, actualRequest.getAgent());
260     Assert.assertTrue(
261         channelProvider.isHeaderSent(
262             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
263             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
264   }
265 
266   @Test
setAgentExceptionTest()267   public void setAgentExceptionTest() throws Exception {
268     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
269     mockAgents.addException(exception);
270 
271     try {
272       Agent agent = Agent.newBuilder().build();
273       client.setAgent(agent);
274       Assert.fail("No exception raised");
275     } catch (InvalidArgumentException e) {
276       // Expected exception.
277     }
278   }
279 
280   @Test
deleteAgentTest()281   public void deleteAgentTest() throws Exception {
282     Empty expectedResponse = Empty.newBuilder().build();
283     mockAgents.addResponse(expectedResponse);
284 
285     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
286 
287     client.deleteAgent(parent);
288 
289     List<AbstractMessage> actualRequests = mockAgents.getRequests();
290     Assert.assertEquals(1, actualRequests.size());
291     DeleteAgentRequest actualRequest = ((DeleteAgentRequest) actualRequests.get(0));
292 
293     Assert.assertEquals(parent.toString(), actualRequest.getParent());
294     Assert.assertTrue(
295         channelProvider.isHeaderSent(
296             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
297             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
298   }
299 
300   @Test
deleteAgentExceptionTest()301   public void deleteAgentExceptionTest() throws Exception {
302     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
303     mockAgents.addException(exception);
304 
305     try {
306       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
307       client.deleteAgent(parent);
308       Assert.fail("No exception raised");
309     } catch (InvalidArgumentException e) {
310       // Expected exception.
311     }
312   }
313 
314   @Test
deleteAgentTest2()315   public void deleteAgentTest2() throws Exception {
316     Empty expectedResponse = Empty.newBuilder().build();
317     mockAgents.addResponse(expectedResponse);
318 
319     ProjectName parent = ProjectName.of("[PROJECT]");
320 
321     client.deleteAgent(parent);
322 
323     List<AbstractMessage> actualRequests = mockAgents.getRequests();
324     Assert.assertEquals(1, actualRequests.size());
325     DeleteAgentRequest actualRequest = ((DeleteAgentRequest) actualRequests.get(0));
326 
327     Assert.assertEquals(parent.toString(), actualRequest.getParent());
328     Assert.assertTrue(
329         channelProvider.isHeaderSent(
330             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
331             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
332   }
333 
334   @Test
deleteAgentExceptionTest2()335   public void deleteAgentExceptionTest2() throws Exception {
336     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
337     mockAgents.addException(exception);
338 
339     try {
340       ProjectName parent = ProjectName.of("[PROJECT]");
341       client.deleteAgent(parent);
342       Assert.fail("No exception raised");
343     } catch (InvalidArgumentException e) {
344       // Expected exception.
345     }
346   }
347 
348   @Test
deleteAgentTest3()349   public void deleteAgentTest3() throws Exception {
350     Empty expectedResponse = Empty.newBuilder().build();
351     mockAgents.addResponse(expectedResponse);
352 
353     String parent = "parent-995424086";
354 
355     client.deleteAgent(parent);
356 
357     List<AbstractMessage> actualRequests = mockAgents.getRequests();
358     Assert.assertEquals(1, actualRequests.size());
359     DeleteAgentRequest actualRequest = ((DeleteAgentRequest) actualRequests.get(0));
360 
361     Assert.assertEquals(parent, actualRequest.getParent());
362     Assert.assertTrue(
363         channelProvider.isHeaderSent(
364             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
365             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
366   }
367 
368   @Test
deleteAgentExceptionTest3()369   public void deleteAgentExceptionTest3() throws Exception {
370     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
371     mockAgents.addException(exception);
372 
373     try {
374       String parent = "parent-995424086";
375       client.deleteAgent(parent);
376       Assert.fail("No exception raised");
377     } catch (InvalidArgumentException e) {
378       // Expected exception.
379     }
380   }
381 
382   @Test
searchAgentsTest()383   public void searchAgentsTest() throws Exception {
384     Agent responsesElement = Agent.newBuilder().build();
385     SearchAgentsResponse expectedResponse =
386         SearchAgentsResponse.newBuilder()
387             .setNextPageToken("")
388             .addAllAgents(Arrays.asList(responsesElement))
389             .build();
390     mockAgents.addResponse(expectedResponse);
391 
392     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
393 
394     SearchAgentsPagedResponse pagedListResponse = client.searchAgents(parent);
395 
396     List<Agent> resources = Lists.newArrayList(pagedListResponse.iterateAll());
397 
398     Assert.assertEquals(1, resources.size());
399     Assert.assertEquals(expectedResponse.getAgentsList().get(0), resources.get(0));
400 
401     List<AbstractMessage> actualRequests = mockAgents.getRequests();
402     Assert.assertEquals(1, actualRequests.size());
403     SearchAgentsRequest actualRequest = ((SearchAgentsRequest) actualRequests.get(0));
404 
405     Assert.assertEquals(parent.toString(), actualRequest.getParent());
406     Assert.assertTrue(
407         channelProvider.isHeaderSent(
408             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
409             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
410   }
411 
412   @Test
searchAgentsExceptionTest()413   public void searchAgentsExceptionTest() throws Exception {
414     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
415     mockAgents.addException(exception);
416 
417     try {
418       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
419       client.searchAgents(parent);
420       Assert.fail("No exception raised");
421     } catch (InvalidArgumentException e) {
422       // Expected exception.
423     }
424   }
425 
426   @Test
searchAgentsTest2()427   public void searchAgentsTest2() throws Exception {
428     Agent responsesElement = Agent.newBuilder().build();
429     SearchAgentsResponse expectedResponse =
430         SearchAgentsResponse.newBuilder()
431             .setNextPageToken("")
432             .addAllAgents(Arrays.asList(responsesElement))
433             .build();
434     mockAgents.addResponse(expectedResponse);
435 
436     ProjectName parent = ProjectName.of("[PROJECT]");
437 
438     SearchAgentsPagedResponse pagedListResponse = client.searchAgents(parent);
439 
440     List<Agent> resources = Lists.newArrayList(pagedListResponse.iterateAll());
441 
442     Assert.assertEquals(1, resources.size());
443     Assert.assertEquals(expectedResponse.getAgentsList().get(0), resources.get(0));
444 
445     List<AbstractMessage> actualRequests = mockAgents.getRequests();
446     Assert.assertEquals(1, actualRequests.size());
447     SearchAgentsRequest actualRequest = ((SearchAgentsRequest) actualRequests.get(0));
448 
449     Assert.assertEquals(parent.toString(), actualRequest.getParent());
450     Assert.assertTrue(
451         channelProvider.isHeaderSent(
452             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
453             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
454   }
455 
456   @Test
searchAgentsExceptionTest2()457   public void searchAgentsExceptionTest2() throws Exception {
458     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
459     mockAgents.addException(exception);
460 
461     try {
462       ProjectName parent = ProjectName.of("[PROJECT]");
463       client.searchAgents(parent);
464       Assert.fail("No exception raised");
465     } catch (InvalidArgumentException e) {
466       // Expected exception.
467     }
468   }
469 
470   @Test
searchAgentsTest3()471   public void searchAgentsTest3() throws Exception {
472     Agent responsesElement = Agent.newBuilder().build();
473     SearchAgentsResponse expectedResponse =
474         SearchAgentsResponse.newBuilder()
475             .setNextPageToken("")
476             .addAllAgents(Arrays.asList(responsesElement))
477             .build();
478     mockAgents.addResponse(expectedResponse);
479 
480     String parent = "parent-995424086";
481 
482     SearchAgentsPagedResponse pagedListResponse = client.searchAgents(parent);
483 
484     List<Agent> resources = Lists.newArrayList(pagedListResponse.iterateAll());
485 
486     Assert.assertEquals(1, resources.size());
487     Assert.assertEquals(expectedResponse.getAgentsList().get(0), resources.get(0));
488 
489     List<AbstractMessage> actualRequests = mockAgents.getRequests();
490     Assert.assertEquals(1, actualRequests.size());
491     SearchAgentsRequest actualRequest = ((SearchAgentsRequest) actualRequests.get(0));
492 
493     Assert.assertEquals(parent, actualRequest.getParent());
494     Assert.assertTrue(
495         channelProvider.isHeaderSent(
496             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
497             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
498   }
499 
500   @Test
searchAgentsExceptionTest3()501   public void searchAgentsExceptionTest3() throws Exception {
502     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
503     mockAgents.addException(exception);
504 
505     try {
506       String parent = "parent-995424086";
507       client.searchAgents(parent);
508       Assert.fail("No exception raised");
509     } catch (InvalidArgumentException e) {
510       // Expected exception.
511     }
512   }
513 
514   @Test
trainAgentTest()515   public void trainAgentTest() throws Exception {
516     Empty expectedResponse = Empty.newBuilder().build();
517     Operation resultOperation =
518         Operation.newBuilder()
519             .setName("trainAgentTest")
520             .setDone(true)
521             .setResponse(Any.pack(expectedResponse))
522             .build();
523     mockAgents.addResponse(resultOperation);
524 
525     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
526 
527     client.trainAgentAsync(parent).get();
528 
529     List<AbstractMessage> actualRequests = mockAgents.getRequests();
530     Assert.assertEquals(1, actualRequests.size());
531     TrainAgentRequest actualRequest = ((TrainAgentRequest) actualRequests.get(0));
532 
533     Assert.assertEquals(parent.toString(), actualRequest.getParent());
534     Assert.assertTrue(
535         channelProvider.isHeaderSent(
536             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
537             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
538   }
539 
540   @Test
trainAgentExceptionTest()541   public void trainAgentExceptionTest() throws Exception {
542     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
543     mockAgents.addException(exception);
544 
545     try {
546       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
547       client.trainAgentAsync(parent).get();
548       Assert.fail("No exception raised");
549     } catch (ExecutionException e) {
550       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
551       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
552       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
553     }
554   }
555 
556   @Test
trainAgentTest2()557   public void trainAgentTest2() throws Exception {
558     Empty expectedResponse = Empty.newBuilder().build();
559     Operation resultOperation =
560         Operation.newBuilder()
561             .setName("trainAgentTest")
562             .setDone(true)
563             .setResponse(Any.pack(expectedResponse))
564             .build();
565     mockAgents.addResponse(resultOperation);
566 
567     ProjectName parent = ProjectName.of("[PROJECT]");
568 
569     client.trainAgentAsync(parent).get();
570 
571     List<AbstractMessage> actualRequests = mockAgents.getRequests();
572     Assert.assertEquals(1, actualRequests.size());
573     TrainAgentRequest actualRequest = ((TrainAgentRequest) actualRequests.get(0));
574 
575     Assert.assertEquals(parent.toString(), actualRequest.getParent());
576     Assert.assertTrue(
577         channelProvider.isHeaderSent(
578             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
579             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
580   }
581 
582   @Test
trainAgentExceptionTest2()583   public void trainAgentExceptionTest2() throws Exception {
584     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
585     mockAgents.addException(exception);
586 
587     try {
588       ProjectName parent = ProjectName.of("[PROJECT]");
589       client.trainAgentAsync(parent).get();
590       Assert.fail("No exception raised");
591     } catch (ExecutionException e) {
592       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
593       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
594       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
595     }
596   }
597 
598   @Test
trainAgentTest3()599   public void trainAgentTest3() throws Exception {
600     Empty expectedResponse = Empty.newBuilder().build();
601     Operation resultOperation =
602         Operation.newBuilder()
603             .setName("trainAgentTest")
604             .setDone(true)
605             .setResponse(Any.pack(expectedResponse))
606             .build();
607     mockAgents.addResponse(resultOperation);
608 
609     String parent = "parent-995424086";
610 
611     client.trainAgentAsync(parent).get();
612 
613     List<AbstractMessage> actualRequests = mockAgents.getRequests();
614     Assert.assertEquals(1, actualRequests.size());
615     TrainAgentRequest actualRequest = ((TrainAgentRequest) actualRequests.get(0));
616 
617     Assert.assertEquals(parent, actualRequest.getParent());
618     Assert.assertTrue(
619         channelProvider.isHeaderSent(
620             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
621             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
622   }
623 
624   @Test
trainAgentExceptionTest3()625   public void trainAgentExceptionTest3() throws Exception {
626     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
627     mockAgents.addException(exception);
628 
629     try {
630       String parent = "parent-995424086";
631       client.trainAgentAsync(parent).get();
632       Assert.fail("No exception raised");
633     } catch (ExecutionException e) {
634       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
635       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
636       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
637     }
638   }
639 
640   @Test
exportAgentTest()641   public void exportAgentTest() throws Exception {
642     ExportAgentResponse expectedResponse = ExportAgentResponse.newBuilder().build();
643     Operation resultOperation =
644         Operation.newBuilder()
645             .setName("exportAgentTest")
646             .setDone(true)
647             .setResponse(Any.pack(expectedResponse))
648             .build();
649     mockAgents.addResponse(resultOperation);
650 
651     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
652 
653     ExportAgentResponse actualResponse = client.exportAgentAsync(parent).get();
654     Assert.assertEquals(expectedResponse, actualResponse);
655 
656     List<AbstractMessage> actualRequests = mockAgents.getRequests();
657     Assert.assertEquals(1, actualRequests.size());
658     ExportAgentRequest actualRequest = ((ExportAgentRequest) actualRequests.get(0));
659 
660     Assert.assertEquals(parent.toString(), actualRequest.getParent());
661     Assert.assertTrue(
662         channelProvider.isHeaderSent(
663             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
664             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
665   }
666 
667   @Test
exportAgentExceptionTest()668   public void exportAgentExceptionTest() throws Exception {
669     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
670     mockAgents.addException(exception);
671 
672     try {
673       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
674       client.exportAgentAsync(parent).get();
675       Assert.fail("No exception raised");
676     } catch (ExecutionException e) {
677       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
678       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
679       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
680     }
681   }
682 
683   @Test
exportAgentTest2()684   public void exportAgentTest2() throws Exception {
685     ExportAgentResponse expectedResponse = ExportAgentResponse.newBuilder().build();
686     Operation resultOperation =
687         Operation.newBuilder()
688             .setName("exportAgentTest")
689             .setDone(true)
690             .setResponse(Any.pack(expectedResponse))
691             .build();
692     mockAgents.addResponse(resultOperation);
693 
694     ProjectName parent = ProjectName.of("[PROJECT]");
695 
696     ExportAgentResponse actualResponse = client.exportAgentAsync(parent).get();
697     Assert.assertEquals(expectedResponse, actualResponse);
698 
699     List<AbstractMessage> actualRequests = mockAgents.getRequests();
700     Assert.assertEquals(1, actualRequests.size());
701     ExportAgentRequest actualRequest = ((ExportAgentRequest) actualRequests.get(0));
702 
703     Assert.assertEquals(parent.toString(), actualRequest.getParent());
704     Assert.assertTrue(
705         channelProvider.isHeaderSent(
706             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
707             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
708   }
709 
710   @Test
exportAgentExceptionTest2()711   public void exportAgentExceptionTest2() throws Exception {
712     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
713     mockAgents.addException(exception);
714 
715     try {
716       ProjectName parent = ProjectName.of("[PROJECT]");
717       client.exportAgentAsync(parent).get();
718       Assert.fail("No exception raised");
719     } catch (ExecutionException e) {
720       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
721       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
722       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
723     }
724   }
725 
726   @Test
exportAgentTest3()727   public void exportAgentTest3() throws Exception {
728     ExportAgentResponse expectedResponse = ExportAgentResponse.newBuilder().build();
729     Operation resultOperation =
730         Operation.newBuilder()
731             .setName("exportAgentTest")
732             .setDone(true)
733             .setResponse(Any.pack(expectedResponse))
734             .build();
735     mockAgents.addResponse(resultOperation);
736 
737     String parent = "parent-995424086";
738 
739     ExportAgentResponse actualResponse = client.exportAgentAsync(parent).get();
740     Assert.assertEquals(expectedResponse, actualResponse);
741 
742     List<AbstractMessage> actualRequests = mockAgents.getRequests();
743     Assert.assertEquals(1, actualRequests.size());
744     ExportAgentRequest actualRequest = ((ExportAgentRequest) actualRequests.get(0));
745 
746     Assert.assertEquals(parent, actualRequest.getParent());
747     Assert.assertTrue(
748         channelProvider.isHeaderSent(
749             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
750             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
751   }
752 
753   @Test
exportAgentExceptionTest3()754   public void exportAgentExceptionTest3() throws Exception {
755     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
756     mockAgents.addException(exception);
757 
758     try {
759       String parent = "parent-995424086";
760       client.exportAgentAsync(parent).get();
761       Assert.fail("No exception raised");
762     } catch (ExecutionException e) {
763       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
764       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
765       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
766     }
767   }
768 
769   @Test
importAgentTest()770   public void importAgentTest() throws Exception {
771     Empty expectedResponse = Empty.newBuilder().build();
772     Operation resultOperation =
773         Operation.newBuilder()
774             .setName("importAgentTest")
775             .setDone(true)
776             .setResponse(Any.pack(expectedResponse))
777             .build();
778     mockAgents.addResponse(resultOperation);
779 
780     ImportAgentRequest request =
781         ImportAgentRequest.newBuilder().setParent(ProjectName.of("[PROJECT]").toString()).build();
782 
783     client.importAgentAsync(request).get();
784 
785     List<AbstractMessage> actualRequests = mockAgents.getRequests();
786     Assert.assertEquals(1, actualRequests.size());
787     ImportAgentRequest actualRequest = ((ImportAgentRequest) actualRequests.get(0));
788 
789     Assert.assertEquals(request.getParent(), actualRequest.getParent());
790     Assert.assertEquals(request.getAgentUri(), actualRequest.getAgentUri());
791     Assert.assertEquals(request.getAgentContent(), actualRequest.getAgentContent());
792     Assert.assertTrue(
793         channelProvider.isHeaderSent(
794             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
795             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
796   }
797 
798   @Test
importAgentExceptionTest()799   public void importAgentExceptionTest() throws Exception {
800     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
801     mockAgents.addException(exception);
802 
803     try {
804       ImportAgentRequest request =
805           ImportAgentRequest.newBuilder().setParent(ProjectName.of("[PROJECT]").toString()).build();
806       client.importAgentAsync(request).get();
807       Assert.fail("No exception raised");
808     } catch (ExecutionException e) {
809       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
810       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
811       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
812     }
813   }
814 
815   @Test
restoreAgentTest()816   public void restoreAgentTest() throws Exception {
817     Empty expectedResponse = Empty.newBuilder().build();
818     Operation resultOperation =
819         Operation.newBuilder()
820             .setName("restoreAgentTest")
821             .setDone(true)
822             .setResponse(Any.pack(expectedResponse))
823             .build();
824     mockAgents.addResponse(resultOperation);
825 
826     RestoreAgentRequest request =
827         RestoreAgentRequest.newBuilder().setParent(ProjectName.of("[PROJECT]").toString()).build();
828 
829     client.restoreAgentAsync(request).get();
830 
831     List<AbstractMessage> actualRequests = mockAgents.getRequests();
832     Assert.assertEquals(1, actualRequests.size());
833     RestoreAgentRequest actualRequest = ((RestoreAgentRequest) actualRequests.get(0));
834 
835     Assert.assertEquals(request.getParent(), actualRequest.getParent());
836     Assert.assertEquals(request.getAgentUri(), actualRequest.getAgentUri());
837     Assert.assertEquals(request.getAgentContent(), actualRequest.getAgentContent());
838     Assert.assertTrue(
839         channelProvider.isHeaderSent(
840             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
841             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
842   }
843 
844   @Test
restoreAgentExceptionTest()845   public void restoreAgentExceptionTest() throws Exception {
846     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
847     mockAgents.addException(exception);
848 
849     try {
850       RestoreAgentRequest request =
851           RestoreAgentRequest.newBuilder()
852               .setParent(ProjectName.of("[PROJECT]").toString())
853               .build();
854       client.restoreAgentAsync(request).get();
855       Assert.fail("No exception raised");
856     } catch (ExecutionException e) {
857       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
858       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
859       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
860     }
861   }
862 
863   @Test
getValidationResultTest()864   public void getValidationResultTest() throws Exception {
865     ValidationResult expectedResponse =
866         ValidationResult.newBuilder()
867             .addAllValidationErrors(new ArrayList<ValidationError>())
868             .build();
869     mockAgents.addResponse(expectedResponse);
870 
871     GetValidationResultRequest request =
872         GetValidationResultRequest.newBuilder()
873             .setParent(ProjectName.of("[PROJECT]").toString())
874             .setLanguageCode("languageCode-2092349083")
875             .build();
876 
877     ValidationResult actualResponse = client.getValidationResult(request);
878     Assert.assertEquals(expectedResponse, actualResponse);
879 
880     List<AbstractMessage> actualRequests = mockAgents.getRequests();
881     Assert.assertEquals(1, actualRequests.size());
882     GetValidationResultRequest actualRequest = ((GetValidationResultRequest) actualRequests.get(0));
883 
884     Assert.assertEquals(request.getParent(), actualRequest.getParent());
885     Assert.assertEquals(request.getLanguageCode(), actualRequest.getLanguageCode());
886     Assert.assertTrue(
887         channelProvider.isHeaderSent(
888             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
889             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
890   }
891 
892   @Test
getValidationResultExceptionTest()893   public void getValidationResultExceptionTest() throws Exception {
894     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
895     mockAgents.addException(exception);
896 
897     try {
898       GetValidationResultRequest request =
899           GetValidationResultRequest.newBuilder()
900               .setParent(ProjectName.of("[PROJECT]").toString())
901               .setLanguageCode("languageCode-2092349083")
902               .build();
903       client.getValidationResult(request);
904       Assert.fail("No exception raised");
905     } catch (InvalidArgumentException e) {
906       // Expected exception.
907     }
908   }
909 
910   @Test
listLocationsTest()911   public void listLocationsTest() throws Exception {
912     Location responsesElement = Location.newBuilder().build();
913     ListLocationsResponse expectedResponse =
914         ListLocationsResponse.newBuilder()
915             .setNextPageToken("")
916             .addAllLocations(Arrays.asList(responsesElement))
917             .build();
918     mockLocations.addResponse(expectedResponse);
919 
920     ListLocationsRequest request =
921         ListLocationsRequest.newBuilder()
922             .setName("name3373707")
923             .setFilter("filter-1274492040")
924             .setPageSize(883849137)
925             .setPageToken("pageToken873572522")
926             .build();
927 
928     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
929 
930     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
931 
932     Assert.assertEquals(1, resources.size());
933     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
934 
935     List<AbstractMessage> actualRequests = mockLocations.getRequests();
936     Assert.assertEquals(1, actualRequests.size());
937     ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0));
938 
939     Assert.assertEquals(request.getName(), actualRequest.getName());
940     Assert.assertEquals(request.getFilter(), actualRequest.getFilter());
941     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
942     Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
943     Assert.assertTrue(
944         channelProvider.isHeaderSent(
945             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
946             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
947   }
948 
949   @Test
listLocationsExceptionTest()950   public void listLocationsExceptionTest() throws Exception {
951     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
952     mockLocations.addException(exception);
953 
954     try {
955       ListLocationsRequest request =
956           ListLocationsRequest.newBuilder()
957               .setName("name3373707")
958               .setFilter("filter-1274492040")
959               .setPageSize(883849137)
960               .setPageToken("pageToken873572522")
961               .build();
962       client.listLocations(request);
963       Assert.fail("No exception raised");
964     } catch (InvalidArgumentException e) {
965       // Expected exception.
966     }
967   }
968 
969   @Test
getLocationTest()970   public void getLocationTest() throws Exception {
971     Location expectedResponse =
972         Location.newBuilder()
973             .setName("name3373707")
974             .setLocationId("locationId1541836720")
975             .setDisplayName("displayName1714148973")
976             .putAllLabels(new HashMap<String, String>())
977             .setMetadata(Any.newBuilder().build())
978             .build();
979     mockLocations.addResponse(expectedResponse);
980 
981     GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
982 
983     Location actualResponse = client.getLocation(request);
984     Assert.assertEquals(expectedResponse, actualResponse);
985 
986     List<AbstractMessage> actualRequests = mockLocations.getRequests();
987     Assert.assertEquals(1, actualRequests.size());
988     GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0));
989 
990     Assert.assertEquals(request.getName(), actualRequest.getName());
991     Assert.assertTrue(
992         channelProvider.isHeaderSent(
993             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
994             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
995   }
996 
997   @Test
getLocationExceptionTest()998   public void getLocationExceptionTest() throws Exception {
999     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
1000     mockLocations.addException(exception);
1001 
1002     try {
1003       GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
1004       client.getLocation(request);
1005       Assert.fail("No exception raised");
1006     } catch (InvalidArgumentException e) {
1007       // Expected exception.
1008     }
1009   }
1010 }
1011