Browse Source

flowable-单元测试-helloworld跑通

cuicui 3 years ago
parent
commit
c5a9f5d03d

+ 23 - 8
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/java/cn/iocoder/yudao/module/bpm/AbstractOATest.java

@@ -1,6 +1,9 @@
 package cn.iocoder.yudao.module.bpm;
 
 import org.flowable.engine.*;
+import org.flowable.engine.impl.ProcessEngineImpl;
+import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
+import org.flowable.engine.impl.test.TestHelper;
 import org.flowable.engine.test.FlowableRule;
 import org.junit.AfterClass;
 import org.junit.Before;
@@ -14,12 +17,16 @@ import org.junit.Rule;
  */
 public abstract class AbstractOATest {
 
+    protected String configurationResource = "flowable.cfg.xml";
+
     /**
      * 专门用于测试套件
      */
     @Rule
     public FlowableRule activitiRule = new FlowableRule("flowable.cfg.xml");
 
+    protected ProcessEngineConfigurationImpl processEngineConfiguration;
+
     protected ProcessEngine processEngine;
     protected RepositoryService repositoryService;
     protected RuntimeService runtimeService;
@@ -45,19 +52,27 @@ public abstract class AbstractOATest {
         System.out.println("-------- 结束测试 --------");
     }
 
+    protected void initializeProcessEngine() {
+        processEngine = TestHelper.getProcessEngine(configurationResource);
+    }
+
     /**
      * 初始化变量
      */
     @Before
     public void setUp() throws Exception {
-        processEngine = activitiRule.getProcessEngine();
-        repositoryService = activitiRule.getRepositoryService();
-        runtimeService = activitiRule.getRuntimeService();
-        taskService = activitiRule.getTaskService();
-        historyService = activitiRule.getHistoryService();
-        identityService = activitiRule.getIdentityService();
-        managementService = activitiRule.getManagementService();
-        formService = activitiRule.getFormService();
+        if (processEngine == null) {
+            initializeProcessEngine();
+        }
+
+        processEngineConfiguration = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration();
+        repositoryService = processEngine.getRepositoryService();
+        runtimeService = processEngine.getRuntimeService();
+        taskService = processEngine.getTaskService();
+        historyService = processEngine.getHistoryService();
+        identityService = processEngine.getIdentityService();
+        managementService = processEngine.getManagementService();
+        formService = processEngine.getFormService();
     }
 
 }

+ 160 - 0
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/java/cn/iocoder/yudao/module/bpm/LeaveFormKeyTest.java

