UpgradeService { public void upgradeVersion() { LOG.info("Global upgrade process starting"); int alreadyApplied = 0; int numberOfTasksApplied = 0; for (UpgradeTask task : tasks) { final UpgradeTaskId taskId = task.getId(); if (task.getTarget() != VERSION) { LOG.debug("{} does not target version", taskId); continue; } final String targetId = VERSION.name() + '-' + taskId.getVersion(); if (repository.isAlreadyApplied(targetId, taskId)) { LOG.debug("{} already applied, let's skip it", taskId); alreadyApplied++; } else { LOG.debug("apply upgrade {}", taskId); try { task.run(); } catch (Exception exception) { LOG.error("Failed to apply upgrade {}", taskId, exception); break; } repository.applied(targetId, taskId); numberOfTasksApplied++; } } LOG.info("Global upgrade process finished, {}/{} upgrade(s) performed ({} already applied).", numberOfTasksApplied, tasks.size(), alreadyApplied); }  boolean needUpgrade(); void upgradeVersion(); void upgradeUser(String userId);  }
@Test public void shouldApplyTasksForVersion() throws Exception { List<UpgradeTask> expected = new ArrayList<>(); expected.add(new MockUpgradeTask("1.2.0-PE", "1.2.0-preums", 1, VERSION)); expected.add(new MockUpgradeTask("1.2.0-PE", "1.2.0-deuz", 2, VERSION)); expected.add(new MockUpgradeTask("1.2.0-PE", "1.2.0-troiz", 3, VERSION)); List<UpgradeTask> unexpected = new ArrayList<>(); unexpected.add(new MockUpgradeTask("1.2.0-PE", "1.2.0-deuz", 2, USER)); unexpected.add(new MockUpgradeTask("1.3.0-PE", "1.3.0-1-troiz", 3, USER)); unexpected.add(new MockUpgradeTask("1.4.0-PE", "1.4.0-preums", 1, USER)); tasks.addAll(expected); tasks.addAll(unexpected); when(repository.isAlreadyApplied(anyString(), any())).thenReturn(false); service.upgradeVersion(); for (UpgradeTask task : expected) { final MockUpgradeTask mockTask = (MockUpgradeTask) task; assertTrue(mockTask.applied); } for (UpgradeTask task : unexpected) { final MockUpgradeTask mockTask = (MockUpgradeTask) task; assertFalse(mockTask.applied); } }