Skip to content

Commit 4adbf9e

Browse files
committed
Updated Util method to get Defined Events and its count
Signed-off-by: manick02 <[email protected]>
1 parent 9fca826 commit 4adbf9e

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
/** Provides common utility methods to provide most often needed answers from a workflow */
2727
public final class WorkflowUtils {
2828
private static final int DEFAULT_STARTING_STATE_POSITION = 0;
29+
2930
/**
3031
* Gets State matching Start state.If start is not present returns first state otherwise returns
3132
* null
@@ -70,16 +71,16 @@ public static List<State> getStates(Workflow workflow, DefaultState.Type stateTy
7071
* @return {@code List<io.serverlessworkflow.api.events.EventDefinition>}. Returns {@code NULL}
7172
* when workflow is null or when workflow does not contain events
7273
*/
73-
public static List<EventDefinition> getConsumedEvents(Workflow workflow) {
74-
return getEvents(workflow, EventDefinition.Kind.CONSUMED);
74+
public static List<EventDefinition> getDefinedConsumedEvents(Workflow workflow) {
75+
return getDefinedEvents(workflow, EventDefinition.Kind.CONSUMED);
7576
}
7677

7778
/**
7879
* @return {@code List<io.serverlessworkflow.api.events.EventDefinition>}. Returns {@code NULL}
7980
* when workflow is null or when workflow does not contain events
8081
*/
81-
public static List<EventDefinition> getProducedEvents(Workflow workflow) {
82-
return getEvents(workflow, EventDefinition.Kind.PRODUCED);
82+
public static List<EventDefinition> getDefinedProducedEvents(Workflow workflow) {
83+
return getDefinedEvents(workflow, EventDefinition.Kind.PRODUCED);
8384
}
8485

8586
/**
@@ -89,7 +90,8 @@ public static List<EventDefinition> getProducedEvents(Workflow workflow) {
8990
* @return {@code List<io.serverlessworkflow.api.events.EventDefinition>}. Returns {@code NULL}
9091
* when workflow is null or when workflow does not contain events
9192
*/
92-
public static List<EventDefinition> getEvents(Workflow workflow, EventDefinition.Kind eventKind) {
93+
public static List<EventDefinition> getDefinedEvents(
94+
Workflow workflow, EventDefinition.Kind eventKind) {
9395
if (workflow == null || workflow.getEvents() == null) {
9496
return null;
9597
}
@@ -102,4 +104,20 @@ public static List<EventDefinition> getEvents(Workflow workflow, EventDefinition
102104
.filter(eventDef -> eventDef.getKind() == eventKind)
103105
.collect(Collectors.toList());
104106
}
107+
108+
/** @return {@code int} Returns count of defined event count matching eventKind */
109+
public static int getDefinedEventsCount(Workflow workflow, EventDefinition.Kind eventKind) {
110+
List<EventDefinition> definedEvents = getDefinedEvents(workflow, eventKind);
111+
return definedEvents == null ? 0 : definedEvents.size();
112+
}
113+
114+
/** @return {@code int} Returns count of Defined Consumed Event Count */
115+
public static int getDefinedConsumedEventsCount(Workflow workflow) {
116+
return getDefinedEventsCount(workflow, EventDefinition.Kind.CONSUMED);
117+
}
118+
119+
/** @return {@code int} Returns count of Defined Produced Event Count */
120+
public static int getDefinedProducedEventsCount(Workflow workflow) {
121+
return getDefinedEventsCount(workflow, EventDefinition.Kind.PRODUCED);
122+
}
105123
}

utils/src/test/java/io/serverlessworkflow/util/EventsTest.java renamed to utils/src/test/java/io/serverlessworkflow/util/DefinedEventsTest.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,52 @@
2828
import org.junit.jupiter.params.ParameterizedTest;
2929
import org.junit.jupiter.params.provider.ValueSource;
3030

31-
class EventsTest {
31+
class DefinedEventsTest {
3232
@ParameterizedTest
3333
@ValueSource(strings = {"/events/workflowwithevents.yml"})
34-
public void testGetConsumedEvents(String workflowEvents) {
34+
public void testGetDefinedConsumedEvents(String workflowEvents) {
3535
int consumedEventsCount = 2;
3636
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
37-
List<EventDefinition> consumedEvents = WorkflowUtils.getConsumedEvents(workflow);
37+
List<EventDefinition> consumedEvents = WorkflowUtils.getDefinedConsumedEvents(workflow);
3838
assertEquals(consumedEventsCount, consumedEvents.size());
3939
}
4040

4141
@ParameterizedTest
4242
@ValueSource(strings = {"/events/workflowwithevents.yml"})
43-
public void testGetProducedEvents(String workflowEvents) {
43+
public void testGetDefinedroducedEvents(String workflowEvents) {
4444
int producedEventsCounts = 1;
4545
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
46-
List<EventDefinition> producedEvents = WorkflowUtils.getProducedEvents(workflow);
46+
List<EventDefinition> producedEvents = WorkflowUtils.getDefinedProducedEvents(workflow);
4747
assertEquals(producedEventsCounts, producedEvents.size());
4848
}
4949

50+
@ParameterizedTest
51+
@ValueSource(strings = {"/events/workflowwithevents.yml"})
52+
public void testGetDefinedConsumedEventsCount(String workflowEvents) {
53+
int consumedEventsCountExpected = 2;
54+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
55+
int consumedEventsCount = WorkflowUtils.getDefinedConsumedEventsCount(workflow);
56+
assertEquals(consumedEventsCountExpected, consumedEventsCount);
57+
}
58+
59+
@ParameterizedTest
60+
@ValueSource(strings = {"/events/workflowwithevents.yml"})
61+
public void testGetDefinedroducedEventsCount(String workflowEvents) {
62+
int producedEventsCountExpected = 1;
63+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
64+
int producedEventsCount = WorkflowUtils.getDefinedProducedEventsCount(workflow);
65+
assertEquals(producedEventsCountExpected, producedEventsCount);
66+
}
67+
68+
@Test
69+
public void testGetDefinedEventsForNullWorkflow() {
70+
assertNull(WorkflowUtils.getDefinedEvents(null, EventDefinition.Kind.CONSUMED));
71+
}
72+
5073
@Test
51-
public void testGetEventsForNullWorkflow() {
52-
assertNull(WorkflowUtils.getEvents(null, EventDefinition.Kind.CONSUMED));
74+
public void testGetDefinedEventsCountForNullWorkflow() {
75+
int expectedCount = 0;
76+
assertEquals(
77+
expectedCount, WorkflowUtils.getDefinedEventsCount(null, EventDefinition.Kind.PRODUCED));
5378
}
5479
}

0 commit comments

Comments
 (0)