diff --git a/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/migration/CaseInstanceMigrationManagerImpl.java b/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/migration/CaseInstanceMigrationManagerImpl.java index f7efe19b22c..9c469ba2a1c 100644 --- a/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/migration/CaseInstanceMigrationManagerImpl.java +++ b/modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/migration/CaseInstanceMigrationManagerImpl.java @@ -243,7 +243,7 @@ protected ChangePlanItemStateBuilderImpl prepareChangeStateBuilder(CaseInstance CmmnEngineConfiguration cmmnEngineConfiguration = CommandContextUtil.getCmmnEngineConfiguration(commandContext); if (cmmnEngineConfiguration.isFallbackToDefaultTenant() && cmmnEngineConfiguration.getDefaultTenantProvider() != null) { - if (!Objects.equals(destinationTenantId, cmmnEngineConfiguration.getDefaultTenantProvider().getDefaultTenant(caseInstance.getId(), ScopeTypes.CMMN, caseDefinitionToMigrateTo.getKey()))) { + if (!Objects.equals(destinationTenantId, cmmnEngineConfiguration.getDefaultTenantProvider().getDefaultTenant(caseInstance.getTenantId(), ScopeTypes.CMMN, caseDefinitionToMigrateTo.getKey()))) { throw new FlowableException("Tenant mismatch between Case Instance ('" + caseInstance.getTenantId() + "') and Case Definition ('" + destinationTenantId + "') to migrate to"); } diff --git a/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/migration/CaseInstanceMigrationTest.java b/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/migration/CaseInstanceMigrationTest.java index 54b1c2bc4a7..370fc4334cb 100644 --- a/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/migration/CaseInstanceMigrationTest.java +++ b/modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/migration/CaseInstanceMigrationTest.java @@ -1850,6 +1850,97 @@ void testMultiTenantCaseInstanceMigrationWithDefaultTenantDefinition() { } } + @Test + void testMultiTenantCaseInstanceMigrationWithCustomDefaultTenantProvider() { + DefaultTenantProvider originalDefaultTenantValue = cmmnEngineConfiguration.getDefaultTenantProvider(); + cmmnEngineConfiguration.setFallbackToDefaultTenant(true); + CustomTenantProvider customTenantProvider = new CustomTenantProvider(); + cmmnEngineConfiguration.setDefaultTenantProvider(customTenantProvider); + + try { + // Arrange + CmmnDeployment deployment = cmmnRepositoryService.createDeployment() + .name("my deploy") + .addClasspathResource("org/flowable/cmmn/test/migration/one-task.cmmn.xml") + .tenantId("tenant1-default") + .deploy(); + + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder() + .caseDefinitionKey("testCase") + .tenantId("tenant1") + .start(); + + deployment = cmmnRepositoryService.createDeployment() + .name("my deploy") + .addClasspathResource("org/flowable/cmmn/test/migration/two-task.cmmn.xml") + .tenantId("tenant1-default") + .deploy(); + + CaseDefinition destinationDefinition = cmmnRepositoryService.createCaseDefinitionQuery().deploymentId(deployment.getId()).singleResult(); + + // 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.getCaseDefinitionName()).isEqualTo("Two Task Test Case"); + assertThat(caseInstanceAfterMigration.getCaseDefinitionVersion()).isEqualTo(2); + assertThat(caseInstanceAfterMigration.getCaseDefinitionDeploymentId()).isEqualTo(destinationDefinition.getDeploymentId()); + List planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery() + .caseInstanceId(caseInstance.getId()) + .list(); + assertThat(planItemInstances).hasSize(2); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getCaseDefinitionId) + .containsOnly(destinationDefinition.getId()); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getName) + .containsExactlyInAnyOrder("Task 1", "Task 2"); + assertThat(planItemInstances) + .extracting(PlanItemInstance::getState) + .containsOnly(PlanItemInstanceState.ACTIVE); + + 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()); + } + + 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(2); + 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()); + } + } + + } finally { + cmmnEngineConfiguration.setFallbackToDefaultTenant(false); + cmmnEngineConfiguration.setDefaultTenantProvider(originalDefaultTenantValue); + } + } + @Test void testMultiTenantCaseInstanceMigrationWithTargetDefaultTenantDefinition() { DefaultTenantProvider originalDefaultTenantValue = cmmnEngineConfiguration.getDefaultTenantProvider(); @@ -3324,4 +3415,12 @@ void withSentryIfPartEventDeferred() { // with stages // with new expected case variables + protected class CustomTenantProvider implements DefaultTenantProvider { + + @Override + public String getDefaultTenant(String tenantId, String scope, String scopeKey) { + return tenantId + "-default"; + } + + } }