@Test
public void failOnNonRepeatableStageAndExistingEvents()
{

    // Data preparation
    Program program = createProgram( 'P' );
    
    TrackedEntityInstance tei = createTrackedEntityInstance( 'A', createOrganisationUnit( 'A' ) );
    event.setProgramStage( CodeGenerator.generateUid() );
    event.setProgram( program.getUid() );
    event.setTrackedEntityInstance( tei.getUid() );
    ProgramStage programStage = createProgramStage( 'A', program );
    programStage.setRepeatable( false );
    when( workContext.getProgramStage( programStageIdScheme, event.getProgramStage() ) ).thenReturn( programStage );
    Map<String, Program> programMap = new HashMap<>();
    programMap.put( program.getUid(), program );
    Map<String, ProgramInstance> programInstanceMap = new HashMap<>();
    ProgramInstance programInstance = new ProgramInstance();
    programInstanceMap.put( event.getUid(), programInstance );
    
    Pair<TrackedEntityInstance,Boolean> teiPair = Pair.of( tei, true );
    
    Map<String,Pair<TrackedEntityInstance,Boolean> > teiMap = new HashMap<>();
    teiMap.put( event.getUid(), teiPair );
   
    when( workContext.getTrackedEntityInstanceMap() ).thenReturn( teiMap );
    when( workContext.getProgramsMap() ).thenReturn( programMap );
    when( workContext.getProgramInstanceMap() ).thenReturn( programInstanceMap );
    when( workContext.getServiceDelegator() ).thenReturn( serviceDelegator );
    when( serviceDelegator.getJdbcTemplate() ).thenReturn( jdbcTemplate );
    when( jdbcTemplate.queryForObject( anyString(), eq( Boolean.class ), eq( programStage.getId() ), eq( tei.getId() ) ) )
        .thenReturn( true );
    // Method under test
    ImportSummary summary = rule.check( new ImmutableEvent( event ), workContext );
    assertHasError( summary, event, "Program stage is not repeatable and an event already exists" );
}