feature #I8MW6Q 添加测试用例

This commit is contained in:
Dale Lee
2023-12-24 16:35:34 +08:00
parent 096f9610b2
commit c3c72c5cc1
6 changed files with 262 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
package com.yomahub.liteflow.test.script.javascript.remove;
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.enums.ScriptTypeEnum;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.script.ScriptExecutor;
import com.yomahub.liteflow.script.ScriptExecutorFactory;
import com.yomahub.liteflow.script.exception.ScriptLoadException;
import com.yomahub.liteflow.slot.DefaultContext;
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.List;
/**
* 测试脚本的卸载功能
*
* @author DaleLee
*/
@ExtendWith(SpringExtension.class)
@TestPropertySource(value = "classpath:/remove/application.properties")
@SpringBootTest(classes = LiteFlowJsScriptRemoveELTest.class)
@EnableAutoConfiguration
public class LiteFlowJsScriptRemoveELTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
private final ScriptExecutor scriptExecutor = ScriptExecutorFactory.loadInstance()
.getScriptExecutor(ScriptTypeEnum.JS.getDisplayName());
// 仅卸载脚本
@Test
public void testUnload() {
flowExecutor.reloadRule();
// 获取节点id
List<String> nodeIds = scriptExecutor.getNodeIds();
Assertions.assertEquals(nodeIds.size(), 2);
Assertions.assertTrue(nodeIds.contains("s1"));
Assertions.assertTrue(nodeIds.contains("s2"));
// 保证脚本可以正常运行
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assertions.assertTrue(response.isSuccess());
DefaultContext context = response.getFirstContextBean();
Assertions.assertEquals(Integer.valueOf(6), context.getData("s1"));
// 卸载脚本
scriptExecutor.unLoad("s1");
response = flowExecutor.execute2Resp("chain1", "arg");
Assertions.assertFalse(response.isSuccess());
Assertions.assertEquals(ScriptLoadException.class, response.getCause().getClass());
Assertions.assertEquals("script for node[s1] is not loaded", response.getMessage());
// 脚本已卸载
Assertions.assertFalse(scriptExecutor.getNodeIds().contains("s1"));
}
// 卸载节点和脚本
@Test
public void testRemove() {
flowExecutor.reloadRule();
// 保证脚本可以正常运行
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
Assertions.assertTrue(response.isSuccess());
DefaultContext context = response.getFirstContextBean();
Assertions.assertEquals(Integer.valueOf(5), context.getData("s2"));
// 卸载节点
FlowBus.removeNode("s2");
// 旧 chain 报脚本加载错误
response = flowExecutor.execute2Resp("chain2", "arg");
Assertions.assertEquals(ScriptLoadException.class, response.getCause().getClass());
// 新 chian 会找不到节点
Assertions.assertThrows(ELParseException.class,
() -> LiteFlowChainELBuilder.createChain().setChainId("chain3").setEL(
"THEN(s2)"
).build());
// 节点已卸载
Assertions.assertFalse(FlowBus.containNode("s2"));
// 脚本已卸载
Assertions.assertFalse(scriptExecutor.getNodeIds().contains("s2"));
}
}

View File

@@ -0,0 +1,30 @@
<?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",parseInt(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",parseInt(a*b+c));
]]>
</node>
</nodes>
<chain name="chain1">
THEN(s1);
</chain>
<chain name="chain2">
THEN(s2);
</chain>
</flow>