use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.
the class CountGroupsHandler method doOperation.
@Override
public GroupCounts doOperation(final CountGroups operation, final Context context, final Store store) throws OperationException {
int count = 0;
final GroupCounts groupCounts = new GroupCounts();
if (null != operation.getElements()) {
for (final Element element : operation.getElements()) {
if (null != operation.getLimit()) {
count++;
if (count > operation.getLimit()) {
groupCounts.setLimitHit(true);
break;
}
}
if (element instanceof Entity) {
groupCounts.addEntityGroup(element.getGroup());
} else {
groupCounts.addEdgeGroup(element.getGroup());
}
}
}
return groupCounts;
}
use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.
the class CountGroupsHandlerTest method shouldReturnGroupCountsWithoutLimit.
@Test
public void shouldReturnGroupCountsWithoutLimit() throws OperationException {
// Given
final CountGroupsHandler handler = new CountGroupsHandler();
final Store store = mock(Store.class);
final CountGroups countGroups = mock(CountGroups.class);
final CloseableIterable<Element> elements = getElements();
final Context context = new Context();
given(countGroups.getLimit()).willReturn(null);
given(countGroups.getElements()).willReturn(elements);
// When
final GroupCounts counts = handler.doOperation(countGroups, context, store);
// Then
assertFalse(counts.isLimitHit());
assertEquals(2, counts.getEntityGroups().size());
assertEquals(3, (int) counts.getEntityGroups().get(GROUP1));
assertEquals(1, (int) counts.getEntityGroups().get(GROUP2));
assertEquals(2, counts.getEdgeGroups().size());
assertEquals(1, (int) counts.getEdgeGroups().get(GROUP1));
assertEquals(3, (int) counts.getEdgeGroups().get(GROUP2));
}
use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.
the class CountGroupsHandlerTest method shouldReturnAllGroupCountsWhenLessThanLimit.
@Test
public void shouldReturnAllGroupCountsWhenLessThanLimit() throws OperationException {
// Given
final CountGroupsHandler handler = new CountGroupsHandler();
final Store store = mock(Store.class);
final CountGroups countGroups = mock(CountGroups.class);
final CloseableIterable<Element> elements = getElements();
final Integer limit = 10;
final Context context = new Context();
given(countGroups.getLimit()).willReturn(limit);
given(countGroups.getElements()).willReturn(elements);
// When
final GroupCounts counts = handler.doOperation(countGroups, context, store);
// Then
assertFalse(counts.isLimitHit());
assertEquals(2, counts.getEntityGroups().size());
assertEquals(3, (int) counts.getEntityGroups().get(GROUP1));
assertEquals(1, (int) counts.getEntityGroups().get(GROUP2));
assertEquals(2, counts.getEdgeGroups().size());
assertEquals(1, (int) counts.getEdgeGroups().get(GROUP1));
assertEquals(3, (int) counts.getEdgeGroups().get(GROUP2));
}
use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.
the class OperationServiceIT method shouldReturnGroupCounts.
@Test
public void shouldReturnGroupCounts() throws IOException {
// Given
RestApiTestUtil.addElements(DEFAULT_ELEMENTS);
// When
final Response response = RestApiTestUtil.executeOperationChainChunked(new OperationChain.Builder().first(new GetAllElements<>()).then(new CountGroups()).build());
// Then
final GroupCounts groupCounts = response.readEntity(new GenericType<GroupCounts>() {
});
verifyGroupCounts(groupCounts);
}
use of uk.gov.gchq.gaffer.data.GroupCounts in project Gaffer by gchq.
the class CountGroupsIT method shouldCountGroupsOfElementsWhenMoreElementsThanLimit.
@Test
public void shouldCountGroupsOfElementsWhenMoreElementsThanLimit() throws OperationException, InterruptedException {
// Given
final User user = new User();
final int limit = 5;
final Entity entity = new Entity(TestGroups.ENTITY_2, VERTEX);
entity.putProperty(TestPropertyNames.INT, 100);
// When
final GroupCounts counts = graph.execute(new Builder().first(new GetAllElements<>()).then(new CountGroups(limit)).build(), user);
// Then
int totalCount = (null != counts.getEntityGroups().get(TestGroups.ENTITY) ? counts.getEntityGroups().get(TestGroups.ENTITY) : 0);
totalCount += (null != counts.getEdgeGroups().get(TestGroups.EDGE) ? counts.getEdgeGroups().get(TestGroups.EDGE) : 0);
assertEquals(limit, totalCount);
}
Aggregations