Skip to content

Commit 1c7329a

Browse files
committed
Implemented GetworkflowProducedEvents and GetworkflowProducedEventCount utility methods
Signed-off-by: manick02 <[email protected]>
1 parent 13c09d2 commit 1c7329a

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.serverlessworkflow.utils;
1717

1818
import io.serverlessworkflow.api.Workflow;
19+
import io.serverlessworkflow.api.end.End;
1920
import io.serverlessworkflow.api.events.EventDefinition;
2021
import io.serverlessworkflow.api.interfaces.State;
2122
import io.serverlessworkflow.api.start.Start;
@@ -145,7 +146,22 @@ public static List<EventDefinition> getWorkflowConsumedEvents(Workflow workflow)
145146
* @return Returns {@code List<EventDefinition>}
146147
*/
147148
public static List<EventDefinition> getWorkflowProducedEvents(Workflow workflow) {
148-
return null;
149+
if (workflow == null || workflow.getStates() == null || workflow.getStates().size() == 0) {
150+
return null;
151+
}
152+
List<EventDefinition> definedProducedEvents =
153+
getDefinedEvents(workflow, EventDefinition.Kind.PRODUCED);
154+
Set<String> uniqueEvents = new HashSet<>();
155+
for (State state : workflow.getStates()) {
156+
End end = state.getEnd();
157+
if (end != null || end.getProduceEvents() != null || end.getProduceEvents().size() != 0) {
158+
end.getProduceEvents()
159+
.forEach(produceEvent -> uniqueEvents.add(produceEvent.getEventRef()));
160+
}
161+
}
162+
return definedProducedEvents.stream()
163+
.filter(eventDefinition -> uniqueEvents.contains(eventDefinition.getName()))
164+
.collect(Collectors.toList());
149165
}
150166

151167
/**
@@ -202,4 +218,13 @@ public static int getWorkflowConsumedEventsCount(Workflow workflow) {
202218
List<EventDefinition> workflowConsumedEvents = getWorkflowConsumedEvents(workflow);
203219
return workflowConsumedEvents == null ? 0 : workflowConsumedEvents.size();
204220
}
221+
222+
/**
223+
* @return Returns {@code int} Count of the workflow produced events. <strong>Does not</strong>
224+
* consider sub-workflows in the count
225+
*/
226+
public static int getWorkflowProducedEventsCount(Workflow workflow) {
227+
List<EventDefinition> workflowProducedEvents = getWorkflowProducedEvents(workflow);
228+
return workflowProducedEvents == null ? 0 : workflowProducedEvents.size();
229+
}
205230
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,27 @@ public void testGetDefinedConsumedEventsCount(String workflowEvents) {
5050
int workflowConsumedEventsCount = WorkflowUtils.getWorkflowConsumedEventsCount(workflow);
5151
Arrays.asList(expectedEventsCount, workflowConsumedEventsCount);
5252
}
53+
54+
@ParameterizedTest
55+
@ValueSource(strings = {"/events/workflowwithproducedevents.yml"})
56+
public void testGetWorkflowProducedEvents(String workflowProducedEvents) {
57+
int expectedEventsCount = 1;
58+
Collection<String> expectedProducedEvent = Arrays.asList("ApplicationSubmitted");
59+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowProducedEvents);
60+
List<EventDefinition> producedEvents = WorkflowUtils.getWorkflowProducedEvents(workflow);
61+
assertNotNull(producedEvents);
62+
assertEquals(expectedEventsCount, producedEvents.size());
63+
for (EventDefinition producedEvent : producedEvents) {
64+
assertTrue(expectedProducedEvent.contains(producedEvent.getName()));
65+
}
66+
}
67+
68+
@ParameterizedTest
69+
@ValueSource(strings = {"/events/workflowwithproducedevents.yml"})
70+
public void testGetWorkflowProducedEventsCount(String workflowProducedEvents) {
71+
int expectedEventsCount = 1;
72+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowProducedEvents);
73+
int producedEventsCount = WorkflowUtils.getWorkflowProducedEventsCount(workflow);
74+
assertEquals(expectedEventsCount, producedEventsCount);
75+
}
5376
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
kind: produced
10+
correlation:
11+
- contextAttributeName: applicantId
12+
- name: SATScoresReceived
13+
type: org.application.satscores
14+
source: applicationsource
15+
correlation:
16+
- contextAttributeName: applicantId
17+
- name: RecommendationLetterReceived
18+
type: org.application.recommendationLetter
19+
source: applicationsource
20+
correlation:
21+
- contextAttributeName: applicantId
22+
functions:
23+
- name: finalizeApplicationFunction
24+
operation: http://myapis.org/collegeapplicationapi.json#finalize
25+
states:
26+
- name: FinalizeApplication
27+
type: event
28+
exclusive: false
29+
onEvents:
30+
- eventRefs:
31+
- ApplicationSubmitted
32+
- SATScoresReceived
33+
- RecommendationLetterReceived
34+
actions:
35+
- functionRef:
36+
refName: finalizeApplicationFunction
37+
arguments:
38+
student: "${ .applicantId }"
39+
end:
40+
terminate: true
41+
produceEvents:
42+
- eventRef: ApplicationSubmitted
43+
data: "${ .provisionedOrders }"
44+
45+
- name: CancelApplication
46+
type: event
47+
exclusive: false
48+
onEvents:
49+
- eventRefs:
50+
- ApplicationSubmitted
51+
- SATScoresReceived
52+
- RecommendationLetterReceived
53+
actions:
54+
- functionRef:
55+
refName: finalizeApplicationFunction
56+
arguments:
57+
student: "${ .applicantId }"
58+
end:
59+
terminate: true

0 commit comments

Comments
 (0)