diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/java/cn/iocoder/yudao/module/bpm/MultiInstancesTest.java b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/java/cn/iocoder/yudao/module/bpm/MultiInstancesTest.java new file mode 100644 index 000000000..f0672b028 --- /dev/null +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/java/cn/iocoder/yudao/module/bpm/MultiInstancesTest.java @@ -0,0 +1,203 @@ +package cn.iocoder.yudao.module.bpm; + +import org.flowable.engine.history.HistoricDetail; +import org.flowable.engine.history.HistoricFormProperty; +import org.flowable.engine.history.HistoricProcessInstance; +import org.flowable.engine.history.HistoricVariableUpdate; +import org.flowable.engine.repository.ProcessDefinition; +import org.flowable.engine.runtime.ProcessInstance; +import org.flowable.engine.test.Deployment; +import org.flowable.task.api.Task; +import org.junit.Test; + +import java.text.SimpleDateFormat; +import java.util.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author henryyan + * testMultiInstanceForUserTask 会签 + */ +public class MultiInstancesTest extends AbstractOATest { + + /** + * Java Service多实例(是否顺序结果一样) + */ + @Test + @Deployment(resources = {"diagrams/chapter9/testMultiInstanceFixedNumbers.bpmn"}) + public void testParallel() throws Exception { + Map variables = new HashMap(); + long loop = 3; + variables.put("loop", loop); + variables.put("counter", 0); // 计数器 + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testMultiInstanceFixedNumbers", variables); + Object variable = runtimeService.getVariable(processInstance.getId(), "counter"); + assertEquals(loop, variable); + } + + /** + * 用户任务多实例--顺序 + */ + @Test + @Deployment(resources = {"diagrams/chapter9/testMultiInstanceForUserTask.sequential.bpmn"}) + public void testForUserSequence() throws Exception { + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testMultiInstanceForUserTask"); + long count = taskService.createTaskQuery().processInstanceId(processInstance.getId()).count(); + assertEquals(1, count); + + Task task = taskService.createTaskQuery().singleResult(); + taskService.complete(task.getId()); + count = taskService.createTaskQuery().processInstanceId(processInstance.getId()).count(); + assertEquals(1, count); + + task = taskService.createTaskQuery().singleResult(); + taskService.complete(task.getId()); + count = taskService.createTaskQuery().processInstanceId(processInstance.getId()).count(); + assertEquals(1, count); + + task = taskService.createTaskQuery().singleResult(); + taskService.complete(task.getId()); + count = taskService.createTaskQuery().processInstanceId(processInstance.getId()).count(); + assertEquals(0, count); + } + + /** + * 用户任务多实例--并行 + */ + @Test + @Deployment(resources = {"diagrams/chapter9/testMultiInstanceForUserTask.nosequential.bpmn"}) + public void testForUserNoSequential() throws Exception { + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testMultiInstanceForUserTask"); + long count = taskService.createTaskQuery().processInstanceId(processInstance.getId()).count(); + assertEquals(3, count); + } + + /** + * 用户任务多实例,通过用户数量决定实例个数--并行 + */ + @Test + @Deployment(resources = {"diagrams/chapter9/testMultiInstanceForUserTask.users.nosequential.bpmn"}) + public void testForUserCreateByUsersNoSequential() throws Exception { + Map variables = new HashMap(); + List users = Arrays.asList("user1", "user2", "user3"); + variables.put("users", users); + runtimeService.startProcessInstanceByKey("testMultiInstanceForUserTask", variables); + for (String userId : users) { + assertEquals(1, taskService.createTaskQuery().taskAssignee(userId).count()); + } + } + + /** + * 用户任务多实例,通过用户数量决定实例个数--顺序 + */ + @Test + @Deployment(resources = {"diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.bpmn"}) + public void testForUserCreateByUsersSequential() throws Exception { + Map variables = new HashMap(); + List users = Arrays.asList("user1", "user2", "user3"); + variables.put("users", users); + runtimeService.startProcessInstanceByKey("testMultiInstanceForUserTask", variables); + for (String userId : users) { + Task task = taskService.createTaskQuery().taskAssignee(userId).singleResult(); + taskService.complete(task.getId()); + } + } + + /** + * 用户任务多实例,按照任务完成的百分比比率决定是否提前结束流程 + */ + @Test + @Deployment(resources = {"diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.with.complete.conditon.bpmn"}) + public void testForUserCreateByUsersSequentialWithCompleteCondition() throws Exception { + Map variables = new HashMap(); + List users = Arrays.asList("user1", "user2", "user3"); + variables.put("users", users); + variables.put("rate", 0.6d); + runtimeService.startProcessInstanceByKey("testMultiInstanceForUserTask", variables); + + Task task = taskService.createTaskQuery().taskAssignee("user1").singleResult(); + taskService.complete(task.getId()); + + task = taskService.createTaskQuery().taskAssignee("user2").singleResult(); + taskService.complete(task.getId()); + + long count = historyService.createHistoricProcessInstanceQuery().finished().count(); + assertEquals(1, count); + + } + + /** + * 用户任务多实例,按照任务完成的百分比比率决定是否提前结束流程 + */ + @Test + @Deployment(resources = {"diagrams/chapter9/testMultiInstanceForUserTask.exception.bpmn"}) + public void testForUserCreateByUsersException() throws Exception { + Map variables = new HashMap(); + List users = Arrays.asList("user1", "user2", "user3"); + variables.put("users", users); + runtimeService.startProcessInstanceByKey("testMultiInstanceForUserTask", variables); + + Task task = taskService.createTaskQuery().taskAssignee("user1").singleResult(); + taskService.complete(task.getId()); + + task = taskService.createTaskQuery().taskAssignee("user2").singleResult(); + taskService.complete(task.getId()); + + task = taskService.createTaskQuery().taskAssignee("user3").singleResult(); + taskService.complete(task.getId()); + + List list = taskService.createTaskQuery().list(); + for (Task task2 : list) { + System.out.println("============" + task2.getName()); + } + + } + ///////////////////////////////////////////////// + /** + * 全部通过 + */ + @Test + @Deployment(resources = {"diagrams/chapter9/leave-countersign.bpmn"}) + public void testAllApproved() throws Exception { + Map variables = new HashMap(); + List users = Arrays.asList("groupLeader", "deptLeader", "hr"); + variables.put("users", users); + identityService.setAuthenticatedUserId("henryyan"); + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave-countersign", variables); + for (String user : users) { + Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskAssignee(user).singleResult(); + Map taskVariables = new HashMap(); + taskVariables.put("approved", "true"); + taskService.complete(task.getId(), taskVariables); + } + + Task task = taskService.createTaskQuery().taskAssignee("henryyan").singleResult(); + assertNotNull(task); + assertEquals("销假", task.getName()); + } + + /** + * 部分通过 + */ + @Test + @Deployment(resources = {"diagrams/chapter9/leave-countersign.bpmn"}) + public void testNotAllApproved() throws Exception { + Map variables = new HashMap(); + List users = Arrays.asList("groupLeader", "deptLeader", "hr"); + variables.put("users", users); + identityService.setAuthenticatedUserId("henryyan"); + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave-countersign", variables); + for (String user : users) { + Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskAssignee(user).singleResult(); + Map taskVariables = new HashMap(); + taskVariables.put("approved", "false"); + taskService.complete(task.getId(), taskVariables); + } + + Task task = taskService.createTaskQuery().taskAssignee("henryyan").singleResult(); + assertNotNull(task); + assertEquals("调整申请", task.getName()); + } +} diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/leave-countersign.bpmn b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/leave-countersign.bpmn new file mode 100644 index 000000000..bdb56857d --- /dev/null +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/leave-countersign.bpmn @@ -0,0 +1,152 @@ + + + + 请假流程演示-会签 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/leave-countersign.png b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/leave-countersign.png new file mode 100644 index 000000000..e2a6f2ca2 Binary files /dev/null and b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/leave-countersign.png differ diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceFixedNumbers.bpmn b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceFixedNumbers.bpmn new file mode 100644 index 000000000..998570919 --- /dev/null +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceFixedNumbers.bpmn @@ -0,0 +1,44 @@ + + + + + + + ${loop} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.exception.bpmn b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.exception.bpmn new file mode 100644 index 000000000..05a87d939 --- /dev/null +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.exception.bpmn @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.exception.png b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.exception.png new file mode 100644 index 000000000..5ac829387 Binary files /dev/null and b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.exception.png differ diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.nosequential.bpmn b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.nosequential.bpmn new file mode 100644 index 000000000..9790144fe --- /dev/null +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.nosequential.bpmn @@ -0,0 +1,35 @@ + + + + + + + 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.sequential.bpmn b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.sequential.bpmn new file mode 100644 index 000000000..c7bc82b4f --- /dev/null +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.sequential.bpmn @@ -0,0 +1,35 @@ + + + + + + + 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.nosequential.bpmn b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.nosequential.bpmn new file mode 100644 index 000000000..ca117190c --- /dev/null +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.nosequential.bpmn @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.bpmn b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.bpmn new file mode 100644 index 000000000..5dbd39676 --- /dev/null +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.bpmn @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.with.complete.conditon.bpmn b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.with.complete.conditon.bpmn new file mode 100644 index 000000000..d8fc63a55 --- /dev/null +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.with.complete.conditon.bpmn @@ -0,0 +1,35 @@ + + + + + + + ${nrOfCompletedInstances / nrOfInstances >= rate} + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.with.complete.conditon.png b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.with.complete.conditon.png new file mode 100644 index 000000000..c20f451b8 Binary files /dev/null and b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/diagrams/chapter9/testMultiInstanceForUserTask.users.sequential.with.complete.conditon.png differ