Search in sources :

Example 1 with ValidationException

use of ddf.catalog.validation.ValidationException in project ddf by codice.

the class MetacardValidityMarkerPlugin method validate.

private <T> T validate(T item, Function<T, Metacard> itemToMetacard, Map<String, Integer> counter) {
    Set<String> errors = new HashSet<>();
    Set<String> warnings = new HashSet<>();
    Set<String> errorValidators = new HashSet<>();
    Set<String> warningValidators = new HashSet<>();
    Metacard metacard = itemToMetacard.apply(item);
    Set<String> tags = metacard.getTags();
    tags.remove(VALID_TAG);
    tags.remove(INVALID_TAG);
    String valid = VALID_TAG;
    for (MetacardValidator validator : metacardValidators) {
        try {
            validator.validate(metacard);
        } catch (ValidationException e) {
            String validatorName = getValidatorName(validator);
            boolean validationErrorsExist = CollectionUtils.isNotEmpty(e.getErrors());
            boolean validationWarningsExist = CollectionUtils.isNotEmpty(e.getWarnings());
            valid = INVALID_TAG;
            if ((isValidatorEnforced(validatorName) && validationErrorsExist && enforceErrors) || isValidatorEnforced(validatorName) && validationWarningsExist && enforceWarnings) {
                INGEST_LOGGER.debug("The metacard with id={} is being removed from the operation because it failed the enforced validator [{}].", metacard.getId(), validatorName);
                return null;
            } else {
                getValidationProblems(validatorName, e, errors, warnings, errorValidators, warningValidators, counter);
            }
        }
    }
    tags.add(valid);
    metacard.setAttribute(new AttributeImpl(Metacard.TAGS, new ArrayList<String>(tags)));
    metacard.setAttribute(new AttributeImpl(Validation.VALIDATION_ERRORS, (List<Serializable>) new ArrayList<Serializable>(errors)));
    metacard.setAttribute(new AttributeImpl(Validation.VALIDATION_WARNINGS, (List<Serializable>) new ArrayList<Serializable>(warnings)));
    metacard.setAttribute(new AttributeImpl(Validation.FAILED_VALIDATORS_WARNINGS, (List<Serializable>) new ArrayList<Serializable>(warningValidators)));
    metacard.setAttribute(new AttributeImpl(Validation.FAILED_VALIDATORS_ERRORS, (List<Serializable>) new ArrayList<Serializable>(errorValidators)));
    return item;
}
Also used : Metacard(ddf.catalog.data.Metacard) Serializable(java.io.Serializable) ValidationException(ddf.catalog.validation.ValidationException) MetacardValidator(ddf.catalog.validation.MetacardValidator) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 2 with ValidationException

use of ddf.catalog.validation.ValidationException in project ddf by codice.

the class MetacardValidityMarkerPluginTest method getMockFailingValidatorWithErrorsAndWarnings.

private MetacardValidator getMockFailingValidatorWithErrorsAndWarnings() throws ValidationException {
    ValidationException validationException = mock(ValidationException.class);
    when(validationException.getErrors()).thenReturn(Collections.singletonList(SAMPLE_ERROR));
    when(validationException.getWarnings()).thenReturn(Collections.singletonList(SAMPLE_WARNING));
    MetacardValidator metacardValidator = mock(MetacardValidator.class, withSettings().extraInterfaces(Describable.class));
    doThrow(validationException).when(metacardValidator).validate(any(Metacard.class));
    when(((Describable) metacardValidator).getId()).thenReturn(ID);
    return metacardValidator;
}
Also used : Metacard(ddf.catalog.data.Metacard) ValidationException(ddf.catalog.validation.ValidationException) MetacardValidator(ddf.catalog.validation.MetacardValidator) Describable(ddf.catalog.util.Describable)

Example 3 with ValidationException

use of ddf.catalog.validation.ValidationException in project ddf by codice.

the class MetacardValidityMarkerPluginTest method getMockFailingValidatorNoDescribable.

private MetacardValidator getMockFailingValidatorNoDescribable() throws ValidationException {
    MetacardValidator metacardValidator = mock(MetacardValidator.class);
    ValidationException validationException = mock(ValidationException.class);
    when(validationException.getErrors()).thenReturn(Collections.singletonList(SAMPLE_ERROR));
    doThrow(validationException).when(metacardValidator).validate(argThat(isMetacardWithTitle(FIRST)));
    return metacardValidator;
}
Also used : ValidationException(ddf.catalog.validation.ValidationException) MetacardValidator(ddf.catalog.validation.MetacardValidator)

Example 4 with ValidationException

use of ddf.catalog.validation.ValidationException in project ddf by codice.

the class TestDuplicationValidator method testValidateWithValidationErrorAndWarning.

@Test
public void testValidateWithValidationErrorAndWarning() throws ValidationException {
    String[] checksumAttribute = { Metacard.CHECKSUM };
    String[] idAttribute = { Metacard.ID };
    ValidationException expectedException = null;
    validator.setWarnOnDuplicateAttributes(checksumAttribute);
    validator.setErrorOnDuplicateAttributes(idAttribute);
    try {
        validator.validate(testMetacard);
    } catch (ValidationException e) {
        expectedException = e;
    }
    assertThat(expectedException, is(not(nullValue())));
    assertThat(expectedException.getMessage(), is(not(nullValue())));
    assertThat(expectedException.getErrors().size(), is(1));
    assertThat(expectedException.getWarnings().size(), is(1));
    expectedException.getWarnings().forEach(warning -> assertThat(warning, containsString(Metacard.CHECKSUM)));
    expectedException.getErrors().forEach(error -> assertThat(error, containsString(Metacard.ID)));
}
Also used : ValidationException(ddf.catalog.validation.ValidationException) Matchers.anyString(org.mockito.Matchers.anyString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.Test)

Example 5 with ValidationException

use of ddf.catalog.validation.ValidationException in project ddf by codice.

the class DummyValidationPostQueryPlugin method process.

public QueryResponse process(QueryResponse input) throws PluginExecutionException, StopProcessingException {
    String methodName = "process()";
    LOGGER.debug("ENTERING: {}", methodName);
    if (input != null) {
        List<Result> cards = input.getResults();
        // Validate each metacard in the results
        for (Result card : cards) {
            // Catch validation errors and warnings on each card
            try {
                LOGGER.debug("validating card {}", card.getMetacard().getId());
                validator.validate(card.getMetacard());
            } catch (ValidationException e) {
                LOGGER.error(e.getMessage());
                LOGGER.info("Errors: {}", e.getErrors());
                LOGGER.info("Warnings: {}", e.getWarnings());
            }
        }
    }
    LOGGER.debug("EXITING: {}", methodName);
    return input;
}
Also used : ValidationException(ddf.catalog.validation.ValidationException) Result(ddf.catalog.data.Result)

Aggregations

ValidationException (ddf.catalog.validation.ValidationException)9 MetacardValidator (ddf.catalog.validation.MetacardValidator)6 Metacard (ddf.catalog.data.Metacard)4 Describable (ddf.catalog.util.Describable)4 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 Result (ddf.catalog.data.Result)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 StringContains.containsString (org.hamcrest.core.StringContains.containsString)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 SAXException (org.xml.sax.SAXException)1