ProgramStageInstanceBasicCheck implements Checker { @Override public ImportSummary check( final ImmutableEvent event, final WorkContext ctx ) { final ProgramStageInstance programStageInstance = ctx.getProgramStageInstanceMap().get( event.getEvent() ); if ( programStageInstance == null ) { final ImportSummary error = error( "Event ID " + event.getEvent() + " doesn't point to valid event", event.getEvent() ); error.getConflicts().add( new ImportConflict( "Invalid Event ID", event.getEvent() ) ); return error; } else if ( programStageInstance.isDeleted() || ctx.getImportOptions().getImportStrategy().isCreate() ) { return error( "Event ID " + event.getEvent() + " was already used and/or deleted. This event can not be modified." ) .setReference( event.getEvent() ); } return success(); }  @Override ImportSummary check( final ImmutableEvent event, final WorkContext ctx );  }
@Test public void failOnMissingProgramStageInstance() { when( workContext.getProgramStageInstanceMap() ).thenReturn( new HashMap<>() ); ImportSummary summary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( summary, event, "Event ID " + event.getEvent() + " doesn't point to valid event" ); }
@Test public void failOnDeletedProgramStageInstance() { Map<String, ProgramStageInstance> programStageInstanceMap = new HashMap<>(); ProgramStageInstance psi = new ProgramStageInstance(); psi.setDeleted( true ); programStageInstanceMap.put( event.getEvent(), psi ); when( workContext.getProgramStageInstanceMap() ).thenReturn( programStageInstanceMap ); ImportSummary summary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( summary, event, "Event ID " + event.getEvent() + " was already used and/or deleted. This event can not be modified." ); }
@Test public void failOnProgramStageInstanceAndInvalidImportOption() { Map<String, ProgramStageInstance> programStageInstanceMap = new HashMap<>(); ProgramStageInstance psi = new ProgramStageInstance(); programStageInstanceMap.put( event.getEvent(), psi ); ImportOptions importOptions = ImportOptions.getDefaultImportOptions(); importOptions.setImportStrategy( ImportStrategy.CREATE ); when( workContext.getImportOptions() ).thenReturn( importOptions ); when( workContext.getProgramStageInstanceMap() ).thenReturn( programStageInstanceMap ); ImportSummary summary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( summary, event, "Event ID " + event.getEvent() + " was already used and/or deleted. This event can not be modified." ); }