Skip to content

Commit 4be73bb

Browse files
authored
Merge pull request #132 from manick02/utils-wf-states
Implement GetStates by StateType utility method #127
2 parents 759e674 + f976e47 commit 4be73bb

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import io.serverlessworkflow.api.Workflow;
1919
import io.serverlessworkflow.api.interfaces.State;
2020
import io.serverlessworkflow.api.start.Start;
21+
import io.serverlessworkflow.api.states.DefaultState;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
2124

2225
/** Provides common utility methods to provide most often needed answers from a workflow */
2326
public final class WorkflowUtils {
@@ -44,4 +47,21 @@ public static State getStartingState(Workflow workflow) {
4447
.get();
4548
}
4649
}
50+
51+
/**
52+
* Gets List of States matching stateType
53+
*
54+
* @param workflow
55+
* @param stateType
56+
* @return {@code List<State>}. Returns {@code null} when workflow is null.
57+
*/
58+
public static List<State> getStates(Workflow workflow, DefaultState.Type stateType) {
59+
if (workflow == null || workflow.getStates() == null) {
60+
return null;
61+
}
62+
63+
return workflow.getStates().stream()
64+
.filter(state -> state.getType() == stateType)
65+
.collect(Collectors.toList());
66+
}
4767
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.interfaces.State;
23+
import io.serverlessworkflow.api.states.DefaultState;
24+
import io.serverlessworkflow.util.testutil.TestUtils;
25+
import io.serverlessworkflow.utils.WorkflowUtils;
26+
import java.util.List;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.ValueSource;
30+
31+
class GetStatesTest {
32+
33+
@ParameterizedTest
34+
@ValueSource(strings = {"/getStates/workflowwithstates.yml"})
35+
public void testGetStatesByDefaultState(String workflowWithState) {
36+
int matchingEvents = 2;
37+
int notMatchingEvents = 0;
38+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowWithState);
39+
List<State> matchingStates = WorkflowUtils.getStates(workflow, DefaultState.Type.EVENT);
40+
List<State> notMatchingStates = WorkflowUtils.getStates(workflow, DefaultState.Type.SLEEP);
41+
assertEquals(matchingEvents, matchingStates.size());
42+
assertEquals(notMatchingEvents, notMatchingStates.size());
43+
}
44+
45+
@Test
46+
public void testGetsStatesForNullWorkflow() {
47+
assertNull(WorkflowUtils.getStates(null, DefaultState.Type.EVENT));
48+
}
49+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
correlation:
10+
- contextAttributeName: applicantId
11+
- name: SATScoresReceived
12+
type: org.application.satscores
13+
source: applicationsource
14+
correlation:
15+
- contextAttributeName: applicantId
16+
- name: RecommendationLetterReceived
17+
type: org.application.recommendationLetter
18+
source: applicationsource
19+
correlation:
20+
- contextAttributeName: applicantId
21+
functions:
22+
- name: finalizeApplicationFunction
23+
operation: http://myapis.org/collegeapplicationapi.json#finalize
24+
states:
25+
- name: FinalizeApplication
26+
type: event
27+
exclusive: false
28+
onEvents:
29+
- eventRefs:
30+
- ApplicationSubmitted
31+
- SATScoresReceived
32+
- RecommendationLetterReceived
33+
actions:
34+
- functionRef:
35+
refName: finalizeApplicationFunction
36+
arguments:
37+
student: "${ .applicantId }"
38+
end:
39+
terminate: true
40+
41+
- name: CancelApplication
42+
type: event
43+
exclusive: false
44+
onEvents:
45+
- eventRefs:
46+
- ApplicationSubmitted
47+
- SATScoresReceived
48+
- RecommendationLetterReceived
49+
actions:
50+
- functionRef:
51+
refName: finalizeApplicationFunction
52+
arguments:
53+
student: "${ .applicantId }"
54+
end:
55+
terminate: true

0 commit comments

Comments
 (0)