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;
}
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;
}
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;
}
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)));
}
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;
}
Aggregations