Skip to content

Commit 698cf69

Browse files
manick02cb-manick
authored andcommitted
Renamed Workflows to WorkflowUtils
Added style guide Removed Inject-Resources dependency which was causing failure in CI Changed the way to read resource file Signed-off-by: cb-manick <[email protected]>
1 parent 067c961 commit 698cf69

File tree

6 files changed

+160
-109
lines changed

6 files changed

+160
-109
lines changed

utils/pom.xml

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>serverlessworkflow-util</artifactId>
13-
<name>Serverless Workflow :: Util</name>
13+
<name>Serverless Workflow :: Utils</name>
1414
<version>${project.parent.version}</version>
1515
<packaging>jar</packaging>
1616
<description>Java SDK for Serverless Workflow Specification</description>
@@ -58,19 +58,6 @@
5858
<artifactId>assertj-core</artifactId>
5959
<scope>test</scope>
6060
</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>
7461
</dependencies>
7562

7663
<build>
@@ -115,17 +102,26 @@
115102
</execution>
116103
</executions>
117104
</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>
118125
</plugins>
119126
</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>
131127
</project>

utils/src/main/java/io/serverlessworkflow/util/Workflows.java

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import java.util.Objects;
22+
23+
/** Provides common utility methods to provide most often needed answers from a workflow */
24+
public final class WorkflowUtils {
25+
26+
/**
27+
* Gets State matching Start state name If start is not present returns first state Returns null
28+
* otherwise
29+
*
30+
* @param workflow workflow
31+
* @return {@code state} when present else returns {@code null}
32+
*/
33+
public static State getStartingState(Workflow workflow) {
34+
Objects.requireNonNull(workflow);
35+
if (workflow.getStates() == null || workflow.getStates().isEmpty()) {
36+
return null;
37+
}
38+
39+
Start start = workflow.getStart();
40+
if (start == null) {
41+
return workflow.getStates().stream().findFirst().get();
42+
} else {
43+
return workflow.getStates().stream()
44+
.filter(state -> state.getName().equals(start.getStateName()))
45+
.findFirst()
46+
.get();
47+
}
48+
}
49+
}

utils/src/test/java/io/serverlessworkflow/util/StartStateTest.java

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,47 @@
1616

1717
package io.serverlessworkflow.util;
1818

19-
import com.adelean.inject.resources.junit.jupiter.GivenTextResource;
20-
import com.adelean.inject.resources.junit.jupiter.TestWithResources;
19+
import static org.junit.jupiter.api.Assertions.*;
20+
2121
import io.serverlessworkflow.api.Workflow;
2222
import io.serverlessworkflow.api.interfaces.State;
2323
import io.serverlessworkflow.util.testutil.TestUtils;
24-
import org.junit.jupiter.api.Assertions;
24+
import io.serverlessworkflow.utils.WorkflowUtils;
2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.ValueSource;
2628

27-
import java.util.Optional;
28-
29-
@TestWithResources
3029
class StartStateTest {
3130

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-
}
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+
assertThrows(NullPointerException.class, () -> WorkflowUtils.getStartingState(null));
61+
}
5862
}
59-

utils/src/test/java/io/serverlessworkflow/util/testutil/TestUtils.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,39 @@
1616
package io.serverlessworkflow.util.testutil;
1717

1818
import io.serverlessworkflow.api.Workflow;
19+
import java.io.IOException;
20+
import java.io.InputStreamReader;
21+
import java.io.Reader;
1922

2023
public class TestUtils {
24+
2125
public static Workflow createWorkflow(String source) {
2226
return Workflow.fromSource(source);
2327
}
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+
}
2454
}
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

0 commit comments

Comments
 (0)