diff --git a/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/CustomSlot.java b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/CustomSlot.java new file mode 100644 index 000000000..58156478a --- /dev/null +++ b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/CustomSlot.java @@ -0,0 +1,15 @@ +package com.yomahub.liteflow.test.executor; + +import com.yomahub.liteflow.entity.data.AbsSlot; + +public class CustomSlot extends AbsSlot { + private String name; + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } +} diff --git a/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/FlowExecutorTest.java b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/FlowExecutorTest.java new file mode 100644 index 000000000..ebf590740 --- /dev/null +++ b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/FlowExecutorTest.java @@ -0,0 +1,63 @@ +package com.yomahub.liteflow.test.executor; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.property.LiteflowConfig; +import com.yomahub.liteflow.test.BaseTest; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.util.ReflectionUtils; + +/** + * 无spring环境下FlowExecutor + * 调用方法execute、invoke单元测试 + * @author zendwang + * @since 2.5.1 + */ +public class FlowExecutorTest extends BaseTest { + + @Test + public void testMethodExecute() { + LiteflowConfig config = new LiteflowConfig(); + config.setRuleSource("executor/flow.json"); + FlowExecutor executor = new FlowExecutor(); + executor.setLiteflowConfig(config); + executor.init(); + LiteflowResponse response = executor.execute("chain1", "test0", CustomSlot.class); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("custom", response.getSlot().getName()); + } + + @Test(expected=RuntimeException.class) + public void testMethodExecuteWithException() throws Exception{ + LiteflowConfig config = new LiteflowConfig(); + config.setRuleSource("executor/flow.json"); + FlowExecutor executor = new FlowExecutor(); + executor.setLiteflowConfig(config); + executor.init(); + LiteflowResponse response = executor.execute("chain1", "test1", CustomSlot.class); + Assert.assertFalse(response.isSuccess()); + ReflectionUtils.rethrowException(response.getCause()); + } + + @Test + public void testMethodInvoke() throws Exception { + LiteflowConfig config = new LiteflowConfig(); + config.setRuleSource("executor/flow.json"); + FlowExecutor executor = new FlowExecutor(); + executor.setLiteflowConfig(config); + executor.init(); + CustomSlot slot = executor.invoke("chain1", "test0", CustomSlot.class); + Assert.assertEquals("custom", slot.getName()); + } + + @Test(expected=RuntimeException.class) + public void testMethodInvokeWithException() throws Exception { + LiteflowConfig config = new LiteflowConfig(); + config.setRuleSource("executor/flow.json"); + FlowExecutor executor = new FlowExecutor(); + executor.setLiteflowConfig(config); + executor.init(); + executor.invoke("chain1", "test1", CustomSlot.class); + } +} diff --git a/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/cmp/ACmp.java b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/cmp/ACmp.java new file mode 100644 index 000000000..18b757503 --- /dev/null +++ b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/cmp/ACmp.java @@ -0,0 +1,20 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.executor.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!"); + } +} diff --git a/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/cmp/BCmp.java b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/cmp/BCmp.java new file mode 100644 index 000000000..54977af85 --- /dev/null +++ b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/cmp/BCmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.executor.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!"); + } + +} diff --git a/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/cmp/CCmp.java b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/cmp/CCmp.java new file mode 100644 index 000000000..0325fcb48 --- /dev/null +++ b/liteflow-core/src/test/java/com/yomahub/liteflow/test/executor/cmp/CCmp.java @@ -0,0 +1,34 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.executor.cmp; + +import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.test.executor.CustomSlot; +import org.springframework.stereotype.Component; + +@Component("c") +public class CCmp extends NodeComponent { + + @Override + public void process() { + if(this.getSlot() instanceof CustomSlot) { + CustomSlot slot = this.getSlot(); + String str = slot.getRequestData(); + if(StrUtil.isNotBlank(str) && str.equals("test0")) { + slot.setName("custom"); + } + if(StrUtil.isNotBlank(str) && str.equals("test1")) { + slot.setName("custom"); + throw new RuntimeException("customException"); + } + } + System.out.println("CCmp executed!"); + } + +} diff --git a/liteflow-core/src/test/resources/executor/flow.json b/liteflow-core/src/test/resources/executor/flow.json new file mode 100644 index 000000000..300679405 --- /dev/null +++ b/liteflow-core/src/test/resources/executor/flow.json @@ -0,0 +1,28 @@ +{ + "flow": { + "nodes": { + "node": [ + { + "id": "a", + "class": "com.yomahub.liteflow.test.executor.cmp.ACmp" + }, + { + "id": "b", + "class": "com.yomahub.liteflow.test.executor.cmp.BCmp" + }, + { + "id": "c", + "class": "com.yomahub.liteflow.test.executor.cmp.CCmp" + } + ] + }, + "chain": [ + { + "name": "chain1", + "condition": [ + {"type": "then", "value": "a,b,c"} + ] + } + ] + } +} \ No newline at end of file