Skip to content

Commit

Permalink
Mark migration as failed when exceptions occur (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
svacas authored May 27, 2022
1 parent 659e817 commit f9ca03e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void execute(MigrationReport report) throws Exception {
applicationModel =
generateTargetApplicationModel(outputProject, targetProjectType, sourceProjectBasePath, projectParentGAV, projectGAV);
try {
MigrationTaskException firstMigrationTaskException = null;
for (AbstractMigrationTask task : migrationTasks) {
if (task.getApplicableProjectTypes().contains(targetProjectType)) {
task.setApplicationModel(applicationModel);
Expand All @@ -108,6 +109,9 @@ public void execute(MigrationReport report) throws Exception {
if (cancelOnError) {
throw ex;
} else {
if (firstMigrationTaskException == null) {
firstMigrationTaskException = ex;
}
logger.error("Failed to apply task, rolling back and continuing with the next one.", ex);
}
} catch (RuntimeException e) {
Expand All @@ -116,6 +120,9 @@ public void execute(MigrationReport report) throws Exception {
}
}
}
if (firstMigrationTaskException != null) {
throw firstMigrationTaskException;
}
} finally {
generateReport(report, applicationModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -133,16 +134,27 @@ public void executeWithTaskThatFailsNotStopExecution() throws Exception {
.withOuputVersion(MULE_413_VERSION)
.build();

AbstractMigrationTask migrationTask = mock(AbstractMigrationTask.class);
AbstractMigrationTask migrationTask1 = mock(AbstractMigrationTask.class);
doThrow(MigrationTaskException.class)
.when(migrationTask)
.when(migrationTask1)
.execute(any(MigrationReport.class));
when(migrationTask.getApplicableProjectTypes()).thenReturn(singleton(MULE_FOUR_APPLICATION));
when(migrationTask1.getApplicableProjectTypes()).thenReturn(singleton(MULE_FOUR_APPLICATION));

migrationTasks.add(migrationTask);
AbstractMigrationTask migrationTask2 = mock(AbstractMigrationTask.class);
when(migrationTask2.getApplicableProjectTypes()).thenReturn(singleton(MULE_FOUR_APPLICATION));

migrationTasks.add(migrationTask1);
migrationTasks.add(migrationTask2);
Whitebox.setInternalState(migrationJob, "migrationTasks", migrationTasks);
migrationJob.execute(new DefaultMigrationReport());
verify(migrationTask, times(1)).execute(any(MigrationReport.class));
try {
migrationJob.execute(new DefaultMigrationReport());
fail("expected MigrationTaskException");
} catch (MigrationTaskException mte) {
verify(migrationTask1, times(1)).execute(any(MigrationReport.class));
verify(migrationTask2, times(1)).execute(any(MigrationReport.class));
} catch (Exception e) {
fail("expected MigrationTaskException");
}
}

@Test(expected = MigrationTaskException.class)
Expand Down

0 comments on commit f9ca03e

Please sign in to comment.