@Test public void shouldReturnErroredResponseIfDistributionCannotBeInitiated() { String username = "User"; session.setAttribute(UserAuthenticationSuccessHandler.USER, username); Distribution distribution = make(a(defaultDistribution)); doThrow(new DataException("no facilities in delivery zone")).when(service).create(distribution); ResponseEntity<OpenLmisResponse> response = controller.create(distribution, httpServletRequest); assertThat(response.getStatusCode(), is(PRECONDITION_FAILED)); assertThat(response.getBody().getErrorMsg(), is("no facilities in delivery zone")); }
@RequestMapping(value = "/distributions", method = POST, headers = ACCEPT_JSON) @PreAuthorize("@distributionPermissionService.hasPermission(principal, 'MANAGE_DISTRIBUTION', #distribution)") public ResponseEntity<OpenLmisResponse> create(@RequestBody Distribution distribution, HttpServletRequest request) { Distribution existingDistribution = distributionService.get(distribution); if (existingDistribution != null) { return returnInitiatedDistribution(distribution, existingDistribution); } distribution.setCreatedBy(loggedInUserId(request)); distribution.setModifiedBy(loggedInUserId(request)); try { Distribution initiatedDistribution = distributionService.create(distribution); OpenLmisResponse openLmisResponse = new OpenLmisResponse("distribution", initiatedDistribution); openLmisResponse.addData(SUCCESS, messageService.message("message.distribution.created.success", distribution.getDeliveryZone().getName(), distribution.getProgram().getName(), distribution.getPeriod().getName())); return openLmisResponse.response(CREATED); } catch (DataException dataException) { return OpenLmisResponse.error(dataException.getLocalizedMessage(), PRECONDITION_FAILED); } }
DistributionController extends BaseController { @RequestMapping(value = "/distributions", method = POST, headers = ACCEPT_JSON) @PreAuthorize("@distributionPermissionService.hasPermission(principal, 'MANAGE_DISTRIBUTION', #distribution)") public ResponseEntity<OpenLmisResponse> create(@RequestBody Distribution distribution, HttpServletRequest request) { Distribution existingDistribution = distributionService.get(distribution); if (existingDistribution != null) { return returnInitiatedDistribution(distribution, existingDistribution); } distribution.setCreatedBy(loggedInUserId(request)); distribution.setModifiedBy(loggedInUserId(request)); try { Distribution initiatedDistribution = distributionService.create(distribution); OpenLmisResponse openLmisResponse = new OpenLmisResponse("distribution", initiatedDistribution); openLmisResponse.addData(SUCCESS, messageService.message("message.distribution.created.success", distribution.getDeliveryZone().getName(), distribution.getProgram().getName(), distribution.getPeriod().getName())); return openLmisResponse.response(CREATED); } catch (DataException dataException) { return OpenLmisResponse.error(dataException.getLocalizedMessage(), PRECONDITION_FAILED); } } }
DistributionController extends BaseController { @RequestMapping(value = "/distributions", method = POST, headers = ACCEPT_JSON) @PreAuthorize("@distributionPermissionService.hasPermission(principal, 'MANAGE_DISTRIBUTION', #distribution)") public ResponseEntity<OpenLmisResponse> create(@RequestBody Distribution distribution, HttpServletRequest request) { Distribution existingDistribution = distributionService.get(distribution); if (existingDistribution != null) { return returnInitiatedDistribution(distribution, existingDistribution); } distribution.setCreatedBy(loggedInUserId(request)); distribution.setModifiedBy(loggedInUserId(request)); try { Distribution initiatedDistribution = distributionService.create(distribution); OpenLmisResponse openLmisResponse = new OpenLmisResponse("distribution", initiatedDistribution); openLmisResponse.addData(SUCCESS, messageService.message("message.distribution.created.success", distribution.getDeliveryZone().getName(), distribution.getProgram().getName(), distribution.getPeriod().getName())); return openLmisResponse.response(CREATED); } catch (DataException dataException) { return OpenLmisResponse.error(dataException.getLocalizedMessage(), PRECONDITION_FAILED); } }  }
DistributionController extends BaseController { @RequestMapping(value = "/distributions", method = POST, headers = ACCEPT_JSON) @PreAuthorize("@distributionPermissionService.hasPermission(principal, 'MANAGE_DISTRIBUTION', #distribution)") public ResponseEntity<OpenLmisResponse> create(@RequestBody Distribution distribution, HttpServletRequest request) { Distribution existingDistribution = distributionService.get(distribution); if (existingDistribution != null) { return returnInitiatedDistribution(distribution, existingDistribution); } distribution.setCreatedBy(loggedInUserId(request)); distribution.setModifiedBy(loggedInUserId(request)); try { Distribution initiatedDistribution = distributionService.create(distribution); OpenLmisResponse openLmisResponse = new OpenLmisResponse("distribution", initiatedDistribution); openLmisResponse.addData(SUCCESS, messageService.message("message.distribution.created.success", distribution.getDeliveryZone().getName(), distribution.getProgram().getName(), distribution.getPeriod().getName())); return openLmisResponse.response(CREATED); } catch (DataException dataException) { return OpenLmisResponse.error(dataException.getLocalizedMessage(), PRECONDITION_FAILED); } }  @RequestMapping(value = "/distributions", method = POST, headers = ACCEPT_JSON) @PreAuthorize("@distributionPermissionService.hasPermission(principal, 'MANAGE_DISTRIBUTION', #distribution)") ResponseEntity<OpenLmisResponse> create(@RequestBody Distribution distribution, HttpServletRequest request); @RequestMapping(value = "/distributions/{id}/facilities/{facilityId}", method = PUT, headers = ACCEPT_JSON) @PreAuthorize("@distributionPermissionService.hasPermission(principal, 'MANAGE_DISTRIBUTION', #id)") ResponseEntity<OpenLmisResponse> sync(@RequestBody FacilityDistributionDTO facilityDistributionDTO, @PathVariable Long id,
                                               @PathVariable Long facilityId, HttpServletRequest httpServletRequest); }
