Skip to content

Commit 0ebdc3a

Browse files
manick02cb-manick
authored andcommitted
Added Utils Module
Defined Workflows class - Workflows is the utility class for workflows Added a getStartingState utility method to get starting state Signed-off-by: cb-manick <[email protected]>
1 parent 8662d36 commit 0ebdc3a

File tree

7 files changed

+345
-0
lines changed

7 files changed

+345
-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: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 :: Util</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+
<dependency>
62+
<groupId>com.adelean</groupId>
63+
<artifactId>inject-resources-core</artifactId>
64+
<version>0.1.0</version>
65+
<scope>test</scope>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>com.adelean</groupId>
70+
<artifactId>inject-resources-junit-jupiter</artifactId>
71+
<version>0.1.0</version>
72+
<scope>test</scope>
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-checkstyle-plugin</artifactId>
81+
<configuration>
82+
<checkstyleRules>
83+
<module name="Checker">
84+
<module name="RegexpHeader">
85+
<property name="header" value="${checkstyle.header.template}"/>
86+
<property name="fileExtensions" value="${checkstyle.header.extensions}"/>
87+
</module>
88+
<module name="TreeWalker">
89+
<module name="RegexpSinglelineJava">
90+
<property name="format" value="@author"/>
91+
<property name="message" value="No @author tag allowed"/>
92+
</module>
93+
</module>
94+
</module>
95+
</checkstyleRules>
96+
<outputFile>${project.build.directory}/checkstyle.log</outputFile>
97+
<includeTestSourceDirectory>true</includeTestSourceDirectory>
98+
<includeResources>true</includeResources>
99+
<includeTestResources>true</includeTestResources>
100+
<consoleOutput>false</consoleOutput>
101+
<failsOnError>false</failsOnError>
102+
<logViolationsToConsole>${checkstyle.logViolationsToConsole}</logViolationsToConsole>
103+
<failOnViolation>${checkstyle.failOnViolation}</failOnViolation>
104+
<sourceDirectories>
105+
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
106+
<sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
107+
</sourceDirectories>
108+
</configuration>
109+
<executions>
110+
<execution>
111+
<phase>compile</phase>
112+
<goals>
113+
<goal>check</goal>
114+
</goals>
115+
</execution>
116+
</executions>
117+
</plugin>
118+
</plugins>
119+
</build>
120+
<!--inject-resources-junit-repository-->
121+
<repositories>
122+
<repository>
123+
<snapshots>
124+
<enabled>false</enabled>
125+
</snapshots>
126+
<id>central</id>
127+
<name>bintray</name>
128+
<url>https://jcenter.bintray.com</url>
129+
</repository>
130+
</repositories>
131+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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;
17+
18+
import io.serverlessworkflow.api.Workflow;
19+
import io.serverlessworkflow.api.interfaces.State;
20+
import io.serverlessworkflow.api.start.Start;
21+
22+
import java.util.Objects;
23+
import java.util.Optional;
24+
25+
/**
26+
* Provides common utility methods to answer most often needed answers from a workflow
27+
*/
28+
public final class Workflows {
29+
30+
/**
31+
* Gets Starting State <
32+
* If start is not present returns first state
33+
* Else returns state matching first
34+
*
35+
* @param workflow workflow
36+
* @return {@code state}
37+
*/
38+
public static Optional<State> getStartingState(Workflow workflow) {
39+
Objects.nonNull(workflow);
40+
Start start = workflow.getStart();
41+
if (start == null) {
42+
return workflow.getStates().stream().findFirst();
43+
} else {
44+
return workflow.
45+
getStates().
46+
stream().filter(state -> state.getName().equals(start.getStateName()))
47+
.findFirst();
48+
49+
}
50+
}
51+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 com.adelean.inject.resources.junit.jupiter.GivenTextResource;
20+
import com.adelean.inject.resources.junit.jupiter.TestWithResources;
21+
import io.serverlessworkflow.api.Workflow;
22+
import io.serverlessworkflow.api.interfaces.State;
23+
import io.serverlessworkflow.util.testutil.TestUtils;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.Test;
26+
27+
import java.util.Optional;
28+
29+
@TestWithResources
30+
class StartStateTest {
31+
32+
@GivenTextResource("/start/workflowwithstartstate.yml")
33+
static String WORKFLOW_WITH_START_SPECIFIED;
34+
35+
@GivenTextResource("/start/workflowwithstartnotspecified.yml")
36+
static String WORKFLOW_WITH_START_NOT_SPECIFIED;
37+
38+
@Test
39+
public void testGetStartState() {
40+
Workflow workflow = TestUtils.createWorkflow(WORKFLOW_WITH_START_SPECIFIED);
41+
Optional<State> startingState = Workflows.getStartingState(workflow);
42+
Assertions.assertTrue(startingState.isPresent());
43+
Assertions.assertEquals(startingState.get().getName(), workflow.getStart().getStateName());
44+
}
45+
46+
@Test
47+
public void testGetStartStateForWorkflowWithStartNotSpecified() {
48+
Workflow workflow = TestUtils.createWorkflow(WORKFLOW_WITH_START_NOT_SPECIFIED);
49+
Optional<State> startingState = Workflows.getStartingState(workflow);
50+
Assertions.assertTrue(startingState.isPresent());
51+
Assertions.assertEquals(workflow.getStates().get(0).getName(), startingState.get().getName());
52+
}
53+
54+
@Test
55+
public void testGetStateForNullWorkflow() {
56+
Assertions.assertThrows(NullPointerException.class, () -> Workflows.getStartingState(null));
57+
}
58+
}
59+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
20+
public class TestUtils {
21+
public static Workflow createWorkflow(String source) {
22+
return Workflow.fromSource(source);
23+
}
24+
}
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
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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
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

0 commit comments

Comments
 (0)