FixedHourScheduledJob implements ScheduledJob { @Override public final boolean shouldExecute(LocalDateTime lastExecuteTime) { var now = LocalDateTime.now(clock); var today = LocalDate.now(clock); var yesterday = today.minus(1, ChronoUnit.DAYS); var lastExecuteDate = lastExecuteTime.toLocalDate(); if(yesterday.isAfter(lastExecuteDate)) { return true; } else if(today.isAfter(lastExecuteDate)) { return now.getHour() >= this.jobExecutionHour; } else { return lastExecuteTime.getHour() < this.jobExecutionHour; } } protected  FixedHourScheduledJob(Clock clock, int jobExecutionHour); @Override final boolean shouldExecute(LocalDateTime lastExecuteTime);  }
@Test public void shouldUpdateReturnsTrueWhenLastExecuteAtLeastTwoDaysAgo() { var lastExecuteTime = LocalDateTime.now(clock).minus(2L, ChronoUnit.DAYS); var actualShouldUpdate = fixedHourJob.shouldExecute(lastExecuteTime); assertThat(actualShouldUpdate).isTrue(); }
@Test public void shouldUpdateReturnsTrueWhenLastExecuteWasTodayButBeforeUpdateHour() { var lastExecuteHour = EXECUTION_HOUR - 1; var lastExecuteTime = LocalDate.now(clock).atTime(lastExecuteHour, 0); var actualShouldUpdate = fixedHourJob.shouldExecute(lastExecuteTime); assertThat(actualShouldUpdate).isTrue(); }
@Test public void shouldUpdateReturnsFalseWhenLastExecuteWasTodayAfterUpdateHour() { var updateHour = 4; var instant = Instant.parse("2019-03-14T05:00:00.00Z"); var zoneId = ZoneId.systemDefault(); var localClock = Clock.fixed(instant, zoneId); var localFixedHourJob = new FixedHourJob(localClock, updateHour); var lastExecuteTime = LocalDateTime.now(localClock); var actualShouldUpdate = localFixedHourJob.shouldExecute(lastExecuteTime); assertThat(actualShouldUpdate).isFalse(); }
@Test public void shouldUpdateReturnsTrueWhenLastExecuteWasYesterdayAndUpdateHourPassed() { var updateHour = 4; var instant = Instant.parse("2019-03-14T05:00:00.00Z"); var zoneId = ZoneId.systemDefault(); var localClock = Clock.fixed(instant, zoneId); var localFixedHourJob = new FixedHourJob(localClock, updateHour); var lastExecuteTime = LocalDateTime.now(localClock).minus(1L, ChronoUnit.DAYS); var actualShouldUpdate = localFixedHourJob.shouldExecute(lastExecuteTime); assertThat(actualShouldUpdate).isTrue(); }
@Test public void shouldUpdateReturnsFalseWhenLasExecuteWasYesterdayButUpdateHourDidNotPass() { var updateHour = 7; var instant = Instant.parse("2019-03-14T05:00:00.00Z"); var zoneId = ZoneId.systemDefault(); var localClock = Clock.fixed(instant, zoneId); var localFixedHourJob = new FixedHourJob(localClock, updateHour); var lastExecuteTime = LocalDateTime.now(localClock).minus(1L, ChronoUnit.DAYS); var actualShouldUpdate = localFixedHourJob.shouldExecute(lastExecuteTime); assertThat(actualShouldUpdate).isFalse(); }