@Test public void shouldFetchUnsyncedPeriodForAProgramInADeliveryZone() throws Exception { ProcessingPeriod period1 = new ProcessingPeriod(1L); ProcessingPeriod period2 = new ProcessingPeriod(2L); List<ProcessingPeriod> expectedPeriods = asList(period1, period2); Date elevenDaysFromNow = LocalDate.now().plusDays(11).toDate(); Long zoneId = 1L; Long programId = 3L; when(scheduleService.getPeriodsForDeliveryZoneAndProgram(zoneId, programId, elevenDaysFromNow)).thenReturn(expectedPeriods); when(distributionService.getSyncedPeriodsForDeliveryZoneAndProgram(zoneId, programId)).thenReturn(asList(1L)); ResponseEntity<OpenLmisResponse> response = controller.getPeriodsForProgramInDeliveryZone(request, ZONE_ID, programId); assertThat((List<ProcessingPeriod>) response.getBody().getData().get("periods"), is(asList(period2))); verify(scheduleService).getPeriodsForDeliveryZoneAndProgram(zoneId, programId, elevenDaysFromNow); verify(distributionService).getSyncedPeriodsForDeliveryZoneAndProgram(zoneId, programId); }
@RequestMapping(value = "deliveryZones/{zoneId}/programs/{programId}/periods", method = GET, headers = ACCEPT_JSON) public ResponseEntity<OpenLmisResponse> getPeriodsForProgramInDeliveryZone(HttpServletRequest request, @PathVariable long zoneId, @PathVariable final long programId) { if (permissionService.hasPermissionOnZone(loggedInUserId(request), zoneId)) { Date elevenDaysFromNow = LocalDate.now().plusDays(11).toDate(); List<ProcessingPeriod> periodsForDeliveryZoneAndProgram = scheduleService.getPeriodsForDeliveryZoneAndProgram(zoneId, programId, elevenDaysFromNow); final List<Long> syncedPeriods = distributionService.getSyncedPeriodsForDeliveryZoneAndProgram(zoneId, programId); Collection unsyncedPeriodsForZoneAndProgram = CollectionUtils.select(periodsForDeliveryZoneAndProgram, new Predicate() { @Override public boolean evaluate(Object o) { ProcessingPeriod period = (ProcessingPeriod) o; return !syncedPeriods.contains(period.getId()); } }); return OpenLmisResponse.response(PERIODS, unsyncedPeriodsForZoneAndProgram); } else { return OpenLmisResponse.error(FORBIDDEN_EXCEPTION, HttpStatus.UNAUTHORIZED); } }
DeliveryZoneProgramScheduleController extends BaseController { @RequestMapping(value = "deliveryZones/{zoneId}/programs/{programId}/periods", method = GET, headers = ACCEPT_JSON) public ResponseEntity<OpenLmisResponse> getPeriodsForProgramInDeliveryZone(HttpServletRequest request, @PathVariable long zoneId, @PathVariable final long programId) { if (permissionService.hasPermissionOnZone(loggedInUserId(request), zoneId)) { Date elevenDaysFromNow = LocalDate.now().plusDays(11).toDate(); List<ProcessingPeriod> periodsForDeliveryZoneAndProgram = scheduleService.getPeriodsForDeliveryZoneAndProgram(zoneId, programId, elevenDaysFromNow); final List<Long> syncedPeriods = distributionService.getSyncedPeriodsForDeliveryZoneAndProgram(zoneId, programId); Collection unsyncedPeriodsForZoneAndProgram = CollectionUtils.select(periodsForDeliveryZoneAndProgram, new Predicate() { @Override public boolean evaluate(Object o) { ProcessingPeriod period = (ProcessingPeriod) o; return !syncedPeriods.contains(period.getId()); } }); return OpenLmisResponse.response(PERIODS, unsyncedPeriodsForZoneAndProgram); } else { return OpenLmisResponse.error(FORBIDDEN_EXCEPTION, HttpStatus.UNAUTHORIZED); } } }
DeliveryZoneProgramScheduleController extends BaseController { @RequestMapping(value = "deliveryZones/{zoneId}/programs/{programId}/periods", method = GET, headers = ACCEPT_JSON) public ResponseEntity<OpenLmisResponse> getPeriodsForProgramInDeliveryZone(HttpServletRequest request, @PathVariable long zoneId, @PathVariable final long programId) { if (permissionService.hasPermissionOnZone(loggedInUserId(request), zoneId)) { Date elevenDaysFromNow = LocalDate.now().plusDays(11).toDate(); List<ProcessingPeriod> periodsForDeliveryZoneAndProgram = scheduleService.getPeriodsForDeliveryZoneAndProgram(zoneId, programId, elevenDaysFromNow); final List<Long> syncedPeriods = distributionService.getSyncedPeriodsForDeliveryZoneAndProgram(zoneId, programId); Collection unsyncedPeriodsForZoneAndProgram = CollectionUtils.select(periodsForDeliveryZoneAndProgram, new Predicate() { @Override public boolean evaluate(Object o) { ProcessingPeriod period = (ProcessingPeriod) o; return !syncedPeriods.contains(period.getId()); } }); return OpenLmisResponse.response(PERIODS, unsyncedPeriodsForZoneAndProgram); } else { return OpenLmisResponse.error(FORBIDDEN_EXCEPTION, HttpStatus.UNAUTHORIZED); } }  }
DeliveryZoneProgramScheduleController extends BaseController { @RequestMapping(value = "deliveryZones/{zoneId}/programs/{programId}/periods", method = GET, headers = ACCEPT_JSON) public ResponseEntity<OpenLmisResponse> getPeriodsForProgramInDeliveryZone(HttpServletRequest request, @PathVariable long zoneId, @PathVariable final long programId) { if (permissionService.hasPermissionOnZone(loggedInUserId(request), zoneId)) { Date elevenDaysFromNow = LocalDate.now().plusDays(11).toDate(); List<ProcessingPeriod> periodsForDeliveryZoneAndProgram = scheduleService.getPeriodsForDeliveryZoneAndProgram(zoneId, programId, elevenDaysFromNow); final List<Long> syncedPeriods = distributionService.getSyncedPeriodsForDeliveryZoneAndProgram(zoneId, programId); Collection unsyncedPeriodsForZoneAndProgram = CollectionUtils.select(periodsForDeliveryZoneAndProgram, new Predicate() { @Override public boolean evaluate(Object o) { ProcessingPeriod period = (ProcessingPeriod) o; return !syncedPeriods.contains(period.getId()); } }); return OpenLmisResponse.response(PERIODS, unsyncedPeriodsForZoneAndProgram); } else { return OpenLmisResponse.error(FORBIDDEN_EXCEPTION, HttpStatus.UNAUTHORIZED); } }  @RequestMapping(value = "deliveryZones/{zoneId}/programs/{programId}/periods", method = GET, headers = ACCEPT_JSON) ResponseEntity<OpenLmisResponse> getPeriodsForProgramInDeliveryZone(HttpServletRequest request, @PathVariable long zoneId,
                                                                             @PathVariable final long programId); }
