CleanupBootService extends LifecycleSupport { @Override protected void doStart() { createCleanupTask(); } @Inject  CleanupBootService(final TaskScheduler taskScheduler);   }
@Test public void shouldCreateTaskOnStart() throws Exception { underTest.doStart(); verify(taskScheduler).scheduleTask(any(), any()); }
@Test public void scheduleTaskDailyAt1am() throws Exception { underTest.doStart(); verify(scheduleFactory, times(2)).cron(any(Date.class), eq("0 0 1 * * ?")); }
@Test public void setTaskName() throws Exception { underTest.doStart(); assertThat(taskConfig.getName(), is(equalTo(TASK_NAME))); }
@Test public void doNotCreateTaskIfAlreadyExists() throws Exception { when(taskScheduler.listsTasks()).thenReturn(ImmutableList.of(taskInfo)); underTest.doStart(); verify(taskScheduler, never()).scheduleTask(any(), any()); }
@Test public void duplicatesRemoved() { TaskInfo nameMismatch = mockTask("foo", CleanupBootService.CRON); TaskInfo cronMismatch = mockTask(CleanupBootService.TASK_NAME, "1 0 1 * * ?"); TaskInfo scheduleMismatch = mock(TaskInfo.class); when(scheduleMismatch.getConfiguration()).thenReturn(taskConfig); when(scheduleMismatch.getName()).thenReturn(CleanupBootService.TASK_NAME); TaskInfo matchA = mockTask(CleanupBootService.TASK_NAME, CleanupBootService.CRON); TaskInfo matchB = mockTask(CleanupBootService.TASK_NAME, CleanupBootService.CRON); List<TaskInfo> tasks = ImmutableList.of(nameMismatch, cronMismatch, scheduleMismatch, matchA, matchB); when(taskScheduler.listsTasks()).thenReturn(tasks); underTest.doStart(); verify(nameMismatch, never()).remove(); verify(cronMismatch, never()).remove(); verify(scheduleMismatch, never()).remove(); verify(matchA, never()).remove(); verify(matchB).remove(); }