### Question:
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);  }### Answer:
@Test public void testPostCommandReturnsNotFoundForNonExistingCommand() throws ExecutionException, InterruptedException { CompletableFuture<ResponseEntity> futureResult = testSubject.postCommand("non-existing-command", "irrelevant"); assertTrue(futureResult.isDone()); assertEquals(HttpStatus.NOT_FOUND, futureResult.get().getStatusCode()); }
@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()); }
@Test public void testPostCommandPublishesTheCommand() throws Exception { CreatePortfolioCommand testCmd = new CreatePortfolioCommand(new PortfolioId(), new UserId()); ObjectMapper realMapper = new ObjectMapper(); String testCommandJson = realMapper.writeValueAsString(testCmd); when(objectMapper.readValue(testCommandJson, CreatePortfolioCommand.class)).thenReturn(testCmd); when(commandGateway.send(any())).thenReturn(CompletableFuture.completedFuture("irrelevant")); CompletableFuture<ResponseEntity> futureResult = testSubject.postCommand(testCmd.getClass().getSimpleName(), testCommandJson); assertTrue(futureResult.isDone()); assertEquals(HttpStatus.OK, futureResult.get().getStatusCode()); verify(objectMapper).readValue(testCommandJson, testCmd.getClass()); verify(commandGateway).send(testCmd); }
@Test public void testPostCommandPublishesTheCommandForAnEmptyCommandPayloadIfTheCommandAllowsIt() throws Exception { CreateOrderBookCommand testCmd = new CreateOrderBookCommand(); when(commandGateway.send(any())).thenReturn(CompletableFuture.completedFuture("irrelevant")); CompletableFuture<ResponseEntity> futureResult = testSubject.postCommand(testCmd.getClass().getSimpleName(), null); assertTrue(futureResult.isDone()); assertEquals(HttpStatus.OK, futureResult.get().getStatusCode()); verify(commandGateway).send(any(CreateOrderBookCommand.class)); }