Commit c5a9f5d03d09de326c00aabe479a5945b2cfb12d
1 parent
310591ce
flowable-单元测试-helloworld跑通
Showing
3 changed files
with
184 additions
and
9 deletions
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/java/cn/iocoder/yudao/module/bpm/AbstractOATest.java
| 1 | package cn.iocoder.yudao.module.bpm; | 1 | package cn.iocoder.yudao.module.bpm; |
| 2 | 2 | ||
| 3 | import org.flowable.engine.*; | 3 | import org.flowable.engine.*; |
| 4 | +import org.flowable.engine.impl.ProcessEngineImpl; | ||
| 5 | +import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; | ||
| 6 | +import org.flowable.engine.impl.test.TestHelper; | ||
| 4 | import org.flowable.engine.test.FlowableRule; | 7 | import org.flowable.engine.test.FlowableRule; |
| 5 | import org.junit.AfterClass; | 8 | import org.junit.AfterClass; |
| 6 | import org.junit.Before; | 9 | import org.junit.Before; |
| @@ -14,12 +17,16 @@ import org.junit.Rule; | @@ -14,12 +17,16 @@ import org.junit.Rule; | ||
| 14 | */ | 17 | */ |
| 15 | public abstract class AbstractOATest { | 18 | public abstract class AbstractOATest { |
| 16 | 19 | ||
| 20 | + protected String configurationResource = "flowable.cfg.xml"; | ||
| 21 | + | ||
| 17 | /** | 22 | /** |
| 18 | * 专门用于测试套件 | 23 | * 专门用于测试套件 |
| 19 | */ | 24 | */ |
| 20 | @Rule | 25 | @Rule |
| 21 | public FlowableRule activitiRule = new FlowableRule("flowable.cfg.xml"); | 26 | public FlowableRule activitiRule = new FlowableRule("flowable.cfg.xml"); |
| 22 | 27 | ||
| 28 | + protected ProcessEngineConfigurationImpl processEngineConfiguration; | ||
| 29 | + | ||
| 23 | protected ProcessEngine processEngine; | 30 | protected ProcessEngine processEngine; |
| 24 | protected RepositoryService repositoryService; | 31 | protected RepositoryService repositoryService; |
| 25 | protected RuntimeService runtimeService; | 32 | protected RuntimeService runtimeService; |
| @@ -45,19 +52,27 @@ public abstract class AbstractOATest { | @@ -45,19 +52,27 @@ public abstract class AbstractOATest { | ||
| 45 | System.out.println("-------- 结束测试 --------"); | 52 | System.out.println("-------- 结束测试 --------"); |
| 46 | } | 53 | } |
| 47 | 54 | ||
| 55 | + protected void initializeProcessEngine() { | ||
| 56 | + processEngine = TestHelper.getProcessEngine(configurationResource); | ||
| 57 | + } | ||
| 58 | + | ||
| 48 | /** | 59 | /** |
| 49 | * 初始化变量 | 60 | * 初始化变量 |
| 50 | */ | 61 | */ |
| 51 | @Before | 62 | @Before |
| 52 | public void setUp() throws Exception { | 63 | public void setUp() throws Exception { |
| 53 | - processEngine = activitiRule.getProcessEngine(); | ||
| 54 | - repositoryService = activitiRule.getRepositoryService(); | ||
| 55 | - runtimeService = activitiRule.getRuntimeService(); | ||
| 56 | - taskService = activitiRule.getTaskService(); | ||
| 57 | - historyService = activitiRule.getHistoryService(); | ||
| 58 | - identityService = activitiRule.getIdentityService(); | ||
| 59 | - managementService = activitiRule.getManagementService(); | ||
| 60 | - formService = activitiRule.getFormService(); | 64 | + if (processEngine == null) { |
| 65 | + initializeProcessEngine(); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + processEngineConfiguration = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration(); | ||
| 69 | + repositoryService = processEngine.getRepositoryService(); | ||
| 70 | + runtimeService = processEngine.getRuntimeService(); | ||
| 71 | + taskService = processEngine.getTaskService(); | ||
| 72 | + historyService = processEngine.getHistoryService(); | ||
| 73 | + identityService = processEngine.getIdentityService(); | ||
| 74 | + managementService = processEngine.getManagementService(); | ||
| 75 | + formService = processEngine.getFormService(); | ||
| 61 | } | 76 | } |
| 62 | 77 | ||
| 63 | } | 78 | } |
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/java/cn/iocoder/yudao/module/bpm/LeaveFormKeyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm; | ||
| 2 | + | ||
| 3 | +import org.flowable.engine.history.HistoricDetail; | ||
| 4 | +import org.flowable.engine.history.HistoricFormProperty; | ||
| 5 | +import org.flowable.engine.history.HistoricProcessInstance; | ||
| 6 | +import org.flowable.engine.history.HistoricVariableUpdate; | ||
| 7 | +import org.flowable.engine.repository.ProcessDefinition; | ||
| 8 | +import org.flowable.engine.runtime.ProcessInstance; | ||
| 9 | +import org.flowable.engine.test.Deployment; | ||
| 10 | +import org.flowable.task.api.Task; | ||
| 11 | +import org.junit.Test; | ||
| 12 | + | ||
| 13 | +import java.text.SimpleDateFormat; | ||
| 14 | +import java.util.Calendar; | ||
| 15 | +import java.util.HashMap; | ||
| 16 | +import java.util.List; | ||
| 17 | +import java.util.Map; | ||
| 18 | + | ||
| 19 | +import static org.junit.Assert.assertEquals; | ||
| 20 | +import static org.junit.Assert.assertNotNull; | ||
| 21 | + | ||
| 22 | +/** | ||
| 23 | + * @author henryyan | ||
| 24 | + */ | ||
| 25 | +public class LeaveFormKeyTest extends AbstractOATest { | ||
| 26 | + | ||
| 27 | + @Test | ||
| 28 | + @Deployment(resources = {"chapter6/leave-formkey/leave-formkey.bpmn", "chapter6/leave-formkey/leave-start.form", | ||
| 29 | + "chapter6/leave-formkey/approve-deptLeader.form", "chapter6/leave-formkey/approve-hr.form", "chapter6/leave-formkey/report-back.form", | ||
| 30 | + "chapter6/leave-formkey/modify-apply.form"}) | ||
| 31 | + public void allPass() throws Exception { | ||
| 32 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
| 33 | + Map<String, String> variables = new HashMap<String, String>(); | ||
| 34 | + Calendar ca = Calendar.getInstance(); | ||
| 35 | + String startDate = sdf.format(ca.getTime()); | ||
| 36 | + ca.add(Calendar.DAY_OF_MONTH, 2); // 当前日期加2天 | ||
| 37 | + String endDate = sdf.format(ca.getTime()); | ||
| 38 | + | ||
| 39 | + // 启动流程 | ||
| 40 | + variables.put("startDate", startDate); | ||
| 41 | + variables.put("endDate", endDate); | ||
| 42 | + variables.put("reason", "公休"); | ||
| 43 | + | ||
| 44 | + ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult(); | ||
| 45 | + | ||
| 46 | + // 读取启动表单 | ||
| 47 | + Object renderedStartForm = formService.getRenderedStartForm(processDefinition.getId()); | ||
| 48 | + assertNotNull(renderedStartForm); | ||
| 49 | + | ||
| 50 | + // 启动流程 | ||
| 51 | + // 设置当前用户 | ||
| 52 | + String currentUserId = "henryyan"; | ||
| 53 | + identityService.setAuthenticatedUserId(currentUserId); | ||
| 54 | + ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), variables); | ||
| 55 | + assertNotNull(processInstance); | ||
| 56 | + | ||
| 57 | + // 部门领导审批通过 | ||
| 58 | + Task deptLeaderTask = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult(); | ||
| 59 | + assertNotNull(formService.getRenderedTaskForm(deptLeaderTask.getId())); | ||
| 60 | + variables = new HashMap<String, String>(); | ||
| 61 | + variables.put("deptLeaderApproved", "true"); | ||
| 62 | + formService.submitTaskFormData(deptLeaderTask.getId(), variables); | ||
| 63 | + | ||
| 64 | + // 人事审批通过 | ||
| 65 | + Task hrTask = taskService.createTaskQuery().taskCandidateGroup("hr").singleResult(); | ||
| 66 | + assertNotNull(formService.getRenderedTaskForm(hrTask.getId())); | ||
| 67 | + variables = new HashMap<String, String>(); | ||
| 68 | + variables.put("hrApproved", "true"); | ||
| 69 | + formService.submitTaskFormData(hrTask.getId(), variables); | ||
| 70 | + | ||
| 71 | + // 销假(根据申请人的用户ID读取) | ||
| 72 | + Task reportBackTask = taskService.createTaskQuery().taskAssignee(currentUserId).singleResult(); | ||
| 73 | + assertNotNull(formService.getRenderedTaskForm(reportBackTask.getId())); | ||
| 74 | + variables = new HashMap<String, String>(); | ||
| 75 | + variables.put("reportBackDate", sdf.format(ca.getTime())); | ||
| 76 | + formService.submitTaskFormData(reportBackTask.getId(), variables); | ||
| 77 | + | ||
| 78 | + // 验证流程是否已经结束 | ||
| 79 | + HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().finished().singleResult(); | ||
| 80 | + assertNotNull(historicProcessInstance); | ||
| 81 | + | ||
| 82 | + // 读取历史变量 | ||
| 83 | + Map<String, Object> historyVariables = packageVariables(processInstance); | ||
| 84 | + | ||
| 85 | + // 验证执行结果 | ||
| 86 | + assertEquals("ok", historyVariables.get("result")); | ||
| 87 | + | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * 领导驳回后申请人取消申请 | ||
| 92 | + */ | ||
| 93 | + @Test | ||
| 94 | + @Deployment(resources = "chapter6/dynamic-form/leave.bpmn") | ||
| 95 | + public void cancelApply() throws Exception { | ||
| 96 | + | ||
| 97 | + // 设置当前用户 | ||
| 98 | + String currentUserId = "henryyan"; | ||
| 99 | + identityService.setAuthenticatedUserId(currentUserId); | ||
| 100 | + | ||
| 101 | + ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave").singleResult(); | ||
| 102 | + | ||
| 103 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
| 104 | + Map<String, String> variables = new HashMap<String, String>(); | ||
| 105 | + Calendar ca = Calendar.getInstance(); | ||
| 106 | + String startDate = sdf.format(ca.getTime()); | ||
| 107 | + ca.add(Calendar.DAY_OF_MONTH, 2); | ||
| 108 | + String endDate = sdf.format(ca.getTime()); | ||
| 109 | + | ||
| 110 | + // 启动流程 | ||
| 111 | + variables.put("startDate", startDate); | ||
| 112 | + variables.put("endDate", endDate); | ||
| 113 | + variables.put("reason", "公休"); | ||
| 114 | + ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), variables); | ||
| 115 | + assertNotNull(processInstance); | ||
| 116 | + | ||
| 117 | + // 部门领导审批通过 | ||
| 118 | + Task deptLeaderTask = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult(); | ||
| 119 | + variables = new HashMap<String, String>(); | ||
| 120 | + variables.put("deptLeaderApproved", "false"); | ||
| 121 | + formService.submitTaskFormData(deptLeaderTask.getId(), variables); | ||
| 122 | + | ||
| 123 | + // 调整申请 | ||
| 124 | + Task modifyApply = taskService.createTaskQuery().taskAssignee(currentUserId).singleResult(); | ||
| 125 | + variables = new HashMap<String, String>(); | ||
| 126 | + variables.put("reApply", "false"); | ||
| 127 | + variables.put("startDate", startDate); | ||
| 128 | + variables.put("endDate", endDate); | ||
| 129 | + variables.put("reason", "公休"); | ||
| 130 | + formService.submitTaskFormData(modifyApply.getId(), variables); | ||
| 131 | + | ||
| 132 | + // 读取历史变量 | ||
| 133 | + Map<String, Object> historyVariables = packageVariables(processInstance); | ||
| 134 | + | ||
| 135 | + // 验证执行结果 | ||
| 136 | + assertEquals("canceled", historyVariables.get("result")); | ||
| 137 | + | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + /** | ||
| 141 | + * 读取历史变量并封装到Map中 | ||
| 142 | + */ | ||
| 143 | + private Map<String, Object> packageVariables(ProcessInstance processInstance) { | ||
| 144 | + Map<String, Object> historyVariables = new HashMap<String, Object>(); | ||
| 145 | + List<HistoricDetail> list = historyService.createHistoricDetailQuery().processInstanceId(processInstance.getId()).list(); | ||
| 146 | + for (HistoricDetail historicDetail : list) { | ||
| 147 | + if (historicDetail instanceof HistoricFormProperty) { | ||
| 148 | + // 表单中的字段 | ||
| 149 | + HistoricFormProperty field = (HistoricFormProperty) historicDetail; | ||
| 150 | + historyVariables.put(field.getPropertyId(), field.getPropertyValue()); | ||
| 151 | + System.out.println("form field: taskId=" + field.getTaskId() + ", " + field.getPropertyId() + " = " + field.getPropertyValue()); | ||
| 152 | + } else if (historicDetail instanceof HistoricVariableUpdate) { | ||
| 153 | + HistoricVariableUpdate variable = (HistoricVariableUpdate) historicDetail; | ||
| 154 | + historyVariables.put(variable.getVariableName(), variable.getValue()); | ||
| 155 | + System.out.println("variable: " + variable.getVariableName() + " = " + variable.getValue()); | ||
| 156 | + } | ||
| 157 | + } | ||
| 158 | + return historyVariables; | ||
| 159 | + } | ||
| 160 | +} |
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/flowable.cfg.xml
| @@ -15,7 +15,7 @@ | @@ -15,7 +15,7 @@ | ||
| 15 | <!--HistoryLevel--> | 15 | <!--HistoryLevel--> |
| 16 | <property name="history" value="full" /> | 16 | <property name="history" value="full" /> |
| 17 | <property name="databaseSchemaUpdate" value="true" /> | 17 | <property name="databaseSchemaUpdate" value="true" /> |
| 18 | - <property name="flowable5CompatibilityEnabled" value="true" /> | 18 | + <property name="flowable5CompatibilityEnabled" value="false" /> |
| 19 | <!-- <property name="jobExecutorActivate" value="false" />--> | 19 | <!-- <property name="jobExecutorActivate" value="false" />--> |
| 20 | </bean> | 20 | </bean> |
| 21 | </beans> | 21 | </beans> |
| 22 | \ No newline at end of file | 22 | \ No newline at end of file |