QuerySensorSchedulerAction implements ISchedulerAction { @Override public void execute(final ISchedulerEntry entry) throws SchedulerActionExecutionException { try { Object queryExecutorDependency = entry.getState().getValue(queryExecutorFieldName); IQueryExecutor queryExecutor = IOC.resolve(IOC.resolve(IOC.getKeyForKeyStorage(), queryExecutorDependency)); Collection<? extends Number> data = queryExecutor.execute(entry); Long time = entry.getLastTime(); IObject message = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")); message.setValue(dataFieldName, data); message.setValue(periodStartFieldName, time); message.setValue(periodEndFieldName, time); Object chainId = IOC.resolve(Keys.getOrAdd("chain_id_from_map_name"), entry.getState().getValue(statisticsChainFieldName)); MessageBus.send(message, chainId); } catch (ReadValueException | InvalidArgumentException | QueryExecutionException | ResolutionException | ChangeValueException | SendingMessageException e) { throw new SchedulerActionExecutionException("Error occurred querying or sending statistics.", e); } } QuerySensorSchedulerAction(); @Override void init(final ISchedulerEntry entry, final IObject args); @Override void execute(final ISchedulerEntry entry);  }
@Test public void Should_executeQueryAndSendData() throws Exception { Collection dataMock = mock(Collection.class); when(queryExecutorMock.execute(same(schedulerEntryMock))).thenReturn(dataMock).thenThrow(QueryExecutionException.class); IObject state = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), ("{" + "'queryExecutor':'that query executor'," + "'statisticsChain':'the_statistics_chain'" + "}").replace('\'','"')); when(schedulerEntryMock.getState()).thenReturn(state); when(schedulerEntryMock.getLastTime()).thenReturn(100600L); new QuerySensorSchedulerAction().execute(schedulerEntryMock); ArgumentCaptor<IObject> messageCaptor = ArgumentCaptor.forClass(IObject.class); verify(messageBusHandlerMock).handle(messageCaptor.capture(), eq("the_statistics_chain__0")); assertSame(dataMock, messageCaptor.getValue().getValue(IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), "data"))); assertEquals(100600L, messageCaptor.getValue().getValue(IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), "periodStart"))); assertEquals(100600L, messageCaptor.getValue().getValue(IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), "periodEnd"))); }