Skip to content

Commit

Permalink
added test-groovy-dsl-workflow-action
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-Nauwynck committed Mar 13, 2022
1 parent 4f5d4cf commit e858cc1
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 6 deletions.
28 changes: 22 additions & 6 deletions test-groovy-dsl-workflow-action/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@
<java.version>1.8</java.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.6.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>3.0.10</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>

Expand All @@ -31,26 +41,32 @@
<version>2.11.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0-alpha4</version>
<scope>runtime</scope>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
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;

}
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;
}


}
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);
}
}
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 test-groovy-dsl-workflow-action/src/test/resources/application.yml
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


0 comments on commit e858cc1

Please sign in to comment.