|
| 1 | +package com.example.activitiwithspring; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.activiti.engine.ActivitiException; |
| 9 | +import org.activiti.engine.ProcessEngine; |
| 10 | +import org.activiti.engine.ProcessEngines; |
| 11 | +import org.activiti.engine.RepositoryService; |
| 12 | +import org.activiti.engine.RuntimeService; |
| 13 | +import org.activiti.engine.TaskService; |
| 14 | +import org.activiti.engine.runtime.ProcessInstance; |
| 15 | +import org.activiti.engine.task.Task; |
| 16 | +import static org.junit.Assert.assertNotNull; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +public class ProcessExecutionTests { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenBPMN_whenDeployProcess_thenDeployed() { |
| 24 | + ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); |
| 25 | + RepositoryService repositoryService = processEngine.getRepositoryService(); |
| 26 | + repositoryService.createDeployment() |
| 27 | + .addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml") |
| 28 | + .deploy(); |
| 29 | + Long count = repositoryService.createProcessDefinitionQuery().count(); |
| 30 | + assertTrue(count >= 1); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void givenProcessDefinition_whenStartProcessInstance_thenProcessRunning() { |
| 35 | + ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); |
| 36 | + RepositoryService repositoryService = processEngine.getRepositoryService(); |
| 37 | + repositoryService.createDeployment() |
| 38 | + .addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml") |
| 39 | + .deploy(); |
| 40 | + |
| 41 | + Map<String, Object> variables = new HashMap<String, Object>(); |
| 42 | + variables.put("employeeName", "Kermit"); |
| 43 | + variables.put("numberOfDays", new Integer(4)); |
| 44 | + variables.put("vacationMotivation", "I'm really tired!"); |
| 45 | + |
| 46 | + RuntimeService runtimeService = processEngine.getRuntimeService(); |
| 47 | + ProcessInstance processInstance = runtimeService |
| 48 | + .startProcessInstanceByKey("vacationRequest", variables); |
| 49 | + |
| 50 | + Long count = runtimeService.createProcessInstanceQuery().count(); |
| 51 | + assertTrue(count >= 1); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenProcessInstance_whenCompleteTask_thenProcessExecutionContinues() { |
| 56 | + ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); |
| 57 | + RepositoryService repositoryService = processEngine.getRepositoryService(); |
| 58 | + repositoryService.createDeployment() |
| 59 | + .addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml") |
| 60 | + .deploy(); |
| 61 | + |
| 62 | + Map<String, Object> variables = new HashMap<String, Object>(); |
| 63 | + variables.put("employeeName", "Kermit"); |
| 64 | + variables.put("numberOfDays", new Integer(4)); |
| 65 | + variables.put("vacationMotivation", "I'm really tired!"); |
| 66 | + |
| 67 | + RuntimeService runtimeService = processEngine.getRuntimeService(); |
| 68 | + ProcessInstance processInstance = runtimeService |
| 69 | + .startProcessInstanceByKey("vacationRequest", variables); |
| 70 | + |
| 71 | + TaskService taskService = processEngine.getTaskService(); |
| 72 | + List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("management").list(); |
| 73 | + |
| 74 | + Task task = tasks.get(0); |
| 75 | + |
| 76 | + Map<String, Object> taskVariables = new HashMap<String, Object>(); |
| 77 | + taskVariables.put("vacationApproved", "false"); |
| 78 | + taskVariables.put("comments", "We have a tight deadline!"); |
| 79 | + taskService.complete(task.getId(), taskVariables); |
| 80 | + |
| 81 | + Task currentTask = taskService.createTaskQuery().taskName("Modify vacation request").singleResult(); |
| 82 | + assertNotNull(currentTask); |
| 83 | + } |
| 84 | + |
| 85 | + @Test(expected = ActivitiException.class) |
| 86 | + public void givenProcessDefinition_whenSuspend_thenNoProcessInstanceCreated() { |
| 87 | + ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); |
| 88 | + RepositoryService repositoryService = processEngine.getRepositoryService(); |
| 89 | + repositoryService.createDeployment() |
| 90 | + .addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml") |
| 91 | + .deploy(); |
| 92 | + |
| 93 | + RuntimeService runtimeService = processEngine.getRuntimeService(); |
| 94 | + repositoryService.suspendProcessDefinitionByKey("vacationRequest"); |
| 95 | + runtimeService.startProcessInstanceByKey("vacationRequest"); |
| 96 | + } |
| 97 | +} |
0 commit comments