@@ -0,0 +1,160 @@
+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.Calendar;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * @author henryyan
+ */
+public class LeaveFormKeyTest extends AbstractOATest {
+
+    @Test
+    @Deployment(resources = {"chapter6/leave-formkey/leave-formkey.bpmn", "chapter6/leave-formkey/leave-start.form",
+            "chapter6/leave-formkey/approve-deptLeader.form", "chapter6/leave-formkey/approve-hr.form", "chapter6/leave-formkey/report-back.form",
+            "chapter6/leave-formkey/modify-apply.form"})
+    public void allPass() throws Exception {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        Map<String, String> variables = new HashMap<String, String>();
+        Calendar ca = Calendar.getInstance();
+        String startDate = sdf.format(ca.getTime());
+        ca.add(Calendar.DAY_OF_MONTH, 2); // 当前日期加2天
+        String endDate = sdf.format(ca.getTime());
+
+        // 启动流程
+        variables.put("startDate", startDate);
+        variables.put("endDate", endDate);
+        variables.put("reason", "公休");
+
+        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
+
+        // 读取启动表单
+        Object renderedStartForm = formService.getRenderedStartForm(processDefinition.getId());
+        assertNotNull(renderedStartForm);
+
+        // 启动流程
+        // 设置当前用户
+        String currentUserId = "henryyan";
+        identityService.setAuthenticatedUserId(currentUserId);
+        ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), variables);
+        assertNotNull(processInstance);
+
+        // 部门领导审批通过
+        Task deptLeaderTask = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult();
+        assertNotNull(formService.getRenderedTaskForm(deptLeaderTask.getId()));
+        variables = new HashMap<String, String>();
+        variables.put("deptLeaderApproved", "true");
+        formService.submitTaskFormData(deptLeaderTask.getId(), variables);
+
+        // 人事审批通过
+        Task hrTask = taskService.createTaskQuery().taskCandidateGroup("hr").singleResult();
+        assertNotNull(formService.getRenderedTaskForm(hrTask.getId()));
+        variables = new HashMap<String, String>();
+        variables.put("hrApproved", "true");
+        formService.submitTaskFormData(hrTask.getId(), variables);
+
+        // 销假(根据申请人的用户ID读取)
+        Task reportBackTask = taskService.createTaskQuery().taskAssignee(currentUserId).singleResult();
+        assertNotNull(formService.getRenderedTaskForm(reportBackTask.getId()));
+        variables = new HashMap<String, String>();
+        variables.put("reportBackDate", sdf.format(ca.getTime()));
+        formService.submitTaskFormData(reportBackTask.getId(), variables);
+
+        // 验证流程是否已经结束
+        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().finished().singleResult();
+        assertNotNull(historicProcessInstance);
+
+        // 读取历史变量
+        Map<String, Object> historyVariables = packageVariables(processInstance);
+
+        // 验证执行结果
+        assertEquals("ok", historyVariables.get("result"));
+
+    }
+
+    /**
+     * 领导驳回后申请人取消申请
+     */
+    @Test
+    @Deployment(resources = "chapter6/dynamic-form/leave.bpmn")
+    public void cancelApply() throws Exception {
+
+        // 设置当前用户
+        String currentUserId = "henryyan";
+        identityService.setAuthenticatedUserId(currentUserId);
+
+        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave").singleResult();
+
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        Map<String, String> variables = new HashMap<String, String>();
+        Calendar ca = Calendar.getInstance();
+        String startDate = sdf.format(ca.getTime());
+        ca.add(Calendar.DAY_OF_MONTH, 2);
+        String endDate = sdf.format(ca.getTime());
+
+        // 启动流程
+        variables.put("startDate", startDate);
+        variables.put("endDate", endDate);
+        variables.put("reason", "公休");
+        ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), variables);
+        assertNotNull(processInstance);
+
+        // 部门领导审批通过
+        Task deptLeaderTask = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult();
+        variables = new HashMap<String, String>();
+        variables.put("deptLeaderApproved", "false");
+        formService.submitTaskFormData(deptLeaderTask.getId(), variables);
+
+        // 调整申请
+        Task modifyApply = taskService.createTaskQuery().taskAssignee(currentUserId).singleResult();
+        variables = new HashMap<String, String>();
+        variables.put("reApply", "false");
+        variables.put("startDate", startDate);
+        variables.put("endDate", endDate);
+        variables.put("reason", "公休");
+        formService.submitTaskFormData(modifyApply.getId(), variables);
+
+        // 读取历史变量
+        Map<String, Object> historyVariables = packageVariables(processInstance);
+
+        // 验证执行结果
+        assertEquals("canceled", historyVariables.get("result"));
+
+    }
+
+    /**
+     * 读取历史变量并封装到Map中
+     */
+    private Map<String, Object> packageVariables(ProcessInstance processInstance) {
+        Map<String, Object> historyVariables = new HashMap<String, Object>();
+        List<HistoricDetail> list = historyService.createHistoricDetailQuery().processInstanceId(processInstance.getId()).list();
+        for (HistoricDetail historicDetail : list) {
+            if (historicDetail instanceof HistoricFormProperty) {
+                // 表单中的字段
+                HistoricFormProperty field = (HistoricFormProperty) historicDetail;
+                historyVariables.put(field.getPropertyId(), field.getPropertyValue());
+                System.out.println("form field: taskId=" + field.getTaskId() + ", " + field.getPropertyId() + " = " + field.getPropertyValue());
+            } else if (historicDetail instanceof HistoricVariableUpdate) {
+                HistoricVariableUpdate variable = (HistoricVariableUpdate) historicDetail;
+                historyVariables.put(variable.getVariableName(), variable.getValue());
+                System.out.println("variable: " + variable.getVariableName() + " = " + variable.getValue());
+            }
+        }
+        return historyVariables;
+    }
+}

+ 1 - 1
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/test/resources/flowable.cfg.xml

@@ -15,7 +15,7 @@
 		<!--HistoryLevel-->
 		<property name="history" value="full" />
 		<property name="databaseSchemaUpdate" value="true" />
-		<property name="flowable5CompatibilityEnabled" value="true" />
+		<property name="flowable5CompatibilityEnabled" value="false" />
 <!--		<property name="jobExecutorActivate" value="false" />-->
 	</bean>
 </beans>