Search in sources :

Example 1 with MetacardValidator

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;
}
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 MetacardValidator

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;
}
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 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;
}
Also used : ValidationException(ddf.catalog.validation.ValidationException) MetacardValidator(ddf.catalog.validation.MetacardValidator)

Example 4 with 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()));
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardValidator(ddf.catalog.validation.MetacardValidator) UpdateRequest(ddf.catalog.operation.UpdateRequest) CreateRequest(ddf.catalog.operation.CreateRequest) Test(org.junit.Test)

Example 5 with MetacardValidator

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;
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) Callable(java.util.concurrent.Callable) MetacardType(ddf.catalog.data.MetacardType) MetacardValidator(ddf.catalog.validation.MetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) BundleContext(org.osgi.framework.BundleContext) HashSet(java.util.HashSet)

Aggregations

MetacardValidator (ddf.catalog.validation.MetacardValidator)12 Metacard (ddf.catalog.data.Metacard)6 ValidationException (ddf.catalog.validation.ValidationException)6 Describable (ddf.catalog.util.Describable)5 ArrayList (java.util.ArrayList)4 RequiredAttributesMetacardValidator (ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator)3 HashSet (java.util.HashSet)3 CreateRequest (ddf.catalog.operation.CreateRequest)2 UpdateRequest (ddf.catalog.operation.UpdateRequest)2 List (java.util.List)2 Callable (java.util.concurrent.Callable)2 Test (org.junit.Test)2 BundleContext (org.osgi.framework.BundleContext)2 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)1 MetacardType (ddf.catalog.data.MetacardType)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)1 Serializable (java.io.Serializable)1 Hashtable (java.util.Hashtable)1 Set (java.util.Set)1