@Test
public void verifyProgramInstanceAreSetIntoPreheat()
{

    // Given
    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 ) );
    // When
    this.hook.preheat( params, trackerPreheat );
    // Then
    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 ) );
}