Skip to content

Commit 264f7e4

Browse files
authored
Merge pull request #134 from manick02/utils-wf-events-1
Utiliity method GetWorkflowConsumedEvent, GetWorkflowConsumedEventCount, GetWorkflowProducedEvent and GetWorkflowProducedEventCount#127
2 parents 336d0e2 + 62ef635 commit 264f7e4

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
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;
23+
import io.serverlessworkflow.api.states.CallbackState;
2224
import io.serverlessworkflow.api.states.DefaultState;
25+
import io.serverlessworkflow.api.states.EventState;
26+
import io.serverlessworkflow.api.states.SwitchState;
27+
import java.util.ArrayList;
28+
import java.util.HashSet;
2329
import java.util.List;
30+
import java.util.Set;
2431
import java.util.stream.Collectors;
2532

2633
/** Provides common utility methods to provide most often needed answers from a workflow */
@@ -120,4 +127,104 @@ public static int getDefinedConsumedEventsCount(Workflow workflow) {
120127
public static int getDefinedProducedEventsCount(Workflow workflow) {
121128
return getDefinedEventsCount(workflow, EventDefinition.Kind.PRODUCED);
122129
}
130+
131+
/**
132+
* Gets Consumed Events of parent workflow Iterates through states in parent workflow and collects
133+
* all the ConsumedEvents. Sub Workflows of the Workflow <strong>are not</strong> considered for
134+
* getting Consumed Events
135+
*
136+
* @return Returns {@code List<EventDefinition>}
137+
*/
138+
public static List<EventDefinition> getWorkflowConsumedEvents(Workflow workflow) {
139+
return getWorkflowEventDefinitions(workflow, EventDefinition.Kind.CONSUMED);
140+
}
141+
142+
/**
143+
* Gets Produced Events of parent workflow Iterates through states in parent workflow and collects
144+
* all the Produced Events.
145+
*
146+
* @return Returns {@code List<EventDefinition>}
147+
*/
148+
public static List<EventDefinition> getWorkflowProducedEvents(Workflow workflow) {
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());
165+
}
166+
167+
/**
168+
* Gets Events of parent workflow matching {@code EventDefinition.Kind} Iterates through states in
169+
* parent workflow and collects all the events matching {@code EventDefinition.Kind} .
170+
*
171+
* @return Returns {@code List<EventDefinition>}
172+
*/
173+
private static List<EventDefinition> getWorkflowEventDefinitions(
174+
Workflow workflow, EventDefinition.Kind eventKind) {
175+
if (workflow == null || workflow.getStates() == null || workflow.getStates().size() == 0) {
176+
return null;
177+
}
178+
List<EventDefinition> definedConsumedEvents = getDefinedEvents(workflow, eventKind);
179+
if (definedConsumedEvents == null) return null;
180+
Set<String> uniqEventReferences = new HashSet<>();
181+
List<String> eventReferencesFromState = getWorkflowConsumedEventsFromState(workflow);
182+
uniqEventReferences.addAll(eventReferencesFromState);
183+
return definedConsumedEvents.stream()
184+
.filter(x -> uniqEventReferences.contains(x.getName()))
185+
.collect(Collectors.toList());
186+
}
187+
188+
private static List<String> getWorkflowConsumedEventsFromState(Workflow workflow) {
189+
List<String> eventReferences = new ArrayList<>();
190+
for (State state : workflow.getStates()) {
191+
if (state instanceof SwitchState) {
192+
SwitchState switchState = (SwitchState) state;
193+
if (switchState.getEventConditions() != null) {
194+
switchState
195+
.getEventConditions()
196+
.forEach(eventCondition -> eventReferences.add(eventCondition.getEventRef()));
197+
}
198+
} else if (state instanceof CallbackState) {
199+
CallbackState callbackState = (CallbackState) state;
200+
if (callbackState.getEventRef() != null) eventReferences.add(callbackState.getEventRef());
201+
} else if (state instanceof EventState) {
202+
EventState eventState = (EventState) state;
203+
if (eventState.getOnEvents() != null) {
204+
eventState
205+
.getOnEvents()
206+
.forEach(onEvents -> eventReferences.addAll(onEvents.getEventRefs()));
207+
}
208+
}
209+
}
210+
return eventReferences;
211+
}
212+
213+
/**
214+
* @return Returns {@code int } Count of the workflow consumed events. <strong>Does not</strong>
215+
* consider sub-workflows
216+
*/
217+
public static int getWorkflowConsumedEventsCount(Workflow workflow) {
218+
List<EventDefinition> workflowConsumedEvents = getWorkflowConsumedEvents(workflow);
219+
return workflowConsumedEvents == null ? 0 : workflowConsumedEvents.size();
220+
}
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+
}
123230
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.*;
20+
21+
import io.serverlessworkflow.api.Workflow;
22+
import io.serverlessworkflow.api.events.EventDefinition;
23+
import io.serverlessworkflow.util.testutil.TestUtils;
24+
import io.serverlessworkflow.utils.WorkflowUtils;
25+
import java.util.*;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.ValueSource;
28+
29+
public class EventsTest {
30+
@ParameterizedTest
31+
@ValueSource(strings = {"/events/workflowwithevents.yml"})
32+
public void testGetDefinedConsumedEvents(String workflowEvents) {
33+
int expectedEventsCount = 2;
34+
Collection<String> expectedConsumedEvent =
35+
Arrays.asList("SATScoresReceived", "RecommendationLetterReceived");
36+
Set<String> uniqueExpectedConsumedEvent = new HashSet<>(expectedConsumedEvent);
37+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
38+
List<EventDefinition> consumedEvents = WorkflowUtils.getWorkflowConsumedEvents(workflow);
39+
assertEquals(expectedEventsCount, consumedEvents.size());
40+
for (EventDefinition consumedEvent : consumedEvents) {
41+
assertTrue(uniqueExpectedConsumedEvent.contains(consumedEvent.getName()));
42+
}
43+
}
44+
45+
@ParameterizedTest
46+
@ValueSource(strings = {"/events/workflowwithevents.yml"})
47+
public void testGetDefinedConsumedEventsCount(String workflowEvents) {
48+
int expectedEventsCount = 2;
49+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
50+
int workflowConsumedEventsCount = WorkflowUtils.getWorkflowConsumedEventsCount(workflow);
51+
Arrays.asList(expectedEventsCount, workflowConsumedEventsCount);
52+
}
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+
}
76+
}
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)