Skip to content

Commit 2ae111e

Browse files
committed
Added GetNumState for State Type WorkflowUtils #127
Signed-off-by: manick02 <[email protected]>
1 parent a54ae4e commit 2ae111e

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
import java.util.stream.Collectors;
3030

3131
/** Provides common utility methods to provide most often needed answers from a workflow */
32+
@SuppressWarnings("StreamToLoop")
3233
public final class WorkflowUtils {
3334
private static final int DEFAULT_STARTING_STATE_POSITION = 0;
34-
private static final int DEFAULT_STATE_COUNT = 0;
35+
private static final long DEFAULT_STATE_COUNT = 0;
3536

3637
/**
3738
* Gets State matching Start state. If start is not present returns first state. If start is
@@ -268,21 +269,25 @@ public static List<Action> getActionsForFunctionDefinition(
268269

269270
/**
270271
* Gets Num of State in the workflow does not consider child workflow
272+
*
271273
* @param workflow
272274
* @return
273275
*/
274-
public static int getNumOfStates(Workflow workflow) {
276+
public static long getNumOfStates(Workflow workflow) {
275277
return hasStates(workflow) ? workflow.getStates().size() : DEFAULT_STATE_COUNT;
276278
}
277279

278280
/**
281+
* Gets Num of States for State Type
279282
*
280283
* @param workflow
281284
* @param type
282285
* @return
283286
*/
284-
public static int getNumOfStates(Workflow workflow,DefaultState.Type type) {
285-
return hasStates(workflow) ? workflow.getStates().size() : DEFAULT_STATE_COUNT;
287+
public static long getNumOfStates(Workflow workflow, DefaultState.Type type) {
288+
return hasStates(workflow)
289+
? workflow.getStates().stream().filter(state -> state.getType() == type).count()
290+
: DEFAULT_STATE_COUNT;
286291
}
287292

288293
private static List<Action> getActionsWhichUsesFunctionDefinition(

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020

2121
import io.serverlessworkflow.api.Workflow;
22+
import io.serverlessworkflow.api.states.DefaultState;
2223
import io.serverlessworkflow.util.testutil.TestUtils;
2324
import io.serverlessworkflow.utils.WorkflowUtils;
2425
import org.junit.jupiter.params.ParameterizedTest;
@@ -40,4 +41,13 @@ public void testGetNumStatesForNoStateInWorkflow(String workflowWithStates) {
4041
int expectedStatesCount = 0;
4142
assertEquals(expectedStatesCount, WorkflowUtils.getNumOfStates(workflow));
4243
}
44+
45+
@ParameterizedTest
46+
@ValueSource(strings = {"/getStates/workflowwithstates.yml"})
47+
public void testGetNumStatesOfEventType(String workflowWithStates) {
48+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowWithStates);
49+
int expectedStatesCount = 2;
50+
assertEquals(
51+
expectedStatesCount, WorkflowUtils.getNumOfStates(workflow, DefaultState.Type.EVENT));
52+
}
4353
}

0 commit comments

Comments
 (0)