EventDataValueAggregator { public Map<String, Set<EventDataValue>> aggregateDataValues( List<Event> events, Map<String, ProgramStageInstance> programStageInstanceMap, ImportOptions importOptions ) { checkNotNull( programStageInstanceMap ); checkNotNull( events ); Map<String, Set<EventDataValue>> eventDataValueMap = new HashMap<>(); for ( Event event : events ) { if ( isNew( event.getUid(), programStageInstanceMap ) ) { eventDataValueMap.put( event.getUid(), getDataValues( event.getDataValues() ) ); } else { eventDataValueMap.put( event.getUid(), aggregateForUpdate( event, programStageInstanceMap, importOptions ) ); } } return eventDataValueMap; }  Map<String, Set<EventDataValue>> aggregateDataValues( List<Event> events,
        Map<String, ProgramStageInstance> programStageInstanceMap, ImportOptions importOptions );  }
@Test public void verifyAggregateDataValuesOnNewEvent() { Event event1 = createEvent( createDataValue( "abcd", "val1" ), createDataValue( "efgh", "val2" ), createDataValue( "ilmn", "val3" ) ); Map<String, Set<EventDataValue>> dataValues = subject.aggregateDataValues( Lists.newArrayList( event1 ), new HashMap<>(), importOptions ); assertThat( dataValues, is( notNullValue() ) ); assertThat( dataValues.keySet(), hasSize( 1 ) ); assertThat( dataValues.get( event1.getUid() ), hasSize( 3 ) ); assertDataValue( dataValues.get( event1.getUid() ), "abcd", "val1" ); assertDataValue( dataValues.get( event1.getUid() ), "efgh", "val2" ); assertDataValue( dataValues.get( event1.getUid() ), "ilmn", "val3" ); }
@Test public void verifyAggregateDataValuesOnExistingPSI() { Event event1 = createEvent( createDataValue( "abcd", "val1" ), createDataValue( "efgh", "val2" ), createDataValue( "ilmn", "val3" ) ); Map<String, ProgramStageInstance> programStageInstanceMap = new HashMap<>(); programStageInstanceMap.put( event1.getUid(), createPsi( event1.getUid(), createEventDataValue( "abcd", "val1" ), createEventDataValue( "efgh", "val2" ), createEventDataValue( "ilmn", "val3" ) ) ); Map<String, Set<EventDataValue>> dataValues = subject.aggregateDataValues( Lists.newArrayList( event1 ), programStageInstanceMap, importOptions ); assertThat( dataValues, is( notNullValue() ) ); assertThat( dataValues.keySet(), hasSize( 1 ) ); assertThat( dataValues.get( event1.getUid() ), hasSize( 3 ) ); assertDataValue( dataValues.get( event1.getUid() ), "abcd", "val1" ); assertDataValue( dataValues.get( event1.getUid() ), "efgh", "val2" ); assertDataValue( dataValues.get( event1.getUid() ), "ilmn", "val3" ); }
@Test public void verifyAggregateDataValuesOnExistingPSI_PayloadHasLessDataValues() { Event event1 = createEvent( createDataValue( "abcd", "val1" ) ); Map<String, ProgramStageInstance> programStageInstanceMap = new HashMap<>(); programStageInstanceMap.put( event1.getUid(), createPsi( event1.getUid(), createEventDataValue( "abcd", "val1" ), createEventDataValue( "efgh", "val2" ), createEventDataValue( "ilmn", "val3" ) ) ); Map<String, Set<EventDataValue>> dataValues = subject.aggregateDataValues( Lists.newArrayList( event1 ), programStageInstanceMap, importOptions ); assertThat( dataValues, is( notNullValue() ) ); assertThat( dataValues.keySet(), hasSize( 1 ) ); assertThat( dataValues.get( event1.getUid() ), hasSize( 1 ) ); assertDataValue( dataValues.get( event1.getUid() ), "abcd", "val1" ); }
@Test public void verifyAggregateDataValuesOnExistingPSI_PayloadHasMoreDataValues() { Event event1 = createEvent( createDataValue( "abcd", "val1" ), createDataValue( "efgh", "val2" ), createDataValue( "ilmn", "val3" ), createDataValue( "gggg", "val4" ) ); Map<String, ProgramStageInstance> programStageInstanceMap = new HashMap<>(); programStageInstanceMap.put( event1.getUid(), createPsi( event1.getUid(), createEventDataValue( "abcd", "val1" ), createEventDataValue( "efgh", "val2" ), createEventDataValue( "ilmn", "val3" ) ) ); Map<String, Set<EventDataValue>> dataValues = subject.aggregateDataValues( Lists.newArrayList( event1 ), programStageInstanceMap, importOptions ); assertThat( dataValues, is( notNullValue() ) ); assertThat( dataValues.keySet(), hasSize( 1 ) ); assertThat( dataValues.get( event1.getUid() ), hasSize( 4 ) ); assertDataValue( dataValues.get( event1.getUid() ), "abcd", "val1" ); assertDataValue( dataValues.get( event1.getUid() ), "efgh", "val2" ); assertDataValue( dataValues.get( event1.getUid() ), "ilmn", "val3" ); assertDataValue( dataValues.get( event1.getUid() ), "gggg", "val4" ); }
@Test public void verifyAggregateDataValuesOnExistingPSI_PayloadHasNoDataValues() { Event event1 = new Event(); event1.setUid( CodeGenerator.generateUid() ); event1.setDataValues( Sets.newHashSet() ); Map<String, ProgramStageInstance> programStageInstanceMap = new HashMap<>(); programStageInstanceMap.put( event1.getUid(), createPsi( event1.getUid(), createEventDataValue( "abcd", "val1" ), createEventDataValue( "efgh", "val2" ), createEventDataValue( "ilmn", "val3" ) ) ); Map<String, Set<EventDataValue>> dataValues = subject.aggregateDataValues( Lists.newArrayList( event1 ), programStageInstanceMap, importOptions ); assertThat( dataValues, is( notNullValue() ) ); assertThat( dataValues.keySet(), hasSize( 1 ) ); assertThat( dataValues.get( event1.getUid() ), hasSize( 0 ) ); }
@Test public void verifyAggregateDataValuesOnExistingPSI_PayloadHasEmptyDataValues() { Event event1 = createEvent( createDataValue( "abcd", "" ), createDataValue( "efgh", "" ), createDataValue( "ilmn", "" ) ); Map<String, ProgramStageInstance> programStageInstanceMap = new HashMap<>(); programStageInstanceMap.put( event1.getUid(), createPsi( event1.getUid(), createEventDataValue( "abcd", "val1" ), createEventDataValue( "efgh", "val2" ), createEventDataValue( "ilmn", "val3" ) ) ); Map<String, Set<EventDataValue>> dataValues = subject.aggregateDataValues( Lists.newArrayList( event1 ), programStageInstanceMap, importOptions ); assertThat( dataValues, is( notNullValue() ) ); assertThat( dataValues.keySet(), hasSize( 1 ) ); assertThat( dataValues.get( event1.getUid() ), hasSize( 0 ) ); }
@Test public void verifyAggregateDataValuesOnExistingPSIwithMerge() { Event event1 = createEvent( createDataValue( "abcd", "val5" ) ); importOptions.setMergeDataValues( true ); Map<String, ProgramStageInstance> programStageInstanceMap = new HashMap<>(); programStageInstanceMap.put( event1.getUid(), createPsi( event1.getUid(), createEventDataValue( "abcd", "val1" ), createEventDataValue( "efgh", "val2" ), createEventDataValue( "ilmn", "val3" ) ) ); Map<String, Set<EventDataValue>> dataValues = subject.aggregateDataValues( Lists.newArrayList( event1 ), programStageInstanceMap, importOptions ); assertThat( dataValues, is( notNullValue() ) ); assertThat( dataValues.keySet(), hasSize( 1 ) ); assertThat( dataValues.get( event1.getUid() ), hasSize( 3 ) ); assertDataValue( dataValues.get( event1.getUid() ), "abcd", "val5" ); assertDataValue( dataValues.get( event1.getUid() ), "efgh", "val2" ); assertDataValue( dataValues.get( event1.getUid() ), "ilmn", "val3" ); }
@Test public void verifyAggregateDataValuesOnExistingPSIwithMergeAndEmptyDataValue() { Event event1 = createEvent( createDataValue( "abcd", "" ) ); importOptions.setMergeDataValues( true ); Map<String, ProgramStageInstance> programStageInstanceMap = new HashMap<>(); programStageInstanceMap.put( event1.getUid(), createPsi( event1.getUid(), createEventDataValue( "abcd", "val1" ), createEventDataValue( "efgh", "val2" ), createEventDataValue( "ilmn", "val3" ) ) ); Map<String, Set<EventDataValue>> dataValues = subject.aggregateDataValues( Lists.newArrayList( event1 ), programStageInstanceMap, importOptions ); assertThat( dataValues, is( notNullValue() ) ); assertThat( dataValues.keySet(), hasSize( 1 ) ); assertThat( dataValues.get( event1.getUid() ), hasSize( 2 ) ); assertDataValue( dataValues.get( event1.getUid() ), "efgh", "val2" ); assertDataValue( dataValues.get( event1.getUid() ), "ilmn", "val3" ); }