Skip to content

Commit 9fca826

Browse files
committed
Implemented Utility Method- GetConsumedEvents and GetProducedEvents
Signed-off-by: manick02 <[email protected]>
1 parent 4be73bb commit 9fca826

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

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

Lines changed: 38 additions & 0 deletions
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.events.EventDefinition;
1920
import io.serverlessworkflow.api.interfaces.State;
2021
import io.serverlessworkflow.api.start.Start;
2122
import io.serverlessworkflow.api.states.DefaultState;
@@ -64,4 +65,41 @@ public static List<State> getStates(Workflow workflow, DefaultState.Type stateTy
6465
.filter(state -> state.getType() == stateType)
6566
.collect(Collectors.toList());
6667
}
68+
69+
/**
70+
* @return {@code List<io.serverlessworkflow.api.events.EventDefinition>}. Returns {@code NULL}
71+
* when workflow is null or when workflow does not contain events
72+
*/
73+
public static List<EventDefinition> getConsumedEvents(Workflow workflow) {
74+
return getEvents(workflow, EventDefinition.Kind.CONSUMED);
75+
}
76+
77+
/**
78+
* @return {@code List<io.serverlessworkflow.api.events.EventDefinition>}. Returns {@code NULL}
79+
* when workflow is null or when workflow does not contain events
80+
*/
81+
public static List<EventDefinition> getProducedEvents(Workflow workflow) {
82+
return getEvents(workflow, EventDefinition.Kind.PRODUCED);
83+
}
84+
85+
/**
86+
* Gets list of event definition matching eventKind
87+
*
88+
* @param workflow
89+
* @return {@code List<io.serverlessworkflow.api.events.EventDefinition>}. Returns {@code NULL}
90+
* when workflow is null or when workflow does not contain events
91+
*/
92+
public static List<EventDefinition> getEvents(Workflow workflow, EventDefinition.Kind eventKind) {
93+
if (workflow == null || workflow.getEvents() == null) {
94+
return null;
95+
}
96+
List<EventDefinition> eventDefs = workflow.getEvents().getEventDefs();
97+
if (eventDefs == null) {
98+
return null;
99+
}
100+
101+
return eventDefs.stream()
102+
.filter(eventDef -> eventDef.getKind() == eventKind)
103+
.collect(Collectors.toList());
104+
}
67105
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
import static org.junit.jupiter.api.Assertions.assertNull;
21+
22+
import io.serverlessworkflow.api.Workflow;
23+
import io.serverlessworkflow.api.events.EventDefinition;
24+
import io.serverlessworkflow.util.testutil.TestUtils;
25+
import io.serverlessworkflow.utils.WorkflowUtils;
26+
import java.util.List;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.ValueSource;
30+
31+
class EventsTest {
32+
@ParameterizedTest
33+
@ValueSource(strings = {"/events/workflowwithevents.yml"})
34+
public void testGetConsumedEvents(String workflowEvents) {
35+
int consumedEventsCount = 2;
36+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
37+
List<EventDefinition> consumedEvents = WorkflowUtils.getConsumedEvents(workflow);
38+
assertEquals(consumedEventsCount, consumedEvents.size());
39+
}
40+
41+
@ParameterizedTest
42+
@ValueSource(strings = {"/events/workflowwithevents.yml"})
43+
public void testGetProducedEvents(String workflowEvents) {
44+
int producedEventsCounts = 1;
45+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
46+
List<EventDefinition> producedEvents = WorkflowUtils.getProducedEvents(workflow);
47+
assertEquals(producedEventsCounts, producedEvents.size());
48+
}
49+
50+
@Test
51+
public void testGetEventsForNullWorkflow() {
52+
assertNull(WorkflowUtils.getEvents(null, EventDefinition.Kind.CONSUMED));
53+
}
54+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
42+
- name: CancelApplication
43+
type: event
44+
exclusive: false
45+
onEvents:
46+
- eventRefs:
47+
- ApplicationSubmitted
48+
- SATScoresReceived
49+
- RecommendationLetterReceived
50+
actions:
51+
- functionRef:
52+
refName: finalizeApplicationFunction
53+
arguments:
54+
student: "${ .applicantId }"
55+
end:
56+
terminate: true

0 commit comments

Comments
 (0)