ContractorController { @RequestMapping(value = "/egworks/contractors", method = GET, produces = MediaType.APPLICATION_JSON_VALUE) public String getContractors(@RequestParam(value = "code", required = false) final String code, final HttpServletResponse response) { if (StringUtils.isBlank(code)) { response.setStatus(HttpServletResponse.SC_CREATED); return JsonConvertor.convert(externalContractorService.populateContractor()); } else { final RestErrors restErrors = new RestErrors(); final Contractor contractor = contractorService.getContractorByCode(code); if (contractor == null) { restErrors.setErrorCode(RestApiConstants.THIRD_PARTY_ERR_CODE_NOT_EXIST_CONTRACTOR); restErrors.setErrorMessage(RestApiConstants.THIRD_PARTY_ERR_MSG_NOT_EXIST_CONTRACTOR); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return JsonConvertor.convert(restErrors); } else { response.setStatus(HttpServletResponse.SC_CREATED); return JsonConvertor.convert(externalContractorService.populateContractorData(contractor)); } } }  @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 shouldValidateGetContractor() throws IOException { requestJson = IOUtils.toString( Thread.currentThread().getContextClassLoader().getResourceAsStream("createcontractor-Json.json"), ApplicationConstant.DEFAULT_CHARACTER_ENCODING); when(contractorService.getContractorByCode(Matchers.anyString())).thenReturn(null); responseJson = contractorController.getContractors(requestJson, response); final JSONObject jsonObject = new JSONObject(responseJson); assertEquals(RestApiConstants.THIRD_PARTY_ERR_CODE_NOT_EXIST_CONTRACTOR, jsonObject.getString("errorCode")); }
@Test public void shouldValidateGetContractorForCode() throws IOException { final ContractorHelper contractorHelper = new ContractorHelper(); contractorHelper.setCode("JUNITCODE"); contractorHelper.setName("JUNITNAME"); requestJson = IOUtils.toString( Thread.currentThread().getContextClassLoader().getResourceAsStream("createcontractor-Json.json"), ApplicationConstant.DEFAULT_CHARACTER_ENCODING); when(externalContractorService.populateContractorData(Matchers.anyObject())).thenReturn(contractorHelper); when(contractorService.getContractorByCode(Matchers.anyString())).thenReturn(contractor); responseJson = contractorController.getContractors(requestJson, response); final JSONObject jsonObject = new JSONObject(responseJson); assertEquals(contractorHelper.getCode(), jsonObject.getString("code")); }