调整测试代码的组织,保持主代码整洁
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
package com.lanyuanxiaoyao.flowable.test;
|
||||
|
||||
import com.lanyuanxiaoyao.flowable.core.helper.MapHelper;
|
||||
import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager;
|
||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableAction;
|
||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance;
|
||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableNode;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 集成测试
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20241231
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class TestFlowableManager {
|
||||
protected abstract FlowableManager flowableManager();
|
||||
|
||||
private FlowableNode createManualNode() {
|
||||
return createManualNode(UUID.randomUUID().toString(), null);
|
||||
}
|
||||
|
||||
private FlowableNode createManualNode(String nodeId) {
|
||||
return createManualNode(nodeId, null);
|
||||
}
|
||||
|
||||
private FlowableNode createManualNode(String nodeId, Map<FlowableAction, String> nextNodes) {
|
||||
return new FlowableNode(
|
||||
nodeId,
|
||||
UUID.randomUUID().toString(),
|
||||
UUID.randomUUID().toString(),
|
||||
FlowableNode.Type.MANUAL,
|
||||
null,
|
||||
nextNodes
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单节点审批
|
||||
*/
|
||||
@Test
|
||||
public void testSingleNode() {
|
||||
FlowableManager manager = flowableManager();
|
||||
FlowableNode node1 = createManualNode();
|
||||
manager.create(node1);
|
||||
Assertions.assertNotNull(manager.getNode(node1.getNodeId()));
|
||||
|
||||
String instanceId = manager.start(node1.getNodeId());
|
||||
Assertions.assertEquals(FlowableInstance.Status.RUNNING, manager.getInstance(instanceId).getStatus());
|
||||
Assertions.assertTrue(manager.listHistories(instanceId).isEmpty());
|
||||
|
||||
manager.approve(instanceId, "3ca20a11-dfb6-435b-8dcc-d00c6c0abd7f");
|
||||
Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus());
|
||||
Assertions.assertEquals(1, manager.listHistories(instanceId).size());
|
||||
Assertions.assertEquals(FlowableAction.APPROVE, manager.listHistories(instanceId).get(0).getAction());
|
||||
Assertions.assertEquals("3ca20a11-dfb6-435b-8dcc-d00c6c0abd7f", manager.listHistories(instanceId).get(0).getComment());
|
||||
|
||||
instanceId = manager.start(node1.getNodeId());
|
||||
manager.reject(instanceId, "3ca20a11-dfb6-435b-8dcc-d00c6c0abd7f");
|
||||
Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus());
|
||||
Assertions.assertEquals(FlowableAction.REJECT, manager.listHistories(instanceId).get(0).getAction());
|
||||
Assertions.assertEquals("3ca20a11-dfb6-435b-8dcc-d00c6c0abd7f", manager.listHistories(instanceId).get(0).getComment());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiNode() {
|
||||
FlowableManager manager = flowableManager();
|
||||
FlowableNode node1 = createManualNode(
|
||||
"02779cbe-0d82-4e09-9bf8-60885400d100",
|
||||
MapHelper.of(
|
||||
FlowableAction.APPROVE, "1e126640-34ae-40f9-b55f-9cb8099d638f",
|
||||
FlowableAction.REJECT, "02779cbe-0d82-4e09-9bf8-60885400d100"
|
||||
)
|
||||
);
|
||||
FlowableNode node2 = createManualNode(
|
||||
"1e126640-34ae-40f9-b55f-9cb8099d638f",
|
||||
MapHelper.of(FlowableAction.REJECT, "02779cbe-0d82-4e09-9bf8-60885400d100")
|
||||
);
|
||||
manager.create(node1, node2);
|
||||
|
||||
String instanceId = manager.start(node1.getNodeId());
|
||||
|
||||
manager.reject(instanceId);
|
||||
Assertions.assertEquals(FlowableInstance.Status.RUNNING, manager.getInstance(instanceId).getStatus());
|
||||
Assertions.assertEquals(node1.getNodeId(), manager.getNode(manager.getInstance(instanceId).getCurrentNodeId()).getNodeId());
|
||||
|
||||
manager.approve(instanceId);
|
||||
manager.reject(instanceId, "我觉得不行");
|
||||
Assertions.assertEquals(FlowableInstance.Status.RUNNING, manager.getInstance(instanceId).getStatus());
|
||||
Assertions.assertEquals(node1.getNodeId(), manager.getNode(manager.getInstance(instanceId).getCurrentNodeId()).getNodeId());
|
||||
|
||||
manager.approve(instanceId);
|
||||
manager.approve(instanceId);
|
||||
Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus());
|
||||
Assertions.assertEquals(node2.getNodeId(), manager.getNode(manager.getInstance(instanceId).getCurrentNodeId()).getNodeId());
|
||||
|
||||
Assertions.assertEquals(FlowableAction.APPROVE, manager.listHistories(instanceId).get(4).getAction());
|
||||
Assertions.assertEquals(5, manager.listHistories(instanceId).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTerminal() {
|
||||
FlowableManager manager = flowableManager();
|
||||
FlowableNode node1 = createManualNode();
|
||||
manager.create(node1);
|
||||
|
||||
String instanceId = manager.start(node1.getNodeId());
|
||||
|
||||
Assertions.assertEquals(FlowableInstance.Status.RUNNING, manager.getInstance(instanceId).getStatus());
|
||||
|
||||
manager.terminal(instanceId, "d896b642-a1d8-499c-92e7-bed63581f2f8");
|
||||
Assertions.assertEquals(FlowableInstance.Status.ERROR, manager.getInstance(instanceId).getStatus());
|
||||
Assertions.assertEquals(1, manager.listHistories(instanceId).size());
|
||||
Assertions.assertEquals(FlowableAction.TERMINAL, manager.listHistories(instanceId).get(0).getAction());
|
||||
Assertions.assertEquals("d896b642-a1d8-499c-92e7-bed63581f2f8", manager.listHistories(instanceId).get(0).getComment());
|
||||
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> manager.approve(instanceId));
|
||||
}
|
||||
|
||||
protected abstract Class<?> getAutomaticNodeClass();
|
||||
|
||||
@Test
|
||||
public void testAutomaticNode() {
|
||||
FlowableManager manager = flowableManager();
|
||||
FlowableNode node = new FlowableNode(
|
||||
"2733d930-7a4b-491e-b1ca-4d5811435e9f",
|
||||
"自动节点",
|
||||
"自动节点",
|
||||
FlowableNode.Type.AUTOMATIC,
|
||||
getAutomaticNodeClass().getName(),
|
||||
null
|
||||
);
|
||||
manager.create(node);
|
||||
String instanceId = manager.start(node.getNodeId());
|
||||
Assertions.assertEquals(FlowableInstance.Status.COMPLETED, manager.getInstance(instanceId).getStatus());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.lanyuanxiaoyao.flowable.test;
|
||||
|
||||
import com.lanyuanxiaoyao.flowable.core.helper.ListHelper;
|
||||
import com.lanyuanxiaoyao.flowable.core.helper.StringHelper;
|
||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableHistory;
|
||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance;
|
||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableNode;
|
||||
import com.lanyuanxiaoyao.flowable.core.repository.FlowableRepository;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 内存存储
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20241231
|
||||
*/
|
||||
public class InMemoryFlowableRepository implements FlowableRepository {
|
||||
private static final Map<String, FlowableNode> nodes = new HashMap<>();
|
||||
private static final Map<String, FlowableInstance> instances = new HashMap<>();
|
||||
private static final Map<String, List<FlowableHistory>> histories = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void saveNode(FlowableNode node) {
|
||||
nodes.put(node.getNodeId(), node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveNode(List<FlowableNode> nodes) {
|
||||
for (FlowableNode node : nodes) {
|
||||
saveNode(node);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FlowableNode getNode(String nodeId) {
|
||||
return nodes.get(nodeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FlowableNode> listNodes() {
|
||||
return new ArrayList<>(nodes.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveInstance(FlowableInstance instance) {
|
||||
instances.put(instance.getInstanceId(), instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FlowableInstance getInstance(String instantId) {
|
||||
return instances.getOrDefault(instantId, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FlowableInstance> listInstances() {
|
||||
return new ArrayList<>(instances.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveHistory(FlowableHistory history) {
|
||||
String instanceId = history.getInstanceId();
|
||||
if (!histories.containsKey(instanceId)) {
|
||||
histories.put(instanceId, new ArrayList<>());
|
||||
}
|
||||
histories.get(instanceId).add(history);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FlowableHistory getHistory(String historyId) {
|
||||
return histories.values()
|
||||
.stream()
|
||||
.flatMap(List::stream)
|
||||
.filter(history -> StringHelper.equals(history.getHistoryId(), historyId))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
@Override
|
||||
public List<FlowableHistory> listHistories(String instanceId) {
|
||||
return histories.getOrDefault(instanceId, ListHelper.empty());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveInstanceAndHistory(FlowableInstance instance, FlowableHistory history) {
|
||||
saveInstance(instance);
|
||||
saveHistory(history);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.lanyuanxiaoyao.flowable.test;
|
||||
|
||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableAction;
|
||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableInstance;
|
||||
import com.lanyuanxiaoyao.flowable.core.model.FlowableNode;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250102
|
||||
*/
|
||||
public class SimpleAutoAction implements FlowableNode.AutoAction {
|
||||
@Override
|
||||
public FlowableAction action(FlowableInstance instance, FlowableNode node, Map<String, Object> metadata) {
|
||||
return FlowableAction.APPROVE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.lanyuanxiaoyao.flowable.test;
|
||||
|
||||
import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager;
|
||||
import java.util.UUID;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20241231
|
||||
*/
|
||||
public class SimpleFlowableManager extends FlowableManager {
|
||||
public SimpleFlowableManager() {
|
||||
super(new InMemoryFlowableRepository(), () -> UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
protected <T> T createBean(String classpath) {
|
||||
return (T) Class.forName(classpath).newInstance();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.lanyuanxiaoyao.flowable.test;
|
||||
|
||||
import com.lanyuanxiaoyao.flowable.core.manager.FlowableManager;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250102
|
||||
*/
|
||||
public class TestSimpleFlowableManager extends TestFlowableManager {
|
||||
@Override
|
||||
protected FlowableManager flowableManager() {
|
||||
return new SimpleFlowableManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getAutomaticNodeClass() {
|
||||
return SimpleAutoAction.class;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user