Search in sources :

Example 1 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class Operation method validate.

/**
 * Validates an operation. This should be used to validate that fields have been be configured correctly.
 * By default no validation is applied. Override this method to implement validation.
 *
 * @return validation result.
 */
default ValidationResult validate() {
    final ValidationResult result = new ValidationResult();
    HashSet<Field> fields = Sets.<Field>newHashSet();
    Class<?> currentClass = this.getClass();
    while (null != currentClass) {
        fields.addAll(Arrays.asList(currentClass.getDeclaredFields()));
        currentClass = currentClass.getSuperclass();
    }
    for (final Field field : fields) {
        final Required[] annotations = field.getAnnotationsByType(Required.class);
        if (null != annotations && annotations.length > 0) {
            if (field.isAccessible()) {
                validateRequiredFieldPresent(result, field);
            } else {
                AccessController.doPrivileged((PrivilegedAction<Operation>) () -> {
                    field.setAccessible(true);
                    validateRequiredFieldPresent(result, field);
                    return null;
                });
            }
        }
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) Required(uk.gov.gchq.gaffer.commonutil.Required) ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Example 2 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class GetWalks method validate.

@Override
public ValidationResult validate() {
    final ValidationResult result = InputOutput.super.validate();
    final int getEdgeOperations = getNumberOfGetEdgeOperations();
    if (getEdgeOperations < 1) {
        result.addError("No hops were provided. " + HOP_DEFINITION);
    } else {
        int i = 0;
        for (final OperationChain<Iterable<Element>> operation : operations) {
            if (operation.getOperations().isEmpty()) {
                result.addError("Operation chain " + i + " contains no operations");
            } else {
                final Operation firstOp = operation.getOperations().get(0);
                if (firstOp instanceof Input) {
                    if (null != ((Input) firstOp).getInput()) {
                        result.addError("The input for operations must be null.");
                    }
                } else {
                    result.addError("The first operation in operation chain " + i + ": " + firstOp.getClass().getName() + " is not be able to accept the input seeds. It must implement " + Input.class.getName());
                }
            }
            if (getNumberOfGetEdgeOperationsWithoutRepeats(operation) < 1 && i < (operations.size() - 1)) {
                // An operation does not contain a hop
                result.addError("All operations must contain a single hop. Operation " + i + " does not contain a hop. The only exception is the last operation, which is allowed to just fetch Entities. " + HOP_DEFINITION);
            } else if (getNumberOfGetEdgeOperationsWithoutRepeats(operation) > 1) {
                // An operation does not contain a hop
                result.addError("All operations must contain a single hop. Operation " + i + " contains multiple hops.");
            }
            i++;
        }
    }
    return result;
}
Also used : Input(uk.gov.gchq.gaffer.operation.io.Input) MultiEntityIdInput(uk.gov.gchq.gaffer.operation.io.MultiEntityIdInput) Operation(uk.gov.gchq.gaffer.operation.Operation) ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Example 3 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class ValidatedElements method handleInvalidItem.

@Override
protected void handleInvalidItem(final Element item) {
    final ValidationResult result = getValidator().validateWithValidationResult(item);
    final String elementDescription = null != item ? item.toString() : "<unknown>";
    String validationResultErrors;
    if (result.isValid()) {
        validationResultErrors = "";
    } else {
        validationResultErrors = " \n" + result.getErrorString();
    }
    throw new IllegalArgumentException("Element of type " + elementDescription + " is not valid." + validationResultErrors);
}
Also used : ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Example 4 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class SchemaEdgeDefinitionTest method shouldFailValidationWhenEdgeSourceOrDestinationNull.

@Test
public void shouldFailValidationWhenEdgeSourceOrDestinationNull() {
    // Given
    final SchemaEdgeDefinition elementDef = new SchemaEdgeDefinition.Builder().source(null).destination("dest").build();
    final Schema schema = new Schema.Builder().edge(TestGroups.EDGE, elementDef).type("src", String.class).type("dest", String.class).build();
    final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
    // When
    final ValidationResult result = validator.validate(elementDef);
    // Then
    assertFalse(result.isValid());
}
Also used : Builder(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition.Builder) Builder(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition.Builder) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 5 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class SchemaElementDefinitionValidatorTest method shouldValidateComponentTypesAndErrorWhenPropertyNameIsAReservedWord.

@Test
public void shouldValidateComponentTypesAndErrorWhenPropertyNameIsAReservedWord() {
    for (final IdentifierType identifierType : IdentifierType.values()) {
        // Given
        final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
        final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
        final Set<String> properties = new HashSet<>();
        properties.add(TestPropertyNames.COUNT);
        properties.add(identifierType.name());
        given(elementDef.getIdentifiers()).willReturn(new HashSet<>());
        given(elementDef.getProperties()).willReturn(properties);
        // When
        final ValidationResult result = validator.validateComponentTypes(elementDef);
        // Then
        assertFalse(result.isValid());
        assertTrue(result.getErrorString().contains("reserved word"));
    }
}
Also used : ValidationResult(uk.gov.gchq.koryphe.ValidationResult) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

ValidationResult (uk.gov.gchq.koryphe.ValidationResult)132 Test (org.junit.jupiter.api.Test)86 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)32 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)27 HashMap (java.util.HashMap)13 OperationTest (uk.gov.gchq.gaffer.operation.OperationTest)12 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)11 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)11 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)9 ExampleTransformFunction (uk.gov.gchq.gaffer.function.ExampleTransformFunction)9 OperationChainValidator (uk.gov.gchq.gaffer.store.operation.OperationChainValidator)9 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)8 Schema (uk.gov.gchq.gaffer.store.schema.Schema)8 Signature (uk.gov.gchq.koryphe.signature.Signature)8 Map (java.util.Map)7 OperationChainOptimiser (uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser)7 Set (java.util.Set)6 Operation (uk.gov.gchq.gaffer.operation.Operation)6 Element (uk.gov.gchq.gaffer.data.element.Element)5 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)5