use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.
the class AggregateHandlerTest method shouldFailValidationWhenTypeArgumentOfBinaryOperatorInFunctionIsIncorrect.
@Test
public void shouldFailValidationWhenTypeArgumentOfBinaryOperatorInFunctionIsIncorrect() {
// Given
final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().property(TestPropertyNames.TIMESTAMP, "timestamp.long").build()).type("timestamp.long", new TypeDefinition(Long.class)).build();
given(store.getSchema()).willReturn(schema);
input.add(entity);
input.add(entity1);
input.add(entity2);
entities.put(TestGroups.ENTITY, new AggregatePair(new ElementAggregator.Builder().select(TestPropertyNames.TIMESTAMP).execute(new Or()).build()));
final Aggregate aggregate = new Aggregate.Builder().input(input).entities(entities).build();
// When / Then
assertThatExceptionOfType(OperationException.class).isThrownBy(() -> handler.doOperation(aggregate, context, store)).withMessageContaining("Incompatible types.");
}
use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.
the class AggregateValidator method validateOperation.
@Override
protected ValidationResult validateOperation(final Aggregate operation, final Schema schema) {
final ValidationResult result = new ValidationResult();
final Map<String, ?> entities = null != operation.getEntities() ? operation.getEntities() : new HashMap<>();
final Map<String, ?> edges = null != operation.getEdges() ? operation.getEdges() : new HashMap<>();
for (final Map.Entry<String, ?> entry : edges.entrySet()) {
result.add(validateEdge(entry, schema));
result.add(validateElementAggregator(entry, schema));
result.add(validateAggregatePropertyClasses(schema.getEdge(entry.getKey()), (AggregatePair) entry.getValue()));
}
for (final Map.Entry<String, ?> entry : entities.entrySet()) {
result.add(validateEntity(entry, schema));
result.add(validateElementAggregator(entry, schema));
result.add(validateAggregatePropertyClasses(schema.getEntity(entry.getKey()), (AggregatePair) entry.getValue()));
}
return result;
}
use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.
the class AggregateHandlerTest method shouldFailValidationWhenElementAggregatorOperationIsNull.
@Test
public void shouldFailValidationWhenElementAggregatorOperationIsNull() {
// Given
final Schema schema = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().aggregator(new ElementAggregator.Builder().select("count").execute(null).build()).groupBy("timestamp").build()).build();
given(store.getSchema()).willReturn(schema);
input.add(edge);
input.add(edge1);
input.add(edge2);
edges.put(TestGroups.EDGE, new AggregatePair());
final Aggregate aggregate = new Aggregate.Builder().input(input).edges(edges).build();
// When / Then
assertThatExceptionOfType(OperationException.class).isThrownBy(() -> handler.doOperation(aggregate, context, store)).withMessageContaining("Schema contains an ElementAggregator with a null function.");
}
use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.
the class AggregateHandlerTest method shouldAggregateTheSameFromSchemaOrOperation.
@Test
public void shouldAggregateTheSameFromSchemaOrOperation() throws OperationException {
// Given
final Schema schema = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().build()).build();
final Store store1 = mock(Store.class);
final List<Element> input1 = new ArrayList<>();
final Set<Element> expected1 = new HashSet<>();
final Map<String, AggregatePair> edges1 = new HashMap<>();
final Schema schema1 = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().aggregator(new ElementAggregator.Builder().select("count").execute(new Sum()).select("turns").execute(new Sum()).build()).groupBy("timestamp").build()).build();
given(store.getSchema()).willReturn(schema);
given(store1.getSchema()).willReturn(schema1);
input.add(edge);
input.add(edge1);
input.add(edge2);
final Edge localEdge = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 2L).property("turns", 6).property("count", 2L).build();
final Edge localEdge1 = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 1L).property("turns", 9).property("count", 5L).build();
final Edge localEdge2 = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 2L).property("turns", 4).property("count", 1L).build();
input1.add(localEdge);
input1.add(localEdge1);
input1.add(localEdge2);
final AggregatePair pair = new AggregatePair(new String[] { "timestamp" }, new ElementAggregator.Builder().select("count").execute(new Sum()).select("turns").execute(new Sum()).build());
final AggregatePair otherPair = new AggregatePair();
edges.put(TestGroups.EDGE, pair);
edges1.put(TestGroups.EDGE, otherPair);
final Edge expectedEdge = new Edge.Builder().group(TestGroups.EDGE).property("timestamp", 2L).property("turns", 10).property("count", 3L).build();
expected.add(expectedEdge);
expected.add(edge1);
expected1.add(expectedEdge);
expected1.add(edge1);
final Aggregate aggregate = new Aggregate.Builder().input(input).edges(edges).build();
final Aggregate aggregate1 = new Aggregate.Builder().input(input1).edges(edges1).build();
// When
final Iterable<? extends Element> results = handler.doOperation(aggregate, context, store);
final Set<Element> resultsSet = Sets.newHashSet(results);
final Iterable<? extends Element> results1 = handler.doOperation(aggregate1, context, store1);
final Set<Element> resultsSet1 = Sets.newHashSet(results1);
// Then
assertEquals(expected, resultsSet);
assertEquals(expected1, resultsSet1);
}
use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.
the class AggregateHandlerTest method shouldAggregateElementsWhenAggregatorNotProvidedInSchema.
@Test
public void shouldAggregateElementsWhenAggregatorNotProvidedInSchema() throws OperationException {
// Given
final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().groupBy("timestamp").build()).build();
given(store.getSchema()).willReturn(schema);
input.add(entity);
input.add(entity1);
input.add(entity2);
input.add(entity3);
final AggregatePair pair = new AggregatePair(new ElementAggregator.Builder().select("count").execute(new Sum()).build());
entities.put(TestGroups.ENTITY, pair);
final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property("timestamp", 3L).property("count", 5).build();
final Entity expectedEntity1 = new Entity.Builder().group(TestGroups.ENTITY).property("timestamp", 2L).property("count", 6).build();
expected.add(expectedEntity);
expected.add(expectedEntity1);
final Aggregate aggregate = new Aggregate.Builder().input(input).entities(entities).build();
// When
final Iterable<? extends Element> results = handler.doOperation(aggregate, context, store);
final Set<Element> resultsSet = Sets.newHashSet(results);
// Then
assertEquals(expected, resultsSet);
}
Aggregations