补充测试用例

This commit is contained in:
bryan31
2022-03-05 16:14:42 +08:00
parent 6b0cc1fd07
commit 5a85461be9
6 changed files with 198 additions and 0 deletions

View File

@@ -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<DefaultSlot> response = flowExecutor.execute2Resp("chain4", "test");
Assert.assertFalse(response.isSuccess());
Assert.assertNotNull(response.getCause());
Assert.assertNotNull(response.getSlot());
}
}

View File

@@ -0,0 +1,28 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @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!");
}
}

View File

@@ -0,0 +1,33 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @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!");
}
}

View File

@@ -0,0 +1,22 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @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!");
}
}

View File

@@ -0,0 +1,25 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @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!");
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="a" class="com.yomahub.liteflow.test.exception.cmp.ACmp"/>
<node id="b" class="com.yomahub.liteflow.test.exception.cmp.BCmp"/>
<node id="c" class="com.yomahub.liteflow.test.exception.cmp.CCmp"/>
<node id="d" class="com.yomahub.liteflow.test.exception.cmp.DCmp"/>
</nodes>
<chain name="chain1">
<then value="a,b,c"/>
</chain>
<chain name="chain2">
</chain>
<chain name="chain3">
<then value="a" />
<when value="b,c" errorResume="false" />
</chain>
<chain name="chain4">
<then value="c,d"/>
</chain>
</flow>