Search in sources :

Example 1 with MetacardValidationReport

use of ddf.catalog.validation.report.MetacardValidationReport in project ddf by codice.

the class RequiredAttributesMetacardValidator method validate.

@Override
public void validate(final Metacard metacard) throws ValidationException {
    final Optional<MetacardValidationReport> reportOptional = validateMetacard(metacard);
    if (reportOptional.isPresent()) {
        final List<String> errors = reportOptional.get().getMetacardValidationViolations().stream().map(ValidationViolation::getMessage).collect(Collectors.toList());
        final ValidationExceptionImpl exception = new ValidationExceptionImpl();
        exception.setErrors(errors);
        throw exception;
    }
}
Also used : MetacardValidationReport(ddf.catalog.validation.report.MetacardValidationReport) ValidationExceptionImpl(ddf.catalog.validation.impl.ValidationExceptionImpl)

Example 2 with MetacardValidationReport

use of ddf.catalog.validation.report.MetacardValidationReport in project ddf by codice.

the class DuplicationValidator method validate.

@Override
public void validate(Metacard metacard) throws ValidationException {
    final Optional<MetacardValidationReport> report = validateMetacard(metacard);
    if (report.isPresent()) {
        final List<String> errors = report.get().getMetacardValidationViolations().stream().filter(validationViolation -> validationViolation.getSeverity().equals(ValidationViolation.Severity.ERROR)).map(ValidationViolation::getMessage).collect(Collectors.toList());
        final List<String> warnings = report.get().getMetacardValidationViolations().stream().filter(validationViolation -> validationViolation.getSeverity().equals(ValidationViolation.Severity.WARNING)).map(ValidationViolation::getMessage).collect(Collectors.toList());
        String message = String.format("Duplicate data found in catalog for ID {%s}.", metacard.getId());
        final ValidationExceptionImpl exception = new ValidationExceptionImpl(message);
        exception.setErrors(errors);
        exception.setWarnings(warnings);
        throw exception;
    }
}
Also used : MetacardValidationReport(ddf.catalog.validation.report.MetacardValidationReport) ValidationExceptionImpl(ddf.catalog.validation.impl.ValidationExceptionImpl)

Example 3 with MetacardValidationReport

use of ddf.catalog.validation.report.MetacardValidationReport in project ddf by codice.

the class ReportingMetacardValidatorImpl method validate.

@Override
public void validate(final Metacard metacard) throws ValidationException {
    final Optional<MetacardValidationReport> reportOptional = validateMetacard(metacard);
    if (reportOptional.isPresent()) {
        final MetacardValidationReport report = reportOptional.get();
        final List<String> warnings = new ArrayList<>();
        final List<String> errors = new ArrayList<>();
        getMessages(report.getAttributeValidationViolations(), warnings, errors);
        getMessages(report.getMetacardValidationViolations(), warnings, errors);
        final ValidationExceptionImpl exception = new ValidationExceptionImpl();
        exception.setWarnings(warnings);
        exception.setErrors(errors);
        throw exception;
    }
}
Also used : MetacardValidationReport(ddf.catalog.validation.report.MetacardValidationReport) ArrayList(java.util.ArrayList)

Example 4 with MetacardValidationReport

use of ddf.catalog.validation.report.MetacardValidationReport in project ddf by codice.

the class ReportingMetacardValidatorImplTest method testValidateInvalidMetacardByReport.

@Test
public void testValidateInvalidMetacardByReport() {
    final Optional<MetacardValidationReport> reportOptional = validator.validateMetacard(getInvalidMetacard());
    assertThat("Expected some validation violations but no report was returned (which indicates that there are no violations).", reportOptional.isPresent(), is(true));
    final MetacardValidationReport report = reportOptional.get();
    // 2 title violations and 1 violation each for modified, effective, and content type
    assertThat(report.getAttributeValidationViolations(), hasSize(5));
    assertThat(report.getMetacardValidationViolations(), empty());
    final List<String> violatedAttributes = report.getAttributeValidationViolations().stream().flatMap(violation -> violation.getAttributes().stream()).collect(Collectors.toList());
    assertThat(violatedAttributes, containsInAnyOrder(Metacard.TITLE, Metacard.TITLE, Metacard.MODIFIED, Metacard.EFFECTIVE, Metacard.CONTENT_TYPE));
}
Also used : ValidationException(ddf.catalog.validation.ValidationException) Date(java.util.Date) EnumerationValidator(ddf.catalog.validation.impl.validator.EnumerationValidator) SizeValidator(ddf.catalog.validation.impl.validator.SizeValidator) AttributeValidatorRegistry(ddf.catalog.validation.AttributeValidatorRegistry) Assert.assertThat(org.junit.Assert.assertThat) Metacard(ddf.catalog.data.Metacard) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) PatternValidator(ddf.catalog.validation.impl.validator.PatternValidator) FutureDateValidator(ddf.catalog.validation.impl.validator.FutureDateValidator) Before(org.junit.Before) PastDateValidator(ddf.catalog.validation.impl.validator.PastDateValidator) MetacardValidationReport(ddf.catalog.validation.report.MetacardValidationReport) Matchers.empty(org.hamcrest.Matchers.empty) Test(org.junit.Test) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) DAYS(java.time.temporal.ChronoUnit.DAYS) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) MetacardValidationReport(ddf.catalog.validation.report.MetacardValidationReport) Test(org.junit.Test)

Example 5 with MetacardValidationReport

use of ddf.catalog.validation.report.MetacardValidationReport 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

MetacardValidationReport (ddf.catalog.validation.report.MetacardValidationReport)6 ValidationExceptionImpl (ddf.catalog.validation.impl.ValidationExceptionImpl)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Sets (com.google.common.collect.Sets)1 Metacard (ddf.catalog.data.Metacard)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 AttributeValidatorRegistry (ddf.catalog.validation.AttributeValidatorRegistry)1 ValidationException (ddf.catalog.validation.ValidationException)1 EnumerationValidator (ddf.catalog.validation.impl.validator.EnumerationValidator)1 FutureDateValidator (ddf.catalog.validation.impl.validator.FutureDateValidator)1 PastDateValidator (ddf.catalog.validation.impl.validator.PastDateValidator)1 PatternValidator (ddf.catalog.validation.impl.validator.PatternValidator)1 SizeValidator (ddf.catalog.validation.impl.validator.SizeValidator)1 ValidationViolation (ddf.catalog.validation.violation.ValidationViolation)1 Instant (java.time.Instant)1 DAYS (java.time.temporal.ChronoUnit.DAYS)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1