diff --git a/src/main/java/org/openlmis/requisition/service/PeriodService.java b/src/main/java/org/openlmis/requisition/service/PeriodService.java index 2d61704dd..20f72a65d 100644 --- a/src/main/java/org/openlmis/requisition/service/PeriodService.java +++ b/src/main/java/org/openlmis/requisition/service/PeriodService.java @@ -246,8 +246,21 @@ public ProcessingPeriodDto findPeriod(UUID programId, UUID facilityId, UUID sugg period = findCurrentPeriodForInitiate(programId, facilityId); } - if (period == null - || (null != suggestedPeriodId && !suggestedPeriodId.equals(period.getId()))) { + if (suggestedPeriodId != null && period != null) { + + ProcessingPeriodDto proposedPeriod = periodReferenceDataService + .searchById(suggestedPeriodId); + + if (!proposedPeriod.getId().equals(period.getId())) { + period = proposedPeriod; + } else { + throw new ValidationMessageException(new Message( + ERROR_PERIOD_SHOULD_BE_OLDEST_AND_NOT_ASSOCIATED)); + } + + } + + if (period == null) { throw new ValidationMessageException(new Message( ERROR_PERIOD_SHOULD_BE_OLDEST_AND_NOT_ASSOCIATED)); } diff --git a/src/main/java/org/openlmis/requisition/web/BaseRequisitionController.java b/src/main/java/org/openlmis/requisition/web/BaseRequisitionController.java index 3b738b271..93fdb95e6 100644 --- a/src/main/java/org/openlmis/requisition/web/BaseRequisitionController.java +++ b/src/main/java/org/openlmis/requisition/web/BaseRequisitionController.java @@ -217,19 +217,10 @@ InitiateResult doInitiate(UUID programId, UUID facilityId, UUID suggestedPeriod, profiler.start("CHECK_FACILITY_SUPPORTS_PROGRAM"); facilitySupportsProgramHelper.checkIfFacilitySupportsProgram(facility, programId); - profiler.start("SKIP_FIND_PROCESSING_PERIOD_WHEN_NO_SUGGESTED"); - ProcessingPeriodDto period; + profiler.start("FIND_PROCESSING_PERIOD"); - if (suggestedPeriod != null) { - - period = periodService.getPeriod(suggestedPeriod); - } else { - - profiler.start("FIND_PROCESSING_PERIOD"); - - period = periodService + ProcessingPeriodDto period = periodService .findPeriod(programId, facilityId, suggestedPeriod, emergency); - } boolean reportOnly = period.isReportOnly(); diff --git a/src/test/java/org/openlmis/requisition/service/PeriodServiceTest.java b/src/test/java/org/openlmis/requisition/service/PeriodServiceTest.java index 8b4592006..5e28d554e 100644 --- a/src/test/java/org/openlmis/requisition/service/PeriodServiceTest.java +++ b/src/test/java/org/openlmis/requisition/service/PeriodServiceTest.java @@ -96,6 +96,7 @@ public class PeriodServiceTest { private UUID requisitionId = UUID.randomUUID(); private ProcessingPeriodDto currentPeriod; + private ProcessingPeriodDto suggestedPeriod; private ProcessingPeriodDto period1; private ProcessingPeriodDto period2; private ProcessingPeriodDto period3; @@ -115,6 +116,7 @@ public void setUp() throws Exception { period2 = createPeriod(2); period3 = createPeriod(3); period4 = createPeriod(4); + suggestedPeriod = createPeriod(5); } @Test @@ -406,7 +408,33 @@ public void shouldThrowExceptionIfPeriodIsNotTheOldest() { when(periodReferenceDataService.searchByProgramAndFacility(programId, facilityId)) .thenReturn(Lists.newArrayList(currentPeriod)); - periodService.findPeriod(programId, facilityId, UUID.randomUUID(), false); + when(periodReferenceDataService.searchById(currentPeriod.getId())) + .thenReturn(currentPeriod); + + periodService.findPeriod(programId, facilityId, currentPeriod.getId(), false); + } + + @Test + public void shouldThrowExceptionIfPeriodHasNoSuggestedPeriod() { + ProcessingScheduleDto processingScheduleDto = new ProcessingScheduleDtoDataBuilder() + .buildAsDto(); + + currentPeriod.setProcessingSchedule(processingScheduleDto); + currentPeriod.setId(UUID.randomUUID()); + suggestedPeriod.setProcessingSchedule(processingScheduleDto); + suggestedPeriod.setId(UUID.randomUUID()); + + mockSupportedProgramStartDateNotSet(); + when(periodReferenceDataService.searchByProgramAndFacility(programId, facilityId)) + .thenReturn(Lists.newArrayList(currentPeriod)); + + when(periodReferenceDataService.searchById(suggestedPeriod.getId())) + .thenReturn(suggestedPeriod); + + ProcessingPeriodDto period = periodService + .findPeriod(programId, facilityId, suggestedPeriod.getId(), false); + + assertEquals(suggestedPeriod, period); } @Test(expected = ValidationMessageException.class)