Added provider to register and retrieve command handlers.

This commit is contained in:
Markus Geiss
2015-04-26 16:17:44 +02:00
parent 86d12b57a8
commit a9ad69f844
9 changed files with 232 additions and 4 deletions
@@ -0,0 +1,56 @@
/**
* 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.junit.Assert;
import org.junit.Test;
import org.mifosplatform.commands.exception.UnsupportedCommandException;
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
import org.mifosplatform.infrastructure.core.api.JsonCommand;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
public class CommandHandlerProviderTest {
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 CommandProcessingResult result =
registeredHandler.processCommand(
JsonCommand.fromExistingCommand(testCommandId, null, null, null, null, null, null, null, null));
Assert.assertEquals(testCommandId, result.commandId());
} catch (UnsupportedCommandException ucex) {
Assert.fail();
}
}
@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");
}
}
@@ -0,0 +1,23 @@
/**
* 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());
}
}
@@ -0,0 +1,25 @@
/**
* 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.annotation.CommandType;
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
import org.mifosplatform.infrastructure.core.api.JsonCommand;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
@CommandType(entity = "HUMAN", action = "UPDATE")
public class ValidCommandHandler implements NewCommandSourceHandler {
public ValidCommandHandler(final CommandHandlerProvider commandHandlerProvider) {
super();
commandHandlerProvider.registerHandler(this);
}
@Override
public CommandProcessingResult processCommand(JsonCommand command) {
return CommandProcessingResult.commandOnlyResult(command.commandId());
}
}