-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test-groovy-dsl-workflow-action
- Loading branch information
1 parent
4f5d4cf
commit e858cc1
Showing
6 changed files
with
265 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...orkflow-action/src/test/java/fr/an/tests/testdslworkflowaction/springboot/AppParam2s.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package fr.an.tests.testdslworkflowaction.springboot; | ||
|
||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@ConfigurationProperties(prefix = "app") | ||
@Getter @Setter | ||
public class AppParam2s { | ||
|
||
private String prop1; | ||
|
||
private List<Map<String, Object>> workflows; | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...workflow-action/src/test/java/fr/an/tests/testdslworkflowaction/springboot/AppParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package fr.an.tests.testdslworkflowaction.springboot; | ||
|
||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.Getter; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "app") | ||
@Getter | ||
public class AppParams { | ||
|
||
private String prop1; | ||
|
||
private List<Map<String, Object>> workflows; | ||
|
||
public AppParams() { | ||
} | ||
|
||
public void setProp1(String prop1) { | ||
this.prop1 = prop1; | ||
} | ||
|
||
public void setWorkflows(List<Map<String, Object>> workflows) { | ||
this.workflows = workflows; | ||
} | ||
|
||
|
||
} |
139 changes: 139 additions & 0 deletions
139
...ava/fr/an/tests/testdslworkflowaction/springboot/SpringbootWorkflowConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package fr.an.tests.testdslworkflowaction.springboot; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import fr.an.tests.testdslworkflowaction.ast.AbstractWorkflowAction; | ||
import fr.an.tests.testdslworkflowaction.ast.WorkflowActionFactory; | ||
import lombok.val; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes=TestAppConfiguration.class) | ||
public class SpringbootWorkflowConfigurationTest { | ||
|
||
@Autowired | ||
private AppParams appParams; | ||
|
||
@Autowired | ||
private AppParam2s appParam2s; | ||
|
||
@Test | ||
public void testYamlConfig() { | ||
val factory = new WorkflowActionFactory(); | ||
String prop1 = appParams.getProp1(); | ||
Assert.assertEquals("value1", prop1); | ||
List<Map<String,Object>> wfs = appParams.getWorkflows(); | ||
List<AbstractWorkflowAction> actions = objToActions(wfs, factory); | ||
checkWorkflowAction(actions.get(0)); | ||
} | ||
|
||
@Test | ||
public void testYamlConfig2() { | ||
val factory = new WorkflowActionFactory(); | ||
String prop1 = appParam2s.getProp1(); | ||
Assert.assertEquals("value1", prop1); | ||
List<Map<String,Object>> wfs = appParam2s.getWorkflows(); | ||
List<AbstractWorkflowAction> actions = objToActions(wfs, factory); | ||
checkWorkflowAction(actions.get(0)); | ||
} | ||
|
||
protected List<AbstractWorkflowAction> objToActions(List<Map<String,Object>> srcs, WorkflowActionFactory factory) { | ||
val res = new ArrayList<AbstractWorkflowAction>(); | ||
for(val src : srcs) { | ||
res.add(objToAction(src, factory)); | ||
} | ||
return res; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected <T> List<T> toList(Object src) { | ||
val res = new ArrayList<T>(); | ||
if (!(src instanceof Map)) { | ||
throw new RuntimeException("expecting Map, with keys \"0\", \"1\".. "); | ||
} | ||
val srcItems = (Map<String,T>) src; | ||
for(int i = 0; ; i++) { | ||
String key = Integer.toString(i); | ||
val srcItem = (Map<String,T>) srcItems.get(key); | ||
if (srcItem == null) { | ||
break; | ||
} | ||
res.add((T) srcItem); | ||
} | ||
return res; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected AbstractWorkflowAction objToAction(Map<String,Object> src, WorkflowActionFactory factory) { | ||
AbstractWorkflowAction res; | ||
String type = (String) src.get("type"); | ||
String name = (String) src.get("name"); | ||
if (name == null) { | ||
name = "<name>"; | ||
} | ||
switch(type) { | ||
case "parallel": { | ||
val branchItems = new LinkedHashMap<String,AbstractWorkflowAction>(); | ||
List<Map<String,Object>> srcBranchItems = toList(src.get("branchItems")); | ||
for(val srcBranchItem: srcBranchItems) { | ||
val branchName = (String) srcBranchItem.get("branchName"); | ||
val srcItem = (Map<String,Object>) srcBranchItem.get("item"); | ||
val item = objToAction(srcItem, factory); | ||
branchItems.put(branchName, item); | ||
} | ||
res = factory.parallel(name, branchItems); | ||
} break; | ||
case "sequence": { | ||
List<Map<String,Object>> srcItems = toList(src.get("items")); | ||
val items = objToActions(srcItems, factory); | ||
res = factory.sequence(name, items); | ||
} break; | ||
case "simple1": { | ||
String param = (String) src.get("param"); | ||
res = factory.simple1(name, param); | ||
} break; | ||
case "simple2": { | ||
int intParam = toInt(src.get("intParam"), 0); | ||
res = factory.simple2(name, intParam); | ||
} break; | ||
default: | ||
res = null; | ||
} | ||
return res; | ||
} | ||
|
||
private int toInt(Object src, int defaultValue) { | ||
int intParam = 0; | ||
if (src == null) { | ||
intParam = 0; | ||
} else if (src instanceof Number) { | ||
intParam = ((Number) src).intValue(); | ||
} else if (src instanceof String) { | ||
intParam = Integer.parseInt((String) src); | ||
} else { | ||
intParam = 0; | ||
// unrecognized type | ||
} | ||
return intParam; | ||
} | ||
|
||
private void checkWorkflowAction(AbstractWorkflowAction wf) { | ||
val buffer = new ByteArrayOutputStream(); | ||
try(val out = new PrintStream(buffer)) { | ||
wf.printIndent(out, 0); | ||
} | ||
String dumpText = buffer.toString(); | ||
System.out.println("Wf:\n" + dumpText); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...tion/src/test/java/fr/an/tests/testdslworkflowaction/springboot/TestAppConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package fr.an.tests.testdslworkflowaction.springboot; | ||
|
||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@EnableAutoConfiguration | ||
@EnableConfigurationProperties(AppParams.class) | ||
// @ComponentScan("fr.an.tests.testdslworkflowaction.springboot") | ||
public class TestAppConfiguration { | ||
|
||
@Bean | ||
public AppParam2s appParam2s() { | ||
return new AppParam2s(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
test-groovy-dsl-workflow-action/src/test/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
app: | ||
prop1: value1 | ||
workflows: | ||
- type: parallel | ||
name: p1 | ||
branchItems: | ||
- branchName: branch0 | ||
item: | ||
type: sequence | ||
items: | ||
- type: simple1 | ||
name: p1_branch0_item0 | ||
param: "param1" | ||
- type: simple2 | ||
name: p1_branch0_item1 | ||
intParam: 1 | ||
- branchName: branch1 | ||
item: | ||
type: sequence | ||
items: | ||
- type: simple1 | ||
name: p1_branch1_item0 | ||
param: "param2" | ||
- type: simple2 | ||
name: p1_branch1_item1 | ||
intParam: 2 | ||
- branchName: branch2 | ||
item: | ||
type: simple2 | ||
name: p1_branch2 | ||
intParam: 3 | ||
|
||
|