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