CommandHandlerProvider implements ApplicationContextAware { public NewCommandSourceHandler getHandler(final String entity, final String action) { Preconditions.checkArgument(StringUtils.isNoneEmpty(entity), "An entity must be given!"); Preconditions.checkArgument(StringUtils.isNoneEmpty(action), "An action must be given!"); final String key = entity + "|" + action; if (!this.registeredHandlers.containsKey(key)) { throw new UnsupportedCommandException(key); } return (NewCommandSourceHandler) this.applicationContext.getBean(this.registeredHandlers.get(key)); } CommandHandlerProvider(); NewCommandSourceHandler getHandler(final String entity, final String action); @Override void setApplicationContext(ApplicationContext applicationContext);  }
@Test public void shouldRegisterHandler() { final Long testCommandId = 815L; final NewCommandSourceHandler registeredHandler = this.commandHandlerProvider.getHandler("HUMAN", "UPDATE"); final CommandProcessingResult result = registeredHandler .processCommand(JsonCommand.fromExistingCommand(testCommandId, null, null, null, null, null, null, null, null, null, null)); assertEquals(testCommandId, result.commandId()); }
@Test public void shouldThrowUnsupportedCommandException() throws UnsupportedCommandException { Assertions.assertThrows(UnsupportedCommandException.class, () -> { this.commandHandlerProvider.getHandler("WHATEVER", "DOSOMETHING"); }); }