org.apache.tinkerpop.gremlin.structure.io.GraphWriter Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.structure.io.GraphWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@org.junit.Ignore("https://issues.apache.org/jira/browse/TINKERPOP-1509")
public void deserializersTestsTree() {
    final TinkerGraph tg = TinkerFactory.createModern();

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final Tree t = tg.traversal().V().out().out().tree().next();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, t);
        final String json = out.toString();

        final Tree treeRead = (Tree)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        //Map's equals should check each component of the tree recursively
        //on each it will call "equals()" which for Vertices will compare ids, which
        //is ok. Complete vertex deser is checked elsewhere.
        assertEquals(t, treeRead);

    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #2
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsVertex() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    v.property("born", LocalDateTime.of(1971, 1, 2, 20, 50));
    v.property("dead", LocalDateTime.of(1971, 1, 7, 20, 50));

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, v);
        final String json = out.toString();

        // Object works, because there's a type in the payload now
        // Vertex.class would work as well
        // Anything else would not because we check the type in param here with what's in the JSON, for safety.
        final Vertex vRead = (Vertex)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertEquals(v, vRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #3
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsEdge() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final Vertex v2 = tg.addVertex("vertexTest");

    final Edge ed = v.addEdge("knows", v2, "time", LocalDateTime.now());

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, ed);
        final String json = out.toString();

        // Object works, because there's a type in the payload now
        // Edge.class would work as well
        // Anything else would not because we check the type in param here with what's in the JSON, for safety.
        final Edge eRead = (Edge)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertEquals(ed, eRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #4
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final Graph sampleGraph1 = TinkerFactory.createModern();

    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101,
            "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #5
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    final  Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101L);
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #6
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);

    sampleGraph1.addVertex(T.id, 100, "name", "kevin", "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #7
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    sampleGraph1.addVertex(T.id, 100L, "name", "kevin");
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #8
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Thorough types verification for Vertex ids, Vertex props, Edge ids, Edge props
 */
@Test
public void shouldDeserializeGraphSONIntoTinkerGraphKeepingTypes() throws IOException {
    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final Graph sampleGraph1 = TinkerFactory.createModern();
    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin", "theUUID", UUID.randomUUID());
    final Vertex v2 = sampleGraph1.addVertex(T.id, 101L, "name", "henri", "theUUID", UUID.randomUUID());
    v1.addEdge("hello", v2, T.id, 101L,
            "uuid", UUID.randomUUID());

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, sampleGraph1);
        final String json = out.toString();

        final TinkerGraph read = reader.readObject(new ByteArrayInputStream(json.getBytes()), TinkerGraph.class);
        assertTrue(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #9
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsTinkerGraph() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final Vertex v2 = tg.addVertex("vertexTest");

    v.addEdge("knows", v2);

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, tg);
        final String json = out.toString();

        final Graph gRead = (Graph)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertTrue(approximateGraphsCheck(tg, gRead));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #10
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsVertexProperty() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final VertexProperty prop = v.property("born", LocalDateTime.of(1971, 1, 2, 20, 50));

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, prop);
        final String json = out.toString();

        final VertexProperty vPropRead = (VertexProperty)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        //only classes and ids are checked, that's ok, full vertex property ser/de
        //is checked elsewhere.
        assertEquals(prop, vPropRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #11
Source File: GraphSONMessageSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldToStringUnknownObjects() {
    GraphSONMapper gm20 = GraphSONMapper.build().version(GraphSONVersion.V2_0).create();
    GraphSONMapper gm10 = GraphSONMapper.build().version(GraphSONVersion.V1_0).create();

    GraphWriter writer = GraphSONWriter.build().mapper(gm20).create();
    // subsequent creations of GraphWriters and GraphSONMappers should not affect
    // each other.
    GraphWriter writer2 = GraphSONWriter.build().mapper(gm10).create();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        writer.writeObject(baos, new FunObject("value"));
        assertEquals(baos.toString(), "\"value\"");
    } catch (Exception e) {
        fail("should have succeeded serializing the unknown object to a string");
    }
}
 
