OnFeatureLoadingSectionProcessingStrategy implements ISectionStrategy { @Override public void onLoadConfig(final IObject config) throws ConfigurationProcessingException { try { List<IObject> onFeatureLoadingConfig = (List<IObject>) config.getValue(this.sectionNameFieldName); IChainStorage chainStorage = IOC.resolve(IOC.resolve(IOC.getKeyForKeyStorage(), IChainStorage.class.getCanonicalName())); IQueue<ITask> queue = IOC.resolve(Keys.getOrAdd("task_queue")); Integer stackDepth; try { stackDepth = IOC.resolve(Keys.getOrAdd("default_stack_depth")); } catch (ResolutionException e) { stackDepth = 5; } for (IObject task : onFeatureLoadingConfig) { String chainName = (String) task.getValue(this.chainFieldName); Object mapId = IOC.resolve(Keys.getOrAdd("chain_id_from_map_name"), chainName); IReceiverChain chain = chainStorage.resolve(mapId); List<IObject> messages = (List<IObject>)task.getValue(this.messagesFieldName); for (IObject message : messages) { IMessageProcessingSequence processingSequence = IOC.resolve( IOC.resolve(IOC.getKeyForKeyStorage(), "info.smart_tools.smartactors.message_processing_interfaces.message_processing.IMessageProcessingSequence"), stackDepth, chain ); IMessageProcessor messageProcessor = IOC.resolve( IOC.resolve(IOC.getKeyForKeyStorage(), "info.smart_tools.smartactors.message_processing_interfaces.message_processing.IMessageProcessor"), queue, processingSequence ); messageProcessor.process(message, (IObject) IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject"))); } } } catch (ReadValueException | InvalidArgumentException | ResolutionException | MessageProcessorProcessException e) { throw new ConfigurationProcessingException("Error occurred executing \"onFeatureLoading\" configuration section.", e); } catch (ChainNotFoundException e) { throw new ConfigurationProcessingException("Error occurred resolving \"chain\".", e); } } OnFeatureLoadingSectionProcessingStrategy(); @Override void onLoadConfig(final IObject config); @Override IFieldName getSectionName();  }
@Test public void checkOnLoadConfigMethod() throws Exception { ISectionStrategy strategy = new OnFeatureLoadingSectionProcessingStrategy(); IObject config = new DSObject("{\"onFeatureLoading\": [\n" + " {\n" + " \"chain\": \"chain1\",\n" + " \"messages\": [\n" + " {\n" + " \"key\": \"value1\"\n" + " }\n" + " ]\n" + " },\n" + " {\n" + " \"chain\": \"chain2\",\n" + " \"messages\": [\n" + " {\n" + " \"key\": \"value2\"\n" + " },\n" + " {\n" + " \"key\": \"value3\"\n" + " }\n" + " ]\n" + " }\n" + " ]}"); List<IObject> onFeatureLoadingConfigSection = (List<IObject>) config.getValue(new FieldName("onFeatureLoading")); IObject message11 = ((List<IObject>)onFeatureLoadingConfigSection.get(0).getValue(new FieldName("messages"))).get(0); IObject message21 = ((List<IObject>)onFeatureLoadingConfigSection.get(1).getValue(new FieldName("messages"))).get(0); IObject message22 = ((List<IObject>)onFeatureLoadingConfigSection.get(1).getValue(new FieldName("messages"))).get(1); this.chainMapIdResolveStrategy = mock(IResolveDependencyStrategy.class); Object mapId1 = mock(Object.class); Object mapId2 = mock(Object.class); IReceiverChain chain1 = mock(IReceiverChain.class); IReceiverChain chain2 = mock(IReceiverChain.class); when(this.chainMapIdResolveStrategy.resolve("chain1")).thenReturn(mapId1); when(this.chainMapIdResolveStrategy.resolve("chain2")).thenReturn(mapId2); IKey chainIdFromMapNameKey = Keys.getOrAdd("chain_id_from_map_name"); IOC.register(chainIdFromMapNameKey, this.chainMapIdResolveStrategy); when(this.chainStorage.resolve(mapId1)).thenReturn(chain1); when(this.chainStorage.resolve(mapId2)).thenReturn(chain2); this.sequenceResolveStrategy = mock(IResolveDependencyStrategy.class); IKey sequenceKey = Keys.getOrAdd("info.smart_tools.smartactors.message_processing_interfaces.message_processing.IMessageProcessingSequence"); this.sequence = mock(IMessageProcessingSequence.class); IOC.register(sequenceKey, this.sequenceResolveStrategy); when(this.sequenceResolveStrategy.resolve(this.stackDepth, chain1)).thenReturn(sequence); when(this.sequenceResolveStrategy.resolve(this.stackDepth, chain2)).thenReturn(sequence); strategy.onLoadConfig(config); verify(this.chainMapIdResolveStrategy, times(1)).resolve("chain1"); verify(this.chainMapIdResolveStrategy, times(1)).resolve("chain2"); verify(this.chainStorage, times(1)).resolve(mapId1); verify(this.chainStorage, times(1)).resolve(mapId2); verify(this.sequenceResolveStrategy, times(1)).resolve(this.stackDepth, chain1); verify(this.sequenceResolveStrategy, times(2)).resolve(this.stackDepth, chain2); verify(this.messageProcessor, times(1)).process(message11, this.context); verify(this.messageProcessor, times(1)).process(message21, this.context); verify(this.messageProcessor, times(1)).process(message22, this.context); }
@Test (expected = ConfigurationProcessingException.class) public void checkConfigurationProcessingExceptionOnWrongConfig() throws Exception { ISectionStrategy strategy = new OnFeatureLoadingSectionProcessingStrategy(); IObject config = new DSObject("{\"onFeatureLoading\": [\n" + " {\n" + " \"chain\": \"unknownChain\",\n" + " \"messages\": [\n" + " {\n" + " \"key\": \"value\"\n" + " }\n" + " ]\n" + " }]}"); strategy.onLoadConfig(config); fail(); }