Search in sources :

Example 1 with ElementFilter

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);
}
Also used : ConsumerProducerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerProducerFunctionContext) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ExampleTransformFunction(uk.gov.gchq.gaffer.function.ExampleTransformFunction) Test(org.junit.Test)

Example 2 with ElementFilter

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);
}
Also used : ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator)

Example 3 with ElementFilter

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());
}
Also used : ValidationResult(uk.gov.gchq.koryphe.ValidationResult) Predicate(java.util.function.Predicate) TestPropertyNames(uk.gov.gchq.gaffer.commonutil.TestPropertyNames) Set(java.util.Set) HashMap(java.util.HashMap) BinaryOperator(java.util.function.BinaryOperator) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) HashSet(java.util.HashSet) Mockito(org.mockito.Mockito) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) Sets(org.mockito.internal.util.collections.Sets) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) TestGroups(uk.gov.gchq.gaffer.commonutil.TestGroups) Collections(java.util.Collections) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Mockito.mock(org.mockito.Mockito.mock) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 4 with ElementFilter

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());
}
Also used : ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Test(org.junit.jupiter.api.Test)

Example 5 with ElementFilter

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));
}
Also used : ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) IsXMoreThanY(uk.gov.gchq.koryphe.impl.predicate.IsXMoreThanY) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)43 Test (org.junit.jupiter.api.Test)35 Schema (uk.gov.gchq.gaffer.store.schema.Schema)11 Element (uk.gov.gchq.gaffer.data.element.Element)10 ValidationResult (uk.gov.gchq.koryphe.ValidationResult)10 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)8 JSONSerialisationTest (uk.gov.gchq.gaffer.JSONSerialisationTest)6 Context (uk.gov.gchq.gaffer.store.Context)6 Store (uk.gov.gchq.gaffer.store.Store)6 Exists (uk.gov.gchq.koryphe.impl.predicate.Exists)6 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)5 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)5 GlobalViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition)5 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)5 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)5 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)5 HashMap (java.util.HashMap)4 TestStore (uk.gov.gchq.gaffer.integration.store.TestStore)4 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4