Added autodiscovery for command handlers.
This commit is contained in:
+14
-17
@@ -7,30 +7,35 @@ package org.mifosplatform.commands.provider;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mifosplatform.commands.exception.UnsupportedCommandException;
|
||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
||||
import org.mifosplatform.infrastructure.configuration.spring.TestsWithoutDatabaseAndNoJobsConfiguration;
|
||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = TestsWithoutDatabaseAndNoJobsConfiguration.class)
|
||||
public class CommandHandlerProviderTest {
|
||||
|
||||
@Autowired
|
||||
private CommandHandlerProvider commandHandlerProvider;
|
||||
|
||||
public CommandHandlerProviderTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRegisterHandler() {
|
||||
final CommandHandlerProvider provider = new CommandHandlerProvider();
|
||||
try {
|
||||
new ValidCommandHandler(provider);
|
||||
} catch (NullPointerException pex) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
try {
|
||||
final Long testCommandId = 815L;
|
||||
|
||||
final NewCommandSourceHandler registeredHandler = provider.getHandler("HUMAN", "UPDATE");
|
||||
final NewCommandSourceHandler registeredHandler = this.commandHandlerProvider.getHandler("HUMAN", "UPDATE");
|
||||
|
||||
final CommandProcessingResult result =
|
||||
registeredHandler.processCommand(
|
||||
@@ -41,16 +46,8 @@ public class CommandHandlerProviderTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void shouldNotRegisterHandlerAndThrowNullPointerException() {
|
||||
final CommandHandlerProvider provider = new CommandHandlerProvider();
|
||||
new InvalidCommandHandler(provider);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedCommandException.class)
|
||||
public void shouldThrowUnsupportedCommandException() {
|
||||
final CommandHandlerProvider provider = new CommandHandlerProvider();
|
||||
provider.getHandler("WHATEVER", "DOSOMETHING");
|
||||
|
||||
this.commandHandlerProvider.getHandler("WHATEVER", "DOSOMETHING");
|
||||
}
|
||||
}
|
||||
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
/**
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
package org.mifosplatform.commands.provider;
|
||||
|
||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||
|
||||
public class InvalidCommandHandler implements NewCommandSourceHandler {
|
||||
|
||||
public InvalidCommandHandler(final CommandHandlerProvider commandHandlerProvider) {
|
||||
super();
|
||||
commandHandlerProvider.registerHandler(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandProcessingResult processCommand(JsonCommand command) {
|
||||
return CommandProcessingResult.commandOnlyResult(command.commandId());
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -9,13 +9,14 @@ import org.mifosplatform.commands.annotation.CommandType;
|
||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@CommandType(entity = "HUMAN", action = "UPDATE")
|
||||
public class ValidCommandHandler implements NewCommandSourceHandler {
|
||||
|
||||
public ValidCommandHandler(final CommandHandlerProvider commandHandlerProvider) {
|
||||
public ValidCommandHandler() {
|
||||
super();
|
||||
commandHandlerProvider.registerHandler(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user