For #I8YNCB, 完善测试用例

This commit is contained in:
DaleLee
2024-02-02 14:50:31 +08:00
parent bf8894ad82
commit d555a2ab43
15 changed files with 474 additions and 48 deletions

View File

@@ -0,0 +1,139 @@
package com.yomahub.liteflow.test.script.javascript.getnodes;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.flow.element.Node;
import com.yomahub.liteflow.test.BaseTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 根据 chainId 获取节点测试
*/
@ExtendWith(SpringExtension.class)
@TestPropertySource(value = "classpath:/getnodes/application.properties")
@SpringBootTest(classes = LiteFlowScriptGetNodesJsELTest.class)
@EnableAutoConfiguration
@ComponentScan("com.yomahub.liteflow.test.script.javascript.getnodes.cmp")
public class LiteFlowScriptGetNodesJsELTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
@Test
public void getNodesTest1() {
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assertions.assertTrue(response.isSuccess());
List<Node> nodes = FlowBus.getNodesByChainId("chain1");
// 判断数量
Assertions.assertEquals(5, nodes.size());
// 判断 id
List<String> nodeIds = nodes.stream().map(Node::getId)
.collect(Collectors.toList());
List<String> targetIds = Arrays.asList("a", "b", "c", "s1", "s2");
for (String id : targetIds) {
Assertions.assertTrue(nodeIds.contains(id));
}
}
@Test
public void getNodesTest2() {
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
Assertions.assertTrue(response.isSuccess());
List<Node> nodes = FlowBus.getNodesByChainId("chain2");
// 判断总数量
Assertions.assertEquals(6, nodes.size());
// 判断 id 与数量
List<String> nodeIds = nodes.stream().map(Node::getId)
.collect(Collectors.toList());
Map<String, Integer> map = listToMap(nodeIds);
Map<String, Integer> targetMap = new HashMap<>();
targetMap.put("a", 3);
targetMap.put("b", 1);
targetMap.put("s1", 2);
Assertions.assertTrue(targetMap.equals(map));
// 判断 tag
List<String> nodeTags = nodes.stream().map(Node::getTag)
.collect(Collectors.toList());
List<String> targetTags = Arrays.asList("a1", "a2", "a3", "b1", "s11", "s12");
for (String id : targetTags) {
Assertions.assertTrue(nodeTags.contains(id));
}
}
@Test
public void getNodesTest3() {
LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
Assertions.assertTrue(response.isSuccess());
List<Node> nodes = FlowBus.getNodesByChainId("chain3");
// 判断总数量
Assertions.assertEquals(8, nodes.size());
// 判断 id 与数量
List<String> nodeIds = nodes.stream().map(Node::getId)
.collect(Collectors.toList());
Map<String, Integer> map = listToMap(nodeIds);
Map<String, Integer> targetMap = new HashMap<>();
targetMap.put("a", 2);
targetMap.put("b", 2);
targetMap.put("c", 1);
targetMap.put("f", 1);
targetMap.put("s1", 1);
targetMap.put("s2", 1);
Assertions.assertTrue(targetMap.equals(map));
}
@Test
public void getNodesTest4() {
LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg");
Assertions.assertTrue(response.isSuccess());
List<Node> nodes = FlowBus.getNodesByChainId("chain4");
// 判断数量
Assertions.assertEquals(5, nodes.size());
// 判断 id
List<String> nodeIds = nodes.stream().map(Node::getId)
.collect(Collectors.toList());
List<String> targetIds = Arrays.asList("a", "b", "c", "s");
for (String id : targetIds) {
Assertions.assertTrue(nodeIds.contains(id));
}
}
@Test
public void getNodesTest5() {
LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
Assertions.assertTrue(response.isSuccess());
List<Node> nodes = FlowBus.getNodesByChainId("chain5");
// 判断数量
Assertions.assertEquals(3, nodes.size());
// 判断 id
List<String> nodeIds = nodes.stream().map(Node::getId)
.collect(Collectors.toList());
List<String> targetIds = Arrays.asList("x", "y", "s1");
for (String id : targetIds) {
Assertions.assertTrue(nodeIds.contains(id));
}
}
// 统计节点 id 出现的数量
private Map<String, Integer> listToMap(List<String> list) {
Map<String, Integer> map = new HashMap<>();
for (String s : list) {
map.put(s, map.getOrDefault(s, 0) + 1);
}
return map;
}
}

View File

@@ -0,0 +1,21 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.script.javascript.getnodes.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
System.out.println("ACmp executed!");
}
}

View File

@@ -0,0 +1,21 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.script.javascript.getnodes.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
System.out.println("BCmp executed!");
}
}

View File

@@ -0,0 +1,21 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.script.javascript.getnodes.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("CCmp executed!");
}
}

View File

@@ -0,0 +1,12 @@
package com.yomahub.liteflow.test.script.javascript.getnodes.cmp;
import com.yomahub.liteflow.core.NodeIfComponent;
import org.springframework.stereotype.Component;
@Component("f")
public class FCmp extends NodeIfComponent {
@Override
public boolean processIf() throws Exception {
return true;
}
}

View File

@@ -0,0 +1,12 @@
package com.yomahub.liteflow.test.script.javascript.getnodes.cmp;
import com.yomahub.liteflow.core.NodeSwitchComponent;
import org.springframework.stereotype.Component;
@Component("s")
public class SCmp extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
return "f1";
}
}

View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="s1" name="普通脚本1" type="script" language="js">
<![CDATA[
var a=1;
var b=2;
var c=3;
defaultContext.setData("s1",a+b+c);
]]>
</node>
<node id="s2" name="普通脚本2" type="script" language="js">
<![CDATA[
var a=1;
var b=2;
var c=3;
defaultContext.setData("s2",a*b+c);
]]>
</node>
<node id="x" name="条件循环脚本" type="while_script" language="js">
<![CDATA[
return true;
]]>
</node>
<node id="y" name="退出循环脚本" type="break_script" language="js">
<![CDATA[
return true;
]]>
</node>
</nodes>
<chain name="chain1">
THEN(a, b, c, s1, s2);
</chain>
<!-- 重复 node -->
<chain name="chain2">
THEN(a.tag("a1"), a.tag("a2"), a.tag("a3"), b.tag("b1"), s1.tag("s11"), s1.tag("s12"));
</chain>
<!-- 多层 node -->
<chain name="chain3">
THEN(WHEN(a, b), IF(f, WHEN(THEN(c, s2, WHEN(s1, b)), a)));
</chain>
<chain name="chain4">
SWITCH(s).to(FOR(3).DO(a).id("f1"), b, c);
</chain>
<chain name="chain5">
WHILE(x).DO(s1).BREAK(y);
</chain>
</flow>