ContinuouslyRepeatScheduleStrategy implements ISchedulingStrategy { @Override public void init(final ISchedulerEntry entry, final IObject args) throws SchedulingStrategyExecutionException { try { String start = (String) args.getValue(startFieldName); LocalDateTime startTime; TemporalAmount interval = parseInterval((String) args.getValue(intervalFieldName)); LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC); if (start == null) { startTime = now; } else { startTime = LocalDateTime.parse(start); } entry.getState().setValue(startFieldName, startTime.toString()); entry.getState().setValue(intervalFieldName, interval.toString()); if (args.getValue(saveFieldName) == null || (Boolean) args.getValue(saveFieldName)) { entry.save(); } entry.scheduleNext(nextTime(startTime, interval, datetimeToMillis(now))); } catch (ReadValueException | InvalidArgumentException | EntryScheduleException | EntryStorageAccessException | ChangeValueException e) { throw new SchedulingStrategyExecutionException("Error occurred initializing scheduler entry.", e); } } ContinuouslyRepeatScheduleStrategy(); @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_initializeEntry() throws Exception { ISchedulingStrategy strategy = new ContinuouslyRepeatScheduleStrategy(); strategy.init(entry, IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'start':'3410-01-01T00:00:01','interval':'PT24H'}".replace('\'','"'))); assertEquals("3410-01-01T00:00:01", entry.getState().getValue(start)); assertEquals("PT24H", entry.getState().getValue(interval)); verify(entry).scheduleNext(LocalDateTime.parse("3410-01-01T00:00:01").atZone(ZoneOffset.UTC).toInstant().toEpochMilli()); verify(entry).save(); }
@Test public void Should_notSaveEntryIfSaveFlagIsSetToFalse() throws Exception { ISchedulingStrategy strategy = new ContinuouslyRepeatScheduleStrategy(); strategy.init(entry, IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'start':'3410-01-01T00:00:01','interval':'PT24H','save':false}".replace('\'','"'))); verify(entry).scheduleNext(LocalDateTime.parse("3410-01-01T00:00:01").atZone(ZoneOffset.UTC).toInstant().toEpochMilli()); verify(entry, times(0)).save(); }
@Test public void Should_initializeEntryUsingCurrentTimeIfNoTimeGiven() throws Exception { ISchedulingStrategy strategy = new ContinuouslyRepeatScheduleStrategy(); strategy.init(entry, IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'interval':'PT24H'}".replace('\'','"'))); assertTrue(Duration.between( LocalDateTime.parse((String) entry.getState().getValue(start)), LocalDateTime.now(ZoneOffset.UTC)).abs().getSeconds() <= 1); assertEquals("PT24H", entry.getState().getValue(interval)); }
@Test public void Should_initializeEntryWithPeriodInterval() throws Exception { ISchedulingStrategy strategy = new ContinuouslyRepeatScheduleStrategy(); strategy.init(entry, IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), "{'start':'3410-01-01T00:00:01','interval':'P2M'}".replace('\'','"'))); assertEquals("3410-01-01T00:00:01", entry.getState().getValue(start)); assertEquals("P2M", entry.getState().getValue(interval)); verify(entry).scheduleNext(LocalDateTime.parse("3410-01-01T00:00:01").atZone(ZoneOffset.UTC).toInstant().toEpochMilli()); }