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