Skip to content

Commit f336671

Browse files
committed
GetFunctionDefs for action #127
Signed-off-by: manick02 <[email protected]>
1 parent 54b45f1 commit f336671

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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.functions.FunctionDefinition;
2223
import io.serverlessworkflow.api.interfaces.State;
2324
import io.serverlessworkflow.api.start.Start;
2425
import io.serverlessworkflow.api.states.*;
@@ -157,12 +158,12 @@ private static List<EventDefinition> getWorkflowEventDefinitions(
157158

158159
List<String> uniqueWorkflowEventsFromStates = getUniqueWorkflowEventsFromStates(workflow);
159160
List<EventDefinition> definedConsumedEvents = getDefinedEvents(workflow, eventKind);
160-
if(definedConsumedEvents == null) {
161+
if (definedConsumedEvents == null) {
161162
return null;
162163
}
163164
return definedConsumedEvents.stream()
164-
.filter(definedEvent -> uniqueWorkflowEventsFromStates.contains(definedEvent.getName()))
165-
.collect(Collectors.toList());
165+
.filter(definedEvent -> uniqueWorkflowEventsFromStates.contains(definedEvent.getName()))
166+
.collect(Collectors.toList());
166167
}
167168

168169
/** Returns a list of unique event names from workflow states */
@@ -240,6 +241,24 @@ public static int getWorkflowProducedEventsCount(Workflow workflow) {
240241
return workflowProducedEvents == null ? 0 : workflowProducedEvents.size();
241242
}
242243

244+
/** @return Returns function definition for actions */
245+
public static List<FunctionDefinition> getFunctionDefinitionsForAction(
246+
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());
251+
}
252+
return null;
253+
}
254+
255+
private static boolean hasFunctionDefs(Workflow workflow) {
256+
return workflow != null
257+
&& workflow.getFunctions() != null
258+
&& workflow.getFunctions().getFunctionDefs() != null
259+
&& !workflow.getFunctions().getFunctionDefs().isEmpty();
260+
}
261+
243262
/** Returns true if workflow has states, otherwise false */
244263
private static boolean hasStates(Workflow workflow) {
245264
return workflow != null && workflow.getStates() != null && !workflow.getStates().isEmpty();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNull;
21+
22+
import io.serverlessworkflow.api.Workflow;
23+
import io.serverlessworkflow.api.functions.FunctionDefinition;
24+
import io.serverlessworkflow.util.testutil.TestUtils;
25+
import io.serverlessworkflow.utils.WorkflowUtils;
26+
import java.util.List;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
29+
30+
public class FunctionDefinitionsTest {
31+
32+
@ParameterizedTest
33+
@ValueSource(strings = {"/funcdefinitiontest/functiondefinition.yml"})
34+
public void testFunctionDefsForAction(String funcDefinitions) {
35+
String actionLookUp = "finalizeApplicationFunction";
36+
int expectedCount = 1;
37+
Workflow workflow = TestUtils.createWorkflowFromTestResource(funcDefinitions);
38+
List<FunctionDefinition> finalizeApplicationFunction =
39+
WorkflowUtils.getFunctionDefinitionsForAction(workflow, actionLookUp);
40+
assertEquals(expectedCount, finalizeApplicationFunction.size());
41+
}
42+
43+
@ParameterizedTest
44+
@ValueSource(strings = {"/funcdefinitiontest/functiondefinition.yml"})
45+
public void testFunctionDefsForActionNotPresent(String funcDefinitions) {
46+
String actionLookUp = "finalizeApplicationFunctionNotPresent";
47+
int expectedCount = 0;
48+
Workflow workflow = TestUtils.createWorkflowFromTestResource(funcDefinitions);
49+
List<FunctionDefinition> finalizeApplicationFunction =
50+
WorkflowUtils.getFunctionDefinitionsForAction(workflow, actionLookUp);
51+
assertEquals(expectedCount, finalizeApplicationFunction.size());
52+
}
53+
54+
@ParameterizedTest
55+
@ValueSource(strings = {"/funcdefinitiontest/functiondefinition.yml"})
56+
public void testFunctionDefsForNullWorkflow(String funcDefinitions) {
57+
assertNull(WorkflowUtils.getFunctionDefinitionsForAction(null, "TestAction"));
58+
}
59+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
- functionRef:
36+
refName: finalizeApplicationFunction
37+
arguments:
38+
student: "${ .applicantId }"
39+
end:
40+
terminate: true
41+
42+
- name: CancelApplication
43+
type: event
44+
exclusive: false
45+
onEvents:
46+
- eventRefs:
47+
- ApplicationSubmitted
48+
- SATScoresReceived
49+
- RecommendationLetterReceived
50+
actions:
51+
- functionRef:
52+
refName: finalizeApplicationFunction
53+
arguments:
54+
student: "${ .applicantId }"
55+
end:
56+
terminate: true

0 commit comments

Comments
 (0)