@Test public void testPostCommandReturnsNotFoundIfItCannotSerializeTheJsonCommand() throws Exception { Class<CreatePortfolioCommand> testCommandType = CreatePortfolioCommand.class; String faultyCommandJson = "{some-broken-json}"; when(objectMapper.readValue(faultyCommandJson, testCommandType)).thenThrow(new IOException()); CompletableFuture<ResponseEntity> futureResult = testSubject.postCommand(testCommandType.getSimpleName(), faultyCommandJson); assertTrue(futureResult.isDone()); assertEquals(HttpStatus.NOT_FOUND, futureResult.get().getStatusCode()); }
@PostMapping("/{command-type}") public CompletableFuture<ResponseEntity> postCommand(@PathVariable("command-type") String commandType, @RequestBody(required = false) String commandPayloadJson) { if (commandApi == null) { initializeCommandApi(); } logger.debug("Attempting to execute the '{}' command...", commandType); String fullCommandClassName = simpleToFullClassName.get(commandType); if (fullCommandClassName == null) { logger.warn("Tried to publish the command[{}] which does not exist.", commandType); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } Object command; try { Class<?> commandClass = classLoader.loadClass(fullCommandClassName); command = commandPayloadJson == null ? commandClass.newInstance() : objectMapper.readValue(commandPayloadJson, commandClass); } catch (ClassNotFoundException | IOException | InstantiationException | IllegalAccessException e) { logger.error("Failed to instantiate command for [{}] and json [{}]", commandType, commandPayloadJson, e); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } return commandGateway.send(command).thenApply(ResponseEntity::ok); }
CommandController implements BeanClassLoaderAware { @PostMapping("/{command-type}") public CompletableFuture<ResponseEntity> postCommand(@PathVariable("command-type") String commandType, @RequestBody(required = false) String commandPayloadJson) { if (commandApi == null) { initializeCommandApi(); } logger.debug("Attempting to execute the '{}' command...", commandType); String fullCommandClassName = simpleToFullClassName.get(commandType); if (fullCommandClassName == null) { logger.warn("Tried to publish the command[{}] which does not exist.", commandType); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } Object command; try { Class<?> commandClass = classLoader.loadClass(fullCommandClassName); command = commandPayloadJson == null ? commandClass.newInstance() : objectMapper.readValue(commandPayloadJson, commandClass); } catch (ClassNotFoundException | IOException | InstantiationException | IllegalAccessException e) { logger.error("Failed to instantiate command for [{}] and json [{}]", commandType, commandPayloadJson, e); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } return commandGateway.send(command).thenApply(ResponseEntity::ok); } }
CommandController implements BeanClassLoaderAware { @PostMapping("/{command-type}") public CompletableFuture<ResponseEntity> postCommand(@PathVariable("command-type") String commandType, @RequestBody(required = false) String commandPayloadJson) { if (commandApi == null) { initializeCommandApi(); } logger.debug("Attempting to execute the '{}' command...", commandType); String fullCommandClassName = simpleToFullClassName.get(commandType); if (fullCommandClassName == null) { logger.warn("Tried to publish the command[{}] which does not exist.", commandType); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } Object command; try { Class<?> commandClass = classLoader.loadClass(fullCommandClassName); command = commandPayloadJson == null ? commandClass.newInstance() : objectMapper.readValue(commandPayloadJson, commandClass); } catch (ClassNotFoundException | IOException | InstantiationException | IllegalAccessException e) { logger.error("Failed to instantiate command for [{}] and json [{}]", commandType, commandPayloadJson, e); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } return commandGateway.send(command).thenApply(ResponseEntity::ok); } CommandController(CommandGateway commandGateway,
                             ObjectMapper objectMapper,
                             JsonSchemaGenerator jsonSchemaGenerator); }
CommandController implements BeanClassLoaderAware { @PostMapping("/{command-type}") public CompletableFuture<ResponseEntity> postCommand(@PathVariable("command-type") String commandType, @RequestBody(required = false) String commandPayloadJson) { if (commandApi == null) { initializeCommandApi(); } logger.debug("Attempting to execute the '{}' command...", commandType); String fullCommandClassName = simpleToFullClassName.get(commandType); if (fullCommandClassName == null) { logger.warn("Tried to publish the command[{}] which does not exist.", commandType); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } Object command; try { Class<?> commandClass = classLoader.loadClass(fullCommandClassName); command = commandPayloadJson == null ? commandClass.newInstance() : objectMapper.readValue(commandPayloadJson, commandClass); } catch (ClassNotFoundException | IOException | InstantiationException | IllegalAccessException e) { logger.error("Failed to instantiate command for [{}] and json [{}]", commandType, commandPayloadJson, e); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } return commandGateway.send(command).thenApply(ResponseEntity::ok); } CommandController(CommandGateway commandGateway,
                             ObjectMapper objectMapper,
                             JsonSchemaGenerator jsonSchemaGenerator); @GetMapping("/api") Map<String, String> getCommandApi(); @PostMapping("/{command-type}") CompletableFuture<ResponseEntity> postCommand(@PathVariable("command-type") String commandType,
                                                         @RequestBody(required = false) String commandPayloadJson); @Override void setBeanClassLoader(@NotNull ClassLoader classLoader); }
CommandController implements BeanClassLoaderAware { @PostMapping("/{command-type}") public CompletableFuture<ResponseEntity> postCommand(@PathVariable("command-type") String commandType, @RequestBody(required = false) String commandPayloadJson) { if (commandApi == null) { initializeCommandApi(); } logger.debug("Attempting to execute the '{}' command...", commandType); String fullCommandClassName = simpleToFullClassName.get(commandType); if (fullCommandClassName == null) { logger.warn("Tried to publish the command[{}] which does not exist.", commandType); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } Object command; try { Class<?> commandClass = classLoader.loadClass(fullCommandClassName); command = commandPayloadJson == null ? commandClass.newInstance() : objectMapper.readValue(commandPayloadJson, commandClass); } catch (ClassNotFoundException | IOException | InstantiationException | IllegalAccessException e) { logger.error("Failed to instantiate command for [{}] and json [{}]", commandType, commandPayloadJson, e); return CompletableFuture.completedFuture(ResponseEntity.notFound().build()); } return commandGateway.send(command).thenApply(ResponseEntity::ok); } CommandController(CommandGateway commandGateway,
                             ObjectMapper objectMapper,
                             JsonSchemaGenerator jsonSchemaGenerator); @GetMapping("/api") Map<String, String> getCommandApi(); @PostMapping("/{command-type}") CompletableFuture<ResponseEntity> postCommand(@PathVariable("command-type") String commandType,
                                                         @RequestBody(required = false) String commandPayloadJson); @Override void setBeanClassLoader(@NotNull ClassLoader classLoader);  }