DataValueCheck implements Checker { @Override public ImportSummary check( ImmutableEvent event, WorkContext ctx ) { final Set<DataValue> dataValues = event.getDataValues(); final ImportSummary importSummary = new ImportSummary(); final User user = ctx.getImportOptions().getUser(); for ( DataValue dataValue : dataValues ) { if ( !checkHasValidDataElement( importSummary, ctx, dataValue ) || !checkSerializeToJson( importSummary, ctx, dataValue ) ) { importSummary.setStatus( ImportStatus.ERROR ); importSummary.setReference( event.getUid() ); importSummary.incrementIgnored(); return importSummary; } } if ( importSummary.getConflicts().isEmpty() ) { if ( doValidationOfMandatoryAttributes( user ) && isValidationRequired( event, ctx ) ) { validateMandatoryAttributes( importSummary, ctx, event ); } } if ( !importSummary.getConflicts().isEmpty() ) { importSummary.setStatus( ImportStatus.ERROR ); importSummary.setReference( event.getUid() ); importSummary.incrementIgnored(); } return importSummary; }  @Override ImportSummary check( ImmutableEvent event, WorkContext ctx ); void validateMandatoryAttributes( ImportSummary importSummary, WorkContext ctx,
        ImmutableEvent event );  }
@Test public void verifyNoErrorOnNoDataValues() { assertNoError( rule.check( new ImmutableEvent( event ), this.workContext ) ); }
@Test public void verifyNoMandatoryCheckHasNoErrors() { programStageA.setValidationStrategy( ValidationStrategy.ON_COMPLETE ); event.setProgramStage( "prgstg1" ); DataElement de1 = addToDataElementMap( createDataElement( 'A' ) ); DataElement de2 = addToDataElementMap( createDataElement( 'B' ) ); DataValue dv1 = createDataValue( de1.getUid(), "1" ); DataValue dv2 = createDataValue( de2.getUid(), "2" ); event.setDataValues( Sets.newHashSet( dv1, dv2 ) ); assertNoError( rule.check( new ImmutableEvent( event ), this.workContext ) ); }
@Test public void verifyMandatoryCheckFailsOnMandatoryDataElement() { event.setProgramStage( "prgstg1" ); DataElement de1 = addToDataElementMap( createDataElement( 'A' ) ); DataElement de2 = addToDataElementMap( createDataElement( 'B' ) ); DataElement de3 = addToDataElementMap( createDataElement( 'C' ) ); programStageA.setProgramStageDataElements( Sets.newHashSet( createProgramStageDataElement( programStageA, de1, 1, true ), createProgramStageDataElement( programStageA, de2, 2, true ), createProgramStageDataElement( programStageA, de3, 3, true ) ) ); addToDataValueMap( event.getUid(), createEventDataValue( de1.getUid(), "1" ), createEventDataValue( de2.getUid(), "2" ) ); DataValue dv1 = createDataValue( de1.getUid(), "1" ); DataValue dv2 = createDataValue( de2.getUid(), "2" ); event.setDataValues( Sets.newHashSet( dv1, dv2 ) ); final ImportSummary summary = rule.check( new ImmutableEvent( event ), this.workContext ); assertHasError( summary, event, null ); assertThat( summary.getConflicts(), hasSize( 1 ) ); assertThat( summary.getConflicts().iterator().next().getValue(), is( "value_required_but_not_provided" ) ); assertThat( summary.getConflicts().iterator().next().getObject(), is( de3.getUid() ) ); }
@Test public void verifyMandatoryCheckSucceeds() { event.setProgramStage( "prgstg1" ); DataElement de1 = addToDataElementMap( createDataElement( 'A' ) ); DataElement de2 = addToDataElementMap( createDataElement( 'B' ) ); programStageA.setValidationStrategy( ValidationStrategy.ON_UPDATE_AND_INSERT ); programStageA.setProgramStageDataElements( Sets.newHashSet( createProgramStageDataElement( programStageA, de1, 1, true ), createProgramStageDataElement( programStageA, de2, 2, true ) ) ); addToDataValueMap( event.getUid(), createEventDataValue( de1.getUid(), "1" ), createEventDataValue( de2.getUid(), "2" ) ); DataValue dv1 = createDataValue( de1.getUid(), "1" ); DataValue dv2 = createDataValue( de2.getUid(), "2" ); event.setDataValues( Sets.newHashSet( dv1, dv2 ) ); final ImportSummary summary = rule.check( new ImmutableEvent( event ), this.workContext ); assertNoError( summary ); }
@Test public void verifyValidationFailOnMissingDataElement() { event.setProgramStage( "prgstg1" ); DataElement de1 = addToDataElementMap( createDataElement( 'A' ) ); programStageA.setValidationStrategy( ValidationStrategy.ON_UPDATE_AND_INSERT ); programStageA.setProgramStageDataElements( Sets.newHashSet( createProgramStageDataElement( programStageA, de1, 1, true ) ) ); addToDataValueMap( event.getUid(), createEventDataValue( de1.getUid(), "1" ), createEventDataValue( "iDontExist", "2" ) ); DataValue dv1 = createDataValue( de1.getUid(), "1" ); DataValue dv2 = createDataValue( "iDontExist", "2" ); event.setDataValues( Sets.newHashSet( dv1, dv2 ) ); final ImportSummary summary = rule.check( new ImmutableEvent( event ), this.workContext ); assertHasError( summary, event, null ); assertThat( summary.getConflicts(), hasSize( 1 ) ); assertThat( summary.getConflicts().iterator().next().getValue(), is( "iDontExist is not a valid data element" ) ); assertThat( summary.getConflicts().iterator().next().getObject(), is( "dataElement" ) ); }
@Test public void verifyValidationFailOnJsonSerializationError() throws JsonProcessingException { ObjectMapper localObjectMapper = mock( ObjectMapper.class ); when( serviceDelegator.getJsonMapper() ).thenReturn( localObjectMapper ); when( localObjectMapper.writeValueAsString( Mockito.any() ) ) .thenThrow( new JsonProcessingException( "Error" ) { } ); event.setProgramStage( "prgstg1" ); DataElement de1 = addToDataElementMap( createDataElement( 'A' ) ); final Program programA = createProgram( 'A' ); final ProgramStage programStageA = createProgramStage( 'A', programA ); programStageA.setValidationStrategy( ValidationStrategy.ON_UPDATE_AND_INSERT ); programStageA.setProgramStageDataElements( Sets.newHashSet( createProgramStageDataElement( programStageA, de1, 1, true ) ) ); when( workContext.getProgramStage( IdScheme.UID, "prgstg1" ) ).thenReturn( programStageA ); addToDataValueMap( event.getUid(), createEventDataValue( de1.getUid(), "1" ) ); DataValue dv1 = createDataValue( de1.getUid(), "1" ); event.setDataValues( Sets.newHashSet( dv1 ) ); final ImportSummary summary = rule.check( new ImmutableEvent( event ), this.workContext ); assertHasError( summary, event, null ); assertThat( summary.getConflicts(), hasSize( 1 ) ); assertThat( summary.getConflicts().iterator().next().getValue(), is( "Invalid data value found." ) ); assertThat( summary.getConflicts().iterator().next().getObject(), is( de1.getUid() ) ); }