DistributionController extends BaseController { @RequestMapping(value = "/distributions", method = POST, headers = ACCEPT_JSON) @PreAuthorize("@distributionPermissionService.hasPermission(principal, 'MANAGE_DISTRIBUTION', #distribution)") public ResponseEntity<OpenLmisResponse> create(@RequestBody Distribution distribution, HttpServletRequest request) { Distribution existingDistribution = distributionService.get(distribution); if (existingDistribution != null) { return returnInitiatedDistribution(distribution, existingDistribution); } distribution.setCreatedBy(loggedInUserId(request)); distribution.setModifiedBy(loggedInUserId(request)); try { Distribution initiatedDistribution = distributionService.create(distribution); OpenLmisResponse openLmisResponse = new OpenLmisResponse("distribution", initiatedDistribution); openLmisResponse.addData(SUCCESS, messageService.message("message.distribution.created.success", distribution.getDeliveryZone().getName(), distribution.getProgram().getName(), distribution.getPeriod().getName())); return openLmisResponse.response(CREATED); } catch (DataException dataException) { return OpenLmisResponse.error(dataException.getLocalizedMessage(), PRECONDITION_FAILED); } }  @RequestMapping(value = "/distributions", method = POST, headers = ACCEPT_JSON) @PreAuthorize("@distributionPermissionService.hasPermission(principal, 'MANAGE_DISTRIBUTION', #distribution)") ResponseEntity<OpenLmisResponse> create(@RequestBody Distribution distribution, HttpServletRequest request); @RequestMapping(value = "/distributions/{id}/facilities/{facilityId}", method = PUT, headers = ACCEPT_JSON) @PreAuthorize("@distributionPermissionService.hasPermission(principal, 'MANAGE_DISTRIBUTION', #id)") ResponseEntity<OpenLmisResponse> sync(@RequestBody FacilityDistributionDTO facilityDistributionDTO, @PathVariable Long id,
                                               @PathVariable Long facilityId, HttpServletRequest httpServletRequest); static final SimpleDateFormat DATE_FORMAT; }