Example #12
Source File: AbstractEntityGraphFormatConsumer.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Override
protected void processGraph(String documentSourceName, Graph graph) {
  WriterBuilder<? extends GraphWriter> writer;
  switch (format) {
    case GRYO:
      writer = graph.io(IoCore.gryo()).writer();
      break;
    case GRAPHSON:
      writer = graph.io(IoCore.graphson()).writer();
      break;
    case GRAPHML:
      // FALL THROUGH
    default:
      writer = graph.io(IoCore.graphml()).writer().normalize(true);
      break;
  }

  try (final OutputStream os = createOutputStream(documentSourceName)) {
    writer.create().writeGraph(os, graph);
  } catch (IOException e) {
    getMonitor().error("Error writing graph", e);
  }
}
 
Example #13
Source File: AbstractDocumentGraphFormatConsumer.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Override
protected void processGraph(String documentSourceName, Graph graph) {
  WriterBuilder<? extends GraphWriter> writer;
  switch (format) {
    case GRYO:
      writer = graph.io(IoCore.gryo()).writer();
      break;
    case GRAPHSON:
      writer = graph.io(IoCore.graphson()).writer();
      break;
    case GRAPHML:
      // FALL THROUGH
    default:
      writer = graph.io(IoCore.graphml()).writer().normalize(true);
      break;
  }

  try (final OutputStream os = createOutputStream(documentSourceName)) {
    writer.create().writeGraph(os, graph);
  } catch (IOException e) {
    getMonitor().error("Error writing graph", e);
  }
}
 
Example #14
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsTraversalMetrics() {
    final TinkerGraph tg = TinkerFactory.createModern();

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final TraversalMetrics tm = tg.traversal().V(1).as("a").has("name").as("b").
            out("knows").out("created").as("c").
            has("name", "ripple").values("name").as("d").
            identity().as("e").profile().next();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, tm);
        final String json = out.toString();

        final TraversalMetrics traversalMetricsRead = (TraversalMetrics)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        // toString should be enough to compare TraversalMetrics
        assertTrue(tm.toString().equals(traversalMetricsRead.toString()));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #15
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
@org.junit.Ignore("https://issues.apache.org/jira/browse/TINKERPOP-1509")
public void deserializersTestsTree() {
    final TinkerGraph tg = TinkerFactory.createModern();

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final Tree t = tg.traversal().V().out().out().tree().next();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, t);
        final String json = out.toString();

        final Tree treeRead = (Tree)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        //Map's equals should check each component of the tree recursively
        //on each it will call "equals()" which for Vertices will compare ids, which
        //is ok. Complete vertex deser is checked elsewhere.
        assertEquals(t, treeRead);

    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #16
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Thorough types verification for Vertex ids, Vertex props, Edge ids, Edge props
 */
@Test
public void shouldDeserializeGraphSONIntoTinkerGraphKeepingTypes() throws IOException {
    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final Graph sampleGraph1 = TinkerFactory.createModern();
    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin", "theUUID", UUID.randomUUID());
    final Vertex v2 = sampleGraph1.addVertex(T.id, 101L, "name", "henri", "theUUID", UUID.randomUUID());
    v1.addEdge("hello", v2, T.id, 101L,
            "uuid", UUID.randomUUID());

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, sampleGraph1);
        final String json = out.toString();

        final TinkerGraph read = reader.readObject(new ByteArrayInputStream(json.getBytes()), TinkerGraph.class);
        assertTrue(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #17
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    sampleGraph1.addVertex(T.id, 100L, "name", "kevin");
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #18
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsVertexProperty() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final VertexProperty prop = v.property("born", LocalDateTime.of(1971, 1, 2, 20, 50));

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, prop);
        final String json = out.toString();

        final VertexProperty vPropRead = (VertexProperty)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        //only classes and ids are checked, that's ok, full vertex property ser/de
        //is checked elsewhere.
        assertEquals(prop, vPropRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #19
Source File: TestIoAgain.java    From sqlg with MIT License 6 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
    return Arrays.asList(new Object[][]{
            {"graphson-v1", false, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().create()},
            {"graphson-v1-embedded", true, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()},
            {"graphson-v2", false, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create()},
            {"graphson-v2-embedded", true, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()},
            {"graphson-v3", true, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create()},
            {"gryo-v1", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().create()},
            {"gryo-v3", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().create()}
    });
}
 
