FilteringOutUndeclaredDataElementsProcessor implements Processor { @Override public void process( final Event event, final WorkContext ctx ) { if ( StringUtils.isNotBlank( event.getProgramStage() ) ) { Set<String> programStageDataElementUids = getDataElementUidsFromProgramStage( event.getProgramStage(), ctx ); event.setDataValues( event.getDataValues().stream() .filter( dataValue -> programStageDataElementUids.contains( dataValue.getDataElement() ) ) .collect( Collectors.toSet() ) ); } }  @Override void process( final Event event, final WorkContext ctx );  }
@Test public void testNotLinkedDataElementsAreRemovedFromEvent() { WorkContext ctx = WorkContext.builder() .importOptions( ImportOptions.getDefaultImportOptions() ) .programsMap( getProgramMap() ) .build(); Event event = new Event(); event.setProgramStage( PROGRAMSTAGE ); HashSet<DataValue> dataValues = Sets.newHashSet( new DataValue( DATA_ELEMENT_1, "whatever" ), new DataValue( DATA_ELEMENT_2, "another value" ) ); event.setDataValues( dataValues ); preProcessor.process( event, ctx ); Set<String> allowedDataValues = ctx .getProgramStage( ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme(), PROGRAMSTAGE ) .getDataElements().stream() .map( BaseIdentifiableObject::getUid ) .collect( Collectors.toSet() ); Set<String> filteredEventDataValues = event.getDataValues().stream() .map( DataValue::getDataElement ) .collect( Collectors.toSet() ); assertTrue( allowedDataValues.containsAll( filteredEventDataValues ) ); }