• 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.cx.v3;
18 
19 import static com.google.cloud.dialogflow.cx.v3.AgentsClient.ListAgentsPagedResponse;
20 import static com.google.cloud.dialogflow.cx.v3.AgentsClient.ListLocationsPagedResponse;
21 
22 import com.google.api.gax.core.NoCredentialsProvider;
23 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
24 import com.google.api.gax.httpjson.testing.MockHttpService;
25 import com.google.api.gax.rpc.ApiClientHeaderProvider;
26 import com.google.api.gax.rpc.ApiException;
27 import com.google.api.gax.rpc.ApiExceptionFactory;
28 import com.google.api.gax.rpc.InvalidArgumentException;
29 import com.google.api.gax.rpc.StatusCode;
30 import com.google.api.gax.rpc.testing.FakeStatusCode;
31 import com.google.cloud.dialogflow.cx.v3.stub.HttpJsonAgentsStub;
32 import com.google.cloud.location.GetLocationRequest;
33 import com.google.cloud.location.ListLocationsRequest;
34 import com.google.cloud.location.ListLocationsResponse;
35 import com.google.cloud.location.Location;
36 import com.google.common.collect.Lists;
37 import com.google.longrunning.Operation;
38 import com.google.protobuf.Any;
39 import com.google.protobuf.Empty;
40 import com.google.protobuf.FieldMask;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.List;
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 AgentsClientHttpJsonTest {
57   private static MockHttpService mockService;
58   private static AgentsClient client;
59 
60   @BeforeClass
startStaticServer()61   public static void startStaticServer() throws IOException {
62     mockService =
63         new MockHttpService(
64             HttpJsonAgentsStub.getMethodDescriptors(), AgentsSettings.getDefaultEndpoint());
65     AgentsSettings settings =
66         AgentsSettings.newHttpJsonBuilder()
67             .setTransportChannelProvider(
68                 AgentsSettings.defaultHttpJsonTransportProviderBuilder()
69                     .setHttpTransport(mockService)
70                     .build())
71             .setCredentialsProvider(NoCredentialsProvider.create())
72             .build();
73     client = AgentsClient.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
listAgentsTest()90   public void listAgentsTest() throws Exception {
91     Agent responsesElement = Agent.newBuilder().build();
92     ListAgentsResponse expectedResponse =
93         ListAgentsResponse.newBuilder()
94             .setNextPageToken("")
95             .addAllAgents(Arrays.asList(responsesElement))
96             .build();
97     mockService.addResponse(expectedResponse);
98 
99     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
100 
101     ListAgentsPagedResponse pagedListResponse = client.listAgents(parent);
102 
103     List<Agent> resources = Lists.newArrayList(pagedListResponse.iterateAll());
104 
105     Assert.assertEquals(1, resources.size());
106     Assert.assertEquals(expectedResponse.getAgentsList().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
listAgentsExceptionTest()124   public void listAgentsExceptionTest() 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       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
132       client.listAgents(parent);
133       Assert.fail("No exception raised");
134     } catch (InvalidArgumentException e) {
135       // Expected exception.
136     }
137   }
138 
139   @Test
listAgentsTest2()140   public void listAgentsTest2() throws Exception {
141     Agent responsesElement = Agent.newBuilder().build();
142     ListAgentsResponse expectedResponse =
143         ListAgentsResponse.newBuilder()
144             .setNextPageToken("")
145             .addAllAgents(Arrays.asList(responsesElement))
146             .build();
147     mockService.addResponse(expectedResponse);
148 
149     String parent = "projects/project-5833/locations/location-5833";
150 
151     ListAgentsPagedResponse pagedListResponse = client.listAgents(parent);
152 
153     List<Agent> resources = Lists.newArrayList(pagedListResponse.iterateAll());
154 
155     Assert.assertEquals(1, resources.size());
156     Assert.assertEquals(expectedResponse.getAgentsList().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
listAgentsExceptionTest2()174   public void listAgentsExceptionTest2() 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-5833/locations/location-5833";
182       client.listAgents(parent);
183       Assert.fail("No exception raised");
184     } catch (InvalidArgumentException e) {
185       // Expected exception.
186     }
187   }
188 
189   @Test
getAgentTest()190   public void getAgentTest() throws Exception {
191     Agent expectedResponse =
192         Agent.newBuilder()
193             .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
194             .setDisplayName("displayName1714148973")
195             .setDefaultLanguageCode("defaultLanguageCode2069633606")
196             .addAllSupportedLanguageCodes(new ArrayList<String>())
197             .setTimeZone("timeZone-2077180903")
198             .setDescription("description-1724546052")
199             .setAvatarUri("avatarUri-428646061")
200             .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build())
201             .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString())
202             .setSecuritySettings(
203                 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]")
204                     .toString())
205             .setEnableStackdriverLogging(true)
206             .setEnableSpellCorrection(true)
207             .setLocked(true)
208             .setAdvancedSettings(AdvancedSettings.newBuilder().build())
209             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
210             .build();
211     mockService.addResponse(expectedResponse);
212 
213     AgentName name = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
214 
215     Agent actualResponse = client.getAgent(name);
216     Assert.assertEquals(expectedResponse, actualResponse);
217 
218     List<String> actualRequests = mockService.getRequestPaths();
219     Assert.assertEquals(1, actualRequests.size());
220 
221     String apiClientHeaderKey =
222         mockService
223             .getRequestHeaders()
224             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
225             .iterator()
226             .next();
227     Assert.assertTrue(
228         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
229             .matcher(apiClientHeaderKey)
230             .matches());
231   }
232 
233   @Test
getAgentExceptionTest()234   public void getAgentExceptionTest() throws Exception {
235     ApiException exception =
236         ApiExceptionFactory.createException(
237             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
238     mockService.addException(exception);
239 
240     try {
241       AgentName name = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
242       client.getAgent(name);
243       Assert.fail("No exception raised");
244     } catch (InvalidArgumentException e) {
245       // Expected exception.
246     }
247   }
248 
249   @Test
getAgentTest2()250   public void getAgentTest2() throws Exception {
251     Agent expectedResponse =
252         Agent.newBuilder()
253             .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
254             .setDisplayName("displayName1714148973")
255             .setDefaultLanguageCode("defaultLanguageCode2069633606")
256             .addAllSupportedLanguageCodes(new ArrayList<String>())
257             .setTimeZone("timeZone-2077180903")
258             .setDescription("description-1724546052")
259             .setAvatarUri("avatarUri-428646061")
260             .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build())
261             .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString())
262             .setSecuritySettings(
263                 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]")
264                     .toString())
265             .setEnableStackdriverLogging(true)
266             .setEnableSpellCorrection(true)
267             .setLocked(true)
268             .setAdvancedSettings(AdvancedSettings.newBuilder().build())
269             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
270             .build();
271     mockService.addResponse(expectedResponse);
272 
273     String name = "projects/project-2930/locations/location-2930/agents/agent-2930";
274 
275     Agent actualResponse = client.getAgent(name);
276     Assert.assertEquals(expectedResponse, actualResponse);
277 
278     List<String> actualRequests = mockService.getRequestPaths();
279     Assert.assertEquals(1, actualRequests.size());
280 
281     String apiClientHeaderKey =
282         mockService
283             .getRequestHeaders()
284             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
285             .iterator()
286             .next();
287     Assert.assertTrue(
288         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
289             .matcher(apiClientHeaderKey)
290             .matches());
291   }
292 
293   @Test
getAgentExceptionTest2()294   public void getAgentExceptionTest2() throws Exception {
295     ApiException exception =
296         ApiExceptionFactory.createException(
297             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
298     mockService.addException(exception);
299 
300     try {
301       String name = "projects/project-2930/locations/location-2930/agents/agent-2930";
302       client.getAgent(name);
303       Assert.fail("No exception raised");
304     } catch (InvalidArgumentException e) {
305       // Expected exception.
306     }
307   }
308 
309   @Test
createAgentTest()310   public void createAgentTest() throws Exception {
311     Agent expectedResponse =
312         Agent.newBuilder()
313             .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
314             .setDisplayName("displayName1714148973")
315             .setDefaultLanguageCode("defaultLanguageCode2069633606")
316             .addAllSupportedLanguageCodes(new ArrayList<String>())
317             .setTimeZone("timeZone-2077180903")
318             .setDescription("description-1724546052")
319             .setAvatarUri("avatarUri-428646061")
320             .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build())
321             .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString())
322             .setSecuritySettings(
323                 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]")
324                     .toString())
325             .setEnableStackdriverLogging(true)
326             .setEnableSpellCorrection(true)
327             .setLocked(true)
328             .setAdvancedSettings(AdvancedSettings.newBuilder().build())
329             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
330             .build();
331     mockService.addResponse(expectedResponse);
332 
333     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
334     Agent agent = Agent.newBuilder().build();
335 
336     Agent actualResponse = client.createAgent(parent, agent);
337     Assert.assertEquals(expectedResponse, actualResponse);
338 
339     List<String> actualRequests = mockService.getRequestPaths();
340     Assert.assertEquals(1, actualRequests.size());
341 
342     String apiClientHeaderKey =
343         mockService
344             .getRequestHeaders()
345             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
346             .iterator()
347             .next();
348     Assert.assertTrue(
349         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
350             .matcher(apiClientHeaderKey)
351             .matches());
352   }
353 
354   @Test
createAgentExceptionTest()355   public void createAgentExceptionTest() throws Exception {
356     ApiException exception =
357         ApiExceptionFactory.createException(
358             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
359     mockService.addException(exception);
360 
361     try {
362       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
363       Agent agent = Agent.newBuilder().build();
364       client.createAgent(parent, agent);
365       Assert.fail("No exception raised");
366     } catch (InvalidArgumentException e) {
367       // Expected exception.
368     }
369   }
370 
371   @Test
createAgentTest2()372   public void createAgentTest2() throws Exception {
373     Agent expectedResponse =
374         Agent.newBuilder()
375             .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
376             .setDisplayName("displayName1714148973")
377             .setDefaultLanguageCode("defaultLanguageCode2069633606")
378             .addAllSupportedLanguageCodes(new ArrayList<String>())
379             .setTimeZone("timeZone-2077180903")
380             .setDescription("description-1724546052")
381             .setAvatarUri("avatarUri-428646061")
382             .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build())
383             .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString())
384             .setSecuritySettings(
385                 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]")
386                     .toString())
387             .setEnableStackdriverLogging(true)
388             .setEnableSpellCorrection(true)
389             .setLocked(true)
390             .setAdvancedSettings(AdvancedSettings.newBuilder().build())
391             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
392             .build();
393     mockService.addResponse(expectedResponse);
394 
395     String parent = "projects/project-5833/locations/location-5833";
396     Agent agent = Agent.newBuilder().build();
397 
398     Agent actualResponse = client.createAgent(parent, agent);
399     Assert.assertEquals(expectedResponse, actualResponse);
400 
401     List<String> actualRequests = mockService.getRequestPaths();
402     Assert.assertEquals(1, actualRequests.size());
403 
404     String apiClientHeaderKey =
405         mockService
406             .getRequestHeaders()
407             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
408             .iterator()
409             .next();
410     Assert.assertTrue(
411         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
412             .matcher(apiClientHeaderKey)
413             .matches());
414   }
415 
416   @Test
createAgentExceptionTest2()417   public void createAgentExceptionTest2() throws Exception {
418     ApiException exception =
419         ApiExceptionFactory.createException(
420             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
421     mockService.addException(exception);
422 
423     try {
424       String parent = "projects/project-5833/locations/location-5833";
425       Agent agent = Agent.newBuilder().build();
426       client.createAgent(parent, agent);
427       Assert.fail("No exception raised");
428     } catch (InvalidArgumentException e) {
429       // Expected exception.
430     }
431   }
432 
433   @Test
updateAgentTest()434   public void updateAgentTest() throws Exception {
435     Agent expectedResponse =
436         Agent.newBuilder()
437             .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
438             .setDisplayName("displayName1714148973")
439             .setDefaultLanguageCode("defaultLanguageCode2069633606")
440             .addAllSupportedLanguageCodes(new ArrayList<String>())
441             .setTimeZone("timeZone-2077180903")
442             .setDescription("description-1724546052")
443             .setAvatarUri("avatarUri-428646061")
444             .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build())
445             .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString())
446             .setSecuritySettings(
447                 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]")
448                     .toString())
449             .setEnableStackdriverLogging(true)
450             .setEnableSpellCorrection(true)
451             .setLocked(true)
452             .setAdvancedSettings(AdvancedSettings.newBuilder().build())
453             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
454             .build();
455     mockService.addResponse(expectedResponse);
456 
457     Agent agent =
458         Agent.newBuilder()
459             .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
460             .setDisplayName("displayName1714148973")
461             .setDefaultLanguageCode("defaultLanguageCode2069633606")
462             .addAllSupportedLanguageCodes(new ArrayList<String>())
463             .setTimeZone("timeZone-2077180903")
464             .setDescription("description-1724546052")
465             .setAvatarUri("avatarUri-428646061")
466             .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build())
467             .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString())
468             .setSecuritySettings(
469                 SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]")
470                     .toString())
471             .setEnableStackdriverLogging(true)
472             .setEnableSpellCorrection(true)
473             .setLocked(true)
474             .setAdvancedSettings(AdvancedSettings.newBuilder().build())
475             .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
476             .build();
477     FieldMask updateMask = FieldMask.newBuilder().build();
478 
479     Agent actualResponse = client.updateAgent(agent, updateMask);
480     Assert.assertEquals(expectedResponse, actualResponse);
481 
482     List<String> actualRequests = mockService.getRequestPaths();
483     Assert.assertEquals(1, actualRequests.size());
484 
485     String apiClientHeaderKey =
486         mockService
487             .getRequestHeaders()
488             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
489             .iterator()
490             .next();
491     Assert.assertTrue(
492         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
493             .matcher(apiClientHeaderKey)
494             .matches());
495   }
496 
497   @Test
updateAgentExceptionTest()498   public void updateAgentExceptionTest() throws Exception {
499     ApiException exception =
500         ApiExceptionFactory.createException(
501             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
502     mockService.addException(exception);
503 
504     try {
505       Agent agent =
506           Agent.newBuilder()
507               .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
508               .setDisplayName("displayName1714148973")
509               .setDefaultLanguageCode("defaultLanguageCode2069633606")
510               .addAllSupportedLanguageCodes(new ArrayList<String>())
511               .setTimeZone("timeZone-2077180903")
512               .setDescription("description-1724546052")
513               .setAvatarUri("avatarUri-428646061")
514               .setSpeechToTextSettings(SpeechToTextSettings.newBuilder().build())
515               .setStartFlow(FlowName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[FLOW]").toString())
516               .setSecuritySettings(
517                   SecuritySettingsName.of("[PROJECT]", "[LOCATION]", "[SECURITY_SETTINGS]")
518                       .toString())
519               .setEnableStackdriverLogging(true)
520               .setEnableSpellCorrection(true)
521               .setLocked(true)
522               .setAdvancedSettings(AdvancedSettings.newBuilder().build())
523               .setTextToSpeechSettings(TextToSpeechSettings.newBuilder().build())
524               .build();
525       FieldMask updateMask = FieldMask.newBuilder().build();
526       client.updateAgent(agent, updateMask);
527       Assert.fail("No exception raised");
528     } catch (InvalidArgumentException e) {
529       // Expected exception.
530     }
531   }
532 
533   @Test
deleteAgentTest()534   public void deleteAgentTest() throws Exception {
535     Empty expectedResponse = Empty.newBuilder().build();
536     mockService.addResponse(expectedResponse);
537 
538     AgentName name = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
539 
540     client.deleteAgent(name);
541 
542     List<String> actualRequests = mockService.getRequestPaths();
543     Assert.assertEquals(1, actualRequests.size());
544 
545     String apiClientHeaderKey =
546         mockService
547             .getRequestHeaders()
548             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
549             .iterator()
550             .next();
551     Assert.assertTrue(
552         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
553             .matcher(apiClientHeaderKey)
554             .matches());
555   }
556 
557   @Test
deleteAgentExceptionTest()558   public void deleteAgentExceptionTest() throws Exception {
559     ApiException exception =
560         ApiExceptionFactory.createException(
561             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
562     mockService.addException(exception);
563 
564     try {
565       AgentName name = AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
566       client.deleteAgent(name);
567       Assert.fail("No exception raised");
568     } catch (InvalidArgumentException e) {
569       // Expected exception.
570     }
571   }
572 
573   @Test
deleteAgentTest2()574   public void deleteAgentTest2() throws Exception {
575     Empty expectedResponse = Empty.newBuilder().build();
576     mockService.addResponse(expectedResponse);
577 
578     String name = "projects/project-2930/locations/location-2930/agents/agent-2930";
579 
580     client.deleteAgent(name);
581 
582     List<String> actualRequests = mockService.getRequestPaths();
583     Assert.assertEquals(1, actualRequests.size());
584 
585     String apiClientHeaderKey =
586         mockService
587             .getRequestHeaders()
588             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
589             .iterator()
590             .next();
591     Assert.assertTrue(
592         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
593             .matcher(apiClientHeaderKey)
594             .matches());
595   }
596 
597   @Test
deleteAgentExceptionTest2()598   public void deleteAgentExceptionTest2() throws Exception {
599     ApiException exception =
600         ApiExceptionFactory.createException(
601             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
602     mockService.addException(exception);
603 
604     try {
605       String name = "projects/project-2930/locations/location-2930/agents/agent-2930";
606       client.deleteAgent(name);
607       Assert.fail("No exception raised");
608     } catch (InvalidArgumentException e) {
609       // Expected exception.
610     }
611   }
612 
613   @Test
exportAgentTest()614   public void exportAgentTest() throws Exception {
615     ExportAgentResponse expectedResponse = ExportAgentResponse.newBuilder().build();
616     Operation resultOperation =
617         Operation.newBuilder()
618             .setName("exportAgentTest")
619             .setDone(true)
620             .setResponse(Any.pack(expectedResponse))
621             .build();
622     mockService.addResponse(resultOperation);
623 
624     ExportAgentRequest request =
625         ExportAgentRequest.newBuilder()
626             .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
627             .setAgentUri("agentUri1469149223")
628             .setEnvironment(
629                 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]")
630                     .toString())
631             .build();
632 
633     ExportAgentResponse actualResponse = client.exportAgentAsync(request).get();
634     Assert.assertEquals(expectedResponse, actualResponse);
635 
636     List<String> actualRequests = mockService.getRequestPaths();
637     Assert.assertEquals(1, actualRequests.size());
638 
639     String apiClientHeaderKey =
640         mockService
641             .getRequestHeaders()
642             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
643             .iterator()
644             .next();
645     Assert.assertTrue(
646         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
647             .matcher(apiClientHeaderKey)
648             .matches());
649   }
650 
651   @Test
exportAgentExceptionTest()652   public void exportAgentExceptionTest() throws Exception {
653     ApiException exception =
654         ApiExceptionFactory.createException(
655             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
656     mockService.addException(exception);
657 
658     try {
659       ExportAgentRequest request =
660           ExportAgentRequest.newBuilder()
661               .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
662               .setAgentUri("agentUri1469149223")
663               .setEnvironment(
664                   EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]")
665                       .toString())
666               .build();
667       client.exportAgentAsync(request).get();
668       Assert.fail("No exception raised");
669     } catch (ExecutionException e) {
670     }
671   }
672 
673   @Test
restoreAgentTest()674   public void restoreAgentTest() throws Exception {
675     Empty expectedResponse = Empty.newBuilder().build();
676     Operation resultOperation =
677         Operation.newBuilder()
678             .setName("restoreAgentTest")
679             .setDone(true)
680             .setResponse(Any.pack(expectedResponse))
681             .build();
682     mockService.addResponse(resultOperation);
683 
684     RestoreAgentRequest request =
685         RestoreAgentRequest.newBuilder()
686             .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
687             .build();
688 
689     client.restoreAgentAsync(request).get();
690 
691     List<String> actualRequests = mockService.getRequestPaths();
692     Assert.assertEquals(1, actualRequests.size());
693 
694     String apiClientHeaderKey =
695         mockService
696             .getRequestHeaders()
697             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
698             .iterator()
699             .next();
700     Assert.assertTrue(
701         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
702             .matcher(apiClientHeaderKey)
703             .matches());
704   }
705 
706   @Test
restoreAgentExceptionTest()707   public void restoreAgentExceptionTest() throws Exception {
708     ApiException exception =
709         ApiExceptionFactory.createException(
710             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
711     mockService.addException(exception);
712 
713     try {
714       RestoreAgentRequest request =
715           RestoreAgentRequest.newBuilder()
716               .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
717               .build();
718       client.restoreAgentAsync(request).get();
719       Assert.fail("No exception raised");
720     } catch (ExecutionException e) {
721     }
722   }
723 
724   @Test
validateAgentTest()725   public void validateAgentTest() throws Exception {
726     AgentValidationResult expectedResponse =
727         AgentValidationResult.newBuilder()
728             .setName(AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
729             .addAllFlowValidationResults(new ArrayList<FlowValidationResult>())
730             .build();
731     mockService.addResponse(expectedResponse);
732 
733     ValidateAgentRequest request =
734         ValidateAgentRequest.newBuilder()
735             .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
736             .setLanguageCode("languageCode-2092349083")
737             .build();
738 
739     AgentValidationResult actualResponse = client.validateAgent(request);
740     Assert.assertEquals(expectedResponse, actualResponse);
741 
742     List<String> actualRequests = mockService.getRequestPaths();
743     Assert.assertEquals(1, actualRequests.size());
744 
745     String apiClientHeaderKey =
746         mockService
747             .getRequestHeaders()
748             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
749             .iterator()
750             .next();
751     Assert.assertTrue(
752         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
753             .matcher(apiClientHeaderKey)
754             .matches());
755   }
756 
757   @Test
validateAgentExceptionTest()758   public void validateAgentExceptionTest() throws Exception {
759     ApiException exception =
760         ApiExceptionFactory.createException(
761             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
762     mockService.addException(exception);
763 
764     try {
765       ValidateAgentRequest request =
766           ValidateAgentRequest.newBuilder()
767               .setName(AgentName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
768               .setLanguageCode("languageCode-2092349083")
769               .build();
770       client.validateAgent(request);
771       Assert.fail("No exception raised");
772     } catch (InvalidArgumentException e) {
773       // Expected exception.
774     }
775   }
776 
777   @Test
getAgentValidationResultTest()778   public void getAgentValidationResultTest() throws Exception {
779     AgentValidationResult expectedResponse =
780         AgentValidationResult.newBuilder()
781             .setName(AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
782             .addAllFlowValidationResults(new ArrayList<FlowValidationResult>())
783             .build();
784     mockService.addResponse(expectedResponse);
785 
786     AgentValidationResultName name =
787         AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
788 
789     AgentValidationResult actualResponse = client.getAgentValidationResult(name);
790     Assert.assertEquals(expectedResponse, actualResponse);
791 
792     List<String> actualRequests = mockService.getRequestPaths();
793     Assert.assertEquals(1, actualRequests.size());
794 
795     String apiClientHeaderKey =
796         mockService
797             .getRequestHeaders()
798             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
799             .iterator()
800             .next();
801     Assert.assertTrue(
802         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
803             .matcher(apiClientHeaderKey)
804             .matches());
805   }
806 
807   @Test
getAgentValidationResultExceptionTest()808   public void getAgentValidationResultExceptionTest() throws Exception {
809     ApiException exception =
810         ApiExceptionFactory.createException(
811             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
812     mockService.addException(exception);
813 
814     try {
815       AgentValidationResultName name =
816           AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]");
817       client.getAgentValidationResult(name);
818       Assert.fail("No exception raised");
819     } catch (InvalidArgumentException e) {
820       // Expected exception.
821     }
822   }
823 
824   @Test
getAgentValidationResultTest2()825   public void getAgentValidationResultTest2() throws Exception {
826     AgentValidationResult expectedResponse =
827         AgentValidationResult.newBuilder()
828             .setName(AgentValidationResultName.of("[PROJECT]", "[LOCATION]", "[AGENT]").toString())
829             .addAllFlowValidationResults(new ArrayList<FlowValidationResult>())
830             .build();
831     mockService.addResponse(expectedResponse);
832 
833     String name =
834         "projects/project-7481/locations/location-7481/agents/agent-7481/validationResult";
835 
836     AgentValidationResult actualResponse = client.getAgentValidationResult(name);
837     Assert.assertEquals(expectedResponse, actualResponse);
838 
839     List<String> actualRequests = mockService.getRequestPaths();
840     Assert.assertEquals(1, actualRequests.size());
841 
842     String apiClientHeaderKey =
843         mockService
844             .getRequestHeaders()
845             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
846             .iterator()
847             .next();
848     Assert.assertTrue(
849         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
850             .matcher(apiClientHeaderKey)
851             .matches());
852   }
853 
854   @Test
getAgentValidationResultExceptionTest2()855   public void getAgentValidationResultExceptionTest2() throws Exception {
856     ApiException exception =
857         ApiExceptionFactory.createException(
858             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
859     mockService.addException(exception);
860 
861     try {
862       String name =
863           "projects/project-7481/locations/location-7481/agents/agent-7481/validationResult";
864       client.getAgentValidationResult(name);
865       Assert.fail("No exception raised");
866     } catch (InvalidArgumentException e) {
867       // Expected exception.
868     }
869   }
870 
871   @Test
listLocationsTest()872   public void listLocationsTest() throws Exception {
873     Location responsesElement = Location.newBuilder().build();
874     ListLocationsResponse expectedResponse =
875         ListLocationsResponse.newBuilder()
876             .setNextPageToken("")
877             .addAllLocations(Arrays.asList(responsesElement))
878             .build();
879     mockService.addResponse(expectedResponse);
880 
881     ListLocationsRequest request =
882         ListLocationsRequest.newBuilder()
883             .setName("projects/project-3664")
884             .setFilter("filter-1274492040")
885             .setPageSize(883849137)
886             .setPageToken("pageToken873572522")
887             .build();
888 
889     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
890 
891     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
892 
893     Assert.assertEquals(1, resources.size());
894     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
895 
896     List<String> actualRequests = mockService.getRequestPaths();
897     Assert.assertEquals(1, actualRequests.size());
898 
899     String apiClientHeaderKey =
900         mockService
901             .getRequestHeaders()
902             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
903             .iterator()
904             .next();
905     Assert.assertTrue(
906         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
907             .matcher(apiClientHeaderKey)
908             .matches());
909   }
910 
911   @Test
listLocationsExceptionTest()912   public void listLocationsExceptionTest() throws Exception {
913     ApiException exception =
914         ApiExceptionFactory.createException(
915             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
916     mockService.addException(exception);
917 
918     try {
919       ListLocationsRequest request =
920           ListLocationsRequest.newBuilder()
921               .setName("projects/project-3664")
922               .setFilter("filter-1274492040")
923               .setPageSize(883849137)
924               .setPageToken("pageToken873572522")
925               .build();
926       client.listLocations(request);
927       Assert.fail("No exception raised");
928     } catch (InvalidArgumentException e) {
929       // Expected exception.
930     }
931   }
932 
933   @Test
getLocationTest()934   public void getLocationTest() throws Exception {
935     Location expectedResponse =
936         Location.newBuilder()
937             .setName("name3373707")
938             .setLocationId("locationId1541836720")
939             .setDisplayName("displayName1714148973")
940             .putAllLabels(new HashMap<String, String>())
941             .setMetadata(Any.newBuilder().build())
942             .build();
943     mockService.addResponse(expectedResponse);
944 
945     GetLocationRequest request =
946         GetLocationRequest.newBuilder()
947             .setName("projects/project-9062/locations/location-9062")
948             .build();
949 
950     Location actualResponse = client.getLocation(request);
951     Assert.assertEquals(expectedResponse, actualResponse);
952 
953     List<String> actualRequests = mockService.getRequestPaths();
954     Assert.assertEquals(1, actualRequests.size());
955 
956     String apiClientHeaderKey =
957         mockService
958             .getRequestHeaders()
959             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
960             .iterator()
961             .next();
962     Assert.assertTrue(
963         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
964             .matcher(apiClientHeaderKey)
965             .matches());
966   }
967 
968   @Test
getLocationExceptionTest()969   public void getLocationExceptionTest() throws Exception {
970     ApiException exception =
971         ApiExceptionFactory.createException(
972             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
973     mockService.addException(exception);
974 
975     try {
976       GetLocationRequest request =
977           GetLocationRequest.newBuilder()
978               .setName("projects/project-9062/locations/location-9062")
979               .build();
980       client.getLocation(request);
981       Assert.fail("No exception raised");
982     } catch (InvalidArgumentException e) {
983       // Expected exception.
984     }
985   }
986 }
987