DataValidator { String validateAndNormalizeDataValue( final String dataValue, final DataElement dataElement ) throws WebMessageException { final String normalizedBoolean = normalizeBoolean( dataValue, dataElement.getValueType() ); final String valueValid = dataValueIsValid( normalizedBoolean, dataElement ); if ( valueValid != null ) { throw new WebMessageException( conflict( "Invalid value: " + dataValue + ", must match data element type: " + dataElement.getValueType() ) ); } return normalizedBoolean; } DataValidator( final CategoryService categoryService, final OrganisationUnitService organisationUnitService,
        final DataSetService dataSetService, final IdentifiableObjectManager idObjectManager,
        final InputUtils inputUtils, final FileResourceService fileResourceService, final I18nManager i18nManager,
        final CalendarService calendarService, final AggregateAccessManager accessManager );   }
@Test public void validateBooleanDataValueWhenValuesAreAcceptableTrue() throws WebMessageException { final DataElement aBooleanTypeDataElement = new DataElement(); final String normalizedBooleanValue = "true"; aBooleanTypeDataElement.setValueType( BOOLEAN ); String aBooleanDataValue = "true"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); aBooleanDataValue = "1"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); aBooleanDataValue = "t"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); aBooleanDataValue = "True"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); aBooleanDataValue = "TRUE"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); }
@Test public void validateBooleanDataValueWhenValuesAreAcceptableFalse() throws WebMessageException { final DataElement aBooleanTypeDataElement = new DataElement(); final String normalizedBooleanValue = "false"; aBooleanTypeDataElement.setValueType( BOOLEAN ); String aBooleanDataValue = "false"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); aBooleanDataValue = "0"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); aBooleanDataValue = "f"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); aBooleanDataValue = "False"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); aBooleanDataValue = "FALSE"; aBooleanDataValue = dataValidator.validateAndNormalizeDataValue( aBooleanDataValue, aBooleanTypeDataElement ); assertThat( aBooleanDataValue, is( normalizedBooleanValue ) ); }
@Test( expected = WebMessageException.class ) public void validateBooleanDataValueWhenValueIsNotValid() throws WebMessageException { String anInvalidBooleanValue = "InvalidValue"; final DataElement aBooleanTypeDataElement = new DataElement(); aBooleanTypeDataElement.setValueType( BOOLEAN ); dataValidator.validateAndNormalizeDataValue( anInvalidBooleanValue, aBooleanTypeDataElement ); fail( "Should not reach here. It was expected WebMessageException." ); }