Skip to content

Commit aaf4203

Browse files
authored
Merge pull request #137 from manick02/wf-action-utils
GetFunctionDefs for action #127
2 parents 9159902 + 41f16ae commit aaf4203

File tree

3 files changed

+222
-3
lines changed

3 files changed

+222
-3
lines changed

utils/src/main/java/io/serverlessworkflow/utils/WorkflowUtils.java

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import io.serverlessworkflow.api.actions.Action;
2020
import io.serverlessworkflow.api.branches.Branch;
2121
import io.serverlessworkflow.api.events.EventDefinition;
22+
import io.serverlessworkflow.api.events.OnEvents;
23+
import io.serverlessworkflow.api.functions.FunctionDefinition;
24+
import io.serverlessworkflow.api.functions.FunctionRef;
2225
import io.serverlessworkflow.api.interfaces.State;
2326
import io.serverlessworkflow.api.start.Start;
2427
import io.serverlessworkflow.api.states.*;
@@ -157,12 +160,12 @@ private static List<EventDefinition> getWorkflowEventDefinitions(
157160

158161
List<String> uniqueWorkflowEventsFromStates = getUniqueWorkflowEventsFromStates(workflow);
159162
List<EventDefinition> definedConsumedEvents = getDefinedEvents(workflow, eventKind);
160-
if(definedConsumedEvents == null) {
163+
if (definedConsumedEvents == null) {
161164
return null;
162165
}
163166
return definedConsumedEvents.stream()
164-
.filter(definedEvent -> uniqueWorkflowEventsFromStates.contains(definedEvent.getName()))
165-
.collect(Collectors.toList());
167+
.filter(definedEvent -> uniqueWorkflowEventsFromStates.contains(definedEvent.getName()))
168+
.collect(Collectors.toList());
166169
}
167170

168171
/** Returns a list of unique event names from workflow states */
@@ -240,6 +243,106 @@ public static int getWorkflowProducedEventsCount(Workflow workflow) {
240243
return workflowProducedEvents == null ? 0 : workflowProducedEvents.size();
241244
}
242245

246+
/** @return Returns function definition for actions */
247+
public static FunctionDefinition getFunctionDefinitionsForAction(
248+
Workflow workflow, String action) {
249+
if (!hasFunctionDefs(workflow)) return null;
250+
FunctionRef functionRef = getFunctionRefFromAction(workflow, action);
251+
if (functionRef == null) return null;
252+
final Optional<FunctionDefinition> functionDefinition =
253+
workflow.getFunctions().getFunctionDefs().stream()
254+
.filter(functionDef -> functionDef.getName().equals(functionRef.getRefName()))
255+
.distinct()
256+
.findFirst();
257+
258+
return functionDefinition.isPresent() ? functionDefinition.get() : null;
259+
}
260+
261+
private static FunctionRef getFunctionRefFromAction(Workflow workflow, String action) {
262+
if (!hasStates(workflow)) return null;
263+
264+
for (State state : workflow.getStates()) {
265+
if (state instanceof EventState) {
266+
EventState eventState = (EventState) state;
267+
List<OnEvents> onEvents = eventState.getOnEvents();
268+
if (onEvents != null) {
269+
for (OnEvents onEvent : onEvents) {
270+
if (onEvent != null) {
271+
List<Action> onEventActions = onEvent.getActions();
272+
if (onEventActions != null) {
273+
for (Action onEventAction : onEventActions) {
274+
if (onEventAction != null
275+
&& onEventAction.getName() != null
276+
&& onEventAction.getName().equals(action))
277+
return onEventAction.getFunctionRef();
278+
}
279+
}
280+
}
281+
}
282+
}
283+
} else if (state instanceof CallbackState) {
284+
CallbackState callbackState = (CallbackState) state;
285+
final Action callbackStateAction = callbackState.getAction();
286+
if (callbackStateAction != null
287+
&& callbackStateAction.getName() != null
288+
&& callbackStateAction.getName().equals(action)) {
289+
return callbackStateAction.getFunctionRef();
290+
}
291+
292+
} else if (state instanceof OperationState) {
293+
OperationState operationState = (OperationState) state;
294+
final List<Action> operationStateActions = operationState.getActions();
295+
if (operationStateActions != null) {
296+
for (Action operationStateAction : operationStateActions) {
297+
if (operationStateAction != null
298+
&& operationStateAction.getName() != null
299+
&& operationStateAction.getName().equals(action)) {
300+
return operationStateAction.getFunctionRef();
301+
}
302+
}
303+
}
304+
} else if (state instanceof ParallelState) {
305+
ParallelState parallelState = (ParallelState) state;
306+
List<Branch> parallelStateBranches = parallelState.getBranches();
307+
if (parallelStateBranches != null) {
308+
for (Branch branch : parallelStateBranches) {
309+
List<Action> branchActions = branch.getActions();
310+
if (branchActions != null) {
311+
for (Action branchAction : branchActions) {
312+
if (branchAction != null
313+
&& branchAction.getName() != null
314+
&& branchAction.getName().equals(action)) {
315+
return branchAction.getFunctionRef();
316+
}
317+
}
318+
}
319+
}
320+
}
321+
} else if (state instanceof ForEachState) {
322+
ForEachState forEachState = (ForEachState) state;
323+
List<Action> forEachStateActions = forEachState.getActions();
324+
if (forEachStateActions != null) {
325+
for (Action forEachStateAction : forEachStateActions) {
326+
if (forEachStateAction != null
327+
&& forEachStateAction.getName() != null
328+
&& forEachStateAction.getName().equals(action)) {
329+
return forEachStateAction.getFunctionRef();
330+
}
331+
}
332+
}
333+
}
334+
}
335+
336+
return null;
337+
}
338+
339+
private static boolean hasFunctionDefs(Workflow workflow) {
340+
return workflow != null
341+
&& workflow.getFunctions() != null
342+
&& workflow.getFunctions().getFunctionDefs() != null
343+
&& !workflow.getFunctions().getFunctionDefs().isEmpty();
344+
}
345+
243346
/** Returns true if workflow has states, otherwise false */
244347
private static boolean hasStates(Workflow workflow) {
245348
return workflow != null && workflow.getStates() != null && !workflow.getStates().isEmpty();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
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+
* http://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 io.serverlessworkflow.util;
18+
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
import io.serverlessworkflow.api.Workflow;
22+
import io.serverlessworkflow.api.functions.FunctionDefinition;
23+
import io.serverlessworkflow.util.testutil.TestUtils;
24+
import io.serverlessworkflow.utils.WorkflowUtils;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ValueSource;
27+
28+
public class FunctionDefinitionsTest {
29+
30+
@ParameterizedTest
31+
@ValueSource(strings = {"/funcdefinitiontest/functiondefinition.yml"})
32+
public void testFunctionDefsForAction(String funcDefinitions) {
33+
String actionLookUp = "finalizeApplicationAction";
34+
String expectedFunctionRefName = "finalizeApplicationFunction";
35+
Workflow workflow = TestUtils.createWorkflowFromTestResource(funcDefinitions);
36+
FunctionDefinition finalizeApplicationFunctionDefinition =
37+
WorkflowUtils.getFunctionDefinitionsForAction(workflow, actionLookUp);
38+
assertNotNull(finalizeApplicationFunctionDefinition);
39+
assertEquals(expectedFunctionRefName, finalizeApplicationFunctionDefinition.getName());
40+
}
41+
42+
@ParameterizedTest
43+
@ValueSource(strings = {"/funcdefinitiontest/functiondefinition.yml"})
44+
public void testFunctionDefsForActionNotPresent(String funcDefinitions) {
45+
String actionLookUp = "finalizeApplicationFunctionNotPresent";
46+
int expectedCount = 0;
47+
Workflow workflow = TestUtils.createWorkflowFromTestResource(funcDefinitions);
48+
FunctionDefinition finalizeApplicationFunctionDefinition =
49+
WorkflowUtils.getFunctionDefinitionsForAction(workflow, actionLookUp);
50+
assertNull(finalizeApplicationFunctionDefinition);
51+
}
52+
53+
@ParameterizedTest
54+
@ValueSource(strings = {"/funcdefinitiontest/functiondefinition.yml"})
55+
public void testFunctionDefsForNullWorkflow(String funcDefinitions) {
56+
assertNull(WorkflowUtils.getFunctionDefinitionsForAction(null, "TestAction"));
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
id: finalizeCollegeApplication
2+
name: Finalize College Application
3+
version: '1.0'
4+
specVersion: '0.7'
5+
events:
6+
- name: ApplicationSubmitted
7+
type: org.application.submitted
8+
source: applicationsource
9+
kind: produced
10+
correlation:
11+
- contextAttributeName: applicantId
12+
- name: SATScoresReceived
13+
type: org.application.satscores
14+
source: applicationsource
15+
correlation:
16+
- contextAttributeName: applicantId
17+
- name: RecommendationLetterReceived
18+
type: org.application.recommendationLetter
19+
source: applicationsource
20+
correlation:
21+
- contextAttributeName: applicantId
22+
functions:
23+
- name: finalizeApplicationFunction
24+
operation: http://myapis.org/collegeapplicationapi.json#finalize
25+
states:
26+
- name: FinalizeApplication
27+
type: event
28+
exclusive: false
29+
onEvents:
30+
- eventRefs:
31+
- ApplicationSubmitted
32+
- SATScoresReceived
33+
- RecommendationLetterReceived
34+
actions:
35+
- name : finalizeApplicationAction
36+
functionRef:
37+
refName: finalizeApplicationFunction
38+
arguments:
39+
student: "${ .applicantId }"
40+
end:
41+
terminate: true
42+
43+
- name: CancelApplication
44+
type: event
45+
exclusive: false
46+
onEvents:
47+
- eventRefs:
48+
- ApplicationSubmitted
49+
- SATScoresReceived
50+
- RecommendationLetterReceived
51+
actions:
52+
- name : finalizeApplicationAction
53+
functionRef:
54+
refName: finalizeApplicationFunction
55+
arguments:
56+
student: "${ .applicantId }"
57+
end:
58+
terminate: true

0 commit comments

Comments
 (0)