BAEL-79 Intro to Activiti with Spring (#2127)

* Example Code For Evaluation Article

This is an example code for the evaluation article on "Different Types of Bean Injection in Spring"

* Added unit tests

* Minor changes to application context

* Removed code committed for evaluation article

* BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5

* BAEL-944 Demonstrating the problems with new Url pattern matching in Spring 5

* BAEL-944 Exploring the Spring MVC URL Matching Improvements

* BAEL-944 Exploring the Spring MVC URL Matching Improvements

* BAEL-944 Exploring the Spring MVC URL Matching Improvements

* BAEL-944 Code Formatting and solving build issue

* BAEL-944 Resolving build issue due to change in Spring version

* BAEL-944 Resolving build issue

* BAEL-944 Formatting code

* BAEL-944 Moving tests to correct package

* BAEL-944 Moving tests to correct package

* BAEL-944 Replacing @RequestMapping by @GetMapping

* BAEL-944 Remove unnecessary attribute name, "value" in annotations

* BAEL-79 Intro to Activiti with Spring

* BAEL-79 Intro to Activiti with Spring

* BAEL-79 Adding activiti module to the parent modules

* BAEL-79 Using latest version

* BAEL-79 Update Spring boot version that works with Activiti

* BAEL-79 Replace RequestMapping with GetMapping

* BAEL-79 Use Java 8 Syntax

* BAEL-79 Formatting

* BAEL-79 changed module name
This commit is contained in:
Mansi 2017-07-09 14:05:11 +05:30 committed by Grzegorz Piwowarek
parent 13eec7e57c
commit e704d296bf
8 changed files with 379 additions and 0 deletions

View File

@ -25,6 +25,7 @@
</properties>
<modules>
<module>spring-activiti</module>
<module>aws</module>
<module>akka-streams</module>
<module>algorithms</module>

60
spring-activiti/pom.xml Normal file
View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-activiti</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-activiti</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,60 @@
package com.example.activitiwithspring;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ActivitiController {
private static final Logger logger = LoggerFactory.getLogger(ActivitiController.class);
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@GetMapping("/start-process")
public String startProcess() {
runtimeService.startProcessInstanceByKey("my-process");
return "Process started. Number of currently running process instances = " + runtimeService.createProcessInstanceQuery()
.count();
}
@GetMapping("/get-tasks/{processInstanceId}")
public List<TaskRepresentation> getTasks(@PathVariable String processInstanceId) {
List<Task> usertasks = taskService.createTaskQuery()
.processInstanceId(processInstanceId)
.list();
List<TaskRepresentation> tasks = usertasks.stream().map(task -> {
TaskRepresentation taskRepresentation = new TaskRepresentation(task.getId(), task.getName(), task.getProcessInstanceId());
return taskRepresentation;
}).collect(Collectors.toList());
return tasks;
}
@GetMapping("/complete-task-A/{processInstanceId}")
public TaskRepresentation 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());
}
}

View File

@ -0,0 +1,12 @@
package com.example.activitiwithspring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ActivitiWithSpringApplication {
public static void main(String[] args) {
SpringApplication.run(ActivitiWithSpringApplication.class, args);
}
}

View File