Example #20
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsTinkerGraph() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final Vertex v2 = tg.addVertex("vertexTest");

    v.addEdge("knows", v2);

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, tg);
        final String json = out.toString();

        final Graph gRead = (Graph)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertTrue(approximateGraphsCheck(tg, gRead));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #21
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsEdge() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final Vertex v2 = tg.addVertex("vertexTest");

    final Edge ed = v.addEdge("knows", v2, "time", LocalDateTime.now());

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, ed);
        final String json = out.toString();

        // Object works, because there's a type in the payload now
        // Edge.class would work as well
        // Anything else would not because we check the type in param here with what's in the JSON, for safety.
        final Edge eRead = (Edge)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertEquals(ed, eRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #22
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsTraversalMetrics() {
    final TinkerGraph tg = TinkerFactory.createModern();

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final TraversalMetrics tm = tg.traversal().V(1).as("a").has("name").as("b").
            out("knows").out("created").as("c").
            has("name", "ripple").values("name").as("d").
            identity().as("e").profile().next();

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, tm);
        final String json = out.toString();

        final TraversalMetrics traversalMetricsRead = (TraversalMetrics)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        // toString should be enough to compare TraversalMetrics
        assertTrue(tm.toString().equals(traversalMetricsRead.toString()));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #23
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializersTestsVertex() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    v.property("born", LocalDateTime.of(1971, 1, 2, 20, 50));
    v.property("dead", LocalDateTime.of(1971, 1, 7, 20, 50));

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, v);
        final String json = out.toString();

        // Object works, because there's a type in the payload now
        // Vertex.class would work as well
        // Anything else would not because we check the type in param here with what's in the JSON, for safety.
        final Vertex vRead = (Vertex)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        assertEquals(v, vRead);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #24
Source File: TestIoEdge.java    From sqlg with MIT License 6 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
    return Arrays.asList(new Object[][]{
            {"graphson-v1", false, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().create()},
            {"graphson-v1-embedded", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()},
            {"graphson-v2", false, false,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.NO_TYPES).create()).create()},
            {"graphson-v2-embedded", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()},
            {"graphson-v3", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().create()).create()},
            {"gryo-v1", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().create()},
            {"gryo-v3", true, true,
                    (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().create(),
                    (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().create()}
    });
}
 
Example #25
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final Graph sampleGraph1 = TinkerFactory.createModern();

    final Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101,
            "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #26
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Edge ids.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForEdgeIds() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);
    final  Vertex v1 = sampleGraph1.addVertex(T.id, 100, "name", "kevin");
    v1.addEdge("hello", sampleGraph1.traversal().V().has("name", "marko").next(), T.id, 101L);
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized edge Id.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #27
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts the approximateGraphsChecks function fails when expected. Vertex props.
 */
@Test
public void shouldLoseTypesWithGraphSONNoTypesForVertexProps() throws IOException {
    final GraphWriter writer = getWriter(noTypesMapperV2d0);
    final GraphReader reader = getReader(noTypesMapperV2d0);
    final TinkerGraph sampleGraph1 = TinkerGraph.open();
    TinkerFactory.generateModern(sampleGraph1);

    sampleGraph1.addVertex(T.id, 100, "name", "kevin", "uuid", UUID.randomUUID());
    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeGraph(out, sampleGraph1);
        final String json = out.toString();
        final TinkerGraph read = TinkerGraph.open();
        reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
        // Should fail on deserialized vertex prop.
        assertFalse(approximateGraphsCheck(sampleGraph1, read));
    }
}
 
