Skip to content

Commit a54ae4e

Browse files
committed
Added GetNumState WorkflowUtils #127
Signed-off-by: manick02 <[email protected]>
1 parent 3667d0f commit a54ae4e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-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
@@ -31,6 +31,7 @@
3131
/** Provides common utility methods to provide most often needed answers from a workflow */
3232
public final class WorkflowUtils {
3333
private static final int DEFAULT_STARTING_STATE_POSITION = 0;
34+
private static final int DEFAULT_STATE_COUNT = 0;
3435

3536
/**
3637
* Gets State matching Start state. If start is not present returns first state. If start is
@@ -265,6 +266,25 @@ public static List<Action> getActionsForFunctionDefinition(
265266
return getActionsWhichUsesFunctionDefinition(workflow, functionDefinitionName);
266267
}
267268

269+
/**
270+
* Gets Num of State in the workflow does not consider child workflow
271+
* @param workflow
272+
* @return
273+
*/
274+
public static int getNumOfStates(Workflow workflow) {
275+
return hasStates(workflow) ? workflow.getStates().size() : DEFAULT_STATE_COUNT;
276+
}
277+
278+
/**
279+
*
280+
* @param workflow
281+
* @param type
282+
* @return
283+
*/
284+
public static int getNumOfStates(Workflow workflow,DefaultState.Type type) {
285+
return hasStates(workflow) ? workflow.getStates().size() : DEFAULT_STATE_COUNT;
286+
}
287+
268288
private static List<Action> getActionsWhichUsesFunctionDefinition(
269289
Workflow workflow, String functionDefinitionName) {
270290
List<Action> actions = new ArrayList<>();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
21+
import io.serverlessworkflow.api.Workflow;
22+
import io.serverlessworkflow.util.testutil.TestUtils;
23+
import io.serverlessworkflow.utils.WorkflowUtils;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.ValueSource;
26+
27+
public class GetNumTests {
28+
@ParameterizedTest
29+
@ValueSource(strings = {"/getStates/workflowwithstates.yml"})
30+
public void testGetNumStates(String workflowWithStates) {
31+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowWithStates);
32+
int expectedStatesCount = 2;
33+
assertEquals(expectedStatesCount, WorkflowUtils.getNumOfStates(workflow));
34+
}
35+
36+
@ParameterizedTest
37+
@ValueSource(strings = {"/start/workflowwithnostate.yml"})
38+
public void testGetNumStatesForNoStateInWorkflow(String workflowWithStates) {
39+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowWithStates);
40+
int expectedStatesCount = 0;
41+
assertEquals(expectedStatesCount, WorkflowUtils.getNumOfStates(workflow));
42+
}
43+
}

0 commit comments

Comments
 (0)