@Test
public void testMigrateSingleCaseInstanceWithDynamicSubprocess() {

    
    String caseId = createCaseInstanceV1();
    try {
        assertCaseInstance(caseId, deploymentUnitV1.getIdentifier(), CASE_DEF_ID_V1);
        
        caseService.addDynamicSubprocess(caseId, PROCESS_DEF_ID_V1, null);
        
        Collection<ProcessInstanceDesc> pInstances = caseRuntimeDataService.getProcessInstancesForCase(caseId, new QueryContext());
        assertEquals(2, pInstances.size());
        
        for (ProcessInstanceDesc instance : pInstances) {
            assertEquals(deploymentUnitV1.getIdentifier(), instance.getDeploymentId());                
        }
        
        List<TaskSummary> tasks = runtimeDataService.getTasksAssignedAsPotentialOwner(USER, new QueryFilter());
        assertNotNull(tasks);
        assertThat(tasks).hasSize(2);
        
        Map<String, TaskSummary> mappedTasks = mapTaskSummaries(tasks);
        assertThat(mappedTasks).containsKeys("Hello1", "Hello_V1");
        
        Map<String, String> processMapping = new HashMap<>();
        processMapping.put(CASE_DEF_ID_V1, CASE_DEF_ID_V2);
        processMapping.put(PROCESS_DEF_ID_V1, PROCESS_DEF_ID_V2);
        
        CaseMigrationReport report = caseInstanceMigrationService.migrate(caseId, deploymentUnitV2.getIdentifier(), processMapping);
        assertNotNull(report);
        assertThat(report.isSuccessful()).isTrue();
        assertThat(report.getReports()).hasSize(2);
        
        assertCaseInstance(caseId, deploymentUnitV2.getIdentifier(), CASE_DEF_ID_V2);
        
        pInstances = caseRuntimeDataService.getProcessInstancesForCase(caseId, new QueryContext());
        assertEquals(2, pInstances.size());
        
        for (ProcessInstanceDesc instance : pInstances) {
            assertEquals(deploymentUnitV2.getIdentifier(), instance.getDeploymentId());                
        }
        tasks = runtimeDataService.getTasksAssignedAsPotentialOwner(USER, new QueryFilter());
        assertNotNull(tasks);
        assertThat(tasks).hasSize(2);
        
        mappedTasks = mapTaskSummaries(tasks);
        assertThat(mappedTasks).containsKeys("Hello1", "Hello_V2");
        
        userTaskService.completeAutoProgress(mappedTasks.get("Hello_V2").getId(), USER, null);
        
        caseService.destroyCase(caseId);
        CaseInstance instance = caseService.getCaseInstance(caseId);
        Assertions.assertThat(instance.getStatus()).isEqualTo(CaseStatus.CANCELLED.getId());
        caseId = null;
    } catch (Exception e) {
        logger.error("Unexpected error {}", e.getMessage(), e);
        fail("Unexpected exception " + e.getMessage());
    } finally {
        if (caseId != null) {
            caseService.cancelCase(caseId);
        }
    }
}