ActorCollectionReceiver implements IMessageReceiver { @Override public void receive(final IMessageProcessor processor) throws MessageReceiveException, AsynchronousOperationException { try { IObject stepConf = processor.getSequence().getCurrentReceiverArguments(); IFieldName fieldNameForKeyName = IOC.resolve( Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), stepConf.getValue(this.keyFieldName)); Object id = processor.getEnvironment().getValue(fieldNameForKeyName); IMessageReceiver child = resolveReceiver(id, stepConf); try { child.receive(processor); } catch (MessageReceiveException e) { throw new MessageReceiveException( MessageFormat.format("Error occurred in child receiver for key \"{0}\"", id), e); } } catch (ReadValueException | ResolutionException | InvalidArgumentException e) { throw new MessageReceiveException("Could not execute ActorCollectionReceiver.receive.", e); } } ActorCollectionReceiver(final Map<Object, IMessageReceiver> childStorage); @Override void receive(final IMessageProcessor processor);  }
@Test public void Should_createChildReceivers() throws Exception { IMessageReceiver receiver = new ActorCollectionReceiver(new HashMap<>()); receiver.receive(processorMocks[0]); receiver.receive(processorMocks[1]); receiver.receive(processorMocks[2]); receiver.receive(processorMocks[2]); receiver.receive(processorMocks[1]); receiver.receive(processorMocks[1]); verify(childReceiverMocks[0], times(1)).receive(same(processorMocks[0])); verify(childReceiverMocks[1], times(3)).receive(same(processorMocks[1])); verify(childReceiverMocks[2], times(2)).receive(same(processorMocks[2])); verify(creatorMocks[0], times(1)).create(any(), any(), any()); verify(creatorMocks[1], times(1)).create(any(), any(), any()); verify(creatorMocks[2], times(1)).create(any(), any(), any()); }
@Test(expected = MessageReceiveException.class) public void Should_throwWhenChildCreatorReturnsMoreThanOneReceiver() throws Exception { doAnswer(invocation -> { invocation.getArgumentAt(0, IReceiverObjectListener.class).acceptItem(null, childReceiverMocks[0]); invocation.getArgumentAt(0, IReceiverObjectListener.class).acceptItem(null, childReceiverMocks[1]); invocation.getArgumentAt(0, IReceiverObjectListener.class).endItems(); return null; }).when(creatorMocks[0]).create(any(), any(), any()); IMessageReceiver receiver = new ActorCollectionReceiver(new HashMap<>()); receiver.receive(processorMocks[0]); }
@Test(expected = MessageReceiveException.class) public void Should_throwWhenChildCreatorReturnsNullItem() throws Exception { doAnswer(invocation -> { invocation.getArgumentAt(0, IReceiverObjectListener.class).acceptItem(null, null); invocation.getArgumentAt(0, IReceiverObjectListener.class).acceptItem(null, childReceiverMocks[1]); invocation.getArgumentAt(0, IReceiverObjectListener.class).endItems(); return null; }).when(creatorMocks[0]).create(any(), any(), any()); IMessageReceiver receiver = new ActorCollectionReceiver(new HashMap<>()); receiver.receive(processorMocks[0]); }
@Test(expected = MessageReceiveException.class) public void Should_throwWhenChildCreatorReturnsNonReceiverItem() throws Exception { doAnswer(invocation -> { invocation.getArgumentAt(0, IReceiverObjectListener.class).acceptItem(null, new Object()); invocation.getArgumentAt(0, IReceiverObjectListener.class).acceptItem(null, childReceiverMocks[1]); invocation.getArgumentAt(0, IReceiverObjectListener.class).endItems(); return null; }).when(creatorMocks[0]).create(any(), any(), any()); IMessageReceiver receiver = new ActorCollectionReceiver(new HashMap<>()); receiver.receive(processorMocks[0]); }
@Test(expected = MessageReceiveException.class) public void Should_throwWhenChildCreatorReturnsNoItems() throws Exception { doAnswer(invocation -> { invocation.getArgumentAt(0, IReceiverObjectListener.class).endItems(); return null; }).when(creatorMocks[0]).create(any(), any(), any()); IMessageReceiver receiver = new ActorCollectionReceiver(new HashMap<>()); receiver.receive(processorMocks[0]); }
@Test(expected = MessageReceiveException.class) public void Should_throwWhenChildCreatorDoesNotCall_endItems() throws Exception { doAnswer(invocation -> { invocation.getArgumentAt(0, IReceiverObjectListener.class).acceptItem(null, childReceiverMocks[1]); return null; }).when(creatorMocks[0]).create(any(), any(), any()); IMessageReceiver receiver = new ActorCollectionReceiver(new HashMap<>()); receiver.receive(processorMocks[0]); }