ProgramInstancePreProcessor implements Processor { @Override public void process( Event event, WorkContext ctx ) { ProgramInstanceStore programInstanceStore = ctx.getServiceDelegator().getProgramInstanceStore(); Program program = ctx.getProgramsMap().get( event.getProgram() ); if ( program == null ) { return; } ProgramInstance programInstance = ctx.getProgramInstanceMap().get( event.getUid() ); final Optional<TrackedEntityInstance> trackedEntityInstance = ctx.getTrackedEntityInstance( event.getUid() ); if ( program.isRegistration() && programInstance == null ) { List<ProgramInstance> programInstances = new ArrayList<>( programInstanceStore.get( trackedEntityInstance.orElse( null ), program, ProgramStatus.ACTIVE ) ); if ( programInstances.size() == 1 ) { event.setEnrollment( programInstances.get( 0 ).getUid() ); ctx.getProgramInstanceMap().put( event.getUid(), programInstances.get( 0 ) ); } } else if ( program.isWithoutRegistration() && programInstance == null ) { List<ProgramInstance> programInstances = getProgramInstances( ctx.getServiceDelegator().getJdbcTemplate(), program, ProgramStatus.ACTIVE ); if ( programInstances.size() == 1 ) { event.setEnrollment( programInstances.get( 0 ).getUid() ); ctx.getProgramInstanceMap().put( event.getUid(), programInstances.get( 0 ) ); } } }  @Override void process( Event event, WorkContext ctx );  }
@Test public void verifyExitOnNullProgram() { when( workContext.getProgramsMap() ).thenReturn( new HashMap<>() ); subject.process( event, workContext ); assertThat( event.getEnrollment(), is( nullValue() ) ); }
@Test public void verifyEnrollmentIsSetOnEventWhenOneProgramInstanceIsFound() { TrackedEntityInstance tei = createTrackedEntityInstance( createOrganisationUnit( 'A' ) ); when( workContext.getTrackedEntityInstance( event.getUid() ) ).thenReturn( Optional.of( tei ) ); ProgramInstance programInstance = new ProgramInstance(); programInstance.setUid( CodeGenerator.generateUid() ); when( programInstanceStore.get( tei, program, ProgramStatus.ACTIVE ) ) .thenReturn( Lists.newArrayList( programInstance ) ); event.setProgram( program.getUid() ); subject.process( event, workContext ); assertThat( event.getEnrollment(), is( programInstance.getUid() ) ); assertThat( programInstanceMap.get( event.getUid() ), is( programInstance ) ); }
@Test public void verifyEnrollmentIsNotSetOnEventWhenMultipleProgramInstanceAreFound() { 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( programInstanceStore.get( tei, program, ProgramStatus.ACTIVE ) ) .thenReturn( Lists.newArrayList( programInstance1, programInstance2 ) ); event.setProgram( program.getUid() ); subject.process( event, workContext ); assertThat( event.getEnrollment(), is( nullValue() ) ); assertThat( programInstanceMap.get( event.getUid() ), is( nullValue() ) ); }
@Test public void verifyEnrollmentIsSetWithProgramWithoutRegistrationAndOneProgramStageInstance() throws SQLException { Program programWithoutReg = createProgram( 'W' ); programWithoutReg.setProgramType( ProgramType.WITHOUT_REGISTRATION ); Map<String, Program> programMap = new HashMap<>(); programMap.put( programWithoutReg.getUid(), programWithoutReg ); when( workContext.getProgramsMap() ).thenReturn( programMap ); ProgramInstance programInstance = new ProgramInstance(); programInstance.setUid( CodeGenerator.generateUid() ); programInstance.setId( 100L ); when( workContext.getServiceDelegator().getJdbcTemplate() ).thenReturn( jdbcTemplate ); when( mockResultSet.next() ).thenReturn( true ).thenReturn( false ); when( mockResultSet.getLong( "programinstanceid" ) ).thenReturn( programInstance.getId() ); when( mockResultSet.getString( "uid" ) ).thenReturn( programInstance.getUid() ); mockResultSetExtractor( mockResultSet ); event.setProgram( programWithoutReg.getUid() ); subject.process( event, workContext ); assertThat( event.getEnrollment(), is( programInstance.getUid() ) ); assertThat( programInstanceMap.get( event.getUid() ).getUid(), is( programInstance.getUid() ) ); assertThat( programInstanceMap.get( event.getUid() ).getProgram().getUid(), is( programWithoutReg.getUid() ) ); assertThat( sql.getValue(), is( "select pi.programinstanceid, pi.programid, pi.uid from programinstance pi where pi.programid = ? and pi.status = ?" ) ); }
@Test public void verifyEnrollmentIsNotSetWithProgramWithoutRegistrationAndMultipleProgramStageInstances() throws SQLException { Program programWithoutReg = createProgram( 'W' ); programWithoutReg.setProgramType( ProgramType.WITHOUT_REGISTRATION ); Map<String, Program> programMap = new HashMap<>(); programMap.put( programWithoutReg.getUid(), programWithoutReg ); when( workContext.getProgramsMap() ).thenReturn( programMap ); ProgramInstance programInstance1 = new ProgramInstance(); programInstance1.setUid( CodeGenerator.generateUid() ); programInstance1.setId( 100L ); ProgramInstance programInstance2 = new ProgramInstance(); programInstance2.setUid( CodeGenerator.generateUid() ); programInstance2.setId( 100L ); when( workContext.getServiceDelegator().getJdbcTemplate() ).thenReturn( jdbcTemplate ); when( mockResultSet.next() ).thenReturn( true ).thenReturn( true ).thenReturn( false ); when( mockResultSet.getLong( "programinstanceid" ) ).thenReturn( programInstance1.getId(), programInstance2.getId() ); when( mockResultSet.getString( "uid" ) ).thenReturn( programInstance1.getUid(), programInstance2.getUid() ); mockResultSetExtractor( mockResultSet ); event.setProgram( programWithoutReg.getUid() ); subject.process( event, workContext ); assertThat( event.getEnrollment(), is( nullValue() ) ); assertThat( sql.getValue(), is( "select pi.programinstanceid, pi.programid, pi.uid from programinstance pi where pi.programid = ? and pi.status = ?" ) ); }