|
| 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 | +package org.flowable.rest.app; |
| 14 | + |
| 15 | +import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +import org.flowable.cmmn.api.CmmnRuntimeService; |
| 19 | +import org.flowable.cmmn.api.CmmnTaskService; |
| 20 | +import org.flowable.cmmn.api.runtime.CaseInstance; |
| 21 | +import org.flowable.cmmn.engine.test.CmmnDeployment; |
| 22 | +import org.flowable.cmmn.spring.impl.test.FlowableCmmnSpringExtension; |
| 23 | +import org.flowable.engine.RuntimeService; |
| 24 | +import org.flowable.engine.TaskService; |
| 25 | +import org.flowable.engine.runtime.ProcessInstance; |
| 26 | +import org.flowable.engine.test.Deployment; |
| 27 | +import org.flowable.spring.impl.test.FlowableSpringExtension; |
| 28 | +import org.flowable.task.api.Task; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.boot.resttestclient.TestRestTemplate; |
| 33 | +import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; |
| 34 | +import org.springframework.boot.test.context.SpringBootTest; |
| 35 | +import org.springframework.http.HttpStatus; |
| 36 | +import org.springframework.http.ResponseEntity; |
| 37 | + |
| 38 | +import tools.jackson.databind.JsonNode; |
| 39 | + |
| 40 | +import net.javacrumbs.jsonunit.core.Option; |
| 41 | + |
| 42 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 43 | +@ExtendWith({ |
| 44 | + FlowableCmmnSpringExtension.class, |
| 45 | + FlowableSpringExtension.class, |
| 46 | +}) |
| 47 | +@AutoConfigureTestRestTemplate |
| 48 | +public class CrossEngineRestQueryTest { |
| 49 | + |
| 50 | + @Autowired |
| 51 | + protected RuntimeService runtimeService; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + protected TaskService taskService; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + protected CmmnRuntimeService cmmnRuntimeService; |
| 58 | + |
| 59 | + @Autowired |
| 60 | + protected CmmnTaskService cmmnTaskService; |
| 61 | + |
| 62 | + @Autowired |
| 63 | + protected TestRestTemplate restTemplate; |
| 64 | + |
| 65 | + @Test |
| 66 | + @Deployment(resources = "processWithCaseTask.bpmn20.xml") |
| 67 | + @CmmnDeployment(resources = "oneHumanTaskCase.cmmn") |
| 68 | + void queryCaseInstancesByParentProcessInstanceId() { |
| 69 | + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("processWithCaseTask"); |
| 70 | + |
| 71 | + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceQuery() |
| 72 | + .parentProcessInstanceId(processInstance.getId()) |
| 73 | + .singleResult(); |
| 74 | + assertThat(caseInstance).isNotNull(); |
| 75 | + |
| 76 | + ResponseEntity<JsonNode> response = restTemplate.withBasicAuth("rest-admin", "test") |
| 77 | + .getForEntity("/cmmn-api/cmmn-runtime/case-instances?parentProcessInstanceId={id}", |
| 78 | + JsonNode.class, processInstance.getId()); |
| 79 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 80 | + assertThatJson(response.getBody()) |
| 81 | + .when(Option.IGNORING_EXTRA_FIELDS) |
| 82 | + .isEqualTo("{" |
| 83 | + + " data: [" |
| 84 | + + " { id: '" + caseInstance.getId() + "' }" |
| 85 | + + " ]" |
| 86 | + + "}"); |
| 87 | + |
| 88 | + response = restTemplate.withBasicAuth("rest-admin", "test") |
| 89 | + .getForEntity("/cmmn-api/cmmn-runtime/case-instances?parentProcessInstanceId=nonExisting", |
| 90 | + JsonNode.class); |
| 91 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 92 | + assertThatJson(response.getBody()) |
| 93 | + .when(Option.IGNORING_EXTRA_FIELDS) |
| 94 | + .isEqualTo("{ data: [] }"); |
| 95 | + |
| 96 | + response = restTemplate.withBasicAuth("rest-admin", "test") |
| 97 | + .getForEntity("/cmmn-api/cmmn-history/historic-case-instances?parentProcessInstanceId={id}", |
| 98 | + JsonNode.class, processInstance.getId()); |
| 99 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 100 | + assertThatJson(response.getBody()) |
| 101 | + .when(Option.IGNORING_EXTRA_FIELDS) |
| 102 | + .isEqualTo("{" |
| 103 | + + " data: [" |
| 104 | + + " { id: '" + caseInstance.getId() + "' }" |
| 105 | + + " ]" |
| 106 | + + "}"); |
| 107 | + |
| 108 | + Task caseTask = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult(); |
| 109 | + cmmnTaskService.complete(caseTask.getId()); |
| 110 | + |
| 111 | + response = restTemplate.withBasicAuth("rest-admin", "test") |
| 112 | + .getForEntity("/cmmn-api/cmmn-history/historic-case-instances?parentProcessInstanceId={id}", |
| 113 | + JsonNode.class, processInstance.getId()); |
| 114 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 115 | + assertThatJson(response.getBody()) |
| 116 | + .when(Option.IGNORING_EXTRA_FIELDS) |
| 117 | + .isEqualTo("{" |
| 118 | + + " data: [" |
| 119 | + + " { id: '" + caseInstance.getId() + "' }" |
| 120 | + + " ]" |
| 121 | + + "}"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + @Deployment(resources = "oneTaskProcess.bpmn20.xml") |
| 126 | + @CmmnDeployment(resources = "caseWithProcessTask.cmmn") |
| 127 | + void queryProcessInstancesByParentCaseInstanceId() { |
| 128 | + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder() |
| 129 | + .caseDefinitionKey("caseWithProcessTask") |
| 130 | + .start(); |
| 131 | + |
| 132 | + ProcessInstance processInstance = runtimeService.createProcessInstanceQuery() |
| 133 | + .parentCaseInstanceId(caseInstance.getId()) |
| 134 | + .singleResult(); |
| 135 | + assertThat(processInstance).isNotNull(); |
| 136 | + |
| 137 | + ResponseEntity<JsonNode> response = restTemplate.withBasicAuth("rest-admin", "test") |
| 138 | + .getForEntity("/service/runtime/process-instances?parentCaseInstanceId={id}", |
| 139 | + JsonNode.class, caseInstance.getId()); |
| 140 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 141 | + assertThatJson(response.getBody()) |
| 142 | + .when(Option.IGNORING_EXTRA_FIELDS) |
| 143 | + .isEqualTo("{" |
| 144 | + + " data: [" |
| 145 | + + " { id: '" + processInstance.getId() + "' }" |
| 146 | + + " ]" |
| 147 | + + "}"); |
| 148 | + |
| 149 | + response = restTemplate.withBasicAuth("rest-admin", "test") |
| 150 | + .getForEntity("/service/runtime/process-instances?parentCaseInstanceId=nonExisting", |
| 151 | + JsonNode.class); |
| 152 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 153 | + assertThatJson(response.getBody()) |
| 154 | + .when(Option.IGNORING_EXTRA_FIELDS) |
| 155 | + .isEqualTo("{ data: [] }"); |
| 156 | + |
| 157 | + response = restTemplate.withBasicAuth("rest-admin", "test") |
| 158 | + .getForEntity("/service/history/historic-process-instances?parentCaseInstanceId={id}", |
| 159 | + JsonNode.class, caseInstance.getId()); |
| 160 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 161 | + assertThatJson(response.getBody()) |
| 162 | + .when(Option.IGNORING_EXTRA_FIELDS) |
| 163 | + .isEqualTo("{" |
| 164 | + + " data: [" |
| 165 | + + " { id: '" + processInstance.getId() + "' }" |
| 166 | + + " ]" |
| 167 | + + "}"); |
| 168 | + |
| 169 | + Task processTask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); |
| 170 | + taskService.complete(processTask.getId()); |
| 171 | + |
| 172 | + response = restTemplate.withBasicAuth("rest-admin", "test") |
| 173 | + .getForEntity("/service/history/historic-process-instances?parentCaseInstanceId={id}", |
| 174 | + JsonNode.class, caseInstance.getId()); |
| 175 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 176 | + assertThatJson(response.getBody()) |
| 177 | + .when(Option.IGNORING_EXTRA_FIELDS) |
| 178 | + .isEqualTo("{" |
| 179 | + + " data: [" |
| 180 | + + " { id: '" + processInstance.getId() + "' }" |
| 181 | + + " ]" |
| 182 | + + "}"); |
| 183 | + } |
| 184 | +} |
0 commit comments