OnceSchedulingStrategy implements ISchedulingStrategy { @Override public void init(final ISchedulerEntry entry, final IObject args) throws SchedulingStrategyExecutionException { try { LocalDateTime time = LocalDateTime.parse((String) args.getValue(timeFieldName)); Boolean save = (Boolean) args.getValue(saveFieldName); entry.getState().setValue(timeFieldName, time.toString()); if (save) { Boolean ntl = (Boolean) args.getValue(neverTooLateFieldName); entry.getState().setValue(neverTooLateFieldName, ntl); entry.save(); } entry.scheduleNext(datetimeToMillis(time)); } catch (ReadValueException | InvalidArgumentException | EntryStorageAccessException | ChangeValueException | EntryScheduleException e) { throw new SchedulingStrategyExecutionException("Error occurred initializing 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_initializeEntryAndSaveIfNecessary() throws Exception { ISchedulingStrategy strategy = new OnceSchedulingStrategy(); strategy.init(entry, IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'time':'1989-09-11T00:00:05','save':true,'neverTooLate':true}".replace('\'','"'))); verify(entry).save(); verify(entry).scheduleNext(LocalDateTime.parse("1989-09-11T00:00:05").atZone(ZoneOffset.UTC).toInstant().toEpochMilli()); assertEquals("1989-09-11T00:00:05", entry.getState().getValue(time)); assertEquals(true, entry.getState().getValue(ntl)); assertNull(entry.getState().getValue(save)); }
@Test public void Should_initializeEntryAndDoNotSaveItIfNotNecessary() throws Exception { ISchedulingStrategy strategy = new OnceSchedulingStrategy(); strategy.init(entry, IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'time':'1989-09-11T00:00:05','save':false}".replace('\'','"'))); verify(entry, times(0)).save(); verify(entry).scheduleNext(LocalDateTime.parse("1989-09-11T00:00:05").atZone(ZoneOffset.UTC).toInstant().toEpochMilli()); assertEquals("1989-09-11T00:00:05", entry.getState().getValue(time)); assertNull(entry.getState().getValue(ntl)); assertNull(entry.getState().getValue(save)); }