ActorReceiverCreator implements IRoutedObjectCreator { @Override public void createObject(final IRouter router, final IObject description) throws ObjectCreationException, InvalidArgumentException { try { Map<Object, IMessageReceiver> handlerReceiversMap = new HashMap<>(); IWrapperGenerator wg = IOC.resolve(Keys.getOrAdd(IWrapperGenerator.class.getCanonicalName())); IReceiverGenerator rg = IOC.resolve(Keys.getOrAdd(IReceiverGenerator.class.getCanonicalName())); Object object = IOC.resolve( Keys.getOrAdd((String) description.getValue(this.dependency)), description ); List<Method> methods = new LinkedList<>(Arrays.asList(object.getClass().getDeclaredMethods())); methods.removeIf(m -> m.isSynthetic() || !Modifier.isPublic(m.getModifiers())); for (Method m : methods) { Class wrapperInterface = m.getParameterTypes()[0]; Object wrapper = wg.generate(wrapperInterface); IResolveDependencyStrategy strategy = new SingletonStrategy(wrapper); IMessageReceiver handlerReceiver = rg.generate(object, strategy, m.getName()); handlerReceiversMap.put(m.getName(), handlerReceiver); } IMessageReceiver handlerRoutingReceiver = new HandlerRoutingReceiver(handlerReceiversMap); IMessageReceiver actorReceiver = new ActorReceiver(handlerRoutingReceiver); router.register(description.getValue(this.name) , actorReceiver); } catch (Throwable e) { throw new ObjectCreationException("Could not create receiver chain.", e); } } ActorReceiverCreator(); @Override void createObject(final IRouter router, final IObject description);  }
@Test public void checkCreationAndExecution() throws Exception { IOC.register( IOC.resolve(IOC.getKeyForKeyStorage(), "info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), new ResolveByNameIocStrategy( (a) -> { try { return new FieldName((String) a[0]); } catch (Throwable e) { throw new RuntimeException("Could not create new instance of FieldName", e); } } ) ); IField field = mock(IField.class); IOC.register( IOC.resolve(IOC.getKeyForKeyStorage(), IField.class.getCanonicalName()), new ResolveByNameIocStrategy( (a) -> { return field; } ) ); IResolveDependencyStrategy rgs = mock(IResolveDependencyStrategy.class); IReceiverGenerator rg = mock(IReceiverGenerator.class); IOC.register(Keys.getOrAdd(IReceiverGenerator.class.getCanonicalName()), rgs); when(rgs.resolve()).thenReturn(rg); IMessageReceiver mr = mock(IMessageReceiver.class); when(rg.generate(any(CustomActor.class), any(IResolveDependencyStrategy.class), any(String.class))).thenReturn(mr); IObject objectSection = mock(IObject.class); when(objectSection.getValue(new FieldName("name"))).thenReturn("actorID"); when(objectSection.getValue(new FieldName("dependency"))).thenReturn("createSampleActorStrategy"); IResolveDependencyStrategy createSampleActorStrategy = mock(IResolveDependencyStrategy.class); IOC.register(Keys.getOrAdd("createSampleActorStrategy"), createSampleActorStrategy); ConstructorWrapperImpl wrapperImpl = new ConstructorWrapperImpl(); CustomActor a = new CustomActor(wrapperImpl); when(createSampleActorStrategy.resolve(objectSection)) .thenReturn(a); ActorReceiverCreator arc = new ActorReceiverCreator(); IRouter router = new Router(); arc.createObject(router, objectSection); assertEquals(((Router) router).map.size(), 1); IMessageReceiver actorReceiver = router.route("actorID"); assertSame(actorReceiver.getClass(), ActorReceiver.class); IMessageProcessor processor = mock(IMessageProcessor.class); IMessageProcessingSequence sequence = mock(IMessageProcessingSequence.class); IObject currentSequence = mock(IObject.class); IObject env = mock(IObject.class); when(processor.getEnvironment()).thenReturn(env); when(processor.getSequence()).thenReturn(sequence); when(sequence.getCurrentReceiverArguments()).thenReturn(currentSequence); when(field.in(currentSequence)).thenReturn("getSomeValue"); doNothing().when(mr).receive(processor); actorReceiver.receive(processor); verify(mr, times(1)).receive(processor); verify(createSampleActorStrategy, times(1)).resolve(objectSection); verify(rg, times(1)).generate( any(CustomActor.class), any(IResolveDependencyStrategy.class), any(String.class) ); verify(objectSection, times(1)).getValue(new FieldName("name")); verify(objectSection, times(1)).getValue(new FieldName("dependency")); }
@Test (expected = ObjectCreationException.class) public void checkMethodExceptionOnWrongArgs() throws Exception { IOC.register( IOC.resolve(IOC.getKeyForKeyStorage(), "info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), new ResolveByNameIocStrategy( (a) -> { try { return new FieldName((String) a[0]); } catch (Throwable e) { throw new RuntimeException("Could not create new instance of FieldName", e); } } ) ); ActorReceiverCreator arc = new ActorReceiverCreator(); arc.createObject(null, null); fail(); }