1
0

调整测试代码的组织,保持主代码整洁

This commit is contained in:
2025-01-03 09:37:22 +08:00
parent 071beee794
commit 07ee530b79
13 changed files with 103 additions and 37 deletions

36
flowable-example/pom.xml Normal file
View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.lanyuanxiaoyao</groupId>
<artifactId>flowable</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>flowable-example</artifactId>
<dependencies>
<dependency>
<groupId>com.lanyuanxiaoyao</groupId>
<artifactId>flowable-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

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

View File

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

View File

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

View File

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

View File

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