• 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.workflows.executions.v1beta;
18 
19 import static com.google.cloud.workflows.executions.v1beta.ExecutionsClient.ListExecutionsPagedResponse;
20 
21 import com.google.api.gax.core.NoCredentialsProvider;
22 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
23 import com.google.api.gax.httpjson.testing.MockHttpService;
24 import com.google.api.gax.rpc.ApiClientHeaderProvider;
25 import com.google.api.gax.rpc.ApiException;
26 import com.google.api.gax.rpc.ApiExceptionFactory;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.api.gax.rpc.StatusCode;
29 import com.google.api.gax.rpc.testing.FakeStatusCode;
30 import com.google.cloud.workflows.executions.v1beta.stub.HttpJsonExecutionsStub;
31 import com.google.common.collect.Lists;
32 import com.google.protobuf.Timestamp;
33 import java.io.IOException;
34 import java.util.Arrays;
35 import java.util.List;
36 import javax.annotation.Generated;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Assert;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 
44 @Generated("by gapic-generator-java")
45 public class ExecutionsClientHttpJsonTest {
46   private static MockHttpService mockService;
47   private static ExecutionsClient client;
48 
49   @BeforeClass
startStaticServer()50   public static void startStaticServer() throws IOException {
51     mockService =
52         new MockHttpService(
53             HttpJsonExecutionsStub.getMethodDescriptors(), ExecutionsSettings.getDefaultEndpoint());
54     ExecutionsSettings settings =
55         ExecutionsSettings.newHttpJsonBuilder()
56             .setTransportChannelProvider(
57                 ExecutionsSettings.defaultHttpJsonTransportProviderBuilder()
58                     .setHttpTransport(mockService)
59                     .build())
60             .setCredentialsProvider(NoCredentialsProvider.create())
61             .build();
62     client = ExecutionsClient.create(settings);
63   }
64 
65   @AfterClass
stopServer()66   public static void stopServer() {
67     client.close();
68   }
69 
70   @Before
setUp()71   public void setUp() {}
72 
73   @After
tearDown()74   public void tearDown() throws Exception {
75     mockService.reset();
76   }
77 
78   @Test
listExecutionsTest()79   public void listExecutionsTest() throws Exception {
80     Execution responsesElement = Execution.newBuilder().build();
81     ListExecutionsResponse expectedResponse =
82         ListExecutionsResponse.newBuilder()
83             .setNextPageToken("")
84             .addAllExecutions(Arrays.asList(responsesElement))
85             .build();
86     mockService.addResponse(expectedResponse);
87 
88     WorkflowName parent = WorkflowName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]");
89 
90     ListExecutionsPagedResponse pagedListResponse = client.listExecutions(parent);
91 
92     List<Execution> resources = Lists.newArrayList(pagedListResponse.iterateAll());
93 
94     Assert.assertEquals(1, resources.size());
95     Assert.assertEquals(expectedResponse.getExecutionsList().get(0), resources.get(0));
96 
97     List<String> actualRequests = mockService.getRequestPaths();
98     Assert.assertEquals(1, actualRequests.size());
99 
100     String apiClientHeaderKey =
101         mockService
102             .getRequestHeaders()
103             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
104             .iterator()
105             .next();
106     Assert.assertTrue(
107         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
108             .matcher(apiClientHeaderKey)
109             .matches());
110   }
111 
112   @Test
listExecutionsExceptionTest()113   public void listExecutionsExceptionTest() throws Exception {
114     ApiException exception =
115         ApiExceptionFactory.createException(
116             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
117     mockService.addException(exception);
118 
119     try {
120       WorkflowName parent = WorkflowName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]");
121       client.listExecutions(parent);
122       Assert.fail("No exception raised");
123     } catch (InvalidArgumentException e) {
124       // Expected exception.
125     }
126   }
127 
128   @Test
listExecutionsTest2()129   public void listExecutionsTest2() throws Exception {
130     Execution responsesElement = Execution.newBuilder().build();
131     ListExecutionsResponse expectedResponse =
132         ListExecutionsResponse.newBuilder()
133             .setNextPageToken("")
134             .addAllExecutions(Arrays.asList(responsesElement))
135             .build();
136     mockService.addResponse(expectedResponse);
137 
138     String parent = "projects/project-9319/locations/location-9319/workflows/workflow-9319";
139 
140     ListExecutionsPagedResponse pagedListResponse = client.listExecutions(parent);
141 
142     List<Execution> resources = Lists.newArrayList(pagedListResponse.iterateAll());
143 
144     Assert.assertEquals(1, resources.size());
145     Assert.assertEquals(expectedResponse.getExecutionsList().get(0), resources.get(0));
146 
147     List<String> actualRequests = mockService.getRequestPaths();
148     Assert.assertEquals(1, actualRequests.size());
149 
150     String apiClientHeaderKey =
151         mockService
152             .getRequestHeaders()
153             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
154             .iterator()
155             .next();
156     Assert.assertTrue(
157         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
158             .matcher(apiClientHeaderKey)
159             .matches());
160   }
161 
162   @Test
listExecutionsExceptionTest2()163   public void listExecutionsExceptionTest2() throws Exception {
164     ApiException exception =
165         ApiExceptionFactory.createException(
166             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
167     mockService.addException(exception);
168 
169     try {
170       String parent = "projects/project-9319/locations/location-9319/workflows/workflow-9319";
171       client.listExecutions(parent);
172       Assert.fail("No exception raised");
173     } catch (InvalidArgumentException e) {
174       // Expected exception.
175     }
176   }
177 
178   @Test
createExecutionTest()179   public void createExecutionTest() throws Exception {
180     Execution expectedResponse =
181         Execution.newBuilder()
182             .setName(
183                 ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]").toString())
184             .setStartTime(Timestamp.newBuilder().build())
185             .setEndTime(Timestamp.newBuilder().build())
186             .setArgument("argument-1589682499")
187             .setResult("result-934426595")
188             .setError(Execution.Error.newBuilder().build())
189             .setWorkflowRevisionId("workflowRevisionId-1575712907")
190             .build();
191     mockService.addResponse(expectedResponse);
192 
193     WorkflowName parent = WorkflowName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]");
194     Execution execution = Execution.newBuilder().build();
195 
196     Execution actualResponse = client.createExecution(parent, execution);
197     Assert.assertEquals(expectedResponse, actualResponse);
198 
199     List<String> actualRequests = mockService.getRequestPaths();
200     Assert.assertEquals(1, actualRequests.size());
201 
202     String apiClientHeaderKey =
203         mockService
204             .getRequestHeaders()
205             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
206             .iterator()
207             .next();
208     Assert.assertTrue(
209         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
210             .matcher(apiClientHeaderKey)
211             .matches());
212   }
213 
214   @Test
createExecutionExceptionTest()215   public void createExecutionExceptionTest() throws Exception {
216     ApiException exception =
217         ApiExceptionFactory.createException(
218             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
219     mockService.addException(exception);
220 
221     try {
222       WorkflowName parent = WorkflowName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]");
223       Execution execution = Execution.newBuilder().build();
224       client.createExecution(parent, execution);
225       Assert.fail("No exception raised");
226     } catch (InvalidArgumentException e) {
227       // Expected exception.
228     }
229   }
230 
231   @Test
createExecutionTest2()232   public void createExecutionTest2() throws Exception {
233     Execution expectedResponse =
234         Execution.newBuilder()
235             .setName(
236                 ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]").toString())
237             .setStartTime(Timestamp.newBuilder().build())
238             .setEndTime(Timestamp.newBuilder().build())
239             .setArgument("argument-1589682499")
240             .setResult("result-934426595")
241             .setError(Execution.Error.newBuilder().build())
242             .setWorkflowRevisionId("workflowRevisionId-1575712907")
243             .build();
244     mockService.addResponse(expectedResponse);
245 
246     String parent = "projects/project-9319/locations/location-9319/workflows/workflow-9319";
247     Execution execution = Execution.newBuilder().build();
248 
249     Execution actualResponse = client.createExecution(parent, execution);
250     Assert.assertEquals(expectedResponse, actualResponse);
251 
252     List<String> actualRequests = mockService.getRequestPaths();
253     Assert.assertEquals(1, actualRequests.size());
254 
255     String apiClientHeaderKey =
256         mockService
257             .getRequestHeaders()
258             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
259             .iterator()
260             .next();
261     Assert.assertTrue(
262         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
263             .matcher(apiClientHeaderKey)
264             .matches());
265   }
266 
267   @Test
createExecutionExceptionTest2()268   public void createExecutionExceptionTest2() throws Exception {
269     ApiException exception =
270         ApiExceptionFactory.createException(
271             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
272     mockService.addException(exception);
273 
274     try {
275       String parent = "projects/project-9319/locations/location-9319/workflows/workflow-9319";
276       Execution execution = Execution.newBuilder().build();
277       client.createExecution(parent, execution);
278       Assert.fail("No exception raised");
279     } catch (InvalidArgumentException e) {
280       // Expected exception.
281     }
282   }
283 
284   @Test
getExecutionTest()285   public void getExecutionTest() throws Exception {
286     Execution expectedResponse =
287         Execution.newBuilder()
288             .setName(
289                 ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]").toString())
290             .setStartTime(Timestamp.newBuilder().build())
291             .setEndTime(Timestamp.newBuilder().build())
292             .setArgument("argument-1589682499")
293             .setResult("result-934426595")
294             .setError(Execution.Error.newBuilder().build())
295             .setWorkflowRevisionId("workflowRevisionId-1575712907")
296             .build();
297     mockService.addResponse(expectedResponse);
298 
299     ExecutionName name = ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]");
300 
301     Execution actualResponse = client.getExecution(name);
302     Assert.assertEquals(expectedResponse, actualResponse);
303 
304     List<String> actualRequests = mockService.getRequestPaths();
305     Assert.assertEquals(1, actualRequests.size());
306 
307     String apiClientHeaderKey =
308         mockService
309             .getRequestHeaders()
310             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
311             .iterator()
312             .next();
313     Assert.assertTrue(
314         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
315             .matcher(apiClientHeaderKey)
316             .matches());
317   }
318 
319   @Test
getExecutionExceptionTest()320   public void getExecutionExceptionTest() throws Exception {
321     ApiException exception =
322         ApiExceptionFactory.createException(
323             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
324     mockService.addException(exception);
325 
326     try {
327       ExecutionName name = ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]");
328       client.getExecution(name);
329       Assert.fail("No exception raised");
330     } catch (InvalidArgumentException e) {
331       // Expected exception.
332     }
333   }
334 
335   @Test
getExecutionTest2()336   public void getExecutionTest2() throws Exception {
337     Execution expectedResponse =
338         Execution.newBuilder()
339             .setName(
340                 ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]").toString())
341             .setStartTime(Timestamp.newBuilder().build())
342             .setEndTime(Timestamp.newBuilder().build())
343             .setArgument("argument-1589682499")
344             .setResult("result-934426595")
345             .setError(Execution.Error.newBuilder().build())
346             .setWorkflowRevisionId("workflowRevisionId-1575712907")
347             .build();
348     mockService.addResponse(expectedResponse);
349 
350     String name =
351         "projects/project-7317/locations/location-7317/workflows/workflow-7317/executions/execution-7317";
352 
353     Execution actualResponse = client.getExecution(name);
354     Assert.assertEquals(expectedResponse, actualResponse);
355 
356     List<String> actualRequests = mockService.getRequestPaths();
357     Assert.assertEquals(1, actualRequests.size());
358 
359     String apiClientHeaderKey =
360         mockService
361             .getRequestHeaders()
362             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
363             .iterator()
364             .next();
365     Assert.assertTrue(
366         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
367             .matcher(apiClientHeaderKey)
368             .matches());
369   }
370 
371   @Test
getExecutionExceptionTest2()372   public void getExecutionExceptionTest2() throws Exception {
373     ApiException exception =
374         ApiExceptionFactory.createException(
375             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
376     mockService.addException(exception);
377 
378     try {
379       String name =
380           "projects/project-7317/locations/location-7317/workflows/workflow-7317/executions/execution-7317";
381       client.getExecution(name);
382       Assert.fail("No exception raised");
383     } catch (InvalidArgumentException e) {
384       // Expected exception.
385     }
386   }
387 
388   @Test
cancelExecutionTest()389   public void cancelExecutionTest() throws Exception {
390     Execution expectedResponse =
391         Execution.newBuilder()
392             .setName(
393                 ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]").toString())
394             .setStartTime(Timestamp.newBuilder().build())
395             .setEndTime(Timestamp.newBuilder().build())
396             .setArgument("argument-1589682499")
397             .setResult("result-934426595")
398             .setError(Execution.Error.newBuilder().build())
399             .setWorkflowRevisionId("workflowRevisionId-1575712907")
400             .build();
401     mockService.addResponse(expectedResponse);
402 
403     ExecutionName name = ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]");
404 
405     Execution actualResponse = client.cancelExecution(name);
406     Assert.assertEquals(expectedResponse, actualResponse);
407 
408     List<String> actualRequests = mockService.getRequestPaths();
409     Assert.assertEquals(1, actualRequests.size());
410 
411     String apiClientHeaderKey =
412         mockService
413             .getRequestHeaders()
414             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
415             .iterator()
416             .next();
417     Assert.assertTrue(
418         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
419             .matcher(apiClientHeaderKey)
420             .matches());
421   }
422 
423   @Test
cancelExecutionExceptionTest()424   public void cancelExecutionExceptionTest() throws Exception {
425     ApiException exception =
426         ApiExceptionFactory.createException(
427             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
428     mockService.addException(exception);
429 
430     try {
431       ExecutionName name = ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]");
432       client.cancelExecution(name);
433       Assert.fail("No exception raised");
434     } catch (InvalidArgumentException e) {
435       // Expected exception.
436     }
437   }
438 
439   @Test
cancelExecutionTest2()440   public void cancelExecutionTest2() throws Exception {
441     Execution expectedResponse =
442         Execution.newBuilder()
443             .setName(
444                 ExecutionName.of("[PROJECT]", "[LOCATION]", "[WORKFLOW]", "[EXECUTION]").toString())
445             .setStartTime(Timestamp.newBuilder().build())
446             .setEndTime(Timestamp.newBuilder().build())
447             .setArgument("argument-1589682499")
448             .setResult("result-934426595")
449             .setError(Execution.Error.newBuilder().build())
450             .setWorkflowRevisionId("workflowRevisionId-1575712907")
451             .build();
452     mockService.addResponse(expectedResponse);
453 
454     String name =
455         "projects/project-7317/locations/location-7317/workflows/workflow-7317/executions/execution-7317";
456 
457     Execution actualResponse = client.cancelExecution(name);
458     Assert.assertEquals(expectedResponse, actualResponse);
459 
460     List<String> actualRequests = mockService.getRequestPaths();
461     Assert.assertEquals(1, actualRequests.size());
462 
463     String apiClientHeaderKey =
464         mockService
465             .getRequestHeaders()
466             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
467             .iterator()
468             .next();
469     Assert.assertTrue(
470         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
471             .matcher(apiClientHeaderKey)
472             .matches());
473   }
474 
475   @Test
cancelExecutionExceptionTest2()476   public void cancelExecutionExceptionTest2() throws Exception {
477     ApiException exception =
478         ApiExceptionFactory.createException(
479             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
480     mockService.addException(exception);
481 
482     try {
483       String name =
484           "projects/project-7317/locations/location-7317/workflows/workflow-7317/executions/execution-7317";
485       client.cancelExecution(name);
486       Assert.fail("No exception raised");
487     } catch (InvalidArgumentException e) {
488       // Expected exception.
489     }
490   }
491 }
492