EmbeddedSensorCreationStrategy implements IResolveDependencyStrategy { @Override public <T> T resolve(final Object... args) throws ResolveDependencyStrategyException { try { Object statisticsChainId = args[CHAIN_ID_STRATEGY_ARGUMENT_INDEX]; IObject conf = (IObject) args[CONFIG_STRATEGY_ARGUMENT_INDEX]; IObject commonArgs = (IObject) conf.getValue(argsFieldName); Collection<IObject> embedConf = (Collection<IObject>) conf.getValue(embedFieldName); List<IObject> replacements = new ArrayList<>(embedConf.size()); Object targetChainId = IOC.resolve(Keys.getOrAdd("chain_id_from_map_name"), conf.getValue(chainFieldName)); IChainStorage chainStorage = IOC.resolve(Keys.getOrAdd(IChainStorage.class.getCanonicalName())); for (IObject embedItemConf : embedConf) { IObject itemArgs = (IObject) embedItemConf.getValue(argsFieldName); itemArgs = (itemArgs == null) ? commonArgs : itemArgs; final Object itemDependency = embedItemConf.getValue(dependencyFieldName); itemArgs.setValue(statisticsChainFieldName, statisticsChainId); IMessageReceiver sensor = IOC.resolve( IOC.resolve(IOC.getKeyForKeyStorage(), (itemDependency == null) ? DEFAULT_RECEIVER_DEPENDENCY : itemDependency), itemArgs ); IObject replacement = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")); replacement.setValue(stepFieldName, embedItemConf.getValue(stepFieldName)); replacement.setValue(dependencyFieldName, SENSOR_RECEIVER_REPLACEMENT_DEPENDENCY); replacement.setValue(argsFieldName, sensor); replacements.add(replacement); } IObject modification = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")); modification.setValue(modificationFieldName, RECEIVER_REPLACEMENT_MODIFICATION_DEPENDENCY); modification.setValue(replacementsFieldName, replacements); Object modId = chainStorage.update(targetChainId, modification); return (T) new EmbeddedSensorHandle(chainStorage, targetChainId, modId); } catch (ReadValueException | ChangeValueException | InvalidArgumentException | ResolutionException | ChainNotFoundException | ChainModificationException e) { throw new ResolveDependencyStrategyException(e); } } EmbeddedSensorCreationStrategy(); @Override T resolve(final Object... args);  }
@Test public void Should_replaceSomeReceiversByWrappers() throws Exception { chain = mock(IReceiverChain.class); when(chain.get(0)).thenReturn(receivers[0]); when(chain.get(1)).thenReturn(receivers[1]); when(chain.get(2)).thenReturn(receivers[2]); ISensorHandle handle = new EmbeddedSensorCreationStrategy().resolve("stat_chain", IOC.resolve( Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"), ("{" + "'chain':'the_chain'," + "'args':{}," + "'embed':[" + " {" + " 'step':0," + " 'dependency':'additional sensor'" + " }," + " {" + " 'step':2" + " }" + "]" + "}").replace('\'','"') )); assertNotNull(handle); chain.get(0).receive(processor); verify(receivers[0], times(1)).receive(processor); verify(receivers[8], times(1)).receive(processor); chain.get(2).receive(processor); verify(receivers[2], times(1)).receive(processor); verify(receivers[9], times(1)).receive(processor); }