Skip to content

Commit 336d0e2

Browse files
authored
Merge pull request #133 from manick02/utils-wf-events
Implemented Utility Method- GetDefinedConsumedEvents and GetDefinedProducedEvents #127
2 parents 4be73bb + 4adbf9e commit 336d0e2

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

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

Lines changed: 56 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;
@@ -25,6 +26,7 @@
2526
/** Provides common utility methods to provide most often needed answers from a workflow */
2627
public final class WorkflowUtils {
2728
private static final int DEFAULT_STARTING_STATE_POSITION = 0;
29+
2830
/**
2931
* Gets State matching Start state.If start is not present returns first state otherwise returns
3032
* null
@@ -64,4 +66,58 @@ public static List<State> getStates(Workflow workflow, DefaultState.Type stateTy
6466
.filter(state -> state.getType() == stateType)
6567
.collect(Collectors.toList());
6668
}
69+
70+
/**
71+
* @return {@code List<io.serverlessworkflow.api.events.EventDefinition>}. Returns {@code NULL}
72+
* when workflow is null or when workflow does not contain events
73+
*/
74+
public static List<EventDefinition> getDefinedConsumedEvents(Workflow workflow) {
75+
return getDefinedEvents(workflow, EventDefinition.Kind.CONSUMED);
76+
}
77+
78+
/**
79+
* @return {@code List<io.serverlessworkflow.api.events.EventDefinition>}. Returns {@code NULL}
80+
* when workflow is null or when workflow does not contain events
81+
*/
82+
public static List<EventDefinition> getDefinedProducedEvents(Workflow workflow) {
83+
return getDefinedEvents(workflow, EventDefinition.Kind.PRODUCED);
84+
}
85+
86+
/**
87+
* Gets list of event definition matching eventKind
88+
*
89+
* @param workflow
90+
* @return {@code List<io.serverlessworkflow.api.events.EventDefinition>}. Returns {@code NULL}
91+
* when workflow is null or when workflow does not contain events
92+
*/
93+
public static List<EventDefinition> getDefinedEvents(
94+
Workflow workflow, EventDefinition.Kind eventKind) {
95+
if (workflow == null || workflow.getEvents() == null) {
96+
return null;
97+
}
98+
List<EventDefinition> eventDefs = workflow.getEvents().getEventDefs();
99+
if (eventDefs == null) {
100+
return null;
101+
}
102+
103+
return eventDefs.stream()
104+
.filter(eventDef -> eventDef.getKind() == eventKind)
105+
.collect(Collectors.toList());
106+
}
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+
}
67123
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 DefinedEventsTest {
32+
@ParameterizedTest
33+
@ValueSource(strings = {"/events/workflowwithevents.yml"})
34+
public void testGetDefinedConsumedEvents(String workflowEvents) {
35+
int consumedEventsCount = 2;
36+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
37+
List<EventDefinition> consumedEvents = WorkflowUtils.getDefinedConsumedEvents(workflow);
38+
assertEquals(consumedEventsCount, consumedEvents.size());
39+
}
40+
41+
@ParameterizedTest
42+
@ValueSource(strings = {"/events/workflowwithevents.yml"})
43+
public void testGetDefinedroducedEvents(String workflowEvents) {
44+
int producedEventsCounts = 1;
45+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowEvents);
46+
List<EventDefinition> producedEvents = WorkflowUtils.getDefinedProducedEvents(workflow);
47+
assertEquals(producedEventsCounts, producedEvents.size());
48+
}
49+
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+
73+
@Test
74+
public void testGetDefinedEventsCountForNullWorkflow() {
75+
int expectedCount = 0;
76+
assertEquals(
77+
expectedCount, WorkflowUtils.getDefinedEventsCount(null, EventDefinition.Kind.PRODUCED));
78+
}
79+
}
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)