From 5a85461be976a368d9cab9dc739792cde5e38a1e Mon Sep 17 00:00:00 2001 From: bryan31 Date: Sat, 5 Mar 2022 16:14:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/exception/ExceptionTest.java | 66 +++++++++++++++++++ .../liteflow/test/exception/cmp/ACmp.java | 28 ++++++++ .../liteflow/test/exception/cmp/BCmp.java | 33 ++++++++++ .../liteflow/test/exception/cmp/CCmp.java | 22 +++++++ .../liteflow/test/exception/cmp/DCmp.java | 25 +++++++ .../src/test/resources/exception/flow.xml | 24 +++++++ 6 files changed, 198 insertions(+) create mode 100644 liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/ExceptionTest.java create mode 100644 liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/ACmp.java create mode 100644 liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/BCmp.java create mode 100644 liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/CCmp.java create mode 100644 liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/DCmp.java create mode 100644 liteflow-testcase-nospring/src/test/resources/exception/flow.xml diff --git a/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/ExceptionTest.java b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/ExceptionTest.java new file mode 100644 index 000000000..5c6b9ea33 --- /dev/null +++ b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/ExceptionTest.java @@ -0,0 +1,66 @@ +package com.yomahub.liteflow.test.exception; + +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.entity.data.DefaultSlot; +import com.yomahub.liteflow.entity.data.LiteflowResponse; +import com.yomahub.liteflow.exception.ChainNotFoundException; +import com.yomahub.liteflow.exception.FlowExecutorNotInitException; +import com.yomahub.liteflow.exception.FlowSystemException; +import com.yomahub.liteflow.property.LiteflowConfig; +import com.yomahub.liteflow.property.LiteflowConfigGetter; +import com.yomahub.liteflow.test.BaseTest; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * 流程执行异常 + * 单元测试 + * + * @author zendwang + */ +public class ExceptionTest extends BaseTest { + + private static FlowExecutor flowExecutor; + + @BeforeClass + public static void init(){ + LiteflowConfig config = new LiteflowConfig(); + config.setRuleSource("exception/flow.xml"); + config.setWhenMaxWaitSeconds(1); + flowExecutor = FlowExecutor.loadInstance(config); + } + + @Test(expected = FlowExecutorNotInitException.class) + public void testFlowExecutorNotInitException() { + LiteflowConfig config = LiteflowConfigGetter.get(); + config.setRuleSource("error/flow.txt"); + flowExecutor.init(); + } + + @Test(expected = ChainNotFoundException.class) + public void testChainNotFoundException() throws Exception { + flowExecutor.execute("chain0", "it's a request"); + } + + @Test(expected = RuntimeException.class) + public void testComponentCustomException() throws Exception { + flowExecutor.execute("chain1", "exception"); + } + + @Test(expected = FlowSystemException.class) + public void testNoConditionInChainException() throws Throwable { + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "test"); + Assert.assertFalse(response.isSuccess()); + Assert.assertEquals("no conditionList in this chain[chain2]", response.getMessage()); + throw response.getCause(); + } + + @Test + public void testGetSlotFromResponseWhenException() throws Exception{ + LiteflowResponse response = flowExecutor.execute2Resp("chain4", "test"); + Assert.assertFalse(response.isSuccess()); + Assert.assertNotNull(response.getCause()); + Assert.assertNotNull(response.getSlot()); + } +} diff --git a/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/ACmp.java b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/ACmp.java new file mode 100644 index 000000000..8ea4d6c74 --- /dev/null +++ b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/ACmp.java @@ -0,0 +1,28 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.exception.cmp; + +import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.core.NodeComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ACmp extends NodeComponent { + + private static final Logger LOG = LoggerFactory.getLogger(ACmp.class); + + @Override + public void process() { + String str = this.getSlot().getRequestData(); + if(StrUtil.isNotBlank(str) && str.equals("exception")) { + throw new RuntimeException("chain execute execption"); + } + LOG.info("Acomp executed!"); + } + +} diff --git a/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/BCmp.java b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/BCmp.java new file mode 100644 index 000000000..263677b78 --- /dev/null +++ b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/BCmp.java @@ -0,0 +1,33 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.exception.cmp; + +import cn.hutool.core.util.StrUtil; +import com.yomahub.liteflow.core.NodeComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BCmp extends NodeComponent { + + private static final Logger LOG = LoggerFactory.getLogger(BCmp.class); + + @Override + public void process() throws InterruptedException { + String str = this.getSlot().getRequestData(); + if(StrUtil.isNotBlank(str) && str.equals("when")) { + try { + LOG.info("Bcomp sleep begin"); + Thread.sleep(3000); + LOG.info("Bcomp sleep end"); + } catch (InterruptedException e) { + throw e; + } + } + LOG.info("Bcomp executed!"); + } +} diff --git a/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/CCmp.java b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/CCmp.java new file mode 100644 index 000000000..7adc24167 --- /dev/null +++ b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/CCmp.java @@ -0,0 +1,22 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.exception.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CCmp extends NodeComponent { + + private static final Logger LOG = LoggerFactory.getLogger(CCmp.class); + + @Override + public void process() { + LOG.info("Ccomp executed!"); + } +} diff --git a/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/DCmp.java b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/DCmp.java new file mode 100644 index 000000000..171947632 --- /dev/null +++ b/liteflow-testcase-nospring/src/test/java/com/yomahub/liteflow/test/exception/cmp/DCmp.java @@ -0,0 +1,25 @@ +/** + *

Title: liteflow

+ *

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

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.exception.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DCmp extends NodeComponent { + + private static final Logger LOG = LoggerFactory.getLogger(DCmp.class); + + @Override + public void process() { + if(1==1){ + int a = 1/0; + } + LOG.info("Dcomp executed!"); + } +} diff --git a/liteflow-testcase-nospring/src/test/resources/exception/flow.xml b/liteflow-testcase-nospring/src/test/resources/exception/flow.xml new file mode 100644 index 000000000..14c49cfd4 --- /dev/null +++ b/liteflow-testcase-nospring/src/test/resources/exception/flow.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file