ExpirationDaysCheck implements Checker { @Override public ImportSummary check( ImmutableEvent event, WorkContext ctx ) { final ImportOptions importOptions = ctx.getImportOptions(); final Program program = ctx.getProgramsMap().get( event.getProgram() ); final ProgramStageInstance programStageInstance = ctx.getProgramStageInstanceMap().get( event.getEvent() ); if ( importOptions == null || importOptions.getUser() == null || importOptions.getUser().isAuthorized( Authorities.F_EDIT_EXPIRED.getAuthority() ) ) { return success(); } if ( program != null ) { ImportSummary importSummary = checkEventOrPsiCompletedDate( program, event, programStageInstance ); if ( importSummary.isStatus( ImportStatus.ERROR ) ) { return importSummary; } return checkEventOrPsiExpirationDate( program, event, programStageInstance ); } return success(); }  @Override ImportSummary check( ImmutableEvent event, WorkContext ctx );  }
@Test public void failWhenProgramStageInstanceHasNoCompletedDateAndProgramHasExpiryDays() { Program program = createProgram( 'P' ); program.setCompleteEventsExpiryDays( 3 ); Map<String, Program> programMap = new HashMap<>(); programMap.put( program.getUid(), program ); when( workContext.getProgramsMap() ).thenReturn( programMap ); Map<String, ProgramStageInstance> psiMap = new HashMap<>(); ProgramStageInstance psi = new ProgramStageInstance(); psi.setStatus( EventStatus.COMPLETED ); psi.setUid( event.getUid() ); psiMap.put( event.getUid(), psi ); when( workContext.getProgramStageInstanceMap() ).thenReturn( psiMap ); event.setProgram( program.getUid() ); event.setStatus( EventStatus.COMPLETED ); ImportSummary importSummary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( importSummary, event, "Event needs to have completed date" ); }
@Test public void failWhenEventHasNoCompletedDateAndProgramHasExpiryDays() { Program program = createProgram( 'P' ); program.setCompleteEventsExpiryDays( 3 ); Map<String, Program> programMap = new HashMap<>(); programMap.put( program.getUid(), program ); when( workContext.getProgramsMap() ).thenReturn( programMap ); event.setProgram( program.getUid() ); event.setStatus( EventStatus.COMPLETED ); ImportSummary importSummary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( importSummary, event, "Event needs to have completed date" ); }
@Test public void failWhenProgramStageInstanceCompletedDateFallsAfterCurrentDay() { Program program = createProgram( 'P' ); program.setCompleteEventsExpiryDays( 3 ); Map<String, Program> programMap = new HashMap<>(); programMap.put( program.getUid(), program ); when( workContext.getProgramsMap() ).thenReturn( programMap ); Map<String, ProgramStageInstance> psiMap = new HashMap<>(); ProgramStageInstance psi = new ProgramStageInstance(); psi.setStatus( EventStatus.COMPLETED ); psi.setCompletedDate( getTodayMinusDays( 5 ) ); psi.setUid( event.getUid() ); psiMap.put( event.getUid(), psi ); when( workContext.getProgramStageInstanceMap() ).thenReturn( psiMap ); event.setProgram( program.getUid() ); event.setStatus( EventStatus.COMPLETED ); ImportSummary importSummary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( importSummary, event, "The event's completeness date has expired. Not possible to make changes to this event" ); }
@Test public void failWhenProgramStageInstanceHasNoExecDateAndProgramHasPeriodType() { Program program = createProgram( 'P' ); program.setCompleteEventsExpiryDays( 3 ); program.setExpiryPeriodType( PeriodType.getPeriodTypeFromIsoString( "202001" ) ); program.setExpiryDays( 3 ); Map<String, Program> programMap = new HashMap<>(); programMap.put( program.getUid(), program ); when( workContext.getProgramsMap() ).thenReturn( programMap ); Map<String, ProgramStageInstance> psiMap = new HashMap<>(); ProgramStageInstance psi = new ProgramStageInstance(); psi.setCompletedDate( getTodayPlusDays( 5 ) ); psi.setUid( event.getUid() ); psiMap.put( event.getUid(), psi ); when( workContext.getProgramStageInstanceMap() ).thenReturn( psiMap ); event.setProgram( program.getUid() ); ImportSummary importSummary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( importSummary, event, "Event needs to have event date" ); }
@Test public void failWhenProgramStageInstanceHasExecutionDateBeforeAllowedProgramExpiryDaysBasedOnPeriod() { final String monthlyPeriodType = new SimpleDateFormat( "yyyyMM" ).format( new Date() ); Program program = createProgram( 'P' ); program.setExpiryPeriodType( PeriodType.getPeriodTypeFromIsoString( monthlyPeriodType ) ); program.setExpiryDays( 3 ); Map<String, Program> programMap = new HashMap<>(); programMap.put( program.getUid(), program ); when( workContext.getProgramsMap() ).thenReturn( programMap ); Map<String, ProgramStageInstance> psiMap = new HashMap<>(); ProgramStageInstance psi = new ProgramStageInstance(); psi.setExecutionDate( getTodayMinusDays( 35 ) ); psi.setUid( event.getUid() ); psiMap.put( event.getUid(), psi ); when( workContext.getProgramStageInstanceMap() ).thenReturn( psiMap ); event.setProgram( program.getUid() ); ImportSummary importSummary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( importSummary, event, "The program's expiry date has passed. It is not possible to make changes to this event" ); }
@Test public void failWhenEventHasNoDateAndProgramExpiryDaysBasedOnPeriod() { final String monthlyPeriodType = new SimpleDateFormat( "yyyyMM" ).format( new Date() ); Program program = createProgram( 'P' ); program.setExpiryPeriodType( PeriodType.getPeriodTypeFromIsoString( monthlyPeriodType ) ); program.setExpiryDays( 3 ); Map<String, Program> programMap = new HashMap<>(); programMap.put( program.getUid(), program ); when( workContext.getProgramsMap() ).thenReturn( programMap ); event.setProgram( program.getUid() ); ImportSummary importSummary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( importSummary, event, "Event needs to have at least one (event or schedule) date" ); }
@Test public void failWhenEventHasDateBeforeCurrentDateAndProgramExpiryDaysBasedOnPeriod() { final String monthlyPeriodType = new SimpleDateFormat( "yyyyMM" ).format( new Date() ); Program program = createProgram( 'P' ); program.setExpiryPeriodType( PeriodType.getPeriodTypeFromIsoString( monthlyPeriodType ) ); program.setExpiryDays( 3 ); Map<String, Program> programMap = new HashMap<>(); programMap.put( program.getUid(), program ); when( workContext.getProgramsMap() ).thenReturn( programMap ); event.setProgram( program.getUid() ); event.setEventDate( new SimpleDateFormat( "yyyy-MM-dd" ).format( getTodayMinusDays( 100 ) ) ); ImportSummary importSummary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( importSummary, event, "The event's date belongs to an expired period. It is not possible to create such event" ); }