CreateBillController { @RequestMapping(value = "/egf/bill", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) public String createContractorBill(@RequestBody final String requestJson, final HttpServletResponse response) { if (LOG.isDebugEnabled()) LOG.debug("Rest API creating bill with the data: " + requestJson); String responseJson; EgBillregister egBillregister; EgBillregister savedBillregister; ApplicationThreadLocals.setUserId(2L); BillRegister billRegister = null; final JsonObject jsonObject = new JsonObject(); try { billRegister = (BillRegister) getObjectFromJSONRequest(requestJson, BillRegister.class); } catch (final IOException e) { LOG.error(e.getStackTrace()); final List<RestErrors> errorList = new ArrayList<>(0); final RestErrors re = new RestErrors(); re.setErrorCode(e.getMessage()); re.setErrorMessage(e.getMessage()); errorList.add(re); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return JsonConvertor.convert(errorList); } try { final List<RestErrors> errors = billService.validateBillRegister(billRegister); if (!errors.isEmpty()) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return JsonConvertor.convert(errors); } else { egBillregister = new EgBillregister(); billService.createProjectCode(billRegister); billService.populateBillRegister(egBillregister, billRegister); savedBillregister = billService.createBill(egBillregister); responseJson = savedBillregister.getBillnumber(); } } catch (final ValidationException e) { LOG.error(e.getStackTrace()); final List<RestErrors> errorList = new ArrayList<>(0); final List<ValidationError> errors = e.getErrors(); for (final ValidationError ve : errors) { final RestErrors re = new RestErrors(); re.setErrorCode(ve.getKey()); re.setErrorMessage(ve.getMessage()); errorList.add(re); } response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return JsonConvertor.convert(errorList); } catch (final Exception e) { LOG.error(e.getStackTrace()); final List<RestErrors> errorList = new ArrayList<>(0); final RestErrors re = new RestErrors(); re.setErrorCode(e.getMessage()); re.setErrorMessage(e.getMessage()); errorList.add(re); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return JsonConvertor.convert(errorList); } jsonObject.addProperty("successMessage", "Works Bill created Successfully"); jsonObject.addProperty("billNumber", responseJson); response.setStatus(HttpServletResponse.SC_CREATED); return jsonObject.toString(); }  @RequestMapping(value = "/egf/bill", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) String createContractorBill(@RequestBody final String requestJson,
            final HttpServletResponse response); @RequestMapping(value = "/egf/billpaymentdetails", method = GET, produces = MediaType.APPLICATION_JSON_VALUE) String getBillAndPaymentDetails(final HttpServletResponse response,String billNo,String cityCode);  }
@Test public void shouldCreateBillWhenValidJsonProvided() throws Exception { requestJson = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream("createbill-Json.json"), "UTF-8"); when(billService.validateBillRegister(Matchers.anyObject())).thenReturn(errors); responseJson = createBillController.createContractorBill(requestJson, response); final JSONObject jsonObject = new JSONObject(responseJson); assertEquals(egBillregister.getBillnumber(), jsonObject.get("billNumber")); }
@Test public void shouldFailWhenInValidJsonProvided() throws Exception { requestJson = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream("createbill-Json.json"), "UTF-8"); final RestErrors restErrors = new RestErrors(); restErrors.setErrorCode(RestApiConstants.THIRD_PARTY_ERR_CODE_NO_DEPARTMENT); restErrors.setErrorMessage(RestApiConstants.THIRD_PARTY_ERR_MSG_NO_DEPARTMENT); errors.add(restErrors); when(billService.validateBillRegister(Matchers.anyObject())).thenReturn(errors); responseJson = createBillController.createContractorBill(requestJson, response); final JSONArray jsonArray = new JSONArray(responseJson); final JSONObject jsonObject = jsonArray.getJSONObject(0); assertEquals(RestApiConstants.THIRD_PARTY_ERR_CODE_NO_DEPARTMENT, jsonObject.get("errorCode")); }
@Test public void shouldThrowNullPointerExceptionWhenJsonStringIsNull() { thrown.expect(NullPointerException.class); responseJson = createBillController.createContractorBill(requestJson, response); }