FilterHandler implements OutputOperationHandler<Filter, Iterable<? extends Element>> { @Override public Iterable<? extends Element> doOperation(final Filter operation, final Context context, final Store store) throws OperationException { return doOperation(operation, store.getSchema()); }  @Override Iterable<? extends Element> doOperation(final Filter operation, final Context context, final Store store); Iterable<? extends Element> doOperation(final Filter operation, final Schema schema);  }
@Test public void shouldFilterByGroup() throws OperationException { given(store.getSchema()).willReturn(SCHEMA); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE_2) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 1L) .build(); final Edge edge2 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionB") .dest("junctionA") .directed(true) .property(TestPropertyNames.COUNT, 4L) .build(); final Edge edge3 = new Edge.Builder() .group(TestGroups.EDGE_3) .source("junctionC") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 3L) .build(); input.add(edge); input.add(edge1); input.add(edge2); input.add(edge3); expected.add(edge1); final Filter filter = new Filter.Builder() .input(input) .edge(TestGroups.EDGE_2) .build(); final Iterable<? extends Element> results = handler.doOperation(filter, context, store); final List<Element> resultsList = Lists.newArrayList(results); assertEquals(expected, resultsList); }
@Test public void shouldFilterInputBasedOnGroupAndCount() throws OperationException { given(store.getSchema()).willReturn(SCHEMA); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 1L) .build(); final Edge edge2 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionB") .dest("junctionA") .directed(true) .property(TestPropertyNames.COUNT, 4L) .build(); final Edge edge3 = new Edge.Builder() .group(TestGroups.EDGE_2) .source("junctionC") .dest("junctionD") .directed(true) .property(TestPropertyNames.COUNT, 3L) .build(); input.add(edge); input.add(edge1); input.add(edge2); input.add(edge3); expected.add(edge); expected.add(edge2); final Filter filter = new Filter.Builder() .input(input) .edge(TestGroups.EDGE, new ElementFilter.Builder() .select(TestPropertyNames.COUNT) .execute(new IsMoreThan(1L)) .build()) .build(); final Iterable<? extends Element> result = handler.doOperation(filter, context, store); final List<Element> resultList = Streams.toStream(result).collect(Collectors.toList()); assertEquals(expected, resultList); }
@Test public void shouldReturnAllValuesWithNullElementFilters() throws OperationException { given(store.getSchema()).willReturn(SCHEMA); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 1L) .build(); final Edge edge2 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionB") .dest("junctionA") .directed(true) .property(TestPropertyNames.COUNT, 4L) .build(); input.add(edge); input.add(edge1); input.add(edge2); expected.add(edge); expected.add(edge1); expected.add(edge2); final Filter filter = new Filter.Builder() .input(input) .build(); final Iterable<? extends Element> results = handler.doOperation(filter, context, store); final List<Element> resultsList = Lists.newArrayList(results); assertEquals(expected, resultsList); }
@Test public void shouldApplyGlobalFilterAndReturnOnlySpecifiedEdges() throws OperationException { given(store.getSchema()).willReturn(SCHEMA); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE_2) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 1L) .build(); final Edge edge2 = new Edge.Builder() .group(TestGroups.EDGE_2) .source("junctionB") .dest("junctionA") .directed(true) .property(TestPropertyNames.COUNT, 4L) .build(); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.COUNT, 3L) .build(); final Entity entity1 = new Entity.Builder() .group(TestGroups.ENTITY_2) .property(TestPropertyNames.COUNT, 4L) .build(); input.add(edge); input.add(edge1); input.add(edge2); input.add(entity); input.add(entity1); expected.add(edge2); final Filter filter = new Filter.Builder() .input(input) .globalElements(new ElementFilter.Builder() .select(TestPropertyNames.COUNT) .execute(new IsMoreThan(2L)) .build()) .edge(TestGroups.EDGE_2) .build(); final Iterable<? extends Element> results = handler.doOperation(filter, context, store); final List<Element> resultsList = Lists.newArrayList(results); assertEquals(expected, resultsList); }
@Test public void shouldFilterEntitiesAndEdges() throws OperationException { given(store.getSchema()).willReturn(SCHEMA); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE_2) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 1L) .build(); final Edge edge2 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionB") .dest("junctionA") .directed(true) .property(TestPropertyNames.COUNT, 4L) .build(); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.COUNT, 3L) .build(); final Entity entity1 = new Entity.Builder() .group(TestGroups.ENTITY_2) .property(TestPropertyNames.COUNT, 4L) .build(); input.add(edge); input.add(edge1); input.add(edge2); input.add(entity); input.add(entity1); expected.add(edge2); expected.add(entity); expected.add(entity1); final Filter filter = new Filter.Builder() .input(input) .globalElements(new ElementFilter.Builder() .select(TestPropertyNames.COUNT) .execute(new IsMoreThan(2L)) .build()) .build(); final Iterable<? extends Element> results = handler.doOperation(filter, context, store); final List<Element> resultsList = Lists.newArrayList(results); assertEquals(expected, resultsList); }
@Test public void shouldHandleComplexFiltering() throws OperationException { given(store.getSchema()).willReturn(SCHEMA); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE_2) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 1L) .build(); final Edge edge2 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionB") .dest("junctionA") .directed(true) .property(TestPropertyNames.COUNT, 4L) .build(); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.COUNT, 3L) .build(); final Entity entity1 = new Entity.Builder() .group(TestGroups.ENTITY_2) .property(TestPropertyNames.COUNT, 4L) .build(); final Entity entity2 = new Entity.Builder() .group(TestGroups.ENTITY_3) .property(TestPropertyNames.COUNT, 6L) .build(); final Filter filter = new Filter.Builder() .input(input) .globalElements(new ElementFilter.Builder() .select(TestPropertyNames.COUNT) .execute(new IsMoreThan(1L)) .build()) .edge(TestGroups.EDGE, new ElementFilter.Builder() .select(TestPropertyNames.COUNT) .execute(new IsMoreThan(2L)) .build()) .entity(TestGroups.ENTITY_2) .build(); input.add(edge); input.add(edge1); input.add(edge2); input.add(entity); input.add(entity1); input.add(entity2); expected.add(edge2); expected.add(entity1); final Iterable<? extends Element> results = handler.doOperation(filter, context, store); final List<Element> resultsList = Lists.newArrayList(results); assertEquals(expected, resultsList); }
@Test public void shouldThrowErrorForNullInput() { given(store.getSchema()).willReturn(SCHEMA); final Filter filter = new Filter.Builder() .globalElements(new ElementFilter()) .build(); try { handler.doOperation(filter, context, store); fail("Exception expected"); } catch (OperationException e) { assertEquals("Filter operation has null iterable of elements", e.getMessage()); } }
@Test public void shouldReturnNoResultsWhenGlobalElementsFails() throws OperationException { given(store.getSchema()).willReturn(SCHEMA); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE_2) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 1L) .build(); final Edge edge2 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionB") .dest("junctionA") .directed(true) .property(TestPropertyNames.COUNT, 4L) .build(); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.COUNT, 3L) .build(); final Entity entity1 = new Entity.Builder() .group(TestGroups.ENTITY_2) .property(TestPropertyNames.COUNT, 4L) .build(); final Entity entity2 = new Entity.Builder() .group(TestGroups.ENTITY_3) .property(TestPropertyNames.COUNT, 6L) .build(); input.add(edge); input.add(edge1); input.add(edge2); input.add(entity); input.add(entity1); input.add(entity2); final Filter filter = new Filter.Builder() .input(input) .globalElements(new ElementFilter.Builder() .select(TestPropertyNames.COUNT) .execute(new IsMoreThan(10L)) .build()) .globalEdges(new ElementFilter.Builder() .select(TestPropertyNames.COUNT) .execute(new IsMoreThan(2L)) .build()) .build(); final Iterable<? extends Element> results = handler.doOperation(filter, context, store); final List<Element> resultsList = Lists.newArrayList(results); assertEquals(expected, resultsList); }
@Test public void shouldFailValidationWhenSchemaElementDefinitionsAreNull() { given(store.getSchema()).willReturn(new Schema()); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 1L) .build(); input.add(edge); input.add(edge1); final Filter filter = new Filter.Builder() .input(input) .edge(TestGroups.EDGE) .build(); try { final Iterable<? extends Element> results = handler.doOperation(filter, context, store); fail("Exception expected"); } catch (final OperationException e) { assertTrue(e.getMessage().contains("Edge group: " + TestGroups.EDGE + " does not exist in the schema")); } }
@Test public void shouldFailValidationWhenElementFilterOperationIsNull() { given(store.getSchema()).willReturn(SCHEMA); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .directed(true) .property(TestPropertyNames.COUNT, 1L) .build(); input.add(edge); input.add(edge1); final Filter filter = new Filter.Builder() .input(input) .edge(TestGroups.EDGE, new ElementFilter.Builder() .select(TestPropertyNames.COUNT) .execute(null) .build()) .build(); try { final Iterable<? extends Element> results = handler.doOperation(filter, context, store); fail("Exception expected"); } catch (final OperationException e) { assertTrue(e.getMessage().contains(filter.getClass().getSimpleName() + " contains a null function.")); } }
@Test public void shouldFailValidationWhenTypeArgumentOfPredicateIsIncorrect() { final Schema schema = new Schema.Builder() .edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder() .source("junctionA") .destination("junctionB") .property(TestPropertyNames.COUNT, "count.long") .build()) .edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder() .source("junctionA") .destination("junctionB") .property(TestPropertyNames.COUNT, "count.long") .build()) .type("count.long", new TypeDefinition(Long.class)) .build(); given(store.getSchema()).willReturn(schema); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .property(TestPropertyNames.COUNT, 2L) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE) .source("junctionA") .dest("junctionB") .property(TestPropertyNames.COUNT, 1L) .build(); input.add(edge); input.add(edge1); final Filter filter = new Filter.Builder() .input(input) .globalEdges(new ElementFilter.Builder() .select(TestPropertyNames.COUNT) .execute(new IsMoreThan("abcd")) .build()) .build(); try { final Iterable<? extends Element> results = handler.doOperation(filter, context, store); fail("Exception expected"); } catch (final OperationException e) { assertTrue(e.getMessage().contains("is not compatible with the input type:")); } }
@Test public void shouldFilterBasedOnMatchedVertex() throws OperationException { final Schema schema = new Schema.Builder() .edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder() .source("vertex") .destination("vertex") .property(TestPropertyNames.COUNT, "count.long") .build()) .type("vertex", new TypeDefinition(String.class)) .type("count.long", new TypeDefinition(Long.class)) .build(); given(store.getSchema()).willReturn(schema); final Filter filter = new Filter.Builder() .input( new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal1") .dest("destVal1") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build(), new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal2") .dest("destVal2") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build(), new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal3") .dest("destVal3") .matchedVertex(EdgeId.MatchedVertex.DESTINATION) .build(), new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal4") .dest("destVal4") .matchedVertex(EdgeId.MatchedVertex.DESTINATION) .build() ) .edge(TestGroups.EDGE, new ElementFilter.Builder() .select(IdentifierType.MATCHED_VERTEX.name()) .execute(new IsIn("srcVal1", "destVal3")) .build()) .build(); final Iterable<? extends Element> results = handler.doOperation(filter, context, store); ElementUtil.assertElementEquals( Arrays.asList(new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal1") .dest("destVal1") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build(), new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal3") .dest("destVal3") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build()), results); }
@Test public void shouldFilterBasedOnAdjacentMatchedVertex() throws OperationException { final Schema schema = new Schema.Builder() .edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder() .source("vertex") .destination("vertex") .property(TestPropertyNames.COUNT, "count.long") .build()) .type("vertex", new TypeDefinition(String.class)) .type("count.long", new TypeDefinition(Long.class)) .build(); given(store.getSchema()).willReturn(schema); final Filter filter = new Filter.Builder() .input( new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal1") .dest("destVal1") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build(), new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal2") .dest("destVal2") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build(), new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal3") .dest("destVal3") .matchedVertex(EdgeId.MatchedVertex.DESTINATION) .build(), new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal4") .dest("destVal4") .matchedVertex(EdgeId.MatchedVertex.DESTINATION) .build() ) .edge(TestGroups.EDGE, new ElementFilter.Builder() .select(IdentifierType.ADJACENT_MATCHED_VERTEX.name()) .execute(new IsIn("destVal1", "srcVal3")) .build()) .build(); final Iterable<? extends Element> results = handler.doOperation(filter, context, store); ElementUtil.assertElementEquals( Arrays.asList(new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal1") .dest("destVal1") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build(), new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal3") .dest("destVal3") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build()), results); }