优化FlowExecutor中调用方法

This commit is contained in:
zendwang
2021-04-09 11:02:55 +08:00
parent a6957428b5
commit 4b2c197628
6 changed files with 181 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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<CustomSlot> 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<CustomSlot> 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);
}
}

View File

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

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.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!");
}
}

View File

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

View File

@@ -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"}
]
}
]
}
}