|
| 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.cmmn.test.history; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.flowable.cmmn.api.runtime.CaseInstance; |
| 20 | +import org.flowable.cmmn.engine.test.CmmnDeployment; |
| 21 | +import org.flowable.cmmn.engine.test.impl.CmmnHistoryTestHelper; |
| 22 | +import org.flowable.cmmn.test.FlowableCmmnTestCase; |
| 23 | +import org.flowable.common.engine.impl.identity.Authentication; |
| 24 | +import org.flowable.identitylink.api.IdentityLinkType; |
| 25 | +import org.flowable.identitylink.api.history.HistoricIdentityLink; |
| 26 | +import org.flowable.identitylink.service.impl.persistence.entity.HistoricIdentityLinkEntity; |
| 27 | +import org.flowable.task.api.Task; |
| 28 | +import org.flowable.task.api.history.HistoricTaskInstance; |
| 29 | +import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +public class HistoryLevelIdentityLinkTest extends FlowableCmmnTestCase { |
| 33 | + |
| 34 | + @AfterEach |
| 35 | + public void tearDown() { |
| 36 | + Authentication.setAuthenticatedUserId(null); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + @CmmnDeployment(resources = "org/flowable/cmmn/test/history/oneHumanTaskHistoryLevelInstance.cmmn") |
| 41 | + public void testInstanceHistoryLevelStoresCaseInstanceIdentityLinks() { |
| 42 | + Authentication.setAuthenticatedUserId("johndoe"); |
| 43 | + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder() |
| 44 | + .caseDefinitionKey("oneTaskCase") |
| 45 | + .start(); |
| 46 | + |
| 47 | + cmmnRuntimeService.addUserIdentityLink(caseInstance.getId(), "participant1", IdentityLinkType.PARTICIPANT); |
| 48 | + |
| 49 | + CmmnHistoryTestHelper.waitForJobExecutorToProcessAllHistoryJobs(cmmnEngineConfiguration, cmmnManagementService, 7000, 200); |
| 50 | + |
| 51 | + List<HistoricIdentityLink> caseIdentityLinks = cmmnHistoryService.getHistoricIdentityLinksForCaseInstance(caseInstance.getId()); |
| 52 | + assertThat(caseIdentityLinks).hasSize(2); |
| 53 | + assertThat(caseIdentityLinks) |
| 54 | + .extracting(HistoricIdentityLink::getType) |
| 55 | + .containsExactlyInAnyOrder(IdentityLinkType.STARTER, IdentityLinkType.PARTICIPANT); |
| 56 | + assertThat(caseIdentityLinks) |
| 57 | + .extracting(HistoricIdentityLink::getUserId) |
| 58 | + .containsExactlyInAnyOrder("johndoe", "participant1"); |
| 59 | + |
| 60 | + Task task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult(); |
| 61 | + cmmnTaskService.complete(task.getId()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @CmmnDeployment(resources = "org/flowable/cmmn/test/history/oneHumanTaskHistoryLevelInstance.cmmn") |
| 66 | + public void testInstanceHistoryLevelDoesNotStoreTaskIdentityLinks() { |
| 67 | + Authentication.setAuthenticatedUserId("johndoe"); |
| 68 | + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder() |
| 69 | + .caseDefinitionKey("oneTaskCase") |
| 70 | + .start(); |
| 71 | + |
| 72 | + Task task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult(); |
| 73 | + cmmnTaskService.addUserIdentityLink(task.getId(), "candidateUser1", IdentityLinkType.CANDIDATE); |
| 74 | + cmmnTaskService.addGroupIdentityLink(task.getId(), "candidateGroup1", IdentityLinkType.CANDIDATE); |
| 75 | + |
| 76 | + CmmnHistoryTestHelper.waitForJobExecutorToProcessAllHistoryJobs(cmmnEngineConfiguration, cmmnManagementService, 7000, 200); |
| 77 | + |
| 78 | + // At instance level, task identity links should not be stored, |
| 79 | + // but the interceptor creates participant links on the case instance for candidate users |
| 80 | + List<HistoricIdentityLink> caseIdentityLinks = cmmnHistoryService.getHistoricIdentityLinksForCaseInstance(caseInstance.getId()); |
| 81 | + assertThat(caseIdentityLinks) |
| 82 | + .extracting(HistoricIdentityLink::getType) |
| 83 | + .containsOnly(IdentityLinkType.STARTER, IdentityLinkType.PARTICIPANT); |
| 84 | + |
| 85 | + // Verify no historic identity links exist for the task directly (using management command |
| 86 | + // because getHistoricIdentityLinksForTask requires a historic task which doesn't exist at instance level) |
| 87 | + List<?> taskLinks = cmmnEngineConfiguration.getCommandExecutor().execute(commandContext -> { |
| 88 | + return cmmnEngineConfiguration.getIdentityLinkServiceConfiguration() |
| 89 | + .getHistoricIdentityLinkService() |
| 90 | + .findHistoricIdentityLinksByTaskId(task.getId()); |
| 91 | + }); |
| 92 | + assertThat(taskLinks).isEmpty(); |
| 93 | + |
| 94 | + cmmnTaskService.complete(task.getId()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + @CmmnDeployment(resources = "org/flowable/cmmn/test/history/oneHumanTaskHistoryLevelTask.cmmn") |
| 99 | + public void testTaskHistoryLevelStoresCaseInstanceAndTaskIdentityLinks() { |
| 100 | + Authentication.setAuthenticatedUserId("johndoe"); |
| 101 | + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder() |
| 102 | + .caseDefinitionKey("oneTaskCase") |
| 103 | + .start(); |
| 104 | + |
| 105 | + cmmnRuntimeService.addUserIdentityLink(caseInstance.getId(), "participant1", IdentityLinkType.PARTICIPANT); |
| 106 | + |
| 107 | + Task task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult(); |
| 108 | + cmmnTaskService.addUserIdentityLink(task.getId(), "candidateUser1", IdentityLinkType.CANDIDATE); |
| 109 | + cmmnTaskService.addGroupIdentityLink(task.getId(), "candidateGroup1", IdentityLinkType.CANDIDATE); |
| 110 | + |
| 111 | + CmmnHistoryTestHelper.waitForJobExecutorToProcessAllHistoryJobs(cmmnEngineConfiguration, cmmnManagementService, 7000, 200); |
| 112 | + |
| 113 | + // Case instance links: starter + participant + participant from candidate user interceptor |
| 114 | + List<HistoricIdentityLink> caseIdentityLinks = cmmnHistoryService.getHistoricIdentityLinksForCaseInstance(caseInstance.getId()); |
| 115 | + assertThat(caseIdentityLinks).hasSize(3); |
| 116 | + |
| 117 | + // Task identity links: candidate user + candidate group |
| 118 | + List<HistoricIdentityLink> taskIdentityLinks = cmmnHistoryService.getHistoricIdentityLinksForTask(task.getId()); |
| 119 | + assertThat(taskIdentityLinks).hasSize(2); |
| 120 | + assertThat(taskIdentityLinks) |
| 121 | + .extracting(HistoricIdentityLink::getType) |
| 122 | + .containsExactlyInAnyOrder(IdentityLinkType.CANDIDATE, IdentityLinkType.CANDIDATE); |
| 123 | + |
| 124 | + cmmnTaskService.complete(task.getId()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + @CmmnDeployment(resources = "org/flowable/cmmn/test/history/oneHumanTaskHistoryLevelActivity.cmmn") |
| 129 | + public void testActivityHistoryLevelStoresCaseInstanceAndTaskIdentityLinks() { |
| 130 | + Authentication.setAuthenticatedUserId("johndoe"); |
| 131 | + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder() |
| 132 | + .caseDefinitionKey("oneTaskCase") |
| 133 | + .start(); |
| 134 | + |
| 135 | + cmmnRuntimeService.addUserIdentityLink(caseInstance.getId(), "participant1", IdentityLinkType.PARTICIPANT); |
| 136 | + |
| 137 | + Task task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult(); |
| 138 | + cmmnTaskService.addUserIdentityLink(task.getId(), "candidateUser1", IdentityLinkType.CANDIDATE); |
| 139 | + cmmnTaskService.addGroupIdentityLink(task.getId(), "candidateGroup1", IdentityLinkType.CANDIDATE); |
| 140 | + |
| 141 | + CmmnHistoryTestHelper.waitForJobExecutorToProcessAllHistoryJobs(cmmnEngineConfiguration, cmmnManagementService, 7000, 200); |
| 142 | + |
| 143 | + // Case instance links: starter + participant + participant from candidate user interceptor |
| 144 | + List<HistoricIdentityLink> caseIdentityLinks = cmmnHistoryService.getHistoricIdentityLinksForCaseInstance(caseInstance.getId()); |
| 145 | + assertThat(caseIdentityLinks).hasSize(3); |
| 146 | + |
| 147 | + HistoricTaskInstance historyTaskInstance = cmmnHistoryService.createHistoricTaskInstanceQuery().taskId(task.getId()).singleResult(); |
| 148 | + assertThat(historyTaskInstance).isNull(); |
| 149 | + |
| 150 | + // Task identity links: candidate user + candidate group |
| 151 | + // At activity level, historic tasks are not stored, so we query via the service directly |
| 152 | + List<HistoricIdentityLinkEntity> taskLinks = cmmnEngineConfiguration.getCommandExecutor().execute(commandContext -> { |
| 153 | + return cmmnEngineConfiguration.getIdentityLinkServiceConfiguration() |
| 154 | + .getHistoricIdentityLinkService() |
| 155 | + .findHistoricIdentityLinksByTaskId(task.getId()); |
| 156 | + }); |
| 157 | + assertThat(taskLinks).hasSize(0); |
| 158 | + |
| 159 | + cmmnTaskService.complete(task.getId()); |
| 160 | + |
| 161 | + CmmnHistoryTestHelper.waitForJobExecutorToProcessAllHistoryJobs(cmmnEngineConfiguration, cmmnManagementService, 7000, 200); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + @CmmnDeployment(resources = "org/flowable/cmmn/test/history/testOneSimpleHumanTaskWithHistoryLevelNone.cmmn") |
| 166 | + public void testNoneHistoryLevelDoesNotStoreIdentityLinks() { |
| 167 | + Authentication.setAuthenticatedUserId("johndoe"); |
| 168 | + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder() |
| 169 | + .caseDefinitionKey("myCase") |
| 170 | + .start(); |
| 171 | + |
| 172 | + cmmnRuntimeService.addUserIdentityLink(caseInstance.getId(), "participant1", IdentityLinkType.PARTICIPANT); |
| 173 | + |
| 174 | + Task task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult(); |
| 175 | + cmmnTaskService.addUserIdentityLink(task.getId(), "candidateUser1", IdentityLinkType.CANDIDATE); |
| 176 | + |
| 177 | + CmmnHistoryTestHelper.waitForJobExecutorToProcessAllHistoryJobs(cmmnEngineConfiguration, cmmnManagementService, 7000, 200); |
| 178 | + |
| 179 | + List<HistoricIdentityLink> caseIdentityLinks = cmmnHistoryService.getHistoricIdentityLinksForCaseInstance(caseInstance.getId()); |
| 180 | + assertThat(caseIdentityLinks).isEmpty(); |
| 181 | + |
| 182 | + List<?> taskLinks = cmmnEngineConfiguration.getCommandExecutor().execute(commandContext -> { |
| 183 | + return cmmnEngineConfiguration.getIdentityLinkServiceConfiguration() |
| 184 | + .getHistoricIdentityLinkService() |
| 185 | + .findHistoricIdentityLinksByTaskId(task.getId()); |
| 186 | + }); |
| 187 | + assertThat(taskLinks).isEmpty(); |
| 188 | + |
| 189 | + cmmnTaskService.complete(task.getId()); |
| 190 | + } |
| 191 | +} |
0 commit comments