|
| 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 | +} |
0 commit comments