package com.imooc.activiti.coreapi;
import com.google.common.collect.Maps;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceBuilder;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* test
**/
public class RuntimeServiceTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryServiceTest.class);
@Rule
public ActivitiRule activitiRule = new ActivitiRule();
/**
* 根据key启动
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testStartProcess(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
//使用guava new一个hashMap, 用来装变量
Map<String,Object> variables = Maps.newHashMap();
variables.put("key1","value1");
//通过key去启动,获得流程实例对象
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process",variables);
LOGGER.info("processInstance={}",processInstance);
}
/**
* 根据Id启动
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testStartProcessById(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
//获取流程定义对象(创建流程定义)
ProcessDefinition processDefinition = activitiRule.getRepositoryService()
.createNativeProcessDefinitionQuery()
.singleResult();
//使用guava new一个hashMap, 用来装变量
Map<String,Object> variables = Maps.newHashMap();
variables.put("key1","value1");
//通过key去启动,获得流程实例对象
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId(),,variables);
LOGGER.info("processInstance={}",processInstance);
}
/**
* 测试流程实例
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testProcessInstanceBuilder(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
//使用guava new一个hashMap, 用来装变量
Map<String,Object> variables = Maps.newHashMap();
variables.put("key1","value1");
//通过key去启动,获得流程实例对象
ProcessInstanceBuilder processInstanceBuilder = runtimeService.createProcessInstanceBuilder();
ProcessInstance processInstance = processInstanceBuilder.businessKey("businessKey001")
.processDefinitionKey("my-process")
.variable(variables)
.start();
LOGGER.info("processInstance={}",processInstance);
}
/**
* 测试变量
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testVariables(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
//使用guava new一个hashMap, 用来装变量
Map<String,Object> variables = Maps.newHashMap();
variables.put("key1","value1");
variables.put("key2","value2");
//通过key去启动,获得流程实例对象
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process",variables);
LOGGER.info("processInstance={}",processInstance);
runtimeService.setVariable(processInstance.getId(),"key3","value3");
runtimeService.setVariable(processInstance.getId(),"key2","value2_1");
Map<String,Object> variables1 = runtimeService.getvariables(processInstance.getId());
LOGGER.info("variables1={}",variables1);
}
/**
*
*/
@Test
@org.activiti.engine.test.Deployment(resources = {"my-process.bpmn20.xml"})
public void testProcessInstanceQuery(){
RuntimeService runtimeService = activitiRule.getRuntimeService();
//使用guava new一个hashMap, 用来装变量
Map<String,Object> variables = Maps.newHashMap();
//通过key去启动,获得流程实例对象
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process",variables);
LOGGER.info("processInstance={}",processInstance);
//获得流程执行对象
List<Execution> executionList = runtimeService.createNativeProcessInstanceQuery()
.processInstanceId(processInstance.getId())
.listPage(0,100)
.singleResult();
//遍历流程执行对象
for(Execution execution : executionList){
LOGGER.info("execution={}",execution);
}
}
}