TransformHandler implements OutputOperationHandler<Transform, Iterable<? extends Element>> { @Override public Iterable<? extends Element> doOperation(final Transform operation, final Context context, final Store store) throws OperationException { return doOperation(operation, store.getSchema()); }  @Override Iterable<? extends Element> doOperation(final Transform operation, final Context context, final Store store); Iterable<? extends Element> doOperation(final Transform operation, final Schema schema);  }
@Test public void shouldTransformElementsUsingMockFunction() throws OperationException { final Function<String, Integer> function = mock(Function.class); given(function.apply(TestPropertyNames.STRING)).willReturn(6); given(store.getSchema()).willReturn(schema); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .property(TestPropertyNames.PROP_1, TestPropertyNames.STRING) .build(); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, TestPropertyNames.STRING) .property(TestPropertyNames.PROP_2, 3) .build(); final ElementTransformer transformer = new ElementTransformer.Builder() .select(TestPropertyNames.PROP_1) .execute(function) .project(TestPropertyNames.PROP_3) .build(); final Edge expectedEdge = new Edge.Builder() .group(TestGroups.EDGE) .property(TestPropertyNames.PROP_3, 6) .build(); final Entity expectedEntity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_3, 6) .property(TestPropertyNames.PROP_2, 3) .build(); final Transform transform = new Transform.Builder() .input(input) .edge(TestGroups.EDGE, transformer) .entity(TestGroups.ENTITY, transformer) .build(); input.add(edge); input.add(entity); expected.add(expectedEdge); expected.add(expectedEntity); final Iterable<? extends Element> results = handler.doOperation(transform, context, store); final List<Element> resultsList = Lists.newArrayList(results); boolean isSame = false; for (int i = 0; i < resultsList.size(); i++) { isSame = expected.get(i).getProperty(TestPropertyNames.PROP_3).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_3)); } assertTrue(isSame); }
@Test public void shouldTransformElementsUsingIdentityFunction() throws OperationException { given(store.getSchema()).willReturn(schema); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, TestPropertyNames.INT) .property(TestPropertyNames.PROP_2, TestPropertyNames.STRING) .build(); final Entity entity1 = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, TestPropertyNames.INT) .build(); final ElementTransformer transformer = new ElementTransformer.Builder() .select(TestPropertyNames.PROP_1) .execute(new Identity()) .project(TestPropertyNames.PROP_3) .build(); final Entity expectedEntity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_3, TestPropertyNames.INT) .property(TestPropertyNames.PROP_2, TestPropertyNames.STRING) .build(); final Entity expectedEntity1 = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_3, TestPropertyNames.INT) .build(); final Transform transform = new Transform.Builder() .input(input) .entity(TestGroups.ENTITY, transformer) .build(); input.add(entity); input.add(entity1); expected.add(expectedEntity); expected.add(expectedEntity1); final Iterable<? extends Element> results = handler.doOperation(transform, context, store); final List<Element> resultsList = Lists.newArrayList(results); boolean isSame = false; for (int i = 0; i < expected.size(); i++) { isSame = expected.get(i).getProperty(TestPropertyNames.PROP_3).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_3)); } assertTrue(isSame); }
@Test public void shouldTransformElementsUsingInlineFunction() throws OperationException { final Function<String, Integer> function = String::length; final Map<String, ElementTransformer> entities = new HashMap<>(); given(store.getSchema()).willReturn(schema); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, "value") .property(TestPropertyNames.PROP_2, 1) .build(); final Entity entity1 = new Entity.Builder() .group(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_1, "otherValue") .property(TestPropertyNames.PROP_2, 3) .build(); final ElementTransformer transformer = new ElementTransformer.Builder() .select(TestPropertyNames.PROP_1) .execute(function) .project(TestPropertyNames.PROP_3) .build(); entities.put(TestGroups.ENTITY, transformer); entities.put(TestGroups.ENTITY_2, transformer); final Entity expectedEntity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_3, 5) .property(TestPropertyNames.PROP_2, 1) .build(); final Entity expectedEntity1 = new Entity.Builder() .group(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_3, 10) .property(TestPropertyNames.PROP_2, 3) .build(); final Transform transform = new Transform.Builder() .input(input) .entities(entities) .build(); input.add(entity); input.add(entity1); expected.add(expectedEntity); expected.add(expectedEntity1); final Iterable<? extends Element> results = handler.doOperation(transform, context, store); final List<Element> resultsList = Lists.newArrayList(results); boolean isSame = false; for (int i = 0; i < resultsList.size(); i++) { isSame = expected.get(i).getProperty(TestPropertyNames.PROP_3).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_3)); } assertTrue(isSame); }
@Test public void shouldApplyDifferentTransformersToDifferentGroups() throws OperationException { final Function<String, Integer> function = String::length; final Function<String, String> function1 = String::toUpperCase; final Map<String, ElementTransformer> edges = new HashMap<>(); given(store.getSchema()).willReturn(schema); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .property(TestPropertyNames.PROP_1, "testValue") .property(TestPropertyNames.PROP_2, 5) .build(); final Edge edge1 = new Edge.Builder() .group(TestGroups.EDGE_2) .property(TestPropertyNames.PROP_2, "otherValue") .property(TestPropertyNames.PROP_3, 2L) .build(); final ElementTransformer elementTransformer = new ElementTransformer.Builder() .select(TestPropertyNames.PROP_1) .execute(function) .project(TestPropertyNames.PROP_4) .build(); final ElementTransformer elementTransformer1 = new ElementTransformer.Builder() .select(TestPropertyNames.PROP_2) .execute(function1) .project(TestPropertyNames.PROP_4) .build(); final Edge expectedEdge = new Edge.Builder() .group(TestGroups.EDGE) .property(TestPropertyNames.PROP_4, 9) .property(TestPropertyNames.PROP_2, 5) .build(); final Edge expectedEdge1 = new Edge.Builder() .group(TestGroups.EDGE_2) .property(TestPropertyNames.PROP_4, "OTHERVALUE") .property(TestPropertyNames.PROP_3, 2L) .build(); edges.put(TestGroups.EDGE, elementTransformer); edges.put(TestGroups.EDGE_2, elementTransformer1); input.add(edge); input.add(edge1); expected.add(expectedEdge); expected.add(expectedEdge1); final Transform transform = new Transform.Builder() .input(input) .edges(edges) .build(); final Iterable<? extends Element> results = handler.doOperation(transform, context, store); final List<Element> resultsList = Lists.newArrayList(results); boolean isSame = false; for (int i = 0; i < resultsList.size(); i++) { isSame = expected.get(i).getProperty(TestPropertyNames.PROP_4).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_4)); } assertTrue(isSame); }
@Test public void shouldFailValidationWhenSchemaElementDefinitionsAreNull() { given(store.getSchema()).willReturn(new Schema()); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, TestPropertyNames.INT) .property(TestPropertyNames.PROP_2, TestPropertyNames.STRING) .build(); final Entity entity1 = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, TestPropertyNames.INT) .build(); final ElementTransformer transformer = new ElementTransformer.Builder() .select(TestPropertyNames.PROP_1) .execute(new Identity()) .project(TestPropertyNames.PROP_3) .build(); input.add(entity); input.add(entity1); final Transform transform = new Transform.Builder() .input(input) .entity(TestGroups.ENTITY, transformer) .build(); try { final Iterable<? extends Element> results = handler.doOperation(transform, context, store); fail("Exception expected"); } catch (final OperationException e) { assertTrue(e.getMessage().contains("Entity group: " + TestGroups.ENTITY + " does not exist in the schema.")); } }
@Test public void shouldFailValidationWhenElementTransformerOperationIsNull() { given(store.getSchema()).willReturn(schema); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, TestPropertyNames.INT) .property(TestPropertyNames.PROP_2, TestPropertyNames.STRING) .build(); final Entity entity1 = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, TestPropertyNames.INT) .build(); final ElementTransformer transformer = new ElementTransformer.Builder() .select(TestPropertyNames.PROP_1) .execute(null) .project(TestPropertyNames.PROP_3) .build(); input.add(entity); input.add(entity1); final Transform transform = new Transform.Builder() .input(input) .entity(TestGroups.ENTITY, transformer) .build(); try { final Iterable<? extends Element> results = handler.doOperation(transform, context, store); fail("Exception expected"); } catch (final OperationException e) { assertTrue(e.getMessage().contains(transformer.getClass().getSimpleName() + " contains a null function.")); } }
@Test public void shouldFailValidationWhenFunctionSignatureIsInvalid() { final Schema schema = new Schema.Builder() .entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder() .property(TestPropertyNames.PROP_1, TestPropertyNames.STRING) .build()) .type(TestPropertyNames.STRING, new TypeDefinition(String.class)) .build(); given(store.getSchema()).willReturn(schema); final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, TestPropertyNames.INT) .property(TestPropertyNames.PROP_2, TestPropertyNames.STRING) .build(); final Entity entity1 = new Entity.Builder() .group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, TestPropertyNames.INT) .build(); final ElementTransformer transformer = new ElementTransformer.Builder() .select(TestPropertyNames.PROP_1) .execute(new Divide()) .project(TestPropertyNames.PROP_3) .build(); input.add(entity); input.add(entity1); final Transform transform = new Transform.Builder() .input(input) .entity(TestGroups.ENTITY, transformer) .build(); try { final Iterable<? extends Element> results = handler.doOperation(transform, context, store); fail("Exception expected"); } catch (final OperationException e) { assertTrue(e.getMessage().contains("Incompatible number of types"), e.getMessage()); } }
@Test public void shouldSelectMatchedVertexForTransform() throws OperationException { given(store.getSchema()).willReturn(schema); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal") .dest("destVal") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build(); final Transform transform = new Transform.Builder() .input(edge) .edge(TestGroups.EDGE, new ElementTransformer.Builder() .select(IdentifierType.MATCHED_VERTEX.name()) .execute(new Identity()) .project(TestPropertyNames.PROP_3) .build()) .build(); final Iterable<? extends Element> results = handler.doOperation(transform, context, store); final Edge expectedEdge = new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal") .dest("destVal") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .property(TestPropertyNames.PROP_3, "srcVal") .build(); ElementUtil.assertElementEquals(Collections.singletonList(expectedEdge), results); }
@Test public void shouldSelectAdjacentMatchedVertexForTransform() throws OperationException { given(store.getSchema()).willReturn(schema); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal") .dest("destVal") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build(); final Transform transform = new Transform.Builder() .input(edge) .edge(TestGroups.EDGE, new ElementTransformer.Builder() .select(IdentifierType.ADJACENT_MATCHED_VERTEX.name()) .execute(new Identity()) .project(TestPropertyNames.PROP_3) .build()) .build(); final Iterable<? extends Element> results = handler.doOperation(transform, context, store); final Edge expectedEdge = new Edge.Builder() .group(TestGroups.EDGE) .source("srcVal") .dest("destVal") .matchedVertex(EdgeId.MatchedVertex.SOURCE) .property(TestPropertyNames.PROP_3, "destVal") .build(); ElementUtil.assertElementEquals(Collections.singletonList(expectedEdge), results); }