use of uk.gov.gchq.gaffer.function.AggregateFunction 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.AggregateFunction in project Gaffer by gchq.
the class ElementAggregatorTest method shouldSetStateOnElement.
@Test
public void shouldSetStateOnElement() {
// Given
final Object[] state = { "state1", "state2" };
final ElementAggregator aggregator = new ElementAggregator();
final PassThroughFunctionContext<String, AggregateFunction> functionContext1 = mock(PassThroughFunctionContext.class);
final AggregateFunction function = mock(AggregateFunction.class);
given(functionContext1.getFunction()).willReturn(function);
given(function.state()).willReturn(state);
aggregator.addFunction(functionContext1);
final Edge edge = new Edge("group");
// When
aggregator.state(edge);
// Then
final ArgumentCaptor<PropertiesTuple> propertiesTupleCaptor = ArgumentCaptor.forClass(PropertiesTuple.class);
verify(functionContext1).project(propertiesTupleCaptor.capture(), Mockito.eq(state));
assertEquals(edge.getProperties(), propertiesTupleCaptor.getValue().getProperties());
}
use of uk.gov.gchq.gaffer.function.AggregateFunction in project Gaffer by gchq.
the class ElementAggregatorTest method shouldBuildAggregator.
@Test
public void shouldBuildAggregator() {
// Given
final String property1 = "property 1";
final String property2 = "property 2";
final String property3a = "property 3a";
final String property3b = "property 3b";
final String property5 = "property 5";
final AggregateFunction func1 = mock(AggregateFunction.class);
final AggregateFunction func3 = mock(AggregateFunction.class);
final AggregateFunction func4 = mock(AggregateFunction.class);
final AggregateFunction func5 = mock(AggregateFunction.class);
// When - check you can build the selection/function in any order,
// although normally it will be done - select then execute.
final ElementAggregator aggregator = new ElementAggregator.Builder().select(property1).execute(func1).select(property2).select(property3a, property3b).execute(func3).execute(func4).execute(func5).select(property5).build();
// Then
int i = 0;
PassThroughFunctionContext<String, AggregateFunction> context = aggregator.getFunctions().get(i++);
assertEquals(1, context.getSelection().size());
assertEquals(property1, context.getSelection().get(0));
assertSame(func1, context.getFunction());
context = aggregator.getFunctions().get(i++);
assertEquals(1, context.getSelection().size());
assertEquals(property2, context.getSelection().get(0));
context = aggregator.getFunctions().get(i++);
assertEquals(2, context.getSelection().size());
assertEquals(property3a, context.getSelection().get(0));
assertEquals(property3b, context.getSelection().get(1));
assertSame(func3, context.getFunction());
context = aggregator.getFunctions().get(i++);
assertSame(func4, context.getFunction());
context = aggregator.getFunctions().get(i++);
assertSame(func5, context.getFunction());
assertEquals(1, context.getSelection().size());
assertEquals(property5, context.getSelection().get(0));
assertEquals(i, aggregator.getFunctions().size());
}
use of uk.gov.gchq.gaffer.function.AggregateFunction in project Gaffer by gchq.
the class ElementAggregatorTest method shouldSetStateOnProperties.
@Test
public void shouldSetStateOnProperties() {
// Given
final Object[] state = { "state1", "state2" };
final ElementAggregator aggregator = new ElementAggregator();
final PassThroughFunctionContext<String, AggregateFunction> functionContext1 = mock(PassThroughFunctionContext.class);
final AggregateFunction function = mock(AggregateFunction.class);
given(functionContext1.getFunction()).willReturn(function);
given(function.state()).willReturn(state);
aggregator.addFunction(functionContext1);
final Properties properties = new Properties();
// When
aggregator.state(properties);
// Then
final ArgumentCaptor<PropertiesTuple> propertiesTupleCaptor = ArgumentCaptor.forClass(PropertiesTuple.class);
verify(functionContext1).project(propertiesTupleCaptor.capture(), Mockito.eq(state));
assertSame(properties, propertiesTupleCaptor.getValue().getProperties());
}
use of uk.gov.gchq.gaffer.function.AggregateFunction in project Gaffer by gchq.
the class ElementAggregatorTest method shouldWrapElementInElementTupleAndCallSuper.
@Test
public void shouldWrapElementInElementTupleAndCallSuper() {
// Given
final String reference = "reference1";
final String value = "value";
final ElementAggregator aggregator = new ElementAggregator();
final PassThroughFunctionContext<String, AggregateFunction> functionContext1 = mock(PassThroughFunctionContext.class);
final AggregateFunction function = mock(AggregateFunction.class);
given(functionContext1.getFunction()).willReturn(function);
final List<String> references = Collections.singletonList(reference);
given(functionContext1.getSelection()).willReturn(references);
aggregator.addFunction(functionContext1);
final Edge edge = new Edge("group");
edge.putProperty(reference, value);
final ArgumentCaptor<PropertiesTuple> propertiesTupleCaptor = ArgumentCaptor.forClass(PropertiesTuple.class);
given(functionContext1.select(propertiesTupleCaptor.capture())).willReturn(new String[] { value });
// When
aggregator.aggregate(edge);
// Then
assertEquals(edge.getProperties(), propertiesTupleCaptor.getValue().getProperties());
verify(functionContext1, times(2)).getFunction();
final ArgumentCaptor<Object[]> argumentCaptor = ArgumentCaptor.forClass(Object[].class);
verify(function).aggregate(argumentCaptor.capture());
assertEquals(value, argumentCaptor.getValue()[0]);
}
Aggregations