ProgramInstanceByTeiHook implements TrackerPreheatHook { @Override public void preheat( TrackerPreheatParams params, TrackerPreheat preheat ) { final Map<TrackerIdScheme, Map<String, ProgramInstance>> enrollmentsMap = preheat.getEnrollments(); final Map<String, ProgramInstance> enrollments = enrollmentsMap.getOrDefault( TrackerIdScheme.UID, new HashMap<>() ); List<Event> eventWithoutPI = getEventsWithoutProgramInstance( params, enrollments.values().stream().map( BaseIdentifiableObject::getUid ).collect( Collectors.toList() ) ); if ( isNotEmpty( eventWithoutPI ) ) { preheat.setProgramInstances( getProgramInstancesByProgramAndTei( preheat, eventWithoutPI ) ); } } ProgramInstanceByTeiHook( ProgramInstanceStore programInstanceStore ); @Override void preheat( TrackerPreheatParams params, TrackerPreheat preheat );  }
@Test public void verifyEventWithoutProgramOrTeiAreSkipped() { Program program1 = createProgram( 'A' ); program1.setUid( CodeGenerator.generateUid() ); OrganisationUnit ou1 = createOrganisationUnit( 'A' ); TrackedEntityInstance t1 = createTrackedEntityInstance( 'A', ou1 ); TrackedEntityInstance t2 = createTrackedEntityInstance( 'B', ou1 ); TrackedEntityInstance t3 = createTrackedEntityInstance( 'C', ou1 ); TrackedEntityInstance t4 = createTrackedEntityInstance( 'D', ou1 ); ProgramInstance p1 = createProgramInstance( program1 ); ProgramInstance p2 = createProgramInstance( program1 ); ProgramInstance p3 = createProgramInstance( program1 ); TrackerPreheatParams params = TrackerPreheatParams.builder().build(); Event e1 = createEvent( p1, t1 ); Event e2 = createEvent( p2, t2 ); Event e3 = createEvent( p3, t3 ); Event e4 = createEvent( null, null, t4 ); Event e5 = createEvent( p3, null, null ); params.setEvents( Lists.newArrayList( e1, e2, e3, e4, e5) ); TrackerPreheat trackerPreheat = new TrackerPreheat(); Map<TrackerIdScheme, Map<String, ProgramInstance>> enrollmentMap = new HashMap<>(); Map<String, ProgramInstance> programInstanceMap = new HashMap<>(); programInstanceMap.put( p1.getUid(), p1 ); programInstanceMap.put( p2.getUid(), p2 ); programInstanceMap.put( p3.getUid(), p3 ); enrollmentMap.put( TrackerIdScheme.UID, programInstanceMap ); trackerPreheat.setEnrollments( enrollmentMap ); ProgramInstance p4 = new ProgramInstance(); p4.setUid( CodeGenerator.generateUid() ); p4.setProgram( program1 ); p4.setEntityInstance( t4 ); when( programInstanceStore.getByProgramAndTrackedEntityInstance( anyList(), eq( ProgramStatus.ACTIVE ) ) ) .thenReturn( Collections.singletonList( p4 ) ); this.hook.preheat( params, trackerPreheat ); final Map<String, List<ProgramInstance>> programInstancesByProgramAndTei = trackerPreheat .getProgramInstances(); assertThat( programInstancesByProgramAndTei, is( notNullValue() ) ); assertThat( programInstancesByProgramAndTei.keySet(), hasSize( 0 ) ); }
@Test public void verifyProgramInstanceAreSetIntoPreheat() { Program program1 = createProgram( 'A' ); program1.setUid( CodeGenerator.generateUid() ); OrganisationUnit ou1 = createOrganisationUnit( 'A' ); TrackedEntityInstance t1 = createTrackedEntityInstance( 'A', ou1 ); TrackedEntityInstance t2 = createTrackedEntityInstance( 'B', ou1 ); TrackedEntityInstance t3 = createTrackedEntityInstance( 'C', ou1 ); TrackedEntityInstance t4 = createTrackedEntityInstance( 'D', ou1 ); ProgramInstance p1 = createProgramInstance( program1 ); ProgramInstance p2 = createProgramInstance( program1 ); ProgramInstance p3 = createProgramInstance( program1 ); TrackerPreheatParams params = TrackerPreheatParams.builder().build(); Event e1 = createEvent( p1, t1 ); Event e2 = createEvent( p2, t2 ); Event e3 = createEvent( p3, t3 ); Event e4 = createEvent( null, program1, t4 ); params.setEvents( Lists.newArrayList( e1, e2, e3, e4 ) ); TrackerPreheat trackerPreheat = new TrackerPreheat(); Map<TrackerIdScheme, Map<String, ProgramInstance>> enrollmentMap = new HashMap<>(); Map<String, ProgramInstance> programInstanceMap = new HashMap<>(); programInstanceMap.put( p1.getUid(), p1 ); programInstanceMap.put( p2.getUid(), p2 ); programInstanceMap.put( p3.getUid(), p3 ); enrollmentMap.put( TrackerIdScheme.UID, programInstanceMap ); trackerPreheat.setEnrollments( enrollmentMap ); ProgramInstance p4 = new ProgramInstance(); p4.setUid( CodeGenerator.generateUid() ); p4.setProgram( program1 ); p4.setEntityInstance( t4 ); when( programInstanceStore.getByProgramAndTrackedEntityInstance( anyList(), eq(ProgramStatus.ACTIVE) ) ) .thenReturn( Collections.singletonList( p4 ) ); this.hook.preheat( params, trackerPreheat ); final Map<String, List<ProgramInstance>> programInstancesByProgramAndTei = trackerPreheat .getProgramInstances(); assertThat( programInstancesByProgramAndTei, is( notNullValue() ) ); assertThat( programInstancesByProgramAndTei.get( e4.getUid() ), hasSize( 1 ) ); assertThat( programInstancesByProgramAndTei.get( e4.getUid() ).get( 0 ), is( p4 ) ); }