@Test public void shouldReturnExistingDistributionWithWarningIfAlreadyExist() throws Exception { Long createdById = 10L; Date creationTimeStamp = new Date(); Distribution distribution = make(a(defaultDistribution)); Distribution existingDistribution = make(a(defaultDistribution, with(createdBy, createdById), with(createdDate, creationTimeStamp))); when(service.get(distribution)).thenReturn(existingDistribution); Map<Long, FacilityDistribution> facilityDistributions = new HashMap<>(); when(facilityDistributionService.createFor(distribution)).thenReturn(facilityDistributions); User user = make(a(defaultUser)); when(userService.getById(createdById)).thenReturn(user); when(messageService.message("message.distribution.already.exists", user.getUserName(), DistributionController.DATE_FORMAT.format(creationTimeStamp))). thenReturn("Distribution already initiated by XYZ at 2013-05-03 12:10"); when(messageService.message("message.distribution.created.success", null, null, null)).thenReturn("Distribution created successfully"); ResponseEntity<OpenLmisResponse> response = controller.create(distribution, httpServletRequest); assertThat(response.getStatusCode(), is(OK)); Map<String, Object> responseData = response.getBody().getData(); assertThat((String) responseData.get("message"), is("Distribution already initiated by XYZ at 2013-05-03 12:10")); assertThat((Distribution) responseData.get("distribution"), is(existingDistribution)); assertThat((String) response.getBody().getData().get("success"), is("Distribution created successfully")); assertThat(((Distribution) responseData.get("distribution")).getFacilityDistributions(), is(facilityDistributions)); }
@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; }