ContractorController { @RequestMapping(value = "/egworks/contractor", method = PUT, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) public String updateContractor(@RequestBody final String requestJson, final HttpServletResponse response) throws IOException { List<RestErrors> errors = new ArrayList<>(); final RestErrors restErrors = new RestErrors(); ApplicationThreadLocals.setUserId(2L); if (StringUtils.isBlank(requestJson)) { restErrors.setErrorCode(RestApiConstants.THIRD_PARTY_ERR_CODE_NO_JSON_REQUEST); restErrors.setErrorMessage(RestApiConstants.THIRD_PARTY_ERR_MSG_NO_JSON_REQUEST); errors.add(restErrors); return JsonConvertor.convert(restErrors); } final ContractorHelper contractorHelper = (ContractorHelper) getObjectFromJSONRequest(requestJson, ContractorHelper.class); errors = externalContractorService.validateContactorToUpdate(contractorHelper); if (!errors.isEmpty()) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return JsonConvertor.convert(errors); } else { final Contractor contractor = externalContractorService.populateContractorToUpdate(contractorHelper); final Contractor savedContractor = externalContractorService.updateContractor(contractor); final StringBuilder modifyMessage = new StringBuilder(); modifyMessage.append("Contractor data modified successfully with code ").append(savedContractor.getCode()); response.setStatus(HttpServletResponse.SC_CREATED); return JsonConvertor.convert(modifyMessage); } }  @RequestMapping(value = "/egworks/contractors", method = GET, produces = MediaType.APPLICATION_JSON_VALUE) String getContractors(@RequestParam(value = "code", required = false) final String code,
            final HttpServletResponse response); @RequestMapping(value = "/egworks/contractor", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) String createContractor(@RequestBody final String requestJson,
            final HttpServletResponse response); @RequestMapping(value = "/egworks/contractor", method = PUT, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) String updateContractor(@RequestBody final String requestJson,
            final HttpServletResponse response);  }
@Test public void shouldModifyContractor() throws IOException { requestJson = IOUtils.toString( Thread.currentThread().getContextClassLoader().getResourceAsStream("createcontractor-Json.json"), ApplicationConstant.DEFAULT_CHARACTER_ENCODING); when(externalContractorService.validateContactorToUpdate(Matchers.anyObject())).thenReturn(errors); when(externalContractorService.populateContractorToUpdate(Matchers.anyObject())).thenReturn(contractor); when(externalContractorService.updateContractor(Matchers.anyObject())).thenReturn(contractor); responseJson = contractorController.updateContractor(requestJson, response); final StringBuilder successMessage = new StringBuilder(); successMessage.append("\"Contractor data modified successfully with code ").append(contractor.getCode()).append("\""); assertEquals(responseJson, successMessage.toString()); }
@Test public void shouldValidateModifyContractorIsCodeAndNameEmpty() throws IOException { requestJson = IOUtils.toString( Thread.currentThread().getContextClassLoader().getResourceAsStream("createcontractorempty-Json.json"), ApplicationConstant.DEFAULT_CHARACTER_ENCODING); when(externalContractorService.validateContactorToUpdate(Matchers.anyObject())).thenReturn(errors); when(externalContractorService.populateContractorToUpdate(Matchers.anyObject())).thenReturn(contractor); when(externalContractorService.updateContractor(Matchers.anyObject())).thenReturn(contractor); responseJson = contractorController.updateContractor(requestJson, response); final JSONObject jsonObject = new JSONObject(responseJson); assertEquals(RestApiConstants.THIRD_PARTY_ERR_CODE_NO_JSON_REQUEST, jsonObject.getString("errorCode")); }