From 6934a86083e79c4a085bf91cf11307ffd9597138 Mon Sep 17 00:00:00 2001 From: Tijs Rademakers Date: Fri, 23 Jun 2023 09:54:37 +0200 Subject: [PATCH] Added case instance migration tests for event registry events --- ...nstanceEventSubscriptionMigrationTest.java | 263 ++++++++++++++++++ ...event-registry-listener-extratask.cmmn.xml | 19 ++ ...try-listener-repetition-extratask.cmmn.xml | 23 ++ ...vent-registry-listener-repetition.cmmn.xml | 21 ++ ...stener-stage-repetition-extratask.cmmn.xml | 40 +++ ...egistry-listener-stage-repetition.cmmn.xml | 36 +++ .../event-registry-listener.cmmn.xml | 17 ++ 7 files changed, 419 insertions(+) create mode 100644 modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/migration/CaseInstanceEventSubscriptionMigrationTest.java create mode 100644 modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-extratask.cmmn.xml create mode 100644 modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-repetition-extratask.cmmn.xml create mode 100644 modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-repetition.cmmn.xml create mode 100644 modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition-extratask.cmmn.xml create mode 100644 modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition.cmmn.xml create mode 100644 modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener.cmmn.xml diff --git a/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/migration/CaseInstanceEventSubscriptionMigrationTest.java b/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/migration/CaseInstanceEventSubscriptionMigrationTest.java new file mode 100644 index 00000000000..445031400ba --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/migration/CaseInstanceEventSubscriptionMigrationTest.java @@ -0,0 +1,263 @@ +/* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.flowable.cmmn.test.migration; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.flowable.cmmn.api.history.HistoricPlanItemInstance; +import org.flowable.cmmn.api.migration.PlanItemDefinitionMappingBuilder; +import org.flowable.cmmn.api.repository.CaseDefinition; +import org.flowable.cmmn.api.runtime.CaseInstance; +import org.flowable.cmmn.api.runtime.PlanItemDefinitionType; +import org.flowable.cmmn.api.runtime.PlanItemInstance; +import org.flowable.cmmn.api.runtime.PlanItemInstanceState; +import org.flowable.cmmn.engine.test.impl.CmmnHistoryTestHelper; +import org.flowable.common.engine.impl.history.HistoryLevel; +import org.flowable.eventsubscription.api.EventSubscription; +import org.flowable.task.api.Task; +import org.flowable.task.api.history.HistoricTaskInstance; +import org.junit.jupiter.api.Test; + +public class CaseInstanceEventSubscriptionMigrationTest extends AbstractCaseMigrationTest { + + @Test + void withBasicEventRegistryEvent() { + // Arrange + deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/event-registry-listener.cmmn.xml"); + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("testCase").start(); + CaseDefinition destinationDefinition = deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/event-registry-listener-extratask.cmmn.xml"); + + List eventSubscriptions = cmmnRuntimeService.createEventSubscriptionQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(eventSubscriptions).hasSize(1); + + // Act + cmmnMigrationService.createCaseInstanceMigrationBuilder() + .migrateToCaseDefinition(destinationDefinition.getId()) + .addActivatePlanItemDefinitionMapping(PlanItemDefinitionMappingBuilder.createActivatePlanItemDefinitionMappingFor("humanTask2")) + .migrate(caseInstance.getId()); + + // Assert + CaseInstance caseInstanceAfterMigration = cmmnRuntimeService.createCaseInstanceQuery() + .caseInstanceId(caseInstance.getId()) + .singleResult(); + assertThat(caseInstanceAfterMigration.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId()); + assertThat(caseInstanceAfterMigration.getCaseDefinitionKey()).isEqualTo("testCase"); + assertThat(caseInstanceAfterMigration.getCaseDefinitionVersion()).isEqualTo(2); + assertThat(caseInstanceAfterMigration.getCaseDefinitionDeploymentId()).isEqualTo(destinationDefinition.getDeploymentId()); + List planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery() + .caseInstanceId(caseInstance.getId()) + .list(); + assertThat(planItemInstances).hasSize(3); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getCaseDefinitionId) + .containsOnly(destinationDefinition.getId()); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getName) + .containsExactlyInAnyOrder("Event", "My new taskname 1", "Task 2"); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getState) + .containsOnly(PlanItemInstanceState.AVAILABLE, PlanItemInstanceState.ACTIVE); + + eventSubscriptions = cmmnRuntimeService.createEventSubscriptionQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(eventSubscriptions).hasSize(1); + + List tasks = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(tasks).hasSize(2); + for (Task task : tasks) { + assertThat(task.getScopeDefinitionId()).isEqualTo(destinationDefinition.getId()); + cmmnTaskService.complete(task.getId()); + } + + planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(planItemInstances).hasSize(1); + cmmnRuntimeService.triggerPlanItemInstance(planItemInstances.get(0).getId()); + + assertThat(cmmnRuntimeService.createCaseInstanceQuery().caseInstanceId(caseInstance.getId()).count()).isZero(); + + if (CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) { + assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().caseInstanceId(caseInstance.getId()).count()).isEqualTo(1); + assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().caseInstanceId(caseInstance.getId()).singleResult().getCaseDefinitionId()) + .isEqualTo(destinationDefinition.getId()); + + List historicPlanItemInstances = cmmnHistoryService.createHistoricPlanItemInstanceQuery() + .planItemInstanceCaseInstanceId(caseInstance.getId()).list(); + assertThat(historicPlanItemInstances).hasSize(3); + for (HistoricPlanItemInstance historicPlanItemInstance : historicPlanItemInstances) { + assertThat(historicPlanItemInstance.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId()); + } + + List historicTasks = cmmnHistoryService.createHistoricTaskInstanceQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(historicTasks).hasSize(2); + for (HistoricTaskInstance historicTask : historicTasks) { + assertThat(historicTask.getScopeDefinitionId()).isEqualTo(destinationDefinition.getId()); + } + } + } + + @Test + void withEventRegistryEventWithRepetition() { + // Arrange + deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/event-registry-listener-repetition.cmmn.xml"); + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("testCase").start(); + + PlanItemInstance planItemInstance = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionType(PlanItemDefinitionType.GENERIC_EVENT_LISTENER).singleResult(); + assertThat(planItemInstance).isNotNull(); + cmmnRuntimeService.triggerPlanItemInstance(planItemInstance.getId()); + + CaseDefinition destinationDefinition = deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/event-registry-listener-repetition-extratask.cmmn.xml"); + + List eventSubscriptions = cmmnRuntimeService.createEventSubscriptionQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(eventSubscriptions).hasSize(1); + + // Act + cmmnMigrationService.createCaseInstanceMigrationBuilder() + .migrateToCaseDefinition(destinationDefinition.getId()) + .addActivatePlanItemDefinitionMapping(PlanItemDefinitionMappingBuilder.createActivatePlanItemDefinitionMappingFor("humanTask2")) + .migrate(caseInstance.getId()); + + // Assert + CaseInstance caseInstanceAfterMigration = cmmnRuntimeService.createCaseInstanceQuery() + .caseInstanceId(caseInstance.getId()) + .singleResult(); + assertThat(caseInstanceAfterMigration.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId()); + assertThat(caseInstanceAfterMigration.getCaseDefinitionKey()).isEqualTo("testCase"); + assertThat(caseInstanceAfterMigration.getCaseDefinitionVersion()).isEqualTo(2); + assertThat(caseInstanceAfterMigration.getCaseDefinitionDeploymentId()).isEqualTo(destinationDefinition.getDeploymentId()); + List planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery() + .caseInstanceId(caseInstance.getId()) + .list(); + assertThat(planItemInstances).hasSize(3); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getCaseDefinitionId) + .containsOnly(destinationDefinition.getId()); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getName) + .containsExactlyInAnyOrder("Event", "My new taskname 1", "Task 2"); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getState) + .containsOnly(PlanItemInstanceState.AVAILABLE, PlanItemInstanceState.ACTIVE); + + eventSubscriptions = cmmnRuntimeService.createEventSubscriptionQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(eventSubscriptions).hasSize(1); + + List tasks = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(tasks).hasSize(2); + for (Task task : tasks) { + assertThat(task.getScopeDefinitionId()).isEqualTo(destinationDefinition.getId()); + cmmnTaskService.complete(task.getId()); + } + + planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(planItemInstances).hasSize(1); + cmmnRuntimeService.triggerPlanItemInstance(planItemInstances.get(0).getId()); + + if (CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) { + assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().caseInstanceId(caseInstance.getId()).count()).isEqualTo(1); + assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().caseInstanceId(caseInstance.getId()).singleResult().getCaseDefinitionId()) + .isEqualTo(destinationDefinition.getId()); + + List historicPlanItemInstances = cmmnHistoryService.createHistoricPlanItemInstanceQuery() + .planItemInstanceCaseInstanceId(caseInstance.getId()).list(); + assertThat(historicPlanItemInstances).hasSize(5); + for (HistoricPlanItemInstance historicPlanItemInstance : historicPlanItemInstances) { + assertThat(historicPlanItemInstance.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId()); + } + + List historicTasks = cmmnHistoryService.createHistoricTaskInstanceQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(historicTasks).hasSize(2); + for (HistoricTaskInstance historicTask : historicTasks) { + assertThat(historicTask.getScopeDefinitionId()).isEqualTo(destinationDefinition.getId()); + } + } + } + + @Test + void withEventRegistryEventWithRepetitionInStage() { + // Arrange + deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition.cmmn.xml"); + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("testCase").start(); + + PlanItemInstance planItemInstance = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionType(PlanItemDefinitionType.GENERIC_EVENT_LISTENER).singleResult(); + assertThat(planItemInstance).isNotNull(); + cmmnRuntimeService.triggerPlanItemInstance(planItemInstance.getId()); + + CaseDefinition destinationDefinition = deployCaseDefinition("test1", "org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition-extratask.cmmn.xml"); + + List eventSubscriptions = cmmnRuntimeService.createEventSubscriptionQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(eventSubscriptions).hasSize(1); + + // Act + cmmnMigrationService.createCaseInstanceMigrationBuilder() + .migrateToCaseDefinition(destinationDefinition.getId()) + .addActivatePlanItemDefinitionMapping(PlanItemDefinitionMappingBuilder.createActivatePlanItemDefinitionMappingFor("humanTask3")) + .migrate(caseInstance.getId()); + + // Assert + CaseInstance caseInstanceAfterMigration = cmmnRuntimeService.createCaseInstanceQuery() + .caseInstanceId(caseInstance.getId()) + .singleResult(); + assertThat(caseInstanceAfterMigration.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId()); + assertThat(caseInstanceAfterMigration.getCaseDefinitionKey()).isEqualTo("testCase"); + assertThat(caseInstanceAfterMigration.getCaseDefinitionVersion()).isEqualTo(2); + assertThat(caseInstanceAfterMigration.getCaseDefinitionDeploymentId()).isEqualTo(destinationDefinition.getDeploymentId()); + List planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery() + .caseInstanceId(caseInstance.getId()) + .list(); + assertThat(planItemInstances).hasSize(6); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getCaseDefinitionId) + .containsOnly(destinationDefinition.getId()); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getName) + .containsExactlyInAnyOrder("Stage", "Event", "Task 1", "Task 2", "Task 1", "Task 3"); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getState) + .containsOnly(PlanItemInstanceState.WAITING_FOR_REPETITION, PlanItemInstanceState.AVAILABLE, PlanItemInstanceState.ACTIVE); + + eventSubscriptions = cmmnRuntimeService.createEventSubscriptionQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(eventSubscriptions).hasSize(1); + + List tasks = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(tasks).hasSize(3); + for (Task task : tasks) { + assertThat(task.getScopeDefinitionId()).isEqualTo(destinationDefinition.getId()); + cmmnTaskService.complete(task.getId()); + } + + planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery().caseInstanceId(caseInstance.getId()).planItemDefinitionType(PlanItemDefinitionType.GENERIC_EVENT_LISTENER).list(); + assertThat(planItemInstances).hasSize(1); + cmmnRuntimeService.triggerPlanItemInstance(planItemInstances.get(0).getId()); + + if (CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) { + assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().caseInstanceId(caseInstance.getId()).count()).isEqualTo(1); + assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().caseInstanceId(caseInstance.getId()).singleResult().getCaseDefinitionId()) + .isEqualTo(destinationDefinition.getId()); + + List historicPlanItemInstances = cmmnHistoryService.createHistoricPlanItemInstanceQuery() + .planItemInstanceCaseInstanceId(caseInstance.getId()).list(); + assertThat(historicPlanItemInstances).hasSize(9); + for (HistoricPlanItemInstance historicPlanItemInstance : historicPlanItemInstances) { + assertThat(historicPlanItemInstance.getCaseDefinitionId()).isEqualTo(destinationDefinition.getId()); + } + + List historicTasks = cmmnHistoryService.createHistoricTaskInstanceQuery().caseInstanceId(caseInstance.getId()).list(); + assertThat(historicTasks).hasSize(4); + for (HistoricTaskInstance historicTask : historicTasks) { + assertThat(historicTask.getScopeDefinitionId()).isEqualTo(destinationDefinition.getId()); + } + } + } +} diff --git a/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-extratask.cmmn.xml b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-extratask.cmmn.xml new file mode 100644 index 00000000000..063316f0ce9 --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-extratask.cmmn.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + myEvent + + + + + \ No newline at end of file diff --git a/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-repetition-extratask.cmmn.xml b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-repetition-extratask.cmmn.xml new file mode 100644 index 00000000000..0658ae28b5f --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-repetition-extratask.cmmn.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + myEvent + + + + + \ No newline at end of file diff --git a/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-repetition.cmmn.xml b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-repetition.cmmn.xml new file mode 100644 index 00000000000..98e7fb59294 --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-repetition.cmmn.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + myEvent + + + + + \ No newline at end of file diff --git a/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition-extratask.cmmn.xml b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition-extratask.cmmn.xml new file mode 100644 index 00000000000..498f095f090 --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition-extratask.cmmn.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + occur + + + + + + + myEvent + + + + + + + + + \ No newline at end of file diff --git a/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition.cmmn.xml b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition.cmmn.xml new file mode 100644 index 00000000000..83404ae4721 --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener-stage-repetition.cmmn.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + occur + + + + + + + myEvent + + + + + + \ No newline at end of file diff --git a/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener.cmmn.xml b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener.cmmn.xml new file mode 100644 index 00000000000..762b70edf43 --- /dev/null +++ b/modules/flowable-cmmn-engine/src/test/resources/org/flowable/cmmn/test/migration/event-registry-listener.cmmn.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + myEvent + + + + + \ No newline at end of file