Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeeshan Ghalib committed Feb 29, 2016
1 parent 3659273 commit 0e83ba3
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 58 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<artifactId>javax.json</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public DispatcherProducer() {
* @return The correct dispatcher instance.
* @throws IllegalArgumentException if the injection point does not contain any adaptor annotations.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Produces
public Dispatcher produce(final InjectionPoint injectionPoint) {
final Class targetClass = injectionPoint.getMember().getDeclaringClass();
Expand All @@ -45,6 +46,7 @@ public Dispatcher produce(final InjectionPoint injectionPoint) {
}
}

@SuppressWarnings({"rawtypes", "unchecked"})
void register(@Observes final ServiceComponentFoundEvent event) {
getDispatcher(event.getComponent()).register(instantiateHandler(event.getHandlerBean()));
}
Expand All @@ -61,7 +63,7 @@ private Object instantiateHandler(final Bean<Object> bean) {

private AsynchronousDispatcher getDispatcher(final Component component) {
if (!dispatcherMap.containsKey(component)) {
dispatcherMap.put(component, new AsynchronousDispatcher());
dispatcherMap.putIfAbsent(component, new AsynchronousDispatcher());
}

return dispatcherMap.get(component);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
public class AnnotationScanner implements Extension {

@SuppressWarnings("unused")
void afterDeploymentValidation(@Observes final AfterDeploymentValidation event, final BeanManager beanManager) {
beanManager.getBeans(Object.class, new AnnotationLiteral<Any>() {
}).stream().forEach(bean -> getEvent(bean).ifPresent(x -> beanManager.fireEvent(x)));
Expand All @@ -29,6 +30,7 @@ void afterDeploymentValidation(@Observes final AfterDeploymentValidation event,
* @param bean a bean that could be a handler.
* @return an optional event.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
private Optional<ServiceComponentFoundEvent> getEvent(final Bean bean) {
if (bean.getBeanClass().isAnnotationPresent(ServiceComponent.class)) {
return Optional.of(new ServiceComponentFoundEvent(Component.getComponentFromServiceComponent(bean.getBeanClass()), bean));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import uk.gov.justice.services.core.util.CoreUtil;
import uk.gov.justice.services.messaging.Envelope;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Alternative;
import java.util.Objects;

/**
* Sends an action to the next layer using JMS Sender.
Expand All @@ -33,4 +33,18 @@ public void send(final Envelope envelope) {
jmsSender.send(jmsEndpoints.getEndpoint(destinationComponent, contextName), envelope);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DefaultSender that = (DefaultSender) o;
return Objects.equals(jmsSender, that.jmsSender) &&
destinationComponent == that.destinationComponent &&
Objects.equals(jmsEndpoints, that.jmsEndpoints);
}

@Override
public int hashCode() {
return Objects.hash(jmsSender, destinationComponent, jmsEndpoints);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public SenderProducer() {
* @param injectionPoint injection point where the Sender is being injected into.
* @return An implementation of the Sender.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Produces
public Sender produce(final InjectionPoint injectionPoint) {
final Class targetClass = injectionPoint.getMember().getDeclaringClass();
Expand All @@ -53,7 +54,7 @@ public Sender produce(final InjectionPoint injectionPoint) {

private Sender getSender(final Component component) {
if (!senderMap.containsKey(component)) {
senderMap.put(component, new DefaultSender(jmsSender, componentDestination.getDefault(component), jmsEndpoints));
senderMap.putIfAbsent(component, new DefaultSender(jmsSender, componentDestination.getDefault(component), jmsEndpoints));
}
return senderMap.get(component);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public class DispatcherProducerTest {
@Mock
private Metadata metadata;

private TestCommandAPIHandler handlerInstance;
private TestCommandApiHandler handlerInstance;

private DispatcherProducer dispatcherProducer;

@Mock
private InjectionPoint commandAPIInjectionPoint;
private InjectionPoint commandApiInjectionPoint;

@Mock
private InjectionPoint commandControllerInjectionPoint1;
Expand All @@ -51,7 +51,7 @@ public class DispatcherProducerTest {
private InjectionPoint commandControllerInjectionPoint2;

@Mock
private Member commandAPIMember;
private Member commandApiMember;

@Mock
private Member commandControllerMember1;
Expand All @@ -66,10 +66,10 @@ public class DispatcherProducerTest {
private Bean bean;

@Mock
private AsynchronousDispatcher commandAPIDispatcher;
private AsynchronousDispatcher commandApiDispatcher;

@Mock
private HandlerRegistry commandAPIRegistry;
private HandlerRegistry commandApiRegistry;

@Mock
private HandlerRegistry commandControllerRegistry;
Expand All @@ -86,15 +86,15 @@ public class DispatcherProducerTest {
@Before
public void setup() {
dispatcherProducer = new DispatcherProducer();
handlerInstance = new TestCommandAPIHandler();
handlerInstance = new TestCommandApiHandler();

dispatcherProducer.beanManager = beanManager;

when(commandAPIInjectionPoint.getMember()).thenReturn(commandAPIMember);
when(commandApiInjectionPoint.getMember()).thenReturn(commandApiMember);
when(commandControllerInjectionPoint1.getMember()).thenReturn(commandControllerMember1);
when(commandControllerInjectionPoint2.getMember()).thenReturn(commandControllerMember2);

doReturn(TestCommandAPIAdaptor.class).when(commandAPIMember).getDeclaringClass();
doReturn(TestCommandApiAdaptor.class).when(commandApiMember).getDeclaringClass();
doReturn(TestCommandControllerAdaptor1.class).when(commandControllerMember1).getDeclaringClass();
doReturn(TestCommandControllerAdaptor2.class).when(commandControllerMember2).getDeclaringClass();

Expand Down Expand Up @@ -123,7 +123,7 @@ public void shouldReturnExistingDispatcher() throws Exception {

@Test
public void shouldReturnADifferentDispatcher() throws Exception {
Dispatcher dispatcher = dispatcherProducer.produce(commandAPIInjectionPoint);
Dispatcher dispatcher = dispatcherProducer.produce(commandApiInjectionPoint);
assertThat(dispatcher, notNullValue());

Dispatcher anotherDispatcher = dispatcherProducer.produce(commandControllerInjectionPoint2);
Expand All @@ -139,15 +139,15 @@ public void shouldThrowExceptionWithNoAdaptor() throws Exception {
@Test
public void shouldRegisterHandler() throws Exception {
dispatcherProducer.register(new ServiceComponentFoundEvent(COMMAND_API, bean));
Dispatcher dispatcher = dispatcherProducer.produce(commandAPIInjectionPoint);

Dispatcher dispatcher = dispatcherProducer.produce(commandApiInjectionPoint);
assertThat(dispatcher, notNullValue());
dispatcher.dispatch(envelope);
assertThat(handlerInstance.envelope, equalTo(envelope));
}

@Adapter(COMMAND_API)
public static class TestCommandAPIAdaptor {
public static class TestCommandApiAdaptor {
}

@Adapter(COMMAND_CONTROLLER)
Expand All @@ -159,7 +159,7 @@ public static class TestCommandControllerAdaptor2 {
}

@ServiceComponent(COMMAND_API)
public static class TestCommandAPIHandler {
public static class TestCommandApiHandler {

public Envelope envelope;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AnnotationScannerTest {
private ServiceComponentFoundEvent serviceComponentFoundEvent;

@Mock
private Bean<TestCommandAPIHandler> beanMockCommandAPIHandler;
private Bean<TestCommandApiHandler> beanMockCommandApiHandler;

@Mock
private Bean<TestCommandController> beanMockCommandController;
Expand All @@ -53,17 +53,17 @@ public class AnnotationScannerTest {
public void setup() {
annotationScanner = new AnnotationScanner();

doReturn(TestCommandAPIHandler.class).when(beanMockCommandAPIHandler).getBeanClass();
doReturn(TestCommandApiHandler.class).when(beanMockCommandApiHandler).getBeanClass();
doReturn(TestCommandController.class).when(beanMockCommandController).getBeanClass();
doReturn(TestCommandHandler.class).when(beanMockCommandHandler).getBeanClass();
doReturn(Object.class).when(beanMockDummy).getBeanClass();
}

@Test
public void shouldFireCommandAPIFoundEventWithCommandAPI() throws Exception {
public void shouldFireCommandApiFoundEventWithCommandApi() throws Exception {
ArgumentCaptor<ServiceComponentFoundEvent> captor = ArgumentCaptor.forClass(ServiceComponentFoundEvent.class);
doReturn(new HashSet<Bean>() {{
add(beanMockCommandAPIHandler);
add(beanMockCommandApiHandler);
}}).when(beanManager).getBeans(any(), any());

annotationScanner.afterDeploymentValidation(afterDeploymentValidation, beanManager);
Expand Down Expand Up @@ -110,7 +110,7 @@ public void shouldNotFireAnyEventWithNoHandler() throws Exception {
}

@ServiceComponent(COMMAND_API)
public static class TestCommandAPIHandler {
public static class TestCommandApiHandler {
}

@ServiceComponent(COMMAND_CONTROLLER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;
import uk.gov.justice.services.core.jms.exception.JmsSenderException;
import uk.gov.justice.services.core.util.JsonObjectConverter;
import uk.gov.justice.services.messaging.DefaultEnvelope;
Expand Down Expand Up @@ -68,15 +67,11 @@ public class JmsSenderTest {
@Mock
private TextMessage textMessage;

@Mock
private Logger logger;

private JmsSender jmsSender;

@Before
public void setup() throws JMSException, NamingException {
jmsSender = new JmsSender();
jmsSender.logger = logger;
jmsSender.queueConnectionFactory = queueConnectionFactory;
jmsSender.jsonObjectConverter = new JsonObjectConverter();
when(queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE)).thenReturn(session);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package uk.gov.justice.services.core.sender;

import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import uk.gov.justice.services.core.annotation.Component;
import uk.gov.justice.services.core.jms.JmsEndpoints;
import uk.gov.justice.services.core.jms.JmsSender;
import uk.gov.justice.services.messaging.Envelope;
Expand All @@ -13,7 +15,9 @@
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.justice.services.core.annotation.Component.COMMAND_API;
import static uk.gov.justice.services.core.annotation.Component.COMMAND_CONTROLLER;
import static uk.gov.justice.services.core.annotation.Component.COMMAND_HANDLER;

@RunWith(MockitoJUnitRunner.class)
public class DefaultSenderTest {
Expand All @@ -38,10 +42,34 @@ public void setup() {
doNothing().when(jmsSender).send(new JmsEndpoints().getEndpoint(COMMAND_CONTROLLER, CONTEXT_NAME), envelope);
}

@SuppressWarnings({"squid:MethodCyclomaticComplexity", "squid:S1067", "squid:S00122"})
@Test
public void shouldTestEqualsAndHashCode() {
JmsEndpoints jmsEndpoints = new JmsEndpoints();
JmsSender jmsSender = new JmsSender();

final Sender item1 = createSender(jmsSender, COMMAND_API, jmsEndpoints);
final Sender item2 = createSender(jmsSender, COMMAND_API, jmsEndpoints);
final Sender item3 = createSender(null, COMMAND_API, jmsEndpoints);
final Sender item4 = createSender(jmsSender, COMMAND_HANDLER, jmsEndpoints);
final Sender item5 = createSender(jmsSender, COMMAND_API, null);

new EqualsTester()
.addEqualityGroup(item1, item2)
.addEqualityGroup(item3)
.addEqualityGroup(item4)
.addEqualityGroup(item5)
.testEquals();
}

@Test
public void shouldSendEnvelopeToEndpoint() throws Exception {
DefaultSender sender = new DefaultSender(jmsSender, COMMAND_CONTROLLER, new JmsEndpoints());
sender.send(envelope);
verify(jmsSender).send(ENDPOINT, envelope);
}

private Sender createSender(final JmsSender jmsSender, final Component destinationComponent, final JmsEndpoints jmsEndpoints) {
return new DefaultSender(jmsSender, destinationComponent, jmsEndpoints);
}
}
Loading

0 comments on commit 0e83ba3

Please sign in to comment.