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

Commit

Permalink
Merge aa59edc into 0f9aa44
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Apr 4, 2016
2 parents 0f9aa44 + aa59edc commit 81c965f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package uk.gov.justice.services.core.handler;

import static java.lang.String.format;

import uk.gov.justice.services.core.handler.exception.HandlerExecutionException;
import uk.gov.justice.services.core.handler.registry.exception.InvalidHandlerException;
import uk.gov.justice.services.messaging.Envelope;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static java.lang.String.format;

/**
* Encapsulates a handler class instance and a handler method.
*
* <p>
* Asynchronous handler methods will return a null {@link Void} whereas synchronous handler methods
* must return an {@link Envelope}.
*/
Expand Down Expand Up @@ -88,6 +88,7 @@ public Object execute(final Envelope envelope) {

/**
* Check if this handler method is synchronous.
*
* @return true if the method returns a value
*/
public boolean isSynchronous() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uk.gov.justice.services.core.handler;


import uk.gov.justice.services.core.annotation.Handles;
import uk.gov.justice.services.core.handler.registry.exception.InvalidHandlerException;

import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Arrays.stream;

public final class Handlers {

private Handlers() {
}

/**
* Finds all the handler methods from handler object annotated by {@link Handles}
*
* @param handler Handler object to be examined.
* @return List of methods annotated with <code>Handles.class</code>.
*/
public static List<Method> handlerMethodsFrom(final Object handler) {
final Class<?> handlerClass = handler.getClass();
final List<Method> handlerMethods = stream(handlerClass.getMethods())
.filter(method -> method.isAnnotationPresent(Handles.class))
.collect(Collectors.toList());

if (handlerMethods.isEmpty()) {
throw new InvalidHandlerException("Class " + handlerClass + " doesn't have any method annotated with the annotation @Handles");
}

return handlerMethods;
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package uk.gov.justice.services.core.handler.registry;

import static java.lang.String.format;
import static uk.gov.justice.services.core.handler.HandlerUtil.findHandlerMethods;

import uk.gov.justice.services.core.annotation.Handles;
import uk.gov.justice.services.core.handler.HandlerMethod;
import uk.gov.justice.services.core.handler.exception.MissingHandlerException;
import uk.gov.justice.services.core.handler.registry.exception.DuplicateHandlerException;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.String.format;
import static uk.gov.justice.services.core.handler.Handlers.handlerMethodsFrom;

/**
* Service for storing a map of which command handlers handle which commands.
*/
Expand All @@ -36,11 +35,8 @@ public HandlerMethod get(final String name, final boolean isSynchronous) {
* @param handlerInstance handler instance to be registered.
*/
public void register(final Object handlerInstance) {
final List<Method> handlerMethods = findHandlerMethods(handlerInstance.getClass(), Handles.class);

for (Method handlerMethod : handlerMethods) {
register(handlerInstance, handlerMethod);
}
handlerMethodsFrom(handlerInstance).stream()
.forEach(method -> register(handlerInstance, method));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.justice.services.core.handler.HandlerUtil.findHandlerMethods;
import static uk.gov.justice.services.core.handler.Handlers.handlerMethodsFrom;

@RunWith(MockitoJUnitRunner.class)
public class HandlerMethodTest {
Expand Down Expand Up @@ -71,7 +71,7 @@ public void shouldReturnStringDescriptionOfHandlerInstanceAndMethod() {

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWithNullHandlerInstance() {
new HandlerMethod(null, method(AsynchronousCommandHandler.class, "handles"), Void.TYPE);
new HandlerMethod(null, method(new AsynchronousCommandHandler(), "handles"), Void.TYPE);
}

@Test(expected = IllegalArgumentException.class)
Expand All @@ -81,32 +81,32 @@ public void shouldThrowExceptionWithNullMethod() {

@Test(expected = InvalidHandlerException.class)
public void shouldThrowExceptionWithSynchronousMethod() {
new HandlerMethod(asynchronousCommandHandler, method(AsynchronousCommandHandler.class, "handlesSync"), Void.TYPE);
new HandlerMethod(asynchronousCommandHandler, method(new AsynchronousCommandHandler(), "handlesSync"), Void.TYPE);
}

@Test(expected = InvalidHandlerException.class)
public void shouldThrowExceptionWithAsynchronousMethod() {
new HandlerMethod(synchronousCommandHandler, method(SynchronousCommandHandler.class, "handlesAsync"), Envelope.class);
new HandlerMethod(synchronousCommandHandler, method(new SynchronousCommandHandler(), "handlesAsync"), Envelope.class);
}

@Test(expected = IllegalArgumentException.class)
public void shouldOnlyAcceptVoidOrEnvelopeReturnTypes() {
new HandlerMethod(synchronousCommandHandler, method(SynchronousCommandHandler.class, "handles"), Object.class);
new HandlerMethod(synchronousCommandHandler, method(new SynchronousCommandHandler(), "handles"), Object.class);
}

private HandlerMethod asyncHandlerInstance() {
return new HandlerMethod(asynchronousCommandHandler, method(AsynchronousCommandHandler.class, "handles"), Void.TYPE);
return new HandlerMethod(asynchronousCommandHandler, method(new AsynchronousCommandHandler(), "handles"), Void.TYPE);
}

private HandlerMethod syncHandlerInstance() {
return new HandlerMethod(synchronousCommandHandler, method(SynchronousCommandHandler.class, "handles"), Envelope.class);
return new HandlerMethod(synchronousCommandHandler, method(new SynchronousCommandHandler(), "handles"), Envelope.class);
}

private Method method(final Class<?> clazz, final String name) {
return findHandlerMethods(clazz, Handles.class).stream()
.filter(m -> name.equals(m.getName()))
private Method method(final Object object, final String methofName) {
return handlerMethodsFrom(object).stream()
.filter(m -> methofName.equals(m.getName()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(format("Cannot find method with name %s", name)));
.orElseThrow(() -> new IllegalArgumentException(format("Cannot find method with name %s", methofName)));
}

private Envelope testEnvelope(String fileName) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

public class HandlerUtilTest {
public class HandlersTest {

@Test
public void shouldBeAWellDefinedUtilityClass() {
assertUtilityClassWellDefined(HandlerUtil.class);
assertUtilityClassWellDefined(Handlers.class);
}

@Test
public void shouldFindHandlerMethods() {
List<Method> methods = HandlerUtil.findHandlerMethods(CommandHandler.class, Handles.class);
List<Method> methods = Handlers.handlerMethodsFrom(new CommandHandler());
assertThat(methods, notNullValue());
assertThat(methods, IsCollectionWithSize.hasSize(3));
}

@Test(expected = InvalidHandlerException.class)
public void shouldThrowExceptionWithInvalidHandlerMethods() {
HandlerUtil.findHandlerMethods(InvalidHandler.class, Handles.class);
Handlers.handlerMethodsFrom(new InvalidHandler());
}

public static class CommandHandler {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package uk.gov.justice.services.core.handler.registry;

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.nullValue;
import static uk.gov.justice.services.core.annotation.Component.COMMAND_HANDLER;
import static uk.gov.justice.services.core.handler.HandlerMethod.*;

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.Handles;
import uk.gov.justice.services.core.annotation.ServiceComponent;
import uk.gov.justice.services.core.handler.HandlerMethod;
import uk.gov.justice.services.core.handler.exception.MissingHandlerException;
import uk.gov.justice.services.core.handler.registry.exception.DuplicateHandlerException;
import uk.gov.justice.services.core.handler.registry.exception.InvalidHandlerException;
import uk.gov.justice.services.messaging.Envelope;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.nullValue;
import static uk.gov.justice.services.core.annotation.Component.COMMAND_HANDLER;
import static uk.gov.justice.services.core.handler.HandlerMethod.ASYNCHRONOUS;
import static uk.gov.justice.services.core.handler.HandlerMethod.SYNCHRONOUS;

/**
* Unit tests for the HandlerRegistry class.
Expand Down

0 comments on commit 81c965f

Please sign in to comment.