ProgramInstanceCheck implements Checker { @Override public ImportSummary check( ImmutableEvent event, WorkContext ctx ) { Program program = ctx.getProgramsMap().get( event.getProgram() ); ProgramInstance programInstance = ctx.getProgramInstanceMap().get( event.getUid() ); final Optional<TrackedEntityInstance> trackedEntityInstance = ctx.getTrackedEntityInstance( event.getUid() ); String teiUid = ""; if ( trackedEntityInstance.isPresent() ) { teiUid = trackedEntityInstance.get().getUid(); } List<ProgramInstance> programInstances; if ( programInstance == null ) { if ( program.isRegistration() ) { programInstances = new ArrayList<>( ctx.getServiceDelegator().getProgramInstanceStore() .get( trackedEntityInstance.orElse( null ), program, ProgramStatus.ACTIVE ) ); if ( programInstances.isEmpty() ) { return error( "Tracked entity instance: " + teiUid + " is not enrolled in program: " + program.getUid(), event.getEvent() ); } else if ( programInstances.size() > 1 ) { return error( "Tracked entity instance: " + teiUid + " has multiple active enrollments in program: " + program.getUid(), event.getEvent() ); } } else { programInstances = ctx.getServiceDelegator().getProgramInstanceStore().get( program, ProgramStatus.ACTIVE ); if ( programInstances.size() > 1 ) { return error( "Multiple active program instances exists for program: " + program.getUid(), event.getEvent() ); } } } return success(); }  @Override ImportSummary check( ImmutableEvent event, WorkContext ctx );  }
@Test public void failOnNoProgramInstanceByActiveProgramAndTei() { when( workContext.getProgramInstanceMap() ).thenReturn( new HashMap<>() ); TrackedEntityInstance tei = createTrackedEntityInstance( createOrganisationUnit( 'A' ) ); when( workContext.getTrackedEntityInstance( event.getUid() ) ).thenReturn( Optional.of( tei ) ); event.setProgram( program.getUid() ); ImportSummary summary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( summary, event, "Tracked entity instance: " + tei.getUid() + " is not enrolled in program: " + program.getUid() ); }
@Test public void failOnMultipleProgramInstanceByActiveProgramAndTei() { when( workContext.getProgramInstanceMap() ).thenReturn( new HashMap<>() ); TrackedEntityInstance tei = createTrackedEntityInstance( createOrganisationUnit( 'A' ) ); when( workContext.getTrackedEntityInstance( event.getUid() ) ).thenReturn( Optional.of( tei ) ); ProgramInstance programInstance1 = new ProgramInstance(); ProgramInstance programInstance2 = new ProgramInstance(); when( this.programInstanceStore.get( tei, program, ProgramStatus.ACTIVE ) ) .thenReturn( Lists.newArrayList( programInstance1, programInstance2 ) ); event.setProgram( program.getUid() ); ImportSummary summary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( summary, event, "Tracked entity instance: " + tei.getUid() + " has multiple active enrollments in program: " + program.getUid() ); }
@Test public void failOnMultipleProgramInstance() { Program programNoReg = createProgram( 'P' ); programNoReg.setProgramType( ProgramType.WITHOUT_REGISTRATION ); Map<String, Program> programMap = new HashMap<>(); programMap.put( programNoReg.getUid(), programNoReg ); when( workContext.getProgramsMap() ).thenReturn( programMap ); when( workContext.getProgramInstanceMap() ).thenReturn( new HashMap<>() ); TrackedEntityInstance tei = createTrackedEntityInstance( createOrganisationUnit( 'A' ) ); Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap = new HashMap<>(); teiMap.put( event.getUid(), Pair.of( tei, true ) ); when( workContext.getTrackedEntityInstanceMap() ).thenReturn( teiMap ); ProgramInstance programInstance1 = new ProgramInstance(); ProgramInstance programInstance2 = new ProgramInstance(); when( this.programInstanceStore.get( programNoReg, ProgramStatus.ACTIVE ) ) .thenReturn( Lists.newArrayList( programInstance1, programInstance2 ) ); event.setProgram( programNoReg.getUid() ); ImportSummary summary = rule.check( new ImmutableEvent( event ), workContext ); assertHasError( summary, event, "Multiple active program instances exists for program: " + programNoReg.getUid() ); }