Search in sources :

Example 1 with ValidationViolation

use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.

the class RequiredAttributesMetacardValidator method validateMetacard.

@Override
public Optional<MetacardValidationReport> validateMetacard(final Metacard metacard) {
    Preconditions.checkArgument(metacard != null, "The metacard cannot be null.");
    final MetacardType metacardType = metacard.getMetacardType();
    if (metacardTypeName.equals(metacardType.getName())) {
        final Set<ValidationViolation> violations = new HashSet<>();
        for (final String attributeName : requiredAttributes) {
            final Attribute attribute = metacard.getAttribute(attributeName);
            if (attribute != null) {
                final AttributeDescriptor descriptor = metacardType.getAttributeDescriptor(attributeName);
                if (descriptor.isMultiValued()) {
                    if (attribute.getValues().size() == 0) {
                        addRequiredAttributeViolation(attributeName, violations);
                    }
                } else if (attribute.getValue() == null) {
                    addRequiredAttributeViolation(attributeName, violations);
                }
            } else {
                addRequiredAttributeViolation(attributeName, violations);
            }
        }
        if (violations.size() > 0) {
            return getReport(violations);
        }
    }
    return Optional.empty();
}
Also used : ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType) HashSet(java.util.HashSet)

Example 2 with ValidationViolation

use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.

the class ReportingMetacardValidatorImpl method validateMetacard.

@Override
public Optional<MetacardValidationReport> validateMetacard(final Metacard metacard) {
    Preconditions.checkArgument(metacard != null, "The metacard cannot be null.");
    final Set<ValidationViolation> violations = new HashSet<>();
    for (final AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
        final String attributeName = descriptor.getName();
        final Attribute attribute = metacard.getAttribute(attributeName);
        if (attribute != null) {
            for (final AttributeValidator validator : validatorRegistry.getValidators(attributeName)) {
                validator.validate(attribute).ifPresent(report -> violations.addAll(report.getAttributeValidationViolations()));
            }
        }
    }
    if (violations.size() > 0) {
        return getReport(violations);
    }
    return Optional.empty();
}
Also used : ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) AttributeValidator(ddf.catalog.validation.AttributeValidator) HashSet(java.util.HashSet)

Example 3 with ValidationViolation

use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.

the class Validator method getViolationsResult.

private Map<String, ViolationResult> getViolationsResult(Set<ValidationViolation> attributeValidationViolations) {
    Map<String, ViolationResult> violationsResult = new HashMap<>();
    for (ValidationViolation violation : attributeValidationViolations) {
        for (String attribute : violation.getAttributes()) {
            if (!violationsResult.containsKey(attribute)) {
                violationsResult.put(attribute, new ViolationResult());
            }
            ViolationResult violationResponse = violationsResult.get(attribute);
            violationResponse.setAttribute(attribute);
            if (ValidationViolation.Severity.ERROR.equals(violation.getSeverity())) {
                violationResponse.getErrors().add(violation.getMessage());
            } else if (ValidationViolation.Severity.WARNING.equals(violation.getSeverity())) {
                violationResponse.getWarnings().add(violation.getMessage());
            } else {
                throw new RuntimeException("Unexpected Severity Level");
            }
        }
    }
    return violationsResult;
}
Also used : ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) HashMap(java.util.HashMap)

Example 4 with ValidationViolation

use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.

the class DuplicationValidator method reportDuplicates.

