RawObjectCreator implements IRoutedObjectCreator { @Override public void createObject(final IRouter router, final IObject description) throws ObjectCreationException, InvalidArgumentException { if (null == router) { throw new InvalidArgumentException("Router should not be null."); } if (null == description) { throw new InvalidArgumentException("Description should not be null."); } try { Object address = IOC.resolve(Keys.getOrAdd("route_from_object_name"), description.getValue(nameFieldName)); Object receiver = IOC.resolve(Keys.getOrAdd(String.valueOf(description.getValue(dependencyFieldName))), description); if (!(receiver instanceof IMessageReceiver)) { throw new ObjectCreationException("Resolved dependency does not implement receiver interface."); } router.register(address, (IMessageReceiver) receiver); } catch (ReadValueException | InvalidArgumentException | ResolutionException e) { throw new ObjectCreationException("Error occurred creating the receiver.", e); } } RawObjectCreator(); @Override void createObject(final IRouter router, final IObject description);  }
@Test(expected = InvalidArgumentException.class) public void Should_throwWhenRouterIsNull() throws Exception { IRoutedObjectCreator creator = new RawObjectCreator(); creator.createObject(null, descriptionMock); }
@Test(expected = InvalidArgumentException.class) public void Should_throwWhenDescriptionIsNull() throws Exception { IRoutedObjectCreator creator = new RawObjectCreator(); creator.createObject(routerMock, null); }
@Test public void Should_resolveDependencyAndRegisterInRouter() throws Exception { IRoutedObjectCreator creator = new RawObjectCreator(); IMessageReceiver receiverMock = mock(IMessageReceiver.class); when(descriptionMock.getValue(same(dependencyFieldName))).thenReturn("dependency_1"); when(descriptionMock.getValue(same(nameFieldName))).thenReturn("name_1"); when(Keys.getOrAdd("dependency_1")).thenReturn(dependencyKey); when(IOC.resolve(same(dependencyKey), same(descriptionMock))).thenReturn(receiverMock); when(IOC.resolve(same(routeFromObjectNameKey), eq("name_1"))).thenReturn("route_1"); creator.createObject(routerMock, descriptionMock); verify(routerMock).register(eq("route_1"), same(receiverMock)); }
@Test(expected = ObjectCreationException.class) public void Should_throw_When_dependencyIsNotReceiver() throws Exception { IRoutedObjectCreator creator = new RawObjectCreator(); Object notReceiverMock = mock(Object.class); when(descriptionMock.getValue(same(dependencyFieldName))).thenReturn("dependency_1"); when(descriptionMock.getValue(same(nameFieldName))).thenReturn("name_1"); when(Keys.getOrAdd("dependency_1")).thenReturn(dependencyKey); when(IOC.resolve(same(dependencyKey), same(descriptionMock))).thenReturn(notReceiverMock); when(IOC.resolve(same(routeFromObjectNameKey), eq("name_1"))).thenReturn("route_1"); creator.createObject(routerMock, descriptionMock); }
@Test(expected = ObjectCreationException.class) public void Should_wrapExceptions() throws Exception { IRoutedObjectCreator creator = new RawObjectCreator(); Object notReceiverMock = mock(Object.class); when(descriptionMock.getValue(same(dependencyFieldName))).thenReturn("dependency_1"); when(descriptionMock.getValue(same(nameFieldName))).thenReturn("name_1"); when(Keys.getOrAdd("dependency_1")).thenReturn(dependencyKey); when(IOC.resolve(any(), any())).thenThrow(ResolutionException.class); creator.createObject(routerMock, descriptionMock); }