Skip to content

Commit 4ccdfc3

Browse files
authored
Merge pull request #130 from manick02/workflow-util
#127 Workflow util
2 parents 8662d36 + ebcd89b commit 4ccdfc3

File tree

8 files changed

+395
-0
lines changed

8 files changed

+395
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<module>spi</module>
3030
<module>validation</module>
3131
<module>diagram</module>
32+
<module>utils</module>
3233
</modules>
3334

3435
<properties>

utils/pom.xml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>io.serverlessworkflow</groupId>
8+
<artifactId>serverlessworkflow-parent</artifactId>
9+
<version>4.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>serverlessworkflow-util</artifactId>
13+
<name>Serverless Workflow :: Utils</name>
14+
<version>${project.parent.version}</version>
15+
<packaging>jar</packaging>
16+
<description>Java SDK for Serverless Workflow Specification</description>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.slf4j</groupId>
21+
<artifactId>slf4j-api</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>io.serverlessworkflow</groupId>
26+
<artifactId>serverlessworkflow-api</artifactId>
27+
<version>${project.version}</version>
28+
</dependency>
29+
30+
<!-- test -->
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter-api</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-engine</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter-params</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.mockito</groupId>
48+
<artifactId>mockito-core</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>ch.qos.logback</groupId>
53+
<artifactId>logback-classic</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.assertj</groupId>
58+
<artifactId>assertj-core</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
</dependencies>
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-checkstyle-plugin</artifactId>
68+
<configuration>
69+
<checkstyleRules>
70+
<module name="Checker">
71+
<module name="RegexpHeader">
72+
<property name="header" value="${checkstyle.header.template}"/>
73+
<property name="fileExtensions" value="${checkstyle.header.extensions}"/>
74+
</module>
75+
<module name="TreeWalker">
76+
<module name="RegexpSinglelineJava">
77+
<property name="format" value="@author"/>
78+
<property name="message" value="No @author tag allowed"/>
79+
</module>
80+
</module>
81+
</module>
82+
</checkstyleRules>
83+
<outputFile>${project.build.directory}/checkstyle.log</outputFile>
84+
<includeTestSourceDirectory>true</includeTestSourceDirectory>
85+
<includeResources>true</includeResources>
86+
<includeTestResources>true</includeTestResources>
87+
<consoleOutput>false</consoleOutput>
88+
<failsOnError>false</failsOnError>
89+
<logViolationsToConsole>${checkstyle.logViolationsToConsole}</logViolationsToConsole>
90+
<failOnViolation>${checkstyle.failOnViolation}</failOnViolation>
91+
<sourceDirectories>
92+
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
93+
<sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
94+
</sourceDirectories>
95+
</configuration>
96+
<executions>
97+
<execution>
98+
<phase>compile</phase>
99+
<goals>
100+
<goal>check</goal>
101+
</goals>
102+
</execution>
103+
</executions>
104+
</plugin>
105+
<plugin>
106+
<groupId>com.coveo</groupId>
107+
<artifactId>fmt-maven-plugin</artifactId>
108+
<configuration>
109+
<sourceDirectory>src/main/java</sourceDirectory>
110+
<testSourceDirectory>src/test/java</testSourceDirectory>
111+
<verbose>false</verbose>
112+
<filesNamePattern>.*\.java</filesNamePattern>
113+
<skip>false</skip>
114+
<skipSortingImports>false</skipSortingImports>
115+
<style>google</style>
116+
</configuration>
117+
<executions>
118+
<execution>
119+
<goals>
120+
<goal>format</goal>
121+
</goals>
122+
</execution>
123+
</executions>
124+
</plugin>
125+
</plugins>
126+
</build>
127+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
package io.serverlessworkflow.utils;
17+
18+
import io.serverlessworkflow.api.Workflow;
19+
import io.serverlessworkflow.api.interfaces.State;
20+
import io.serverlessworkflow.api.start.Start;
21+
22+
/** Provides common utility methods to provide most often needed answers from a workflow */
23+
public final class WorkflowUtils {
24+
private static int DEFAULT_STARTING_STATE_POSITION = 0;
25+
/**
26+
* Gets State matching Start state.If start is not present returns first state otherwise returns
27+
* null
28+
*
29+
* @param workflow workflow
30+
* @return {@code state} when present else returns {@code null}
31+
*/
32+
public static State getStartingState(Workflow workflow) {
33+
if (workflow == null || workflow.getStates() == null || workflow.getStates().isEmpty()) {
34+
return null;
35+
}
36+
37+
Start start = workflow.getStart();
38+
if (start == null) {
39+
return workflow.getStates().get(DEFAULT_STARTING_STATE_POSITION);
40+
} else {
41+
return workflow.getStates().stream()
42+
.filter(state -> state.getName().equals(start.getStateName()))
43+
.findFirst()
44+
.get();
45+
}
46+
}
47+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.interfaces.State;
23+
import io.serverlessworkflow.util.testutil.TestUtils;
24+
import io.serverlessworkflow.utils.WorkflowUtils;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.ValueSource;
28+
29+
class StartStateTest {
30+
31+
@ParameterizedTest
32+
@ValueSource(strings = {"/start/workflowwithstartstate.yml"})
33+
public void testGetStartState(String workflowWithStartState) {
34+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowWithStartState);
35+
State startingState = WorkflowUtils.getStartingState(workflow);
36+
assertNotNull(startingState);
37+
assertEquals(startingState.getName(), workflow.getStart().getStateName());
38+
}
39+
40+
@ParameterizedTest
41+
@ValueSource(strings = {"/start/workflowwithstartnotspecified.yml"})
42+
public void testGetStartStateForWorkflowWithStartNotSpecified(
43+
String workflowWithStartStateNotSpecified) {
44+
Workflow workflow =
45+
TestUtils.createWorkflowFromTestResource(workflowWithStartStateNotSpecified);
46+
State startingState = WorkflowUtils.getStartingState(workflow);
47+
assertEquals(workflow.getStates().get(0).getName(), startingState.getName());
48+
}
49+
50+
@ParameterizedTest
51+
@ValueSource(strings = {"/start/workflowwithnostate.yml"})
52+
public void testGetStartStateForWorkflowWithNoState(String workflowWithNoState) {
53+
Workflow workflow = TestUtils.createWorkflowFromTestResource(workflowWithNoState);
54+
State startingState = WorkflowUtils.getStartingState(workflow);
55+
assertNull(startingState);
56+
}
57+
58+
@Test
59+
public void testGetStateForNullWorkflow() {
60+
State startingState = WorkflowUtils.getStartingState(null);
61+
assertNull(startingState);
62+
}
63+
}
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+
package io.serverlessworkflow.util.testutil;
17+
18+
import io.serverlessworkflow.api.Workflow;
19+
import java.io.IOException;
20+
import java.io.InputStreamReader;
21+
import java.io.Reader;
22+
23+
public class TestUtils {
24+
25+
public static Workflow createWorkflow(String source) {
26+
return Workflow.fromSource(source);
27+
}
28+
29+
public static Workflow createWorkflowFromTestResource(String fileRelativePath) {
30+
InputStreamReader reader = getTestResourceStreamReader(fileRelativePath);
31+
return createWorkflow(readFileAsString(reader));
32+
}
33+
34+
public static String readFileAsString(Reader reader) {
35+
try {
36+
StringBuilder fileData = new StringBuilder(1000);
37+
char[] buf = new char[1024];
38+
int numRead;
39+
while ((numRead = reader.read(buf)) != -1) {
40+
String readData = String.valueOf(buf, 0, numRead);
41+
fileData.append(readData);
42+
buf = new char[1024];
43+
}
44+
reader.close();
45+
return fileData.toString();
46+
} catch (IOException e) {
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
51+
private static InputStreamReader getTestResourceStreamReader(String fileRelativePath) {
52+
return new InputStreamReader(TestUtils.class.getResourceAsStream(fileRelativePath));
53+
}
54+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
id: finalizeCollegeApplication
2+
name: Finalize College Application
3+
version: '1.0'
4+
specVersion: '0.7'
5+
start: FinalizeApplication
6+
events:
7+
- name: ApplicationSubmitted
8+
type: org.application.submitted
9+
source: applicationsource
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
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
correlation:
10+
- contextAttributeName: applicantId
11+
- name: SATScoresReceived
12+
type: org.application.satscores
13+
source: applicationsource
14+
correlation:
15+
- contextAttributeName: applicantId
16+
- name: RecommendationLetterReceived
17+
type: org.application.recommendationLetter
18+
source: applicationsource
19+
correlation:
20+
- contextAttributeName: applicantId
21+
functions:
22+
- name: finalizeApplicationFunction
23+
operation: http://myapis.org/collegeapplicationapi.json#finalize
24+
states:
25+
- name: FinalizeApplication
26+
type: event
27+
exclusive: false
28+
onEvents:
29+
- eventRefs:
30+
- ApplicationSubmitted
31+
- SATScoresReceived
32+
- RecommendationLetterReceived
33+
actions:
34+
- functionRef:
35+
refName: finalizeApplicationFunction
36+
arguments:
37+
student: "${ .applicantId }"
38+
end:
39+
terminate: true

0 commit comments

Comments
 (0)