|
| 1 | +/* Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | + * you may not use this file except in compliance with the License. |
| 3 | + * You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package org.flowable.rest.service.api.runtime; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import org.apache.http.HttpStatus; |
| 21 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 22 | +import org.apache.http.client.methods.HttpPost; |
| 23 | +import org.apache.http.entity.StringEntity; |
| 24 | +import org.flowable.common.engine.impl.identity.Authentication; |
| 25 | +import org.flowable.engine.runtime.ProcessInstance; |
| 26 | +import org.flowable.engine.test.Deployment; |
| 27 | +import org.flowable.rest.service.BaseSpringRestTestCase; |
| 28 | +import org.flowable.rest.service.api.RestUrls; |
| 29 | +import org.flowable.task.api.Task; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 33 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 34 | + |
| 35 | +public class ProcessInstanceChangeActivityStateResourceTest extends BaseSpringRestTestCase { |
| 36 | + |
| 37 | + @Test |
| 38 | + @Deployment(resources = { "org/flowable/rest/service/api/runtime/parallelTask.bpmn20.xml" }) |
| 39 | + public void testChangeActivityStateManyToOne() throws Exception { |
| 40 | + Authentication.setAuthenticatedUserId("testUser"); |
| 41 | + ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder() |
| 42 | + .processDefinitionKey("startParallelProcess") |
| 43 | + .start(); |
| 44 | + Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); |
| 45 | + assertThat(task.getTaskDefinitionKey()).isEqualTo("taskBefore"); |
| 46 | + |
| 47 | + taskService.complete(task.getId()); |
| 48 | + |
| 49 | + assertThat(taskService.createTaskQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(2); |
| 50 | + |
| 51 | + assertThat(runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(3); |
| 52 | + |
| 53 | + Authentication.setAuthenticatedUserId(null); |
| 54 | + |
| 55 | + ObjectNode requestNode = objectMapper.createObjectNode(); |
| 56 | + ArrayNode cancelActivityArray = requestNode.putArray("cancelActivityIds"); |
| 57 | + cancelActivityArray.add("task1"); |
| 58 | + cancelActivityArray.add("task2"); |
| 59 | + |
| 60 | + ArrayNode startActivityArray = requestNode.putArray("startActivityIds"); |
| 61 | + startActivityArray.add("taskBefore"); |
| 62 | + |
| 63 | + HttpPost httpPost = new HttpPost(buildUrl(RestUrls.URL_PROCESS_INSTANCE_CHANGE_STATE, processInstance.getId())); |
| 64 | + httpPost.setEntity(new StringEntity(requestNode.toString())); |
| 65 | + CloseableHttpResponse response = executeRequest(httpPost, HttpStatus.SC_OK); |
| 66 | + closeResponse(response); |
| 67 | + |
| 68 | + assertThat(taskService.createTaskQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(1); |
| 69 | + |
| 70 | + task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); |
| 71 | + assertThat(task.getTaskDefinitionKey()).isEqualTo("taskBefore"); |
| 72 | + |
| 73 | + assertThat(runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(2); |
| 74 | + |
| 75 | + taskService.complete(task.getId()); |
| 76 | + |
| 77 | + assertThat(taskService.createTaskQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(2); |
| 78 | + |
| 79 | + List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list(); |
| 80 | + for (Task parallelTask : tasks) { |
| 81 | + taskService.complete(parallelTask.getId()); |
| 82 | + } |
| 83 | + |
| 84 | + task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); |
| 85 | + assertThat(task.getTaskDefinitionKey()).isEqualTo("taskAfter"); |
| 86 | + |
| 87 | + taskService.complete(task.getId()); |
| 88 | + |
| 89 | + assertProcessEnded(processInstance.getId()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @Deployment(resources = { "org/flowable/rest/service/api/runtime/parallelTaskWithMI.bpmn20.xml" }) |
| 94 | + public void testChangeActivityStateManyToOneWithMI() throws Exception { |
| 95 | + Authentication.setAuthenticatedUserId("testUser"); |
| 96 | + ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder() |
| 97 | + .processDefinitionKey("startParallelProcess") |
| 98 | + .start(); |
| 99 | + Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); |
| 100 | + assertThat(task.getTaskDefinitionKey()).isEqualTo("taskBefore"); |
| 101 | + |
| 102 | + taskService.complete(task.getId()); |
| 103 | + |
| 104 | + assertThat(taskService.createTaskQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(4); |
| 105 | + |
| 106 | + assertThat(runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(7); |
| 107 | + |
| 108 | + Authentication.setAuthenticatedUserId(null); |
| 109 | + |
| 110 | + ObjectNode requestNode = objectMapper.createObjectNode(); |
| 111 | + ArrayNode cancelActivityArray = requestNode.putArray("cancelActivityIds"); |
| 112 | + cancelActivityArray.add("task1"); |
| 113 | + cancelActivityArray.add("task2"); |
| 114 | + |
| 115 | + ArrayNode startActivityArray = requestNode.putArray("startActivityIds"); |
| 116 | + startActivityArray.add("taskBefore"); |
| 117 | + |
| 118 | + HttpPost httpPost = new HttpPost(buildUrl(RestUrls.URL_PROCESS_INSTANCE_CHANGE_STATE, processInstance.getId())); |
| 119 | + httpPost.setEntity(new StringEntity(requestNode.toString())); |
| 120 | + CloseableHttpResponse response = executeRequest(httpPost, HttpStatus.SC_OK); |
| 121 | + closeResponse(response); |
| 122 | + |
| 123 | + assertThat(taskService.createTaskQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(1); |
| 124 | + |
| 125 | + task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); |
| 126 | + assertThat(task.getTaskDefinitionKey()).isEqualTo("taskBefore"); |
| 127 | + |
| 128 | + assertThat(runtimeService.createExecutionQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(2); |
| 129 | + |
| 130 | + taskService.complete(task.getId()); |
| 131 | + |
| 132 | + assertThat(taskService.createTaskQuery().processInstanceId(processInstance.getId()).count()).isEqualTo(4); |
| 133 | + |
| 134 | + List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list(); |
| 135 | + for (Task parallelTask : tasks) { |
| 136 | + taskService.complete(parallelTask.getId()); |
| 137 | + } |
| 138 | + |
| 139 | + task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); |
| 140 | + assertThat(task.getTaskDefinitionKey()).isEqualTo("taskAfter"); |
| 141 | + |
| 142 | + taskService.complete(task.getId()); |
| 143 | + |
| 144 | + assertProcessEnded(processInstance.getId()); |
| 145 | + } |
| 146 | +} |
0 commit comments