use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.
the class ViewIT method shouldDeserialiseJsonView.
@Test
public void shouldDeserialiseJsonView() throws IOException {
// Given
// When
View view = loadView();
// Then
final ViewElementDefinition edge = view.getEdge(TestGroups.EDGE);
final ElementTransformer transformer = edge.getTransformer();
assertNotNull(transformer);
final List<ConsumerProducerFunctionContext<String, TransformFunction>> contexts = transformer.getFunctions();
assertEquals(1, contexts.size());
final List<String> selection = contexts.get(0).getSelection();
assertEquals(2, selection.size());
assertEquals(TestPropertyNames.PROP_1, selection.get(0));
assertEquals(IdentifierType.SOURCE.name(), selection.get(1));
final List<String> projection = contexts.get(0).getProjection();
assertEquals(1, projection.size());
assertEquals(TestPropertyNames.TRANSIENT_1, projection.get(0));
assertTrue(contexts.get(0).getFunction() instanceof ExampleTransformFunction);
final ElementFilter postFilter = edge.getPostTransformFilter();
assertNotNull(postFilter);
final List<ConsumerFunctionContext<String, FilterFunction>> filterContexts = postFilter.getFunctions();
assertEquals(1, contexts.size());
final List<String> postFilterSelection = filterContexts.get(0).getSelection();
assertEquals(1, postFilterSelection.size());
assertEquals(TestPropertyNames.TRANSIENT_1, postFilterSelection.get(0));
assertTrue(filterContexts.get(0).getFunction() instanceof ExampleFilterFunction);
}
use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.
the class SchemaElementDefinitionValidator method validate.
/**
* Checks each identifier and property has a type associated with it.
* Checks all {@link uk.gov.gchq.gaffer.function.FilterFunction}s and {@link uk.gov.gchq.gaffer.function.AggregateFunction}s defined are
* compatible with the identifiers and properties - this is done by comparing the function input and output types with
* the identifier and property types.
*
* @param elementDef the {@link uk.gov.gchq.gaffer.data.elementdefinition.ElementDefinition} to validate
* @param requiresAggregators true if aggregators are required
* @return true if the element definition is valid, otherwise false and an error is logged
*/
public boolean validate(final SchemaElementDefinition elementDef, final boolean requiresAggregators) {
final ElementFilter validator = elementDef.getValidator();
final ElementAggregator aggregator = elementDef.getAggregator();
return validateComponentTypes(elementDef) && validateAggregator(aggregator, elementDef, requiresAggregators) && validateFunctionArgumentTypes(validator, elementDef) && validateFunctionArgumentTypes(aggregator, elementDef);
}
use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.
the class SchemaElementDefinitionValidatorTest method shouldValidateFunctionSelectionsAndReturnTrueWhenAllFunctionsAreValid.
@Test
public void shouldValidateFunctionSelectionsAndReturnTrueWhenAllFunctionsAreValid() {
// Given
final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
given(elementDef.getPropertyClass("selectionStr")).willReturn((Class) String.class);
given(elementDef.getPropertyClass("selectionInt")).willReturn((Class) Integer.class);
final Predicate<String> function1 = a -> a.contains("true");
final Predicate<Integer> function2 = a -> a > 0;
final ElementFilter elementFilter = new ElementFilter.Builder().select("selectionStr").execute(function1).select("selectionInt").execute(function2).build();
final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
// When
final ValidationResult result = validator.validateFunctionArgumentTypes(elementFilter, elementDef);
// Then
assertTrue(result.isValid());
}
use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.
the class SchemaElementDefinitionTest method shouldReturnValidatorWithNoFunctionsWhenNoVerticesOrProperties.
@Test
public void shouldReturnValidatorWithNoFunctionsWhenNoVerticesOrProperties() {
// Given
final T elementDef = createEmptyBuilder().build();
// When
final ElementFilter validator = elementDef.getValidator();
// Then
assertEquals(0, validator.getComponents().size());
// Check the validator is cached
assertSame(validator, elementDef.getValidator());
}
use of uk.gov.gchq.gaffer.data.element.function.ElementFilter in project Gaffer by gchq.
the class SchemaElementDefinitionTest method shouldReturnFullValidator.
@Test
public void shouldReturnFullValidator() {
// Given
final T elementDef = createBuilder().property("property", PROPERTY_STRING_TYPE).property("property1", PROPERTY_STRING_TYPE).property("property2", PROPERTY_STRING_TYPE).validator(new ElementFilter.Builder().select("property1", "property2").execute(new IsXMoreThanY()).build()).build();
setupSchema(elementDef);
// When
final ElementFilter validator = elementDef.getValidator();
// Then
int i = 0;
assertEquals(IsXMoreThanY.class, validator.getComponents().get(i).getPredicate().getClass());
assertArrayEquals(new String[] { "property1", "property2" }, validator.getComponents().get(i).getSelection());
i++;
if (elementDef instanceof SchemaEdgeDefinition) {
assertEquals(Integer.class.getName(), ((IsA) validator.getComponents().get(i).getPredicate()).getType());
assertArrayEquals(new String[] { IdentifierType.SOURCE.name() }, validator.getComponents().get(i).getSelection());
i++;
assertEquals(Date.class.getName(), ((IsA) validator.getComponents().get(i).getPredicate()).getType());
assertArrayEquals(new String[] { IdentifierType.DESTINATION.name() }, validator.getComponents().get(i).getSelection());
i++;
assertEquals(Boolean.class.getName(), ((IsA) validator.getComponents().get(i).getPredicate()).getType());
assertArrayEquals(new String[] { IdentifierType.DIRECTED.name() }, validator.getComponents().get(i).getSelection());
i++;
} else {
assertArrayEquals(new String[] { IdentifierType.VERTEX.name() }, validator.getComponents().get(i).getSelection());
i++;
}
assertEquals(String.class.getName(), ((IsA) validator.getComponents().get(i).getPredicate()).getType());
assertArrayEquals(new String[] { "property" }, validator.getComponents().get(i).getSelection());
i++;
assertEquals(Exists.class, validator.getComponents().get(i).getPredicate().getClass());
assertArrayEquals(new String[] { "property" }, validator.getComponents().get(i).getSelection());
i++;
assertEquals(String.class.getName(), ((IsA) validator.getComponents().get(i).getPredicate()).getType());
assertArrayEquals(new String[] { "property1" }, validator.getComponents().get(i).getSelection());
i++;
assertEquals(Exists.class, validator.getComponents().get(i).getPredicate().getClass());
assertArrayEquals(new String[] { "property1" }, validator.getComponents().get(i).getSelection());
i++;
assertEquals(String.class.getName(), ((IsA) validator.getComponents().get(i).getPredicate()).getType());
assertArrayEquals(new String[] { "property2" }, validator.getComponents().get(i).getSelection());
i++;
assertEquals(Exists.class, validator.getComponents().get(i).getPredicate().getClass());
assertArrayEquals(new String[] { "property2" }, validator.getComponents().get(i).getSelection());
i++;
assertEquals(i, validator.getComponents().size());
// Check the validator is cached
assertSame(validator, elementDef.getValidator());
assertNotSame(validator, elementDef.getValidator(false));
}
Aggregations