Skip to content

Commit e704d29

Browse files
mansi2392pivovarit
authored andcommitted
BAEL-79 Intro to Activiti with Spring (eugenp#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
1 parent 13eec7e commit e704d29

8 files changed

Lines changed: 379 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</properties>
2626

2727
<modules>
28+
<module>spring-activiti</module>
2829
<module>aws</module>
2930
<module>akka-streams</module>
3031
<module>algorithms</module>

‎spring-activiti/pom.xml‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>spring-activiti</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-activiti</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.4.RELEASE</version>
18+
<relativePath />
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.activiti</groupId>
30+
<artifactId>activiti-spring-boot-starter-basic</artifactId>
31+
<version>6.0.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>com.h2database</groupId>
40+
<artifactId>h2</artifactId>
41+
<scope>runtime</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
60+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.example.activitiwithspring;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
import org.activiti.engine.RuntimeService;
8+
import org.activiti.engine.TaskService;
9+
import org.activiti.engine.task.Task;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.PathVariable;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RestController;
17+
18+
@RestController
19+
public class ActivitiController {
20+
private static final Logger logger = LoggerFactory.getLogger(ActivitiController.class);
21+
@Autowired
22+
private RuntimeService runtimeService;
23+
24+
@Autowired
25+
private TaskService taskService;
26+
27+
@GetMapping("/start-process")
28+
public String startProcess() {
29+
runtimeService.startProcessInstanceByKey("my-process");
30+
return "Process started. Number of currently running process instances = " + runtimeService.createProcessInstanceQuery()
31+
.count();
32+
}
33+
34+
@GetMapping("/get-tasks/{processInstanceId}")
35+
public List<TaskRepresentation> getTasks(@PathVariable String processInstanceId) {
36+
List<Task> usertasks = taskService.createTaskQuery()
37+
.processInstanceId(processInstanceId)
38+
.list();
39+
40+
List<TaskRepresentation> tasks = usertasks.stream().map(task -> {
41+
TaskRepresentation taskRepresentation = new TaskRepresentation(task.getId(), task.getName(), task.getProcessInstanceId());
42+
return taskRepresentation;
43+
}).collect(Collectors.toList());
44+
return tasks;
45+
}
46+
47+
@GetMapping("/complete-task-A/{processInstanceId}")
48+
public TaskRepresentation completeTaskA(@PathVariable String processInstanceId) {
49+
Task task = taskService.createTaskQuery()
50+
.processInstanceId(processInstanceId)
51+
.singleResult();
52+
taskService.complete(task.getId());
53+
logger.info("Task completed");
54+
task = taskService.createTaskQuery()
55+
.processInstanceId(processInstanceId)
56+
.singleResult();
57+
58+
return new TaskRepresentation(task.getId(), task.getName(), task.getProcessInstanceId());
59+
}
60+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.activitiwithspring;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ActivitiWithSpringApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ActivitiWithSpringApplication.class, args);
11+
}
12+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.activitiwithspring;
2+
3+
class TaskRepresentation {
4+
5+
private String id;
6+
private String name;
7+
private String processInstanceId;
8+
9+
public TaskRepresentation() {
10+
super();
11+
}
12+
13+
public TaskRepresentation(String id, String name, String processInstanceId) {
14+
this.id = id;
15+
this.name = name;
16+
this.processInstanceId = processInstanceId;
17+
}
18+
19+
public String getId() {
20+
return id;
21+
}
22+
23+
public void setId(String id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public String getProcessInstanceId() {
36+
return processInstanceId;
37+
}
38+
39+
public void setProcessInstanceId(String processInstanceId) {
40+
this.processInstanceId = processInstanceId;
41+
}
42+
43+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4+
xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
5+
xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
6+
typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
7+
targetNamespace="http://www.activiti.org/processdef" xmlns:modeler="http://activiti.com/modeler"
8+
modeler:version="1.0ev" modeler:exportDateTime="20170619132653"
9+
modeler:modelId="1005315" modeler:modelVersion="1"
10+
modeler:modelLastUpdated="1497875208422">
11+
<process id="my-process" name="my-process"
12+
isExecutable="true">
13+
<startEvent id="startEvent" name="startEvent">
14+
</startEvent>
15+
<sequenceFlow id="sid-1A81F784-4743-4CD6-B81D-A6F61DFBDAF4"
16+
sourceRef="startEvent" targetRef="sid-293091EF-68C4-4A5C-B747-B1C46809CB69">
17+
</sequenceFlow>
18+
<userTask id="sid-293091EF-68C4-4A5C-B747-B1C46809CB69" name="A"
19+
activiti:assignee="$INITIATOR">
20+
</userTask>
21+
<sequenceFlow id="sid-EA5C6B84-3FF8-4B39-8961-92F1E230F3FB"
22+
sourceRef="sid-293091EF-68C4-4A5C-B747-B1C46809CB69" targetRef="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010">
23+
</sequenceFlow>
24+
<userTask id="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010" name="B"
25+
activiti:assignee="$INITIATOR">
26+
</userTask>
27+
<sequenceFlow id="sid-CB9C412E-AD0B-4EFA-B160-E1DA2507A3D9"
28+
sourceRef="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010" targetRef="endEvent">
29+
</sequenceFlow>
30+
<endEvent id="endEvent" name="endEvent">
31+
</endEvent>
32+
</process>
33+
<bpmndi:BPMNDiagram id="BPMNDiagram_say-hello-process">
34+
<bpmndi:BPMNPlane bpmnElement="say-hello-process"
35+
id="BPMNPlane_say-hello-process">
36+
<bpmndi:BPMNShape bpmnElement="startEvent" id="BPMNShape_startEvent">
37+
<omgdc:Bounds height="30.0" width="30.0" x="120.0" y="163.0" />
38+
</bpmndi:BPMNShape>
39+
<bpmndi:BPMNShape bpmnElement="endEvent" id="BPMNShape_endEvent">
40+
<omgdc:Bounds height="28.0" width="28.0" x="525.0" y="164.0" />
41+
</bpmndi:BPMNShape>
42+
<bpmndi:BPMNShape bpmnElement="sid-293091EF-68C4-4A5C-B747-B1C46809CB69"
43+
id="BPMNShape_sid-293091EF-68C4-4A5C-B747-B1C46809CB69">
44+
<omgdc:Bounds height="80.0" width="100.0" x="210.0" y="138.0" />
45+
</bpmndi:BPMNShape>
46+
<bpmndi:BPMNShape bpmnElement="sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010"
47+
id="BPMNShape_sid-7EAE076A-BCD0-416E-98E2-23A7E37C4010">
48+
<omgdc:Bounds height="80.0" width="100.0" x="360.0" y="138.0" />
49+
</bpmndi:BPMNShape>
50+
<bpmndi:BPMNEdge bpmnElement="sid-CB9C412E-AD0B-4EFA-B160-E1DA2507A3D9"
51+
id="BPMNEdge_sid-CB9C412E-AD0B-4EFA-B160-E1DA2507A3D9">
52+
<omgdi:waypoint x="460.0" y="178.0" />
53+
<omgdi:waypoint x="525.0" y="178.0" />
54+
</bpmndi:BPMNEdge>
55+
<bpmndi:BPMNEdge bpmnElement="sid-EA5C6B84-3FF8-4B39-8961-92F1E230F3FB"
56+
id="BPMNEdge_sid-EA5C6B84-3FF8-4B39-8961-92F1E230F3FB">
57+
<omgdi:waypoint x="310.0" y="178.0" />
58+
<omgdi:waypoint x="360.0" y="178.0" />
59+
</bpmndi:BPMNEdge>
60+
<bpmndi:BPMNEdge bpmnElement="sid-1A81F784-4743-4CD6-B81D-A6F61DFBDAF4"
61+
id="BPMNEdge_sid-1A81F784-4743-4CD6-B81D-A6F61DFBDAF4">
62+
<omgdi:waypoint x="150.0" y="178.0" />
63+
<omgdi:waypoint x="210.0" y="178.0" />
64+
</bpmndi:BPMNEdge>
65+
</bpmndi:BPMNPlane>
66+
</bpmndi:BPMNDiagram>
67+
</definitions>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.example.activitiwithspring;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import org.activiti.engine.RuntimeService;
9+
import org.activiti.engine.runtime.ProcessInstance;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.boot.test.context.SpringBootTest;
17+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18+
import org.springframework.test.context.web.WebAppConfiguration;
19+
import org.springframework.test.web.servlet.MockMvc;
20+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
21+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
22+
import org.springframework.web.context.WebApplicationContext;
23+
24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
26+
@RunWith(SpringJUnit4ClassRunner.class)
27+
@WebAppConfiguration
28+
@SpringBootTest
29+
public class ActivitiControllerTest {
30+
private static final Logger logger = LoggerFactory.getLogger(ActivitiControllerTest.class);
31+
private MockMvc mockMvc;
32+
33+
@Autowired
34+
private WebApplicationContext wac;
35+
36+
@Autowired
37+
RuntimeService runtimeService;
38+
39+
@Before
40+
public void setUp() {
41+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
42+
.build();
43+
44+
for (ProcessInstance instance : runtimeService.createProcessInstanceQuery()
45+
.list()) {
46+
runtimeService.deleteProcessInstance(instance.getId(), "Reset Processes");
47+
}
48+
}
49+
50+
@Test
51+
public void givenProcess_whenStartProcess_thenIncreaseInProcessInstanceCount() throws Exception {
52+
53+
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
54+
.andReturn()
55+
.getResponse()
56+
.getContentAsString();
57+
assertEquals("Process started. Number of currently running process instances = 1", responseBody);
58+
59+
responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
60+
.andReturn()
61+
.getResponse()
62+
.getContentAsString();
63+
assertEquals("Process started. Number of currently running process instances = 2", responseBody);
64+
65+
responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
66+
.andReturn()
67+
.getResponse()
68+
.getContentAsString();
69+
assertEquals("Process started. Number of currently running process instances = 3", responseBody);
70+
}
71+
72+
@Test
73+
public void givenProcess_whenProcessInstance_thenReceivedRunningTask() throws Exception {
74+
75+
this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
76+
.andReturn()
77+
.getResponse();
78+
ProcessInstance pi = runtimeService.createProcessInstanceQuery()
79+
.orderByProcessInstanceId()
80+
.desc()
81+
.list()
82+
.get(0);
83+
84+
logger.info("process instance = " + pi.getId());
85+
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/get-tasks/" + pi.getId()))
86+
.andReturn()
87+
.getResponse()
88+
.getContentAsString();
89+
90+
ObjectMapper mapper = new ObjectMapper();
91+
List<TaskRepresentation> tasks = Arrays.asList(mapper.readValue(responseBody, TaskRepresentation[].class));
92+
assertEquals(1, tasks.size());
93+
assertEquals("A", tasks.get(0).getName());
94+
95+
}
96+
97+
@Test
98+
public void givenProcess_whenCompleteTaskA_thenReceivedNextTask() throws Exception {
99+
100+
this.mockMvc.perform(MockMvcRequestBuilders.get("/start-process"))
101+
.andReturn()
102+
.getResponse();
103+
ProcessInstance pi = runtimeService.createProcessInstanceQuery()
104+
.orderByProcessInstanceId()
105+
.desc()
106+
.list()
107+
.get(0);
108+
109+
logger.info("process instance = " + pi.getId());
110+
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/complete-task-A/" + pi.getId()))
111+
.andReturn()
112+
.getResponse()
113+
.getContentAsString();
114+
115+
ObjectMapper mapper = new ObjectMapper();
116+
TaskRepresentation task = mapper.readValue(responseBody, TaskRepresentation.class);
117+
assertEquals("B", task.getName());
118+
119+
}
120+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.activitiwithspring;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class ActivitiWithSpringApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)