OnceSchedulingStrategy implements ISchedulingStrategy { @Override public void restore(final ISchedulerEntry entry) throws SchedulingStrategyExecutionException { try { LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC); LocalDateTime time = LocalDateTime.parse((String) entry.getState().getValue(timeFieldName)); Boolean ntl = (Boolean) entry.getState().getValue(neverTooLateFieldName); if (now.isAfter(time) && !ntl) { entry.cancel(); } else { entry.scheduleNext(datetimeToMillis(time)); } } catch (ReadValueException | InvalidArgumentException | EntryScheduleException | EntryStorageAccessException e) { throw new SchedulingStrategyExecutionException("Error occurred restoring an entry.", e); } } OnceSchedulingStrategy(); @Override void init(final ISchedulerEntry entry, final IObject args); @Override void postProcess(final ISchedulerEntry entry); @Override void restore(final ISchedulerEntry entry); @Override void processException(final ISchedulerEntry entry, final Throwable e); @Override void notifyPaused(final ISchedulerEntry entry); @Override void notifyUnPaused(final ISchedulerEntry entry); @Override void processPausedExecution(final ISchedulerEntry entry);  }
@Test public void Should_restoreEntryAndCancelItWhenItIsTooLateWhenItMayBeTooLate() throws Exception { when(entry.getState()).thenReturn(IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'time':'2000-10-10T00:50:15','neverTooLate':false}".replace('\'','"'))); ISchedulingStrategy strategy = new OnceSchedulingStrategy(); strategy.restore(entry); verify(entry).cancel(); }
@Test public void Should_restoreEntryAndScheduleItWhenItIsTooLateButItIsNeverTooLate() throws Exception { when(entry.getState()).thenReturn(IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'time':'2000-10-10T00:50:15','neverTooLate':true}".replace('\'','"'))); ISchedulingStrategy strategy = new OnceSchedulingStrategy(); strategy.restore(entry); verify(entry).scheduleNext(LocalDateTime.parse("2000-10-10T00:50:15").atZone(ZoneOffset.UTC).toInstant().toEpochMilli()); }
@Test public void Should_restoreEntryAndRescheduleItWhenItIsNotTooLate() throws Exception { ISchedulingStrategy strategy = new OnceSchedulingStrategy(); when(entry.getState()).thenReturn(IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'time':'3410-01-01T00:00:01','neverTooLate':true}".replace('\'','"'))); strategy.restore(entry); verify(entry).scheduleNext(LocalDateTime.parse("3410-01-01T00:00:01").atZone(ZoneOffset.UTC).toInstant().toEpochMilli()); reset(entry); when(entry.getState()).thenReturn(IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'time':'3410-01-01T00:00:01','neverTooLate':false}".replace('\'','"'))); strategy.restore(entry); verify(entry).scheduleNext(LocalDateTime.parse("3410-01-01T00:00:01").atZone(ZoneOffset.UTC).toInstant().toEpochMilli()); }