• 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.ExperimentsClient.ListExperimentsPagedResponse;
20 import static com.google.cloud.dialogflow.cx.v3.ExperimentsClient.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.HttpJsonExperimentsStub;
32 import com.google.cloud.location.GetLocationRequest;
33 import com.google.cloud.location.ListLocationsRequest;
34 import com.google.cloud.location.ListLocationsResponse;
35 import com.google.cloud.location.Location;
36 import com.google.common.collect.Lists;
37 import com.google.protobuf.Any;
38 import com.google.protobuf.Duration;
39 import com.google.protobuf.Empty;
40 import com.google.protobuf.FieldMask;
41 import com.google.protobuf.Timestamp;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.HashMap;
46 import java.util.List;
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 ExperimentsClientHttpJsonTest {
57   private static MockHttpService mockService;
58   private static ExperimentsClient client;
59 
60   @BeforeClass
startStaticServer()61   public static void startStaticServer() throws IOException {
62     mockService =
63         new MockHttpService(
64             HttpJsonExperimentsStub.getMethodDescriptors(),
65             ExperimentsSettings.getDefaultEndpoint());
66     ExperimentsSettings settings =
67         ExperimentsSettings.newHttpJsonBuilder()
68             .setTransportChannelProvider(
69                 ExperimentsSettings.defaultHttpJsonTransportProviderBuilder()
70                     .setHttpTransport(mockService)
71                     .build())
72             .setCredentialsProvider(NoCredentialsProvider.create())
73             .build();
74     client = ExperimentsClient.create(settings);
75   }
76 
77   @AfterClass
stopServer()78   public static void stopServer() {
79     client.close();
80   }
81 
82   @Before
setUp()83   public void setUp() {}
84 
85   @After
tearDown()86   public void tearDown() throws Exception {
87     mockService.reset();
88   }
89 
90   @Test
listExperimentsTest()91   public void listExperimentsTest() throws Exception {
92     Experiment responsesElement = Experiment.newBuilder().build();
93     ListExperimentsResponse expectedResponse =
94         ListExperimentsResponse.newBuilder()
95             .setNextPageToken("")
96             .addAllExperiments(Arrays.asList(responsesElement))
97             .build();
98     mockService.addResponse(expectedResponse);
99 
100     EnvironmentName parent =
101         EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]");
102 
103     ListExperimentsPagedResponse pagedListResponse = client.listExperiments(parent);
104 
105     List<Experiment> resources = Lists.newArrayList(pagedListResponse.iterateAll());
106 
107     Assert.assertEquals(1, resources.size());
108     Assert.assertEquals(expectedResponse.getExperimentsList().get(0), resources.get(0));
109 
110     List<String> actualRequests = mockService.getRequestPaths();
111     Assert.assertEquals(1, actualRequests.size());
112 
113     String apiClientHeaderKey =
114         mockService
115             .getRequestHeaders()
116             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
117             .iterator()
118             .next();
119     Assert.assertTrue(
120         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
121             .matcher(apiClientHeaderKey)
122             .matches());
123   }
124 
125   @Test
listExperimentsExceptionTest()126   public void listExperimentsExceptionTest() throws Exception {
127     ApiException exception =
128         ApiExceptionFactory.createException(
129             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
130     mockService.addException(exception);
131 
132     try {
133       EnvironmentName parent =
134           EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]");
135       client.listExperiments(parent);
136       Assert.fail("No exception raised");
137     } catch (InvalidArgumentException e) {
138       // Expected exception.
139     }
140   }
141 
142   @Test
listExperimentsTest2()143   public void listExperimentsTest2() throws Exception {
144     Experiment responsesElement = Experiment.newBuilder().build();
145     ListExperimentsResponse expectedResponse =
146         ListExperimentsResponse.newBuilder()
147             .setNextPageToken("")
148             .addAllExperiments(Arrays.asList(responsesElement))
149             .build();
150     mockService.addResponse(expectedResponse);
151 
152     String parent =
153         "projects/project-5835/locations/location-5835/agents/agent-5835/environments/environment-5835";
154 
155     ListExperimentsPagedResponse pagedListResponse = client.listExperiments(parent);
156 
157     List<Experiment> resources = Lists.newArrayList(pagedListResponse.iterateAll());
158 
159     Assert.assertEquals(1, resources.size());
160     Assert.assertEquals(expectedResponse.getExperimentsList().get(0), resources.get(0));
161 
162     List<String> actualRequests = mockService.getRequestPaths();
163     Assert.assertEquals(1, actualRequests.size());
164 
165     String apiClientHeaderKey =
166         mockService
167             .getRequestHeaders()
168             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
169             .iterator()
170             .next();
171     Assert.assertTrue(
172         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
173             .matcher(apiClientHeaderKey)
174             .matches());
175   }
176 
177   @Test
listExperimentsExceptionTest2()178   public void listExperimentsExceptionTest2() throws Exception {
179     ApiException exception =
180         ApiExceptionFactory.createException(
181             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
182     mockService.addException(exception);
183 
184     try {
185       String parent =
186           "projects/project-5835/locations/location-5835/agents/agent-5835/environments/environment-5835";
187       client.listExperiments(parent);
188       Assert.fail("No exception raised");
189     } catch (InvalidArgumentException e) {
190       // Expected exception.
191     }
192   }
193 
194   @Test
getExperimentTest()195   public void getExperimentTest() throws Exception {
196     Experiment expectedResponse =
197         Experiment.newBuilder()
198             .setName(
199                 ExperimentName.of(
200                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
201                     .toString())
202             .setDisplayName("displayName1714148973")
203             .setDescription("description-1724546052")
204             .setDefinition(Experiment.Definition.newBuilder().build())
205             .setRolloutConfig(RolloutConfig.newBuilder().build())
206             .setRolloutState(RolloutState.newBuilder().build())
207             .setRolloutFailureReason("rolloutFailureReason958797373")
208             .setResult(Experiment.Result.newBuilder().build())
209             .setCreateTime(Timestamp.newBuilder().build())
210             .setStartTime(Timestamp.newBuilder().build())
211             .setEndTime(Timestamp.newBuilder().build())
212             .setLastUpdateTime(Timestamp.newBuilder().build())
213             .setExperimentLength(Duration.newBuilder().build())
214             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
215             .build();
216     mockService.addResponse(expectedResponse);
217 
218     ExperimentName name =
219         ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]");
220 
221     Experiment actualResponse = client.getExperiment(name);
222     Assert.assertEquals(expectedResponse, actualResponse);
223 
224     List<String> actualRequests = mockService.getRequestPaths();
225     Assert.assertEquals(1, actualRequests.size());
226 
227     String apiClientHeaderKey =
228         mockService
229             .getRequestHeaders()
230             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
231             .iterator()
232             .next();
233     Assert.assertTrue(
234         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
235             .matcher(apiClientHeaderKey)
236             .matches());
237   }
238 
239   @Test
getExperimentExceptionTest()240   public void getExperimentExceptionTest() throws Exception {
241     ApiException exception =
242         ApiExceptionFactory.createException(
243             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
244     mockService.addException(exception);
245 
246     try {
247       ExperimentName name =
248           ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]");
249       client.getExperiment(name);
250       Assert.fail("No exception raised");
251     } catch (InvalidArgumentException e) {
252       // Expected exception.
253     }
254   }
255 
256   @Test
getExperimentTest2()257   public void getExperimentTest2() throws Exception {
258     Experiment expectedResponse =
259         Experiment.newBuilder()
260             .setName(
261                 ExperimentName.of(
262                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
263                     .toString())
264             .setDisplayName("displayName1714148973")
265             .setDescription("description-1724546052")
266             .setDefinition(Experiment.Definition.newBuilder().build())
267             .setRolloutConfig(RolloutConfig.newBuilder().build())
268             .setRolloutState(RolloutState.newBuilder().build())
269             .setRolloutFailureReason("rolloutFailureReason958797373")
270             .setResult(Experiment.Result.newBuilder().build())
271             .setCreateTime(Timestamp.newBuilder().build())
272             .setStartTime(Timestamp.newBuilder().build())
273             .setEndTime(Timestamp.newBuilder().build())
274             .setLastUpdateTime(Timestamp.newBuilder().build())
275             .setExperimentLength(Duration.newBuilder().build())
276             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
277             .build();
278     mockService.addResponse(expectedResponse);
279 
280     String name =
281         "projects/project-4626/locations/location-4626/agents/agent-4626/environments/environment-4626/experiments/experiment-4626";
282 
283     Experiment actualResponse = client.getExperiment(name);
284     Assert.assertEquals(expectedResponse, actualResponse);
285 
286     List<String> actualRequests = mockService.getRequestPaths();
287     Assert.assertEquals(1, actualRequests.size());
288 
289     String apiClientHeaderKey =
290         mockService
291             .getRequestHeaders()
292             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
293             .iterator()
294             .next();
295     Assert.assertTrue(
296         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
297             .matcher(apiClientHeaderKey)
298             .matches());
299   }
300 
301   @Test
getExperimentExceptionTest2()302   public void getExperimentExceptionTest2() throws Exception {
303     ApiException exception =
304         ApiExceptionFactory.createException(
305             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
306     mockService.addException(exception);
307 
308     try {
309       String name =
310           "projects/project-4626/locations/location-4626/agents/agent-4626/environments/environment-4626/experiments/experiment-4626";
311       client.getExperiment(name);
312       Assert.fail("No exception raised");
313     } catch (InvalidArgumentException e) {
314       // Expected exception.
315     }
316   }
317 
318   @Test
createExperimentTest()319   public void createExperimentTest() throws Exception {
320     Experiment expectedResponse =
321         Experiment.newBuilder()
322             .setName(
323                 ExperimentName.of(
324                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
325                     .toString())
326             .setDisplayName("displayName1714148973")
327             .setDescription("description-1724546052")
328             .setDefinition(Experiment.Definition.newBuilder().build())
329             .setRolloutConfig(RolloutConfig.newBuilder().build())
330             .setRolloutState(RolloutState.newBuilder().build())
331             .setRolloutFailureReason("rolloutFailureReason958797373")
332             .setResult(Experiment.Result.newBuilder().build())
333             .setCreateTime(Timestamp.newBuilder().build())
334             .setStartTime(Timestamp.newBuilder().build())
335             .setEndTime(Timestamp.newBuilder().build())
336             .setLastUpdateTime(Timestamp.newBuilder().build())
337             .setExperimentLength(Duration.newBuilder().build())
338             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
339             .build();
340     mockService.addResponse(expectedResponse);
341 
342     EnvironmentName parent =
343         EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]");
344     Experiment experiment = Experiment.newBuilder().build();
345 
346     Experiment actualResponse = client.createExperiment(parent, experiment);
347     Assert.assertEquals(expectedResponse, actualResponse);
348 
349     List<String> actualRequests = mockService.getRequestPaths();
350     Assert.assertEquals(1, actualRequests.size());
351 
352     String apiClientHeaderKey =
353         mockService
354             .getRequestHeaders()
355             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
356             .iterator()
357             .next();
358     Assert.assertTrue(
359         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
360             .matcher(apiClientHeaderKey)
361             .matches());
362   }
363 
364   @Test
createExperimentExceptionTest()365   public void createExperimentExceptionTest() throws Exception {
366     ApiException exception =
367         ApiExceptionFactory.createException(
368             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
369     mockService.addException(exception);
370 
371     try {
372       EnvironmentName parent =
373           EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]");
374       Experiment experiment = Experiment.newBuilder().build();
375       client.createExperiment(parent, experiment);
376       Assert.fail("No exception raised");
377     } catch (InvalidArgumentException e) {
378       // Expected exception.
379     }
380   }
381 
382   @Test
createExperimentTest2()383   public void createExperimentTest2() throws Exception {
384     Experiment expectedResponse =
385         Experiment.newBuilder()
386             .setName(
387                 ExperimentName.of(
388                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
389                     .toString())
390             .setDisplayName("displayName1714148973")
391             .setDescription("description-1724546052")
392             .setDefinition(Experiment.Definition.newBuilder().build())
393             .setRolloutConfig(RolloutConfig.newBuilder().build())
394             .setRolloutState(RolloutState.newBuilder().build())
395             .setRolloutFailureReason("rolloutFailureReason958797373")
396             .setResult(Experiment.Result.newBuilder().build())
397             .setCreateTime(Timestamp.newBuilder().build())
398             .setStartTime(Timestamp.newBuilder().build())
399             .setEndTime(Timestamp.newBuilder().build())
400             .setLastUpdateTime(Timestamp.newBuilder().build())
401             .setExperimentLength(Duration.newBuilder().build())
402             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
403             .build();
404     mockService.addResponse(expectedResponse);
405 
406     String parent =
407         "projects/project-5835/locations/location-5835/agents/agent-5835/environments/environment-5835";
408     Experiment experiment = Experiment.newBuilder().build();
409 
410     Experiment actualResponse = client.createExperiment(parent, experiment);
411     Assert.assertEquals(expectedResponse, actualResponse);
412 
413     List<String> actualRequests = mockService.getRequestPaths();
414     Assert.assertEquals(1, actualRequests.size());
415 
416     String apiClientHeaderKey =
417         mockService
418             .getRequestHeaders()
419             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
420             .iterator()
421             .next();
422     Assert.assertTrue(
423         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
424             .matcher(apiClientHeaderKey)
425             .matches());
426   }
427 
428   @Test
createExperimentExceptionTest2()429   public void createExperimentExceptionTest2() throws Exception {
430     ApiException exception =
431         ApiExceptionFactory.createException(
432             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
433     mockService.addException(exception);
434 
435     try {
436       String parent =
437           "projects/project-5835/locations/location-5835/agents/agent-5835/environments/environment-5835";
438       Experiment experiment = Experiment.newBuilder().build();
439       client.createExperiment(parent, experiment);
440       Assert.fail("No exception raised");
441     } catch (InvalidArgumentException e) {
442       // Expected exception.
443     }
444   }
445 
446   @Test
updateExperimentTest()447   public void updateExperimentTest() throws Exception {
448     Experiment expectedResponse =
449         Experiment.newBuilder()
450             .setName(
451                 ExperimentName.of(
452                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
453                     .toString())
454             .setDisplayName("displayName1714148973")
455             .setDescription("description-1724546052")
456             .setDefinition(Experiment.Definition.newBuilder().build())
457             .setRolloutConfig(RolloutConfig.newBuilder().build())
458             .setRolloutState(RolloutState.newBuilder().build())
459             .setRolloutFailureReason("rolloutFailureReason958797373")
460             .setResult(Experiment.Result.newBuilder().build())
461             .setCreateTime(Timestamp.newBuilder().build())
462             .setStartTime(Timestamp.newBuilder().build())
463             .setEndTime(Timestamp.newBuilder().build())
464             .setLastUpdateTime(Timestamp.newBuilder().build())
465             .setExperimentLength(Duration.newBuilder().build())
466             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
467             .build();
468     mockService.addResponse(expectedResponse);
469 
470     Experiment experiment =
471         Experiment.newBuilder()
472             .setName(
473                 ExperimentName.of(
474                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
475                     .toString())
476             .setDisplayName("displayName1714148973")
477             .setDescription("description-1724546052")
478             .setDefinition(Experiment.Definition.newBuilder().build())
479             .setRolloutConfig(RolloutConfig.newBuilder().build())
480             .setRolloutState(RolloutState.newBuilder().build())
481             .setRolloutFailureReason("rolloutFailureReason958797373")
482             .setResult(Experiment.Result.newBuilder().build())
483             .setCreateTime(Timestamp.newBuilder().build())
484             .setStartTime(Timestamp.newBuilder().build())
485             .setEndTime(Timestamp.newBuilder().build())
486             .setLastUpdateTime(Timestamp.newBuilder().build())
487             .setExperimentLength(Duration.newBuilder().build())
488             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
489             .build();
490     FieldMask updateMask = FieldMask.newBuilder().build();
491 
492     Experiment actualResponse = client.updateExperiment(experiment, updateMask);
493     Assert.assertEquals(expectedResponse, actualResponse);
494 
495     List<String> actualRequests = mockService.getRequestPaths();
496     Assert.assertEquals(1, actualRequests.size());
497 
498     String apiClientHeaderKey =
499         mockService
500             .getRequestHeaders()
501             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
502             .iterator()
503             .next();
504     Assert.assertTrue(
505         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
506             .matcher(apiClientHeaderKey)
507             .matches());
508   }
509 
510   @Test
updateExperimentExceptionTest()511   public void updateExperimentExceptionTest() throws Exception {
512     ApiException exception =
513         ApiExceptionFactory.createException(
514             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
515     mockService.addException(exception);
516 
517     try {
518       Experiment experiment =
519           Experiment.newBuilder()
520               .setName(
521                   ExperimentName.of(
522                           "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
523                       .toString())
524               .setDisplayName("displayName1714148973")
525               .setDescription("description-1724546052")
526               .setDefinition(Experiment.Definition.newBuilder().build())
527               .setRolloutConfig(RolloutConfig.newBuilder().build())
528               .setRolloutState(RolloutState.newBuilder().build())
529               .setRolloutFailureReason("rolloutFailureReason958797373")
530               .setResult(Experiment.Result.newBuilder().build())
531               .setCreateTime(Timestamp.newBuilder().build())
532               .setStartTime(Timestamp.newBuilder().build())
533               .setEndTime(Timestamp.newBuilder().build())
534               .setLastUpdateTime(Timestamp.newBuilder().build())
535               .setExperimentLength(Duration.newBuilder().build())
536               .addAllVariantsHistory(new ArrayList<VariantsHistory>())
537               .build();
538       FieldMask updateMask = FieldMask.newBuilder().build();
539       client.updateExperiment(experiment, updateMask);
540       Assert.fail("No exception raised");
541     } catch (InvalidArgumentException e) {
542       // Expected exception.
543     }
544   }
545 
546   @Test
deleteExperimentTest()547   public void deleteExperimentTest() throws Exception {
548     Empty expectedResponse = Empty.newBuilder().build();
549     mockService.addResponse(expectedResponse);
550 
551     ExperimentName name =
552         ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]");
553 
554     client.deleteExperiment(name);
555 
556     List<String> actualRequests = mockService.getRequestPaths();
557     Assert.assertEquals(1, actualRequests.size());
558 
559     String apiClientHeaderKey =
560         mockService
561             .getRequestHeaders()
562             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
563             .iterator()
564             .next();
565     Assert.assertTrue(
566         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
567             .matcher(apiClientHeaderKey)
568             .matches());
569   }
570 
571   @Test
deleteExperimentExceptionTest()572   public void deleteExperimentExceptionTest() throws Exception {
573     ApiException exception =
574         ApiExceptionFactory.createException(
575             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
576     mockService.addException(exception);
577 
578     try {
579       ExperimentName name =
580           ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]");
581       client.deleteExperiment(name);
582       Assert.fail("No exception raised");
583     } catch (InvalidArgumentException e) {
584       // Expected exception.
585     }
586   }
587 
588   @Test
deleteExperimentTest2()589   public void deleteExperimentTest2() throws Exception {
590     Empty expectedResponse = Empty.newBuilder().build();
591     mockService.addResponse(expectedResponse);
592 
593     String name =
594         "projects/project-4626/locations/location-4626/agents/agent-4626/environments/environment-4626/experiments/experiment-4626";
595 
596     client.deleteExperiment(name);
597 
598     List<String> actualRequests = mockService.getRequestPaths();
599     Assert.assertEquals(1, actualRequests.size());
600 
601     String apiClientHeaderKey =
602         mockService
603             .getRequestHeaders()
604             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
605             .iterator()
606             .next();
607     Assert.assertTrue(
608         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
609             .matcher(apiClientHeaderKey)
610             .matches());
611   }
612 
613   @Test
deleteExperimentExceptionTest2()614   public void deleteExperimentExceptionTest2() throws Exception {
615     ApiException exception =
616         ApiExceptionFactory.createException(
617             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
618     mockService.addException(exception);
619 
620     try {
621       String name =
622           "projects/project-4626/locations/location-4626/agents/agent-4626/environments/environment-4626/experiments/experiment-4626";
623       client.deleteExperiment(name);
624       Assert.fail("No exception raised");
625     } catch (InvalidArgumentException e) {
626       // Expected exception.
627     }
628   }
629 
630   @Test
startExperimentTest()631   public void startExperimentTest() throws Exception {
632     Experiment expectedResponse =
633         Experiment.newBuilder()
634             .setName(
635                 ExperimentName.of(
636                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
637                     .toString())
638             .setDisplayName("displayName1714148973")
639             .setDescription("description-1724546052")
640             .setDefinition(Experiment.Definition.newBuilder().build())
641             .setRolloutConfig(RolloutConfig.newBuilder().build())
642             .setRolloutState(RolloutState.newBuilder().build())
643             .setRolloutFailureReason("rolloutFailureReason958797373")
644             .setResult(Experiment.Result.newBuilder().build())
645             .setCreateTime(Timestamp.newBuilder().build())
646             .setStartTime(Timestamp.newBuilder().build())
647             .setEndTime(Timestamp.newBuilder().build())
648             .setLastUpdateTime(Timestamp.newBuilder().build())
649             .setExperimentLength(Duration.newBuilder().build())
650             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
651             .build();
652     mockService.addResponse(expectedResponse);
653 
654     ExperimentName name =
655         ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]");
656 
657     Experiment actualResponse = client.startExperiment(name);
658     Assert.assertEquals(expectedResponse, actualResponse);
659 
660     List<String> actualRequests = mockService.getRequestPaths();
661     Assert.assertEquals(1, actualRequests.size());
662 
663     String apiClientHeaderKey =
664         mockService
665             .getRequestHeaders()
666             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
667             .iterator()
668             .next();
669     Assert.assertTrue(
670         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
671             .matcher(apiClientHeaderKey)
672             .matches());
673   }
674 
675   @Test
startExperimentExceptionTest()676   public void startExperimentExceptionTest() throws Exception {
677     ApiException exception =
678         ApiExceptionFactory.createException(
679             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
680     mockService.addException(exception);
681 
682     try {
683       ExperimentName name =
684           ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]");
685       client.startExperiment(name);
686       Assert.fail("No exception raised");
687     } catch (InvalidArgumentException e) {
688       // Expected exception.
689     }
690   }
691 
692   @Test
startExperimentTest2()693   public void startExperimentTest2() throws Exception {
694     Experiment expectedResponse =
695         Experiment.newBuilder()
696             .setName(
697                 ExperimentName.of(
698                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
699                     .toString())
700             .setDisplayName("displayName1714148973")
701             .setDescription("description-1724546052")
702             .setDefinition(Experiment.Definition.newBuilder().build())
703             .setRolloutConfig(RolloutConfig.newBuilder().build())
704             .setRolloutState(RolloutState.newBuilder().build())
705             .setRolloutFailureReason("rolloutFailureReason958797373")
706             .setResult(Experiment.Result.newBuilder().build())
707             .setCreateTime(Timestamp.newBuilder().build())
708             .setStartTime(Timestamp.newBuilder().build())
709             .setEndTime(Timestamp.newBuilder().build())
710             .setLastUpdateTime(Timestamp.newBuilder().build())
711             .setExperimentLength(Duration.newBuilder().build())
712             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
713             .build();
714     mockService.addResponse(expectedResponse);
715 
716     String name =
717         "projects/project-4626/locations/location-4626/agents/agent-4626/environments/environment-4626/experiments/experiment-4626";
718 
719     Experiment actualResponse = client.startExperiment(name);
720     Assert.assertEquals(expectedResponse, actualResponse);
721 
722     List<String> actualRequests = mockService.getRequestPaths();
723     Assert.assertEquals(1, actualRequests.size());
724 
725     String apiClientHeaderKey =
726         mockService
727             .getRequestHeaders()
728             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
729             .iterator()
730             .next();
731     Assert.assertTrue(
732         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
733             .matcher(apiClientHeaderKey)
734             .matches());
735   }
736 
737   @Test
startExperimentExceptionTest2()738   public void startExperimentExceptionTest2() throws Exception {
739     ApiException exception =
740         ApiExceptionFactory.createException(
741             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
742     mockService.addException(exception);
743 
744     try {
745       String name =
746           "projects/project-4626/locations/location-4626/agents/agent-4626/environments/environment-4626/experiments/experiment-4626";
747       client.startExperiment(name);
748       Assert.fail("No exception raised");
749     } catch (InvalidArgumentException e) {
750       // Expected exception.
751     }
752   }
753 
754   @Test
stopExperimentTest()755   public void stopExperimentTest() throws Exception {
756     Experiment expectedResponse =
757         Experiment.newBuilder()
758             .setName(
759                 ExperimentName.of(
760                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
761                     .toString())
762             .setDisplayName("displayName1714148973")
763             .setDescription("description-1724546052")
764             .setDefinition(Experiment.Definition.newBuilder().build())
765             .setRolloutConfig(RolloutConfig.newBuilder().build())
766             .setRolloutState(RolloutState.newBuilder().build())
767             .setRolloutFailureReason("rolloutFailureReason958797373")
768             .setResult(Experiment.Result.newBuilder().build())
769             .setCreateTime(Timestamp.newBuilder().build())
770             .setStartTime(Timestamp.newBuilder().build())
771             .setEndTime(Timestamp.newBuilder().build())
772             .setLastUpdateTime(Timestamp.newBuilder().build())
773             .setExperimentLength(Duration.newBuilder().build())
774             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
775             .build();
776     mockService.addResponse(expectedResponse);
777 
778     ExperimentName name =
779         ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]");
780 
781     Experiment actualResponse = client.stopExperiment(name);
782     Assert.assertEquals(expectedResponse, actualResponse);
783 
784     List<String> actualRequests = mockService.getRequestPaths();
785     Assert.assertEquals(1, actualRequests.size());
786 
787     String apiClientHeaderKey =
788         mockService
789             .getRequestHeaders()
790             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
791             .iterator()
792             .next();
793     Assert.assertTrue(
794         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
795             .matcher(apiClientHeaderKey)
796             .matches());
797   }
798 
799   @Test
stopExperimentExceptionTest()800   public void stopExperimentExceptionTest() throws Exception {
801     ApiException exception =
802         ApiExceptionFactory.createException(
803             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
804     mockService.addException(exception);
805 
806     try {
807       ExperimentName name =
808           ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]");
809       client.stopExperiment(name);
810       Assert.fail("No exception raised");
811     } catch (InvalidArgumentException e) {
812       // Expected exception.
813     }
814   }
815 
816   @Test
stopExperimentTest2()817   public void stopExperimentTest2() throws Exception {
818     Experiment expectedResponse =
819         Experiment.newBuilder()
820             .setName(
821                 ExperimentName.of(
822                         "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]")
823                     .toString())
824             .setDisplayName("displayName1714148973")
825             .setDescription("description-1724546052")
826             .setDefinition(Experiment.Definition.newBuilder().build())
827             .setRolloutConfig(RolloutConfig.newBuilder().build())
828             .setRolloutState(RolloutState.newBuilder().build())
829             .setRolloutFailureReason("rolloutFailureReason958797373")
830             .setResult(Experiment.Result.newBuilder().build())
831             .setCreateTime(Timestamp.newBuilder().build())
832             .setStartTime(Timestamp.newBuilder().build())
833             .setEndTime(Timestamp.newBuilder().build())
834             .setLastUpdateTime(Timestamp.newBuilder().build())
835             .setExperimentLength(Duration.newBuilder().build())
836             .addAllVariantsHistory(new ArrayList<VariantsHistory>())
837             .build();
838     mockService.addResponse(expectedResponse);
839 
840     String name =
841         "projects/project-4626/locations/location-4626/agents/agent-4626/environments/environment-4626/experiments/experiment-4626";
842 
843     Experiment actualResponse = client.stopExperiment(name);
844     Assert.assertEquals(expectedResponse, actualResponse);
845 
846     List<String> actualRequests = mockService.getRequestPaths();
847     Assert.assertEquals(1, actualRequests.size());
848 
849     String apiClientHeaderKey =
850         mockService
851             .getRequestHeaders()
852             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
853             .iterator()
854             .next();
855     Assert.assertTrue(
856         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
857             .matcher(apiClientHeaderKey)
858             .matches());
859   }
860 
861   @Test
stopExperimentExceptionTest2()862   public void stopExperimentExceptionTest2() throws Exception {
863     ApiException exception =
864         ApiExceptionFactory.createException(
865             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
866     mockService.addException(exception);
867 
868     try {
869       String name =
870           "projects/project-4626/locations/location-4626/agents/agent-4626/environments/environment-4626/experiments/experiment-4626";
871       client.stopExperiment(name);
872       Assert.fail("No exception raised");
873     } catch (InvalidArgumentException e) {
874       // Expected exception.
875     }
876   }
877 
878   @Test
listLocationsTest()879   public void listLocationsTest() throws Exception {
880     Location responsesElement = Location.newBuilder().build();
881     ListLocationsResponse expectedResponse =
882         ListLocationsResponse.newBuilder()
883             .setNextPageToken("")
884             .addAllLocations(Arrays.asList(responsesElement))
885             .build();
886     mockService.addResponse(expectedResponse);
887 
888     ListLocationsRequest request =
889         ListLocationsRequest.newBuilder()
890             .setName("projects/project-3664")
891             .setFilter("filter-1274492040")
892             .setPageSize(883849137)
893             .setPageToken("pageToken873572522")
894             .build();
895 
896     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
897 
898     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
899 
900     Assert.assertEquals(1, resources.size());
901     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
902 
903     List<String> actualRequests = mockService.getRequestPaths();
904     Assert.assertEquals(1, actualRequests.size());
905 
906     String apiClientHeaderKey =
907         mockService
908             .getRequestHeaders()
909             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
910             .iterator()
911             .next();
912     Assert.assertTrue(
913         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
914             .matcher(apiClientHeaderKey)
915             .matches());
916   }
917 
918   @Test
listLocationsExceptionTest()919   public void listLocationsExceptionTest() throws Exception {
920     ApiException exception =
921         ApiExceptionFactory.createException(
922             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
923     mockService.addException(exception);
924 
925     try {
926       ListLocationsRequest request =
927           ListLocationsRequest.newBuilder()
928               .setName("projects/project-3664")
929               .setFilter("filter-1274492040")
930               .setPageSize(883849137)
931               .setPageToken("pageToken873572522")
932               .build();
933       client.listLocations(request);
934       Assert.fail("No exception raised");
935     } catch (InvalidArgumentException e) {
936       // Expected exception.
937     }
938   }
939 
940   @Test
getLocationTest()941   public void getLocationTest() throws Exception {
942     Location expectedResponse =
943         Location.newBuilder()
944             .setName("name3373707")
945             .setLocationId("locationId1541836720")
946             .setDisplayName("displayName1714148973")
947             .putAllLabels(new HashMap<String, String>())
948             .setMetadata(Any.newBuilder().build())
949             .build();
950     mockService.addResponse(expectedResponse);
951 
952     GetLocationRequest request =
953         GetLocationRequest.newBuilder()
954             .setName("projects/project-9062/locations/location-9062")
955             .build();
956 
957     Location actualResponse = client.getLocation(request);
958     Assert.assertEquals(expectedResponse, actualResponse);
959 
960     List<String> actualRequests = mockService.getRequestPaths();
961     Assert.assertEquals(1, actualRequests.size());
962 
963     String apiClientHeaderKey =
964         mockService
965             .getRequestHeaders()
966             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
967             .iterator()
968             .next();
969     Assert.assertTrue(
970         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
971             .matcher(apiClientHeaderKey)
972             .matches());
973   }
974 
975   @Test
getLocationExceptionTest()976   public void getLocationExceptionTest() throws Exception {
977     ApiException exception =
978         ApiExceptionFactory.createException(
979             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
980     mockService.addException(exception);
981 
982     try {
983       GetLocationRequest request =
984           GetLocationRequest.newBuilder()
985               .setName("projects/project-9062/locations/location-9062")
986               .build();
987       client.getLocation(request);
988       Assert.fail("No exception raised");
989     } catch (InvalidArgumentException e) {
990       // Expected exception.
991     }
992   }
993 }
994