Skip to content

Commit 439d899

Browse files
authored
Merge pull request #141 from manick02/main
Implement numOfStates and numOfStates by stateType #127
2 parents 99dfcaf + 2ae111e commit 439d899

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +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;
35+
private static final long DEFAULT_STATE_COUNT = 0;
3436

3537
/**
3638
* Gets State matching Start state. If start is not present returns first state. If start is
@@ -265,6 +267,29 @@ public static List<Action> getActionsForFunctionDefinition(
265267
return getActionsWhichUsesFunctionDefinition(workflow, functionDefinitionName);
266268
}
267269

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

0 commit comments

Comments
 (0)