DeliveryZoneProgramScheduleController extends BaseController { @RequestMapping(value = "deliveryZones/{zoneId}/programs/{programId}/periods", method = GET, headers = ACCEPT_JSON) public ResponseEntity<OpenLmisResponse> getPeriodsForProgramInDeliveryZone(HttpServletRequest request, @PathVariable long zoneId, @PathVariable final long programId) { if (permissionService.hasPermissionOnZone(loggedInUserId(request), zoneId)) { Date elevenDaysFromNow = LocalDate.now().plusDays(11).toDate(); List<ProcessingPeriod> periodsForDeliveryZoneAndProgram = scheduleService.getPeriodsForDeliveryZoneAndProgram(zoneId, programId, elevenDaysFromNow); final List<Long> syncedPeriods = distributionService.getSyncedPeriodsForDeliveryZoneAndProgram(zoneId, programId); Collection unsyncedPeriodsForZoneAndProgram = CollectionUtils.select(periodsForDeliveryZoneAndProgram, new Predicate() { @Override public boolean evaluate(Object o) { ProcessingPeriod period = (ProcessingPeriod) o; return !syncedPeriods.contains(period.getId()); } }); return OpenLmisResponse.response(PERIODS, unsyncedPeriodsForZoneAndProgram); } else { return OpenLmisResponse.error(FORBIDDEN_EXCEPTION, HttpStatus.UNAUTHORIZED); } }  @RequestMapping(value = "deliveryZones/{zoneId}/programs/{programId}/periods", method = GET, headers = ACCEPT_JSON) ResponseEntity<OpenLmisResponse> getPeriodsForProgramInDeliveryZone(HttpServletRequest request, @PathVariable long zoneId,
                                                                             @PathVariable final long programId); static final String PERIODS; }