Skip to content

Commit 8e699b1

Browse files
committed
Implement FunctionDefinitionsForAction
Signed-off-by: manick02 <[email protected]>
1 parent f336671 commit 8e699b1

File tree

3 files changed

+89
-16
lines changed

3 files changed

+89
-16
lines changed

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

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +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;
2223
import io.serverlessworkflow.api.functions.FunctionDefinition;
24+
import io.serverlessworkflow.api.functions.FunctionRef;
2325
import io.serverlessworkflow.api.interfaces.State;
2426
import io.serverlessworkflow.api.start.Start;
2527
import io.serverlessworkflow.api.states.*;
@@ -242,13 +244,83 @@ public static int getWorkflowProducedEventsCount(Workflow workflow) {
242244
}
243245

244246
/** @return Returns function definition for actions */
245-
public static List<FunctionDefinition> getFunctionDefinitionsForAction(
247+
public static FunctionDefinition getFunctionDefinitionsForAction(
246248
Workflow workflow, String action) {
247-
if (hasFunctionDefs(workflow)) {
248-
return workflow.getFunctions().getFunctionDefs().stream()
249-
.filter(functionDef -> functionDef.getName().equals(action))
250-
.collect(Collectors.toList());
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+
}
251322
}
323+
252324
return null;
253325
}
254326

utils/src/test/java/io/serverlessworkflow/util/FunctionDefinitionsTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616

1717
package io.serverlessworkflow.util;
1818

19-
import static org.junit.jupiter.api.Assertions.assertEquals;
20-
import static org.junit.jupiter.api.Assertions.assertNull;
19+
import static org.junit.jupiter.api.Assertions.*;
2120

2221
import io.serverlessworkflow.api.Workflow;
2322
import io.serverlessworkflow.api.functions.FunctionDefinition;
2423
import io.serverlessworkflow.util.testutil.TestUtils;
2524
import io.serverlessworkflow.utils.WorkflowUtils;
26-
import java.util.List;
2725
import org.junit.jupiter.params.ParameterizedTest;
2826
import org.junit.jupiter.params.provider.ValueSource;
2927

@@ -32,12 +30,13 @@ public class FunctionDefinitionsTest {
3230
@ParameterizedTest
3331
@ValueSource(strings = {"/funcdefinitiontest/functiondefinition.yml"})
3432
public void testFunctionDefsForAction(String funcDefinitions) {
35-
String actionLookUp = "finalizeApplicationFunction";
36-
int expectedCount = 1;
33+
String actionLookUp = "finalizeApplicationAction";
34+
String expectedFunctionRefName = "finalizeApplicationFunction";
3735
Workflow workflow = TestUtils.createWorkflowFromTestResource(funcDefinitions);
38-
List<FunctionDefinition> finalizeApplicationFunction =
36+
FunctionDefinition finalizeApplicationFunctionDefinition =
3937
WorkflowUtils.getFunctionDefinitionsForAction(workflow, actionLookUp);
40-
assertEquals(expectedCount, finalizeApplicationFunction.size());
38+
assertNotNull(finalizeApplicationFunctionDefinition);
39+
assertEquals(expectedFunctionRefName, finalizeApplicationFunctionDefinition.getName());
4140
}
4241

4342
@ParameterizedTest
@@ -46,9 +45,9 @@ public void testFunctionDefsForActionNotPresent(String funcDefinitions) {
4645
String actionLookUp = "finalizeApplicationFunctionNotPresent";
4746
int expectedCount = 0;
4847
Workflow workflow = TestUtils.createWorkflowFromTestResource(funcDefinitions);
49-
List<FunctionDefinition> finalizeApplicationFunction =
48+
FunctionDefinition finalizeApplicationFunctionDefinition =
5049
WorkflowUtils.getFunctionDefinitionsForAction(workflow, actionLookUp);
51-
assertEquals(expectedCount, finalizeApplicationFunction.size());
50+
assertNull(finalizeApplicationFunctionDefinition);
5251
}
5352

5453
@ParameterizedTest

utils/src/test/resources/funcdefinitiontest/functiondefinition.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ states:
3232
- SATScoresReceived
3333
- RecommendationLetterReceived
3434
actions:
35-
- functionRef:
35+
- name : finalizeApplicationAction
36+
functionRef:
3637
refName: finalizeApplicationFunction
3738
arguments:
3839
student: "${ .applicantId }"
@@ -48,7 +49,8 @@ states:
4849
- SATScoresReceived
4950
- RecommendationLetterReceived
5051
actions:
51-
- functionRef:
52+
- name : finalizeApplicationAction
53+
functionRef:
5254
refName: finalizeApplicationFunction
5355
arguments:
5456
student: "${ .applicantId }"

0 commit comments

Comments
 (0)