UpgradeService { public void upgradeUser(String userId) { LOG.info("Upgrade process starting for user {}", userId); int numberOfTasksApplied = 0; for (UpgradeTask task : tasks) { if (task.getTarget() != USER) { LOG.debug("{} does not target users", task.getId()); continue; } final String targetId = USER.name() + '-' + userId; if (repository.isAlreadyApplied(targetId, task.getId())) { LOG.debug("{} already applied for user {}, let's skip it", task.getId(), userId); } else { LOG.debug("apply upgrade {} for user {}", task.getId(), userId); task.run(); repository.applied(targetId, task.getId()); numberOfTasksApplied++; } } LOG.info("Upgrade process finished for user {}, {}/{} upgrade(s) performed", userId, numberOfTasksApplied, tasks.size()); }  boolean needUpgrade(); void upgradeVersion(); void upgradeUser(String userId);  }
@Test public void shouldApplyTasksToUser() throws Exception { List<UpgradeTask> expected = new ArrayList<>(); expected.add(new MockUpgradeTask("1.2.0-PE", "1.2.0-first", 1, USER)); expected.add(new MockUpgradeTask("1.2.0-PE", "1.2.0-second", 2, USER)); expected.add(new MockUpgradeTask("1.2.0-PE", "1.2.0-third", 3, USER)); expected.add(new MockUpgradeTask("1.3.0-PE", "1.3.0-1-fourth", 1, USER)); expected.add(new MockUpgradeTask("1.4.0-PE", "1.4.0-twelfth", 12, USER)); expected.add(new MockUpgradeTask("2.12.0-EE", "2.12.0-fourth", 4, USER)); expected.add(new MockUpgradeTask("10.5.0-EE", "2.12.0-fifth", 5, USER)); List<UpgradeTask> unexpected = new ArrayList<>(); unexpected.add(new MockUpgradeTask("1.2.0-PE", "1.2.0-second", 2, VERSION)); unexpected.add(new MockUpgradeTask("1.3.0-PE", "1.3.0-1-fifth", 5, VERSION)); unexpected.add(new MockUpgradeTask("1.4.0-PE", "1.4.0-eleventh", 11, VERSION)); unexpected.add(new MockUpgradeTask("2.12.0-EE", "2.12.0-third", 3, VERSION)); tasks.addAll(expected); tasks.addAll(unexpected); when(repository.isAlreadyApplied(anyString(), any())).thenReturn(false); service.upgradeUser("Mr Anonymous"); for (UpgradeTask task : expected) { final MockUpgradeTask mockTask = (MockUpgradeTask) task; assertTrue(mockTask.applied); } for (UpgradeTask task : unexpected) { final MockUpgradeTask mockTask = (MockUpgradeTask) task; assertFalse(mockTask.applied); } }
@Test public void shouldNotApplyUserTasks() throws Exception { tasks.add(new MockUpgradeTask("1.3-PE", "first", 1, USER)); tasks.add(new MockUpgradeTask("1.4-EE", "second", 2, USER)); tasks.add(new MockUpgradeTask("2.5-PE", "third", 3, USER)); tasks.add(new MockUpgradeTask("12.3-EE", "fourth", 4, USER)); tasks.add(new MockUpgradeTask("50.4-PE", "fifth", 5, USER)); tasks.add(new MockUpgradeTask("2.12.0-EE", "sixth", 6, USER)); tasks.add(new MockUpgradeTask("10.5.0-EE", "seventh", 7, USER)); when(repository.isAlreadyApplied(anyString(), any())).thenReturn(true); service.upgradeUser("Mr Anonymous"); for (UpgradeTask task : tasks) { final MockUpgradeTask mockTask = (MockUpgradeTask) task; assertFalse(mockTask.applied); } }