use of ddf.catalog.validation.MetacardValidator 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.MetacardValidator 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.MetacardValidator 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.MetacardValidator in project ddf by codice.
the class MetacardValidityMarkerPluginTest method testMetacardFailsEnforcedValidatorNoDescribable.
@Test
public void testMetacardFailsEnforcedValidatorNoDescribable() throws ValidationException, StopProcessingException, PluginExecutionException {
MetacardValidator mockValidator = getMockFailingValidatorNoDescribable();
metacardValidators.add(mockValidator);
enforcedMetacardValidators.add(mockValidator.getClass().getCanonicalName());
CreateRequest createRequest = getMockCreateRequest();
List<Metacard> createdMetacards = createRequest.getMetacards();
verifyEnforcedCreate(createRequest, createdMetacards.subList(1, createdMetacards.size()));
UpdateRequest updateRequest = getMockUpdateRequest();
List<Metacard> updatedMetacards = getUpdatedMetacards(updateRequest);
verifyEnforcedUpdate(updateRequest, updatedMetacards.subList(1, updatedMetacards.size()));
}
use of ddf.catalog.validation.MetacardValidator in project ddf by codice.
the class ValidationParser method parseMetacardTypes.
private List<Callable<Boolean>> parseMetacardTypes(Changeset changeset, List<Outer.MetacardType> metacardTypes) {
List<Callable<Boolean>> staged = new ArrayList<>();
BundleContext context = getBundleContext();
for (Outer.MetacardType metacardType : metacardTypes) {
Set<AttributeDescriptor> attributeDescriptors = new HashSet<>(BasicTypes.BASIC_METACARD.getAttributeDescriptors());
Set<String> requiredAttributes = new HashSet<>();
metacardType.attributes.forEach((attributeName, attribute) -> {
AttributeDescriptor descriptor = attributeRegistry.lookup(attributeName).orElseThrow(() -> new IllegalStateException(String.format("Metacard type [%s] includes the attribute [%s], but that attribute is not in the attribute registry.", metacardType.type, attributeName)));
attributeDescriptors.add(descriptor);
if (attribute.required) {
requiredAttributes.add(attributeName);
}
});
if (!requiredAttributes.isEmpty()) {
final MetacardValidator validator = new RequiredAttributesMetacardValidator(metacardType.type, requiredAttributes);
staged.add(() -> {
ServiceRegistration<MetacardValidator> registration = context.registerService(MetacardValidator.class, validator, null);
changeset.metacardValidatorServices.add(registration);
return registration != null;
});
}
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(NAME_PROPERTY, metacardType.type);
MetacardType type = new MetacardTypeImpl(metacardType.type, attributeDescriptors);
staged.add(() -> {
ServiceRegistration<MetacardType> registration = context.registerService(MetacardType.class, type, properties);
changeset.metacardTypeServices.add(registration);
return registration != null;
});
}
return staged;
}
Aggregations