@ -0,0 +1,43 @@
package com.example.activitiwithspring;
class TaskRepresentation {
private String id;
private String name;
private String processInstanceId;
public TaskRepresentation() {
super();
}
public TaskRepresentation(String id, String name, String processInstanceId) {
this.id = id;
this.name = name;
this.processInstanceId = processInstanceId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProcessInstanceId() {
return processInstanceId;
}
public void setProcessInstanceId(String processInstanceId) {
this.processInstanceId = processInstanceId;
}
}

View File

@ -0,0 +1,67 @@
<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
targetNamespace="http://www.activiti.org/processdef" xmlns:modeler="http://activiti.com/modeler"
modeler:version="1.0ev" modeler:exportDateTime="20170619132653"
modeler:modelId="1005315" modeler:modelVersion="1"
modeler:modelLastUpdated="1497875208422">
<process id="my-process" name="my-process"
isExecutable="true">
<startEvent id="startEvent" name="startEvent">
</startEvent>
<sequenceFlow id="sid-1A81F784-4743-4CD6-B81D-A6F61DFBDAF4"
sourceRef="startEvent" targetRef="sid-293091EF-68C4-4A5C-B747-B1C46809CB69">
</sequenceFlow>
<userTask id="sid-293091EF-68C4-4A5C-B747-B1C46809CB69" name="A"
activiti:assignee="$INITIATOR">
</userTask>
<sequenceFlow id="sid-EA5C6B84-3FF8-4B39-8961-92F1E230F3FB"
sourceRef="sid-293091EF-68C4-4A5C-B747-B1C46809CB69" targetRef="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010">
</sequenceFlow>
<userTask id="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010" name="B"
activiti:assignee="$INITIATOR">
</userTask>
<sequenceFlow id="sid-CB9C412E-AD0B-4EFA-B160-E1DA2507A3D9"
sourceRef="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010" targetRef="endEvent">
</sequenceFlow>
<endEvent id="endEvent" name="endEvent">
</endEvent>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_say-hello-process">
<bpmndi:BPMNPlane bpmnElement="say-hello-process"
id="BPMNPlane_say-hello-process">
<bpmndi:BPMNShape bpmnElement="startEvent" id="BPMNShape_startEvent">
<omgdc:Bounds height="30.0" width="30.0" x="120.0" y="163.0" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endEvent" id="BPMNShape_endEvent">
<omgdc:Bounds height="28.0" width="28.0" x="525.0" y="164.0" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-293091EF-68C4-4A5C-B747-B1C46809CB69"
id="BPMNShape_sid-293091EF-68C4-4A5C-B747-B1C46809CB69">
<omgdc:Bounds height="80.0" width="100.0" x="210.0" y="138.0" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010"
id="BPMNShape_sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010">
<omgdc:Bounds height="80.0" width="100.0" x="360.0" y="138.0" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-CB9C412E-AD0B-4EFA-B160-E1DA2507A3D9"
id="BPMNEdge_sid-CB9C412E-AD0B-4EFA-B160-E1DA2507A3D9">
<omgdi:waypoint x="460.0" y="178.0" />
<omgdi:waypoint x="525.0" y="178.0" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-EA5C6B84-3FF8-4B39-8961-92F1E230F3FB"
id="BPMNEdge_sid-EA5C6B84-3FF8-4B39-8961-92F1E230F3FB">
<omgdi:waypoint x="310.0" y="178.0" />
<omgdi:waypoint x="360.0" y="178.0" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-1A81F784-4743-4CD6-B81D-A6F61DFBDAF4"
id="BPMNEdge_sid-1A81F784-4743-4CD6-B81D-A6F61DFBDAF4">
<omgdi:waypoint x="150.0" y="178.0" />
<omgdi:waypoint x="210.0" y="178.0" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

View File

@ -0,0 +1,120 @@
package com.example.activitiwithspring;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest
public class ActivitiControllerTest {
private static final Logger logger = LoggerFactory.getLogger(ActivitiControllerTest.class);
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Autowired
RuntimeService runtimeService;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
.build();
for (ProcessInstance instance : runtimeService.createProcessInstanceQuery()
.list()) {
runtimeService.deleteProcessInstance(instance.getId(), "Reset Processes");
}
}
@Test
public void givenProcess_whenStartProcess_thenIncreaseInProcessInstanceCount() throws Exception {
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
.andReturn()
.getResponse()
.getContentAsString();
assertEquals("Process started. Number of currently running process instances = 1", responseBody);
responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
.andReturn()
.getResponse()
.getContentAsString();
assertEquals("Process started. Number of currently running process instances = 2", responseBody);
responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
.andReturn()
.getResponse()
.getContentAsString();
assertEquals("Process started. Number of currently running process instances = 3", responseBody);
}
@Test
public void givenProcess_whenProcessInstance_thenReceivedRunningTask() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
.andReturn()
.getResponse();
ProcessInstance pi = runtimeService.createProcessInstanceQuery()
.orderByProcessInstanceId()
.desc()
.list()
.get(0);
logger.info("process instance = " + pi.getId());
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/get-tasks/" + pi.getId()))
.andReturn()
.getResponse()
.getContentAsString();
ObjectMapper mapper = new ObjectMapper();
List<TaskRepresentation> tasks = Arrays.asList(mapper.readValue(responseBody, TaskRepresentation[].class));
assertEquals(1, tasks.size());
assertEquals("A", tasks.get(0).getName());
}
@Test
public void givenProcess_whenCompleteTaskA_thenReceivedNextTask() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
.andReturn()
.getResponse();
ProcessInstance pi = runtimeService.createProcessInstanceQuery()
.orderByProcessInstanceId()
.desc()
.list()
.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());
}
}

View File

@ -0,0 +1,16 @@
package com.example.activitiwithspring;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiWithSpringApplicationTests {
@Test
public void contextLoads() {
}
}