Example #28
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void deserializersTestsProperty() {
    final TinkerGraph tg = TinkerGraph.open();

    final Vertex v = tg.addVertex("vertexTest");
    final Vertex v2 = tg.addVertex("vertexTest");

    final Edge ed = v.addEdge("knows", v2);

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final Property prop = ed.property("since", Year.parse("1993"));

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, prop);
        final String json = out.toString();

        final Property pRead = (Property)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        //can't use equals here, because pRead is detached, its parent element has not been intentionally
        //serialized and "equals()" checks that.
        assertTrue(prop.key().equals(pRead.key()) && prop.value().equals(pRead.value()));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}
 
Example #29
Source File: TestIoEdge.java    From sqlg with MIT License 5 votes vote down vote up
private void assertEdge(final Vertex v1, final Vertex v2, final Edge e, final boolean assertProperties) throws IOException {
    try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        final GraphWriter writer = writerMaker.apply(this.sqlgGraph);
        writer.writeEdge(os, e);

        final AtomicBoolean called = new AtomicBoolean(false);
        final GraphReader reader = readerMaker.apply(this.sqlgGraph);
        try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
            reader.readEdge(bais, edge -> {
                final Edge detachedEdge = (Edge) edge;
                Assert.assertEquals(e.id(), assertIdDirectly ? detachedEdge.id() : this.sqlgGraph.edges(detachedEdge.id().toString()).next().id());
                Assert.assertEquals(v1.id(), assertIdDirectly ? detachedEdge.outVertex().id() : this.sqlgGraph.vertices(detachedEdge.outVertex().id().toString()).next().id());
                Assert.assertEquals(v2.id(), assertIdDirectly ? detachedEdge.inVertex().id() : this.sqlgGraph.vertices(detachedEdge.inVertex().id().toString()).next().id());
                Assert.assertEquals(v1.label(), detachedEdge.outVertex().label());
                Assert.assertEquals(v2.label(), detachedEdge.inVertex().label());
                Assert.assertEquals(e.label(), detachedEdge.label());

                if (assertProperties) {
                    Assert.assertEquals(assertDouble ? 0.5d : 0.5f, e.properties("weight").next().value());
                    Assert.assertEquals("rw", e.properties("acl").next().value());
                } else {
                    Assert.assertEquals(e.keys().size(), IteratorUtils.count(detachedEdge.properties()));
                }

                called.set(true);
                return null;
            });
        }

        Assert.assertTrue(called.get());
    }
}
 
Example #30
Source File: TinkerGraphGraphSONSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void deserializersTestsMetrics() {
    final TinkerGraph tg = TinkerFactory.createModern();

    final GraphWriter writer = getWriter(defaultMapperV2d0);
    final GraphReader reader = getReader(defaultMapperV2d0);

    final TraversalMetrics tm = tg.traversal().V(1).as("a").has("name").as("b").
            out("knows").out("created").as("c").
            has("name", "ripple").values("name").as("d").
            identity().as("e").profile().next();

    final MutableMetrics m = new MutableMetrics(tm.getMetrics(0));
    // making sure nested metrics are included in serde
    m.addNested(new MutableMetrics(tm.getMetrics(1)));

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writer.writeObject(out, m);
        final String json = out.toString();

        final Metrics metricsRead = (Metrics)reader.readObject(new ByteArrayInputStream(json.getBytes()), Object.class);
        // toString should be enough to compare Metrics
        assertTrue(m.toString().equals(metricsRead.toString()));
    } catch (IOException e) {
        e.printStackTrace();
        fail("Should not have thrown exception: " + e.getMessage());
    }
}