ProgramDataFormPeriodService { public Optional<Period> getFirstStandardPeriod() throws LMISException { StockMovementItem firstStockMovement = stockMovementRepository.getFirstStockMovement(); if (firstStockMovement != null) { Period firstPeriod = firstStockMovement.getMovementPeriod(); if (firstPeriod != null && firstPeriod.isOpenToRequisitions()) { return Optional.of(firstPeriod); } } return Optional.absent(); }  Optional<Period> getFirstStandardPeriod();  }
@Test public void shouldGenerateFirstPeriodBasedOnFirstMovementDate() throws Exception { LMISTestApp.getInstance().setCurrentTimeMillis(DateUtil.parseString("2016-10-18", DateUtil.DB_DATE_FORMAT).getTime()); StockMovementItem stockMovementItem = new StockMovementItem(); stockMovementItem.setMovementDate(DateUtil.parseString("2016-10-10", DateUtil.DB_DATE_FORMAT)); when(mockStockMovementRepository.getFirstStockMovement()).thenReturn(stockMovementItem); Period period = periodService.getFirstStandardPeriod().get(); assertThat(period.getBegin(), is(new DateTime(DateUtil.parseString("2016-09-21", DateUtil.DB_DATE_FORMAT)))); assertThat(period.getEnd(), is(new DateTime(DateUtil.parseString("2016-10-20", DateUtil.DB_DATE_FORMAT)))); }
@Test public void shouldNotGenerateFirstPeriodIfNoMovement() throws Exception { when(mockStockMovementRepository.getFirstStockMovement()).thenReturn(null); assertThat(periodService.getFirstStandardPeriod().isPresent(), is(false)); }
@Test public void shouldNotGenerateFirstPeriodIfThereIsMovementButNotPass18thYet() throws Exception { LMISTestApp.getInstance().setCurrentTimeMillis(DateUtil.parseString("2016-10-10", DateUtil.DB_DATE_FORMAT).getTime()); StockMovementItem stockMovementItem = new StockMovementItem(); stockMovementItem.setMovementDate(DateUtil.parseString("2016-10-10", DateUtil.DB_DATE_FORMAT)); when(mockStockMovementRepository.getFirstStockMovement()).thenReturn(stockMovementItem); assertThat(periodService.getFirstStandardPeriod().isPresent(), is(false)); }