MessageBusSectionProcessingStrategy implements ISectionStrategy { @Override public void onLoadConfig(final IObject config) throws ConfigurationProcessingException { try { IObject messageBusObject = (IObject) config.getValue(name); IQueue<ITask> queue = IOC.resolve(Keys.getOrAdd("task_queue")); Integer stackDepth = Integer.valueOf(String.valueOf(messageBusObject.getValue(stackDepthFieldName))); IChainStorage chainStorage = IOC.resolve(IOC.resolve(IOC.getKeyForKeyStorage(), IChainStorage.class.getCanonicalName())); String startChainName = (String) messageBusObject.getValue(startChainNameFieldName); Object mapId = IOC.resolve(Keys.getOrAdd("chain_id_from_map_name"), startChainName); IReceiverChain chain = chainStorage.resolve(mapId); IAction<IObject> finalAction = IOC.resolve(Keys.getOrAdd("send response action")); IMessageBusHandler handler = new MessageBusHandler(queue, stackDepth, chain, finalAction); ScopeProvider.getCurrentScope().setValue(MessageBus.getMessageBusKey(), handler); } catch (ReadValueException | InvalidArgumentException | ScopeProviderException | ScopeException e) { throw new ConfigurationProcessingException("Error occurred loading \"client\" configuration section.", e); } catch (ResolutionException e) { throw new ConfigurationProcessingException("Error occurred resolving \"client\".", e); } catch (ChainNotFoundException e) { throw new ConfigurationProcessingException("Error occurred resolving \"chain\".", e); } } MessageBusSectionProcessingStrategy(); @Override void onLoadConfig(final IObject config); @Override IFieldName getSectionName();  }
@Test public void testLoadingConfig() throws Exception { IResolveDependencyStrategy sequenceStrategy = mock(IResolveDependencyStrategy.class); IOC.register( IOC.resolve(IOC.getKeyForKeyStorage(), "info.smart_tools.smartactors.message_processing_interfaces.message_processing.IMessageProcessingSequence"), sequenceStrategy ); IResolveDependencyStrategy messageProcessorStrategy = mock(IResolveDependencyStrategy.class); IOC.register( IOC.resolve(IOC.getKeyForKeyStorage(), "info.smart_tools.smartactors.message_processing_interfaces.message_processing.IMessageProcessor"), messageProcessorStrategy ); IOC.register( IOC.resolve(IOC.getKeyForKeyStorage(), "info.smart_tools.smartactors.iobject.iobject.IObject"), new ApplyFunctionToArgumentsStrategy( (args) -> { return new DSObject(); } ) ); when(chainStorage.resolve(mapId)).thenReturn(receiverChain); DSObject config = new DSObject("\n" + " {\n" + " \"messageBus\": \n" + " {\n" + " \"routingChain\": \"mainChain\",\n" + " \"stackDepth\": 5\n" + " }\n" + " \n" + " }"); MessageBusSectionProcessingStrategy strategy = new MessageBusSectionProcessingStrategy(); strategy.onLoadConfig(config); IMessageProcessingSequence sequence = mock(IMessageProcessingSequence.class); when(sequenceStrategy.resolve(5, this.receiverChain)).thenReturn(sequence); IMessageProcessor processor = mock(IMessageProcessor.class); when(messageProcessorStrategy.resolve(this.taskQueue, sequence)).thenReturn(processor); IObject result = new DSObject(); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { result.setValue(new FieldName("message"), invocationOnMock.getArguments()[0]); result.setValue(new FieldName("context"), invocationOnMock.getArguments()[1]); return null; } }).when(processor).process(any(IObject.class), any(IObject.class)); IMessageBusHandler handler = (IMessageBusHandler) ScopeProvider.getCurrentScope().getValue(MessageBus.getMessageBusKey()); assertNotNull(handler); Object replyToChainName = mock(Object.class); IObject message = mock(IObject.class); handler.handleForReply(message, replyToChainName); assertSame(result.getValue(new FieldName("message")), message); IObject resultContext = (IObject) result.getValue(new FieldName("context")); List<IAction> actions = (List<IAction>) resultContext.getValue(new FieldName("finalActions")); assertEquals(actions.size(), 1); IAction<IObject> action = actions.get(0); IObject env = new DSObject(); IObject messageForReply = new DSObject(); env.setValue(new FieldName("message"), messageForReply); env.setValue(new FieldName("context"), resultContext); action.execute(env); verify(responseAction, times(1)).execute(env); }