diff --git a/spring-activiti/src/main/java/com/example/activitiwithspring/ActivitiController.java b/spring-activiti/src/main/java/com/example/activitiwithspring/ActivitiController.java index fd184556c4..96b551c03c 100644 --- a/spring-activiti/src/main/java/com/example/activitiwithspring/ActivitiController.java +++ b/spring-activiti/src/main/java/com/example/activitiwithspring/ActivitiController.java @@ -42,16 +42,11 @@ public class ActivitiController { } @GetMapping("/complete-task-A/{processInstanceId}") - public TaskRepresentation completeTaskA(@PathVariable String processInstanceId) { + public void completeTaskA(@PathVariable String processInstanceId) { Task task = taskService.createTaskQuery() .processInstanceId(processInstanceId) .singleResult(); taskService.complete(task.getId()); logger.info("Task completed"); - task = taskService.createTaskQuery() - .processInstanceId(processInstanceId) - .singleResult(); - - return new TaskRepresentation(task.getId(), task.getName(), task.getProcessInstanceId()); } } diff --git a/spring-activiti/src/main/java/com/example/activitiwithspring/servicetasks/SendEmailServiceTask.java b/spring-activiti/src/main/java/com/example/activitiwithspring/servicetasks/SendEmailServiceTask.java new file mode 100644 index 0000000000..c11b48f37a --- /dev/null +++ b/spring-activiti/src/main/java/com/example/activitiwithspring/servicetasks/SendEmailServiceTask.java @@ -0,0 +1,12 @@ +package com.example.activitiwithspring.servicetasks; + +import org.activiti.engine.delegate.DelegateExecution; +import org.activiti.engine.delegate.JavaDelegate; + +public class SendEmailServiceTask implements JavaDelegate { + + public void execute(DelegateExecution execution) { + //logic to sent email confirmation + } + +} diff --git a/spring-activiti/src/main/resources/processes/my-process.bpmn20.xml b/spring-activiti/src/main/resources/processes/my-process.bpmn20.xml index 3ced8d6b7c..6f151af458 100644 --- a/spring-activiti/src/main/resources/processes/my-process.bpmn20.xml +++ b/spring-activiti/src/main/resources/processes/my-process.bpmn20.xml @@ -1,66 +1,35 @@ - - + + - - - + + + - - - - - + - + - + - + - - + + - - - - - - + + + - - - - - - - + + + diff --git a/spring-activiti/src/test/java/com/example/activitiwithspring/ActivitiControllerIntegrationTest.java b/spring-activiti/src/test/java/com/example/activitiwithspring/ActivitiControllerIntegrationTest.java index baca58f6ff..65fd33bfc6 100644 --- a/spring-activiti/src/test/java/com/example/activitiwithspring/ActivitiControllerIntegrationTest.java +++ b/spring-activiti/src/test/java/com/example/activitiwithspring/ActivitiControllerIntegrationTest.java @@ -106,14 +106,12 @@ public class ActivitiControllerIntegrationTest { .get(0); logger.info("process instance = " + pi.getId()); - String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/complete-task-A/" + pi.getId())) - .andReturn() - .getResponse() - .getContentAsString(); - - ObjectMapper mapper = new ObjectMapper(); - TaskRepresentation task = mapper.readValue(responseBody, TaskRepresentation.class); - assertEquals("B", task.getName()); + this.mockMvc.perform(MockMvcRequestBuilders.get("/complete-task-A/" + pi.getId())) + .andReturn() + .getResponse() + .getContentAsString(); + List list = runtimeService.createProcessInstanceQuery().list(); + assertEquals(0, list.size()); } } diff --git a/spring-activiti/src/test/java/com/example/activitiwithspring/ProcessEngineCreationTests.java b/spring-activiti/src/test/java/com/example/activitiwithspring/ProcessEngineCreationTests.java new file mode 100644 index 0000000000..7f07191cde --- /dev/null +++ b/spring-activiti/src/test/java/com/example/activitiwithspring/ProcessEngineCreationTests.java @@ -0,0 +1,64 @@ +package com.example.activitiwithspring; + +import org.activiti.engine.ProcessEngine; +import org.activiti.engine.ProcessEngineConfiguration; +import org.activiti.engine.ProcessEngines; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.junit.Test; + +public class ProcessEngineCreationTests { + + @Test + public void givenXMLConfig_whenGetDefault_thenGotProcessEngine() { + ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); + assertNotNull(processEngine); + assertEquals("root", processEngine.getProcessEngineConfiguration().getJdbcUsername()); + } + + @Test + public void givenXMLConfig_whenCreateDefaultConfiguration_thenGotProcessEngine() { + ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault(); + ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); + assertNotNull(processEngine); + assertEquals("root", processEngine.getProcessEngineConfiguration().getJdbcUsername()); + } + + @Test + public void givenDifferentNameXMLConfig_whenGetProcessEngineConfig_thenGotResult() { + ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("my.activiti.cfg.xml"); + ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); + assertNotNull(processEngine); + assertEquals("baeldung", processEngine.getProcessEngineConfiguration().getJdbcUsername()); + } + + @Test + public void givenDifferentBeanNameInXMLConfig_whenGetProcessEngineConfig_thenGotResult() { + ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration + .createProcessEngineConfigurationFromResource("my.activiti.cfg.xml", "myProcessEngineConfiguration"); + ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); + assertNotNull(processEngine); + assertEquals("baeldung", processEngine.getProcessEngineConfiguration().getJdbcUsername()); + } + + @Test + public void givenNoXMLConfig_whenCreateInMemProcessEngineConfig_thenCreated() { + ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration(); + ProcessEngine processEngine = processEngineConfiguration + .setJdbcUrl("jdbc:h2:mem:my-own-in-mem-db;DB_CLOSE_DELAY=1000") + .buildProcessEngine(); + assertNotNull(processEngine); + assertEquals("sa", processEngine.getProcessEngineConfiguration().getJdbcUsername()); + } + + @Test + public void givenNoXMLConfig_whenCreateProcessEngineConfig_thenCreated() { + ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); + ProcessEngine processEngine = processEngineConfiguration + .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE) + .setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000") + .buildProcessEngine(); + assertNotNull(processEngine); + assertEquals("sa", processEngine.getProcessEngineConfiguration().getJdbcUsername()); + } +} diff --git a/spring-activiti/src/test/java/com/example/activitiwithspring/ProcessExecutionTests.java b/spring-activiti/src/test/java/com/example/activitiwithspring/ProcessExecutionTests.java new file mode 100644 index 0000000000..cae28d5281 --- /dev/null +++ b/spring-activiti/src/test/java/com/example/activitiwithspring/ProcessExecutionTests.java @@ -0,0 +1,97 @@ +package com.example.activitiwithspring; + + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.activiti.engine.ActivitiException; +import org.activiti.engine.ProcessEngine; +import org.activiti.engine.ProcessEngines; +import org.activiti.engine.RepositoryService; +import org.activiti.engine.RuntimeService; +import org.activiti.engine.TaskService; +import org.activiti.engine.runtime.ProcessInstance; +import org.activiti.engine.task.Task; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class ProcessExecutionTests { + + @Test + public void givenBPMN_whenDeployProcess_thenDeployed() { + ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); + RepositoryService repositoryService = processEngine.getRepositoryService(); + repositoryService.createDeployment() + .addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml") + .deploy(); + Long count = repositoryService.createProcessDefinitionQuery().count(); + assertTrue(count >= 1); + } + + @Test + public void givenProcessDefinition_whenStartProcessInstance_thenProcessRunning() { + ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); + RepositoryService repositoryService = processEngine.getRepositoryService(); + repositoryService.createDeployment() + .addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml") + .deploy(); + + Map variables = new HashMap(); + variables.put("employeeName", "Kermit"); + variables.put("numberOfDays", new Integer(4)); + variables.put("vacationMotivation", "I'm really tired!"); + + RuntimeService runtimeService = processEngine.getRuntimeService(); + ProcessInstance processInstance = runtimeService + .startProcessInstanceByKey("vacationRequest", variables); + + Long count = runtimeService.createProcessInstanceQuery().count(); + assertTrue(count >= 1); + } + + @Test + public void givenProcessInstance_whenCompleteTask_thenProcessExecutionContinues() { + ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); + RepositoryService repositoryService = processEngine.getRepositoryService(); + repositoryService.createDeployment() + .addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml") + .deploy(); + + Map variables = new HashMap(); + variables.put("employeeName", "Kermit"); + variables.put("numberOfDays", new Integer(4)); + variables.put("vacationMotivation", "I'm really tired!"); + + RuntimeService runtimeService = processEngine.getRuntimeService(); + ProcessInstance processInstance = runtimeService + .startProcessInstanceByKey("vacationRequest", variables); + + TaskService taskService = processEngine.getTaskService(); + List tasks = taskService.createTaskQuery().taskCandidateGroup("management").list(); + + Task task = tasks.get(0); + + Map taskVariables = new HashMap(); + taskVariables.put("vacationApproved", "false"); + taskVariables.put("comments", "We have a tight deadline!"); + taskService.complete(task.getId(), taskVariables); + + Task currentTask = taskService.createTaskQuery().taskName("Modify vacation request").singleResult(); + assertNotNull(currentTask); + } + + @Test(expected = ActivitiException.class) + public void givenProcessDefinition_whenSuspend_thenNoProcessInstanceCreated() { + ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); + RepositoryService repositoryService = processEngine.getRepositoryService(); + repositoryService.createDeployment() + .addClasspathResource("org/activiti/test/vacationRequest.bpmn20.xml") + .deploy(); + + RuntimeService runtimeService = processEngine.getRuntimeService(); + repositoryService.suspendProcessDefinitionByKey("vacationRequest"); + runtimeService.startProcessInstanceByKey("vacationRequest"); + } +} diff --git a/spring-activiti/src/test/resources/activiti.cfg.xml b/spring-activiti/src/test/resources/activiti.cfg.xml new file mode 100644 index 0000000000..1ee117d936 --- /dev/null +++ b/spring-activiti/src/test/resources/activiti.cfg.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + diff --git a/spring-activiti/src/test/resources/my.activiti.cfg.xml b/spring-activiti/src/test/resources/my.activiti.cfg.xml new file mode 100644 index 0000000000..07e4206dad --- /dev/null +++ b/spring-activiti/src/test/resources/my.activiti.cfg.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-activiti/src/test/resources/org/activiti/test/vacationRequest.bpmn20.xml b/spring-activiti/src/test/resources/org/activiti/test/vacationRequest.bpmn20.xml new file mode 100644 index 0000000000..354c633385 --- /dev/null +++ b/spring-activiti/src/test/resources/org/activiti/test/vacationRequest.bpmn20.xml @@ -0,0 +1,149 @@ + + + + + + + + + + + + + ${employeeName} would like to take ${numberOfDays} day(s) of vacation (Motivation: ${reason}). + + + + + + + + + + management + + + + + + + + + + Your manager has disapproved your vacation request for ${numberOfDays} days. + Reason: ${comments} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file