private ValidationViolation reportDuplicates(final Metacard metacard, String[] attributeNames, ValidationViolation.Severity severity) {
    Set<String> duplicates = new HashSet<>();
    ValidationViolation violation = null;
    final Set<String> uniqueAttributeNames = Stream.of(attributeNames).filter(attribute -> metacard.getAttribute(attribute) != null).collect(Collectors.toSet());
    final Set<Attribute> uniqueAttributes = uniqueAttributeNames.stream().map(attribute -> metacard.getAttribute(attribute)).collect(Collectors.toSet());
    if (!uniqueAttributes.isEmpty()) {
        LOGGER.debug("Checking for duplicates for id {} against attributes [{}]", metacard.getId(), collectionToString(uniqueAttributeNames));
        SourceResponse response = query(uniqueAttributes, metacard.getId());
        if (response != null) {
            response.getResults().forEach(result -> duplicates.add(result.getMetacard().getId()));
        }
        if (!duplicates.isEmpty()) {
            violation = createViolation(uniqueAttributeNames, duplicates, severity);
            LOGGER.debug(violation.getMessage());
        }
    }
    return violation;
}
Also used : QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Arrays(java.util.Arrays) ValidationException(ddf.catalog.validation.ValidationException) ValidationExceptionImpl(ddf.catalog.validation.impl.ValidationExceptionImpl) MetacardValidationReportImpl(ddf.catalog.validation.impl.report.MetacardValidationReportImpl) FilterBuilder(ddf.catalog.filter.FilterBuilder) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogFramework(ddf.catalog.CatalogFramework) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) LoggerFactory(org.slf4j.LoggerFactory) HashSet(java.util.HashSet) CollectionUtils(org.apache.commons.collections.CollectionUtils) Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) MetacardValidationReport(ddf.catalog.validation.report.MetacardValidationReport) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Properties(java.util.Properties) Logger(org.slf4j.Logger) MetacardValidator(ddf.catalog.validation.MetacardValidator) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) FederationException(ddf.catalog.federation.FederationException) Collectors(java.util.stream.Collectors) ReportingMetacardValidator(ddf.catalog.validation.ReportingMetacardValidator) ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) List(java.util.List) SourceResponse(ddf.catalog.operation.SourceResponse) Stream(java.util.stream.Stream) Attribute(ddf.catalog.data.Attribute) ValidationViolationImpl(ddf.catalog.validation.impl.violation.ValidationViolationImpl) Optional(java.util.Optional) Filter(org.opengis.filter.Filter) Preconditions(com.google.common.base.Preconditions) InputStream(java.io.InputStream) ArrayUtils(org.apache.commons.lang.ArrayUtils) ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) SourceResponse(ddf.catalog.operation.SourceResponse) Attribute(ddf.catalog.data.Attribute) HashSet(java.util.HashSet)

Example 5 with ValidationViolation

use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.

the class TestDuplicationValidator method testValidateMetacardWithValidationErrorAndWarning.

@Test
public void testValidateMetacardWithValidationErrorAndWarning() {
    String[] checksumAttribute = { Metacard.CHECKSUM };
    String[] idAttribute = { Metacard.ID };
    validator.setWarnOnDuplicateAttributes(checksumAttribute);
    validator.setErrorOnDuplicateAttributes(idAttribute);
    Optional<MetacardValidationReport> report = validator.validateMetacard(testMetacard);
    assertThat(report.isPresent(), is(true));
    assertThat(report.get().getMetacardValidationViolations(), hasSize(2));
    Map<ValidationViolation.Severity, ValidationViolation> violations = report.get().getMetacardValidationViolations().stream().collect(Collectors.toMap(ValidationViolation::getSeverity, Function.identity()));
    ValidationViolation warnViolation = violations.get(ValidationViolation.Severity.WARNING);
    ValidationViolation errorViolation = violations.get(ValidationViolation.Severity.ERROR);
    assertThat(warnViolation.getAttributes(), is(new HashSet<>(Arrays.asList(checksumAttribute))));
    assertThat(warnViolation.getMessage(), containsString(Metacard.CHECKSUM));
    assertThat(errorViolation.getAttributes(), is(new HashSet<>(Arrays.asList(idAttribute))));
    assertThat(errorViolation.getMessage(), containsString(Metacard.ID));
}
Also used : MetacardValidationReport(ddf.catalog.validation.report.MetacardValidationReport) ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) Matchers.anyString(org.mockito.Matchers.anyString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ValidationViolation (ddf.catalog.validation.violation.ValidationViolation)6 HashSet (java.util.HashSet)5 Attribute (ddf.catalog.data.Attribute)3 MetacardValidationReport (ddf.catalog.validation.report.MetacardValidationReport)3 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)2 Metacard (ddf.catalog.data.Metacard)2 FederationException (ddf.catalog.federation.FederationException)2 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)2 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)2 AttributeValidator (ddf.catalog.validation.AttributeValidator)2 ReportingMetacardValidator (ddf.catalog.validation.ReportingMetacardValidator)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Preconditions (com.google.common.base.Preconditions)1 CatalogFramework (ddf.catalog.CatalogFramework)1 MetacardType (ddf.catalog.data.MetacardType)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1