Search in sources :

Example 6 with MetacardValidator

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

the class ValidationParser method getMetacardValidators.

@SuppressWarnings("unchecked")
private List<MetacardValidator> getMetacardValidators(MetacardValidatorDefinition validatorDefinition) {
    List<MetacardValidator> metacardValidators = new ArrayList<>();
    MetacardValidator metacardValidator = null;
    String validatorName = null;
    Object metacardValidatorObj = validatorDefinition.arguments.get(VALIDATOR_PROPERTY);
    if (metacardValidatorObj instanceof String) {
        validatorName = (String) metacardValidatorObj;
    }
    if (validatorName != null) {
        String metacardType = validatorDefinition.metacardType;
        switch(validatorName) {
            case REQUIRED_ATTRIBUTE_VALIDATOR_PROPERTY:
                {
                    if (metacardType == null) {
                        throw new IllegalStateException("Required Attributes Validator received invalid configuration");
                    }
                    Set<String> requiredAttributes = new HashSet<>();
                    Object requiredAttributesObj = validatorDefinition.arguments.get(REQUIRED_ATTRIBUTES_PROPERTY);
                    if (requiredAttributesObj instanceof List) {
                        List<Object> requiredAttrObjList = (List) requiredAttributesObj;
                        requiredAttributes = requiredAttrObjList.stream().filter(String.class::isInstance).map(String.class::cast).collect(Collectors.toSet());
                    }
                    if (CollectionUtils.isNotEmpty(requiredAttributes)) {
                        metacardValidator = new RequiredAttributesMetacardValidator(metacardType, requiredAttributes);
                    } else {
                        throw new IllegalStateException("Required Attributes Validator received invalid configuration");
                    }
                    break;
                }
            default:
                throw new IllegalStateException("Validator does not exist. (" + validatorName + ")");
        }
    }
    if (metacardValidator != null) {
        metacardValidators.add(metacardValidator);
    }
    return metacardValidators;
}
Also used : Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) HashSet(java.util.HashSet) MetacardValidator(ddf.catalog.validation.MetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator)

Example 7 with MetacardValidator

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

the class ValidationParser method registerMetacardValidators.

private List<Callable<Boolean>> registerMetacardValidators(Changeset changeset, List<MetacardValidatorDefinition> metacardValidatorDefinitions) {
    List<Callable<Boolean>> staged = new ArrayList<>();
    BundleContext context = getBundleContext();
    for (MetacardValidatorDefinition metacardValidatorDefinition : metacardValidatorDefinitions) {
        try {
            List<MetacardValidator> metacardValidators = getMetacardValidators(metacardValidatorDefinition);
            metacardValidators.forEach(metacardValidator -> staged.add(() -> {
                ServiceRegistration<MetacardValidator> registration = context.registerService(MetacardValidator.class, metacardValidator, null);
                changeset.metacardValidatorServices.add(registration);
                return registration != null;
            }));
        } catch (IllegalStateException ise) {
            LOGGER.error("Could not register metacard validator for definition: {}", metacardValidatorDefinition, ise);
        }
    }
    return staged;
}
Also used : MetacardValidator(ddf.catalog.validation.MetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 8 with MetacardValidator

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

the class MetacardValidityMarkerPluginTest method testMetacardPassesEnforcedValidatorsNoDescribable.

@Test
public void testMetacardPassesEnforcedValidatorsNoDescribable() throws StopProcessingException, PluginExecutionException {
    MetacardValidator mockValidator = getMockPassingValidatorNoDescribable();
    metacardValidators.add(mockValidator);
    enforcedMetacardValidators.add(mockValidator.getClass().getCanonicalName());
    CreateRequest createRequest = getMockCreateRequest();
    List<Metacard> createdMetacards = createRequest.getMetacards();
    verifyEnforcedCreate(createRequest, createdMetacards);
    UpdateRequest updateRequest = getMockUpdateRequest();
    List<Metacard> updatedMetacards = getUpdatedMetacards(updateRequest);
    verifyEnforcedUpdate(updateRequest, updatedMetacards);
}
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 9 with MetacardValidator

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

the class MetacardValidityMarkerPluginTest method getMockFailingValidatorWithWarnings.

private MetacardValidator getMockFailingValidatorWithWarnings() throws ValidationException {
    ValidationException validationException = mock(ValidationException.class);
    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 10 with MetacardValidator

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

the class MetacardValidityMarkerPluginTest method getMockEnforcedPassingValidatorWithId.

private MetacardValidator getMockEnforcedPassingValidatorWithId(String id) {
    MetacardValidator metacardValidator = mock(MetacardValidator.class, withSettings().extraInterfaces(Describable.class));
    when(((Describable) metacardValidator).getId()).thenReturn(id);
    return metacardValidator;
}
Also used : MetacardValidator(ddf.catalog.validation.MetacardValidator) Describable(ddf.catalog.util.Describable)

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