ImportStrategyAccumulator { public ImportStrategyAccumulator partitionEvents( List<Event> events, ImportStrategy importStrategy, Map<String, ProgramStageInstance> existingEvents ) { if ( importStrategy.isCreate() ) { create.addAll( events ); } else if ( importStrategy.isCreateAndUpdate() ) { for ( Event event : events ) { sortCreatesAndUpdates( event, create, update, existingEvents ); } } else if ( importStrategy.isUpdate() ) { update.addAll( events ); } else if ( importStrategy.isDelete() ) { final Set<String> existingEventKeys = existingEvents.keySet(); delete.addAll( events.stream() .filter( event -> existingEventKeys.contains( event.getUid() ) ) .collect( Collectors.toSet() ) ); } else if ( importStrategy.isSync() ) { for ( Event event : events ) { if ( event.isDeleted() ) { delete.add( event ); } else { sortCreatesAndUpdates( event, create, update, existingEvents ); } } } return this; }  ImportStrategyAccumulator partitionEvents( List<Event> events, ImportStrategy importStrategy,
        Map<String, ProgramStageInstance> existingEvents );  }
@Test public void verifyCreationOnly() { accumulator.partitionEvents( createEvents( 5 ), ImportStrategy.CREATE, new ArrayMap<>() ); assertAccumulator( 5, 0, 0 ); reset(); accumulator.partitionEvents( createEvents( 5 ), ImportStrategy.CREATE, createProgramStageInstances( "a", "b" ) ); assertAccumulator( 5, 0, 0 ); reset(); accumulator.partitionEvents( createEvents( 5 ), ImportStrategy.NEW, new ArrayMap<>() ); assertAccumulator( 5, 0, 0 ); }
@Test public void verifyCreateAndUpdate() { accumulator.partitionEvents( createEvents( 5 ), ImportStrategy.CREATE_AND_UPDATE, new ArrayMap<>() ); assertAccumulator( 5, 0, 0 ); reset(); final List<Event> events = createEvents( 5 ); accumulator.partitionEvents( events, ImportStrategy.CREATE_AND_UPDATE, createProgramStageInstances( events.get( 0 ).getEvent(), events.get( 1 ).getEvent(), events.get( 2 ).getEvent() ) ); assertAccumulator( 2, 3, 0 ); }
@Test public void verifyUpdateOnly() { accumulator.partitionEvents( createEvents( 5 ), ImportStrategy.UPDATE, new ArrayMap<>() ); assertAccumulator( 0, 5, 0 ); reset(); accumulator.partitionEvents( createEvents( 5 ), ImportStrategy.UPDATES, new ArrayMap<>() ); assertAccumulator( 0, 5, 0 ); }
@Test public void verifyDeleteOnly() { List<Event> events = createEvents( 5 ); accumulator.partitionEvents( events, ImportStrategy.DELETE, existingEventsFromEvents( events ) ); assertAccumulator( 0, 0, 5 ); reset(); events = createEvents( 5 ); accumulator.partitionEvents( events, ImportStrategy.DELETES, existingEventsFromEvents( events ) ); assertAccumulator( 0, 0, 5 ); }
@Test public void verifyDeleteSome() { List<Event> events = createEvents( 5 ); reset(); events = createEvents( 5 ); accumulator.partitionEvents( events, ImportStrategy.DELETES, existingEventsFromEvents( events, 3 ) ); assertAccumulator( 0, 0, 3 ); }
@Test public void verifySync() { accumulator.partitionEvents( createEvents( 5 ), ImportStrategy.SYNC, new ArrayMap<>() ); assertAccumulator( 5, 0, 0 ); reset(); final List<Event> events1 = createEvents( 3 ); events1.get( 0 ).setDeleted( true ); events1.get( 1 ).setDeleted( true ); accumulator.partitionEvents( events1, ImportStrategy.SYNC, new ArrayMap<>() ); assertAccumulator( 1, 0, 2 ); reset(); final List<Event> events2 = createEvents( 10 ); events2.get( 0 ).setDeleted( true ); events2.get( 1 ).setDeleted( true ); accumulator.partitionEvents( events2, ImportStrategy.SYNC, createProgramStageInstances( events2.get( 5 ).getEvent(), events2.get( 6 ).getEvent(), events2.get( 7 ).getEvent() ) ); assertAccumulator( 5, 3, 2 ); }