Skip to content

Commit 1edcda4

Browse files
committed
Implement Workflow consumed Event
Signed-off-by: manick02 <[email protected]>
1 parent 4adbf9e commit 1edcda4

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919
import io.serverlessworkflow.api.events.EventDefinition;
2020
import io.serverlessworkflow.api.interfaces.State;
2121
import io.serverlessworkflow.api.start.Start;
22+
import io.serverlessworkflow.api.states.CallbackState;
2223
import io.serverlessworkflow.api.states.DefaultState;
24+
import io.serverlessworkflow.api.states.EventState;
25+
import io.serverlessworkflow.api.states.SwitchState;
26+
import java.util.ArrayList;
27+
import java.util.HashSet;
2328
import java.util.List;
29+
import java.util.Set;
2430
import java.util.stream.Collectors;
2531

2632
/** Provides common utility methods to provide most often needed answers from a workflow */
@@ -120,4 +126,80 @@ public static int getDefinedConsumedEventsCount(Workflow workflow) {
120126
public static int getDefinedProducedEventsCount(Workflow workflow) {
121127
return getDefinedEventsCount(workflow, EventDefinition.Kind.PRODUCED);
122128
}
129+
130+
/**
131+
* Gets Consumed Events of parent workflow Iterates through states in parent workflow and collects
132+
* all the ConsumedEvents. Sub Workflows of the Workflow <strong>are not</strong> considered for
133+
* getting Consumed Events
134+
*
135+
* @return Returns {@code List<EventDefinition>}
136+
*/
137+
public static List<EventDefinition> getWorkflowConsumedEvents(Workflow workflow) {
138+
return getWorkflowEventDefinitions(workflow, EventDefinition.Kind.CONSUMED);
139+
}
140+
141+
/**
142+
* Gets Produced Events of parent workflow Iterates through states in parent workflow and collects
143+
* all the Produced Events.
144+
*
145+
* @return Returns {@code List<EventDefinition>}
146+
*/
147+
public static List<EventDefinition> getWorkflowProducedEvents(Workflow workflow) {
148+
return null;
149+
}
150+
151+
/**
152+
* Gets Events of parent workflow matching {@code EventDefinition.Kind} Iterates through states in
153+
* parent workflow and collects all the events matching {@code EventDefinition.Kind} .
154+
*
155+
* @return Returns {@code List<EventDefinition>}
156+
*/
157+
private static List<EventDefinition> getWorkflowEventDefinitions(
158+
Workflow workflow, EventDefinition.Kind eventKind) {
159+
if (workflow == null || workflow.getStates() == null || workflow.getStates().size() == 0) {
160+
return null;
161+
}
162+
List<EventDefinition> definedConsumedEvents = getDefinedEvents(workflow, eventKind);
163+
if (definedConsumedEvents == null) return null;
164+
Set<String> uniqEventReferences = new HashSet<>();
165+
List<String> eventReferencesFromState = getWorkflowConsumedEventsFromState(workflow);
166+
uniqEventReferences.addAll(eventReferencesFromState);
167+
return definedConsumedEvents.stream()
168+
.filter(x -> uniqEventReferences.contains(x.getName()))
169+
.collect(Collectors.toList());
170+
}
171+
172+
private static List<String> getWorkflowConsumedEventsFromState(Workflow workflow) {
173+
List<String> eventReferences = new ArrayList<>();
174+
for (State state : workflow.getStates()) {
175+
if (state instanceof SwitchState) {
176+
SwitchState switchState = (SwitchState) state;
177+
if (switchState.getEventConditions() != null) {
178+
switchState
179+
.getEventConditions()
180+
.forEach(eventCondition -> eventReferences.add(eventCondition.getEventRef()));
181+
}
182+
} else if (state instanceof CallbackState) {
183+
CallbackState callbackState = (CallbackState) state;
184+
eventReferences.add(callbackState.getEventRef());
185+
} else if (state instanceof EventState) {
186+
EventState eventState = (EventState) state;
187+
if (eventState.getOnEvents() != null) {
188+
eventState
189+
.getOnEvents()
190+
.forEach(onEvents -> eventReferences.addAll(onEvents.getEventRefs()));
191+
}
192+
}
193+
}
194+
return eventReferences;
195+
}
196+
197+
/**
198+
* @return Returns {@code int } Count of the workflow consumed events. <strong>Does not</strong>
199+
* consider sub-workflows
200+
*/
201+
public static int getWorkflowConsumedEventsCount(Workflow workflow) {
202+
List<EventDefinition> workflowConsumedEvents = getWorkflowConsumedEvents(workflow);
203+
return workflowConsumedEvents == null ? 0 : workflowConsumedEvents.size();
204+
}
123205
}
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.*;
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+
}

0 commit comments

Comments
 (0)