content: package com.alibaba.smart.framework.engine.test.process;
  
  import com.alibaba.smart.framework.engine.constant.DeploymentStatusConstant;
  import com.alibaba.smart.framework.engine.constant.RequestMapSpecialKeyConstant;
  import com.alibaba.smart.framework.engine.model.instance.DeploymentInstance;
  import com.alibaba.smart.framework.engine.model.instance.ExecutionInstance;
  import com.alibaba.smart.framework.engine.model.instance.InstanceStatus;
  import com.alibaba.smart.framework.engine.model.instance.ProcessInstance;
  import com.alibaba.smart.framework.engine.model.instance.TaskInstance;
  import com.alibaba.smart.framework.engine.service.param.command.CreateDeploymentCommand;
  import com.alibaba.smart.framework.engine.service.param.query.PendingTaskQueryParam;
  import com.alibaba.smart.framework.engine.test.DatabaseBaseTestCase;
  import com.alibaba.smart.framework.engine.test.process.bean.Order;
  import com.alibaba.smart.framework.engine.test.process.helper.CustomExceptioinProcessor;
  import com.alibaba.smart.framework.engine.test.process.helper.CustomVariablePersister;
  import com.alibaba.smart.framework.engine.test.process.helper.DefaultMultiInstanceCounter;
  import com.alibaba.smart.framework.engine.test.process.helper.DoNothingLockStrategy;
  import com.alibaba.smart.framework.engine.test.process.helper.dispatcher.DefaultTaskAssigneeDispatcher;
  import com.alibaba.smart.framework.engine.util.IOUtil;
  import org.junit.Assert;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.transaction.annotation.Transactional;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  @ContextConfiguration("/spring/application-test.xml")
  @RunWith(SpringJUnit4ClassRunner.class)
  @Transactional
  public class CompatibleActivitiAndCustomExtensionProcessWithTenantTest extends DatabaseBaseTestCase {
  
      @Override
      protected void initProcessConfiguration() {
          super.initProcessConfiguration();
          processEngineConfiguration.setExceptionProcessor(new CustomExceptioinProcessor());
          processEngineConfiguration.setTaskAssigneeDispatcher(new DefaultTaskAssigneeDispatcher());
          processEngineConfiguration.setMultiInstanceCounter(new DefaultMultiInstanceCounter());
          processEngineConfiguration.setVariablePersister(new CustomVariablePersister());
          processEngineConfiguration.setLockStrategy(new DoNothingLockStrategy());
      }
  
  
  
      @Test
      public void testMultiInstance() throws Exception {
  
  
          String tenantId = "-1";
          //3. 部署流程定义
          CreateDeploymentCommand createDeploymentCommand = new CreateDeploymentCommand();
          String content = IOUtil.readResourceFileAsUTF8String(
              "compatible-activiti-and-custom-extension-process-test.bpmn20.xml");
          createDeploymentCommand.setProcessDefinitionContent(content);
          createDeploymentCommand.setDeploymentUserId("123");
          createDeploymentCommand.setDeploymentStatus(DeploymentStatusConstant.ACTIVE);
          createDeploymentCommand.setProcessDefinitionDesc("desc");
          createDeploymentCommand.setProcessDefinitionName("name");
          createDeploymentCommand.setProcessDefinitionType("group");
          createDeploymentCommand.setTenantId(tenantId);
  
          DeploymentInstance deploymentInstance =  deploymentCommandService.createDeployment(createDeploymentCommand);
  
  
          //4.启动流程实例
  
          Map<String, Object> request = new HashMap();
          request.put(RequestMapSpecialKeyConstant.PROCESS_INSTANCE_START_USER_ID,"123");
          request.put(RequestMapSpecialKeyConstant.PROCESS_DEFINITION_TYPE,"group");
          request.put("processVariable","processVariableValue");
          request.put(RequestMapSpecialKeyConstant.TENANT_ID,tenantId);
  
  
          ProcessInstance processInstance = processCommandService.start(
              deploymentInstance.getProcessDefinitionId(), deploymentInstance.getProcessDefinitionVersion()
                ,request  );
          Assert.assertNotNull(processInstance);
          Assert.assertEquals("group",processInstance.getProcessDefinitionType());
  
  
  
          List<TaskInstance> submitTaskInstanceList=  taskQueryService.findAllPendingTaskList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(3,submitTaskInstanceList.size());
          TaskInstance submitTaskInstance = submitTaskInstanceList.get(0);
  
  
  
          //5.流程流转:构造提交申请参数
          Map<String, Object> submitFormRequest = new HashMap<String, Object>();
          submitFormRequest.put("title", "new_title");
          submitFormRequest.put("qps", "300");
          submitFormRequest.put("capacity","10g");
          //submitFormRequest.put("assigneeUserId","1");
          submitFormRequest.put(RequestMapSpecialKeyConstant.TASK_INSTANCE_TAG, VariableInstanceAndMultiInstanceTest.AGREE);
          submitFormRequest.put(RequestMapSpecialKeyConstant.TASK_INSTANCE_CLAIM_USER_ID,"5");
          submitFormRequest.put("text","123");
          submitFormRequest.put(RequestMapSpecialKeyConstant.TENANT_ID,tenantId);
  
          //submitFormRequest.put(RequestMapSpecialKeyConstant.PROCESS_DEFINITION_TYPE,"group");
  
  
  
          //6.流程流转:处理 submitTask,完成任务申请. 完成userTask1 节点的第一个任务
          taskCommandService.complete(submitTaskInstance.getInstanceId(),submitFormRequest);
  
  
          PendingTaskQueryParam pendingTaskQueryParam = new PendingTaskQueryParam();
          pendingTaskQueryParam.setAssigneeUserId("3");
          pendingTaskQueryParam.setTenantId(tenantId);
  
          List<TaskInstance>  assertedTaskInstanceList=   taskQueryService.findAllPendingTaskList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(2,assertedTaskInstanceList.size());
          Assert.assertEquals("userTask1",assertedTaskInstanceList.get(0).getProcessDefinitionActivityId());
  
  
          List<ExecutionInstance> activeExecutions = executionQueryService.findActiveExecutionList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(2,activeExecutions.size());
          submitTaskInstanceList=  taskQueryService.findAllPendingTaskList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(2,submitTaskInstanceList.size());
          //完成userTask1 节点的第二个任务
          taskCommandService.complete(submitTaskInstanceList.get(0).getInstanceId(),submitFormRequest);
  
          activeExecutions = executionQueryService.findActiveExecutionList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(1,activeExecutions.size());
          submitTaskInstanceList=  taskQueryService.findAllPendingTaskList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(1,submitTaskInstanceList.size());
          //完成userTask1 节点的第三个任务
          taskCommandService.complete(submitTaskInstanceList.get(0).getInstanceId(),submitFormRequest);
  
          //至此,上面3个节点都应该被完成了. 此时会进入新的会签节点.
          submitTaskInstanceList=  taskQueryService.findAllPendingTaskList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(3,submitTaskInstanceList.size());
          Assert.assertEquals("userTask2",submitTaskInstanceList.get(0).getProcessDefinitionActivityId());
  
          Order order = new Order();
          order.setYzje(101.02);
          submitFormRequest.put("order",order);
  
          // 由于userTask2 这个节点的 completionCondition 为 null，所以会是 all 模式。此任务完成后，会进入下一个环节。
          taskCommandService.complete(submitTaskInstanceList.get(0).getInstanceId(),submitFormRequest);
  
          //根据网关选择,此时会进入新的会签节点.
          submitTaskInstanceList=  taskQueryService.findAllPendingTaskList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(3,submitTaskInstanceList.size());
          Assert.assertEquals("userTask3",submitTaskInstanceList.get(0).getProcessDefinitionActivityId());
  
          taskCommandService.complete(submitTaskInstanceList.get(0).getInstanceId(),submitFormRequest);
  
          //此时会进入新的会签节点.
          submitTaskInstanceList=  taskQueryService.findAllPendingTaskList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(3,submitTaskInstanceList.size());
          Assert.assertEquals("UserTask_0ixdrmt",submitTaskInstanceList.get(0).getProcessDefinitionActivityId());
  
          taskCommandService.complete(submitTaskInstanceList.get(0).getInstanceId(),submitFormRequest);
  
  
          //10.由于流程测试已经关闭,需要断言没有需要处理的人,状态关闭.
          ProcessInstance finalProcessInstance = processQueryService.findById(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(InstanceStatus.completed,finalProcessInstance.getStatus());
      }
  
  
      @Test
      public void testCouterSign_Fail_All_modle() throws Exception {
  
  
          String tenantId = "-1";
  
          //3. 部署流程定义
          CreateDeploymentCommand createDeploymentCommand = new CreateDeploymentCommand();
          String content = IOUtil.readResourceFileAsUTF8String(
              "compatible-activiti-and-custom-extension-process-test.bpmn20.xml");
          createDeploymentCommand.setProcessDefinitionContent(content);
          createDeploymentCommand.setDeploymentUserId("123");
          createDeploymentCommand.setDeploymentStatus(DeploymentStatusConstant.ACTIVE);
          createDeploymentCommand.setProcessDefinitionDesc("desc");
          createDeploymentCommand.setProcessDefinitionName("name");
          createDeploymentCommand.setProcessDefinitionType("group");
          createDeploymentCommand.setTenantId(tenantId);
  
          DeploymentInstance deploymentInstance =  deploymentCommandService.createDeployment(createDeploymentCommand);
  
  
          //4.启动流程实例
  
          Map<String, Object> request = new HashMap();
          request.put(RequestMapSpecialKeyConstant.PROCESS_INSTANCE_START_USER_ID,"123");
          request.put(RequestMapSpecialKeyConstant.PROCESS_DEFINITION_TYPE,"group");
          request.put("processVariable","processVariableValue");
          request.put(RequestMapSpecialKeyConstant.TENANT_ID,tenantId);
  
  
          ProcessInstance processInstance = processCommandService.start(
              deploymentInstance.getProcessDefinitionId(), deploymentInstance.getProcessDefinitionVersion()
              ,request  );
          Assert.assertNotNull(processInstance);
          Assert.assertEquals("group",processInstance.getProcessDefinitionType());
  
  
          List<TaskInstance> submitTaskInstanceList=  taskQueryService.findAllPendingTaskList(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(3,submitTaskInstanceList.size());
          TaskInstance submitTaskInstance = submitTaskInstanceList.get(0);
  
  
  
          //5.流程流转:构造提交申请参数
          Map<String, Object> submitFormRequest = new HashMap<String, Object>();
          submitFormRequest.put("title", "new_title");
          submitFormRequest.put("qps", "300");
          submitFormRequest.put("capacity","10g");
          //submitFormRequest.put("assigneeUserId","1");
          submitFormRequest.put(RequestMapSpecialKeyConstant.TASK_INSTANCE_TAG, VariableInstanceAndMultiInstanceTest.DISAGREE);
          submitFormRequest.put("text","123");
  
          //submitFormRequest.put(RequestMapSpecialKeyConstant.PROCESS_DEFINITION_TYPE,"group");
  
          taskCommandService.complete(submitTaskInstance.getInstanceId(),submitFormRequest);
  
  
  
          //6.流程流转:处理 submitTask,完成任务申请.
  
          //10.由于流程测试已经关闭,需要断言没有需要处理的人,状态关闭.
          ProcessInstance finalProcessInstance = processQueryService.findById(processInstance.getInstanceId(),processInstance.getTenantId());
          Assert.assertEquals(InstanceStatus.aborted,finalProcessInstance.getStatus());
      }
  
  
  
  
  
  }