CountGroupsHandler implements OutputOperationHandler<CountGroups, GroupCounts> { @Override public GroupCounts doOperation(final CountGroups operation, final Context context, final Store store) throws OperationException { final GroupCounts groupCounts = new GroupCounts(); try { int count = 0; if (null != operation.getInput()) { for (final Element element : operation.getInput()) { 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()); } } } } finally { CloseableUtil.close(operation); } return groupCounts; }  @Override GroupCounts doOperation(final CountGroups operation,
                                   final Context context, final Store store);  }
@Test public void shouldReturnNoCountsIfElementsAreNull() throws OperationException, IOException { final CountGroupsHandler handler = new CountGroupsHandler(); final Store store = mock(Store.class); final CountGroups countGroups = mock(CountGroups.class); final Context context = new Context(); given(countGroups.getInput()).willReturn(null); final GroupCounts counts = handler.doOperation(countGroups, context, store); assertFalse(counts.isLimitHit()); assertEquals(0, counts.getEntityGroups().size()); assertEquals(0, counts.getEdgeGroups().size()); verify(countGroups).close(); }
@Test public void shouldReturnGroupCountsWithoutLimit() throws OperationException, IOException { final CountGroupsHandler handler = new CountGroupsHandler(); final Store store = mock(Store.class); final CountGroups countGroups = mock(CountGroups.class); final CloseableIterable elements = getElements(); final Context context = new Context(); given(countGroups.getLimit()).willReturn(null); given(countGroups.getInput()).willReturn(elements); final GroupCounts counts = handler.doOperation(countGroups, context, store); 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)); verify(countGroups).close(); }
@Test public void shouldReturnAllGroupCountsWhenLessThanLimit() throws OperationException, IOException { final CountGroupsHandler handler = new CountGroupsHandler(); final Store store = mock(Store.class); final CountGroups countGroups = mock(CountGroups.class); final CloseableIterable elements = getElements(); final Integer limit = 10; final Context context = new Context(); given(countGroups.getLimit()).willReturn(limit); given(countGroups.getInput()).willReturn(elements); final GroupCounts counts = handler.doOperation(countGroups, context, store); 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)); verify(countGroups).close(); }
@Test public void shouldReturnGroupCountsUpToLimit() throws OperationException, IOException { final CountGroupsHandler handler = new CountGroupsHandler(); final Store store = mock(Store.class); final CountGroups countGroups = mock(CountGroups.class); final CloseableIterable elements = getElements(); final Integer limit = 3; final Context context = new Context(); given(countGroups.getLimit()).willReturn(limit); given(countGroups.getInput()).willReturn(elements); final GroupCounts counts = handler.doOperation(countGroups, context, store); assertTrue(counts.isLimitHit()); assertEquals(2, counts.getEntityGroups().size()); assertEquals(2, (int) counts.getEntityGroups().get(GROUP1)); assertEquals(1, (int) counts.getEntityGroups().get(GROUP2)); verify(countGroups).close(); }