use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.
the class SchemaElementDefinitionValidatorTest method shouldValidateFunctionSelectionsAndReturnFalseWhenFunctionTypeDoesNotEqualSelectionType.
@Test
public void shouldValidateFunctionSelectionsAndReturnFalseWhenFunctionTypeDoesNotEqualSelectionType() {
// Given
final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
final Processor<String, ConsumerFunctionContext<String, ConsumerFunction>> processor = mock(Processor.class);
final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
final List<ConsumerFunctionContext<String, ConsumerFunction>> functions = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final ConsumerFunctionContext<String, ConsumerFunction> context = mock(ConsumerFunctionContext.class);
final ConsumerFunction function = mock(ConsumerFunction.class);
given(function.getInputClasses()).willReturn(new Class<?>[] { String.class });
given(context.getFunction()).willReturn(function);
given(context.getSelection()).willReturn(Collections.singletonList("key" + i + "a"));
functions.add(context);
given(elementDef.getClass(context.getSelection().get(0))).willReturn((Class) Integer.class);
}
given(processor.getFunctions()).willReturn(functions);
// When
final boolean isValid = validator.validateFunctionArgumentTypes(processor, elementDef);
// Then
assertFalse(isValid);
}
use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.
the class SchemaTest method testLoadingSchemaFromJson.
@Test
public void testLoadingSchemaFromJson() {
// Edge definitions
SchemaElementDefinition edgeDefinition = schema.getEdge(TestGroups.EDGE);
assertNotNull(edgeDefinition);
assertEquals(EDGE_DESCRIPTION, edgeDefinition.getDescription());
final Map<String, String> propertyMap = edgeDefinition.getPropertyMap();
assertEquals(3, propertyMap.size());
assertEquals("prop.string", propertyMap.get(TestPropertyNames.PROP_2));
assertEquals("prop.date", propertyMap.get(TestPropertyNames.DATE));
assertEquals("timestamp", propertyMap.get(TestPropertyNames.TIMESTAMP));
assertEquals(Sets.newLinkedHashSet(Collections.singletonList(TestPropertyNames.DATE)), edgeDefinition.getGroupBy());
// Check validator
ElementFilter validator = edgeDefinition.getValidator();
final List<ConsumerFunctionContext<String, FilterFunction>> valContexts = validator.getFunctions();
int index = 0;
ConsumerFunctionContext<String, FilterFunction> valContext = valContexts.get(index++);
assertTrue(valContext.getFunction() instanceof IsA);
assertEquals(1, valContext.getSelection().size());
assertEquals(IdentifierType.SOURCE.name(), valContext.getSelection().get(0));
valContext = valContexts.get(index++);
assertTrue(valContext.getFunction() instanceof IsA);
assertEquals(1, valContext.getSelection().size());
assertEquals(IdentifierType.DESTINATION.name(), valContext.getSelection().get(0));
valContext = valContexts.get(index++);
assertTrue(valContext.getFunction() instanceof IsA);
assertEquals(1, valContext.getSelection().size());
assertEquals(IdentifierType.DIRECTED.name(), valContext.getSelection().get(0));
valContext = valContexts.get(index++);
assertTrue(valContext.getFunction() instanceof ExampleFilterFunction);
assertEquals(1, valContext.getSelection().size());
assertEquals(IdentifierType.DIRECTED.name(), valContext.getSelection().get(0));
valContext = valContexts.get(index++);
assertTrue(valContext.getFunction() instanceof IsA);
assertEquals(1, valContext.getSelection().size());
assertEquals(TestPropertyNames.PROP_2, valContext.getSelection().get(0));
valContext = valContexts.get(index++);
assertTrue(valContext.getFunction() instanceof ExampleFilterFunction);
assertEquals(1, valContext.getSelection().size());
assertEquals(TestPropertyNames.PROP_2, valContext.getSelection().get(0));
valContext = valContexts.get(index++);
assertTrue(valContext.getFunction() instanceof IsA);
assertEquals(1, valContext.getSelection().size());
assertEquals(TestPropertyNames.DATE, valContext.getSelection().get(0));
valContext = valContexts.get(index++);
assertTrue(valContext.getFunction() instanceof IsA);
assertEquals(1, valContext.getSelection().size());
assertEquals(TestPropertyNames.TIMESTAMP, valContext.getSelection().get(0));
assertEquals(index, valContexts.size());
TypeDefinition type = edgeDefinition.getPropertyTypeDef(TestPropertyNames.DATE);
assertEquals(Date.class, type.getClazz());
assertEquals(DATE_TYPE_DESCRIPTION, type.getDescription());
assertNull(type.getSerialiser());
assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
// Entity definitions
SchemaElementDefinition entityDefinition = schema.getEntity(TestGroups.ENTITY);
assertNotNull(entityDefinition);
assertEquals(ENTITY_DESCRIPTION, entityDefinition.getDescription());
assertTrue(entityDefinition.containsProperty(TestPropertyNames.PROP_1));
type = entityDefinition.getPropertyTypeDef(TestPropertyNames.PROP_1);
assertEquals(0, entityDefinition.getGroupBy().size());
assertEquals(STRING_TYPE_DESCRIPTION, type.getDescription());
assertEquals(String.class, type.getClazz());
assertNull(type.getSerialiser());
assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
ElementAggregator aggregator = edgeDefinition.getAggregator();
List<PassThroughFunctionContext<String, AggregateFunction>> aggContexts = aggregator.getFunctions();
assertEquals(3, aggContexts.size());
PassThroughFunctionContext<String, AggregateFunction> aggContext = aggContexts.get(0);
assertTrue(aggContext.getFunction() instanceof ExampleAggregateFunction);
assertEquals(1, aggContext.getSelection().size());
assertEquals(TestPropertyNames.PROP_2, aggContext.getSelection().get(0));
aggContext = aggContexts.get(1);
assertTrue(aggContext.getFunction() instanceof ExampleAggregateFunction);
assertEquals(1, aggContext.getSelection().size());
assertEquals(TestPropertyNames.DATE, aggContext.getSelection().get(0));
}
use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.
the class Filter method cloneFunctions.
/**
* Create a deep copy of the {@link uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext}s executed by this
* <code>Filter</code>.
*
* @return Deep copy of {@link uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext}s.
*/
protected List<ConsumerFunctionContext<R, FilterFunction>> cloneFunctions() {
final List<ConsumerFunctionContext<R, FilterFunction>> functionClones = new ArrayList<>();
for (final ConsumerFunctionContext<R, FilterFunction> function : functions) {
final ConsumerFunctionContext<R, FilterFunction> cloneContext = new ConsumerFunctionContext<>();
cloneContext.setSelection(function.getSelection());
final FilterFunction af = function.getFunction();
if (af != null) {
cloneContext.setFunction(af.statelessClone());
}
functionClones.add(cloneContext);
}
return functionClones;
}
use of uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext in project Gaffer by gchq.
the class AndTest method shouldJsonSerialiseAndDeserialise.
@Test
public void shouldJsonSerialiseAndDeserialise() throws SerialisationException {
// Given
final And filter = new And(Collections.singletonList(new ConsumerFunctionContext<Integer, FilterFunction>(new And(), Arrays.asList(0, 1, 2))));
// When
final String json = new String(new JSONSerialiser().serialise(filter, true));
// Then
JsonUtil.assertEquals(String.format("{%n" + " \"class\" : \"uk.gov.gchq.gaffer.function.filter.And\",%n" + " \"functions\" : [ {%n" + " \"function\" : {%n" + " \"class\" : \"uk.gov.gchq.gaffer.function.filter.And\",%n" + " \"functions\" : [ ]%n" + " },%n" + " \"selection\" : [ 0, 1, 2 ]%n" + " } ]%n" + "}"), json);
// When 2
final And deserialisedFilter = new JSONSerialiser().deserialise(json.getBytes(), And.class);
// Then 2
assertNotNull(deserialisedFilter);
}
Aggregations