TaxController { @RequestMapping(value = "/taxservice", method = POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<JsonNode> submitTaxForm(@RequestBody JsonNode jsonNode) throws JsonProcessingException { if(isNotValid(jsonNode)) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } if (specialEmployeesService.isEmployeeBlacklisted(getEmployeeId(jsonNode))) { taxLogger.log(getEmployeeId(jsonNode), getTaxAmount(jsonNode), RESPONSE_BODY_FAIL); return new ResponseEntity<>(createResponse(RESPONSE_BODY_FAIL), HttpStatus.INTERNAL_SERVER_ERROR); } else { specialEmployeesService.sleepIfNecessary(getEmployeeId(jsonNode)); taxLogger.log(getEmployeeId(jsonNode), getTaxAmount(jsonNode), RESPONSE_BODY_SUCCESS); return new ResponseEntity<>(createResponse(RESPONSE_BODY_SUCCESS), HttpStatus.OK); } }  @RequestMapping(value = "/taxservice", method = POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody ResponseEntity<JsonNode> submitTaxForm(@RequestBody JsonNode jsonNode); @RequestMapping(value = "/taxservice", method = GET) @ResponseBody String getWebServiceLog(HttpServletResponse response); @RequestMapping(value = "/reset", method = POST) ResponseEntity resetSpecialEmployeesService(); static final String RESPONSE_BODY_SUCCESS; static final String RESPONSE_BODY_FAIL; static final String EMPLOYEE_ID; static final String AMOUNT; }
@Test public void givenValidTaxTo_whenSubmitTaxForm_ThenALogLineIsCreatedWithStatusOk() throws JsonProcessingException { taxController.submitTaxForm(taxTo); verify(taxLogger, times(1)).log(employeeId, amount, "OK"); }
@Test public void givenBlacklistedEmployee_whenSubmitTaxForm_ThenALogLineIsCreatedWithStatusNotOk() throws JsonProcessingException { when(specialEmployeesServiceMock.isEmployeeBlacklisted(1L)).thenReturn(true); taxController.submitTaxForm(taxTo); verify(taxLogger, times(1)).log(employeeId, amount, "FAILURE"); }
@Test public void givenBlacklistEmployee_whenSubmitTaxForm_thenResponseFails() throws JsonProcessingException { when(specialEmployeesServiceMock.isEmployeeBlacklisted(employeeId)).thenReturn(true); ResponseEntity<JsonNode> response = taxController.submitTaxForm(taxTo); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(response.getBody().get("status").asText()).isEqualTo(TaxController.RESPONSE_BODY_FAIL); }
@Test public void givenEmployeeWithTax_whenSubmitTaxForm_thenItSleepsIfNecessary() throws InterruptedException, JsonProcessingException { taxController.submitTaxForm(taxTo); verify(specialEmployeesServiceMock).sleepIfNecessary(employeeId); }
@Test public void givenBlacklistEmployee_whenSubmitTaxForm_thenDoNotTryToTimeout() throws JsonProcessingException { when(specialEmployeesServiceMock.isEmployeeBlacklisted(employeeId)).thenReturn(true); taxController.submitTaxForm(taxTo); verify(specialEmployeesServiceMock, times(0)).sleepIfNecessary(employeeId); }