private void testSerialization(Function<Geoshape,Geoshape> makeGeoshape) throws Exception { if (makeGeoshape != null) { addGeoshape(makeGeoshape); } GraphWriter writer = writerMaker.apply(graph); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); writer.writeGraph(outputStream, graph); clearGraph(config); open(config); GraphReader reader = readerMaker.apply(graph); ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); reader.readGraph(inputStream, graph); JanusGraphIndexTest.assertGraphOfTheGods(graph); if (makeGeoshape != null) { assertGeoshape(makeGeoshape); } }
@Test public void testGeoShapeSerializationReadWriteAsGraphSONEmbedded() throws Exception { GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true); GraphSONMapper m = graph.io(IoCore.graphson()).mapper().embedTypes(true).create(); GraphWriter writer = graph.io(IoCore.graphson()).writer().mapper(m).create(); FileOutputStream fos = new FileOutputStream("/tmp/test.json"); writer.writeGraph(fos, graph); clearGraph(config); open(config); GraphReader reader = graph.io(IoCore.graphson()).reader().mapper(m).create(); FileInputStream fis = new FileInputStream("/tmp/test.json"); reader.readGraph(fis, graph); TitanIndexTest.assertGraphOfTheGods(graph); }
protected Traverser.Admin<S> read(final File file) { try (final InputStream stream = new FileInputStream(file)) { final Graph graph = (Graph) this.traversal.getGraph().get(); constructReader().readGraph(stream, graph); return EmptyTraverser.instance(); } catch (IOException ioe) { throw new IllegalStateException(String.format("Could not read file %s into graph", this.file), ioe); } }
private static void readGraphMLIntoGraph(final Graph g, final String file) throws IOException { final GraphReader reader = GraphMLReader.build().create(); try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + file)) { reader.readGraph(stream, g); } }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES) public void shouldTransformGraphMLV2ToV3ViaXSLT() throws Exception { final InputStream stylesheet = Thread.currentThread().getContextClassLoader().getResourceAsStream("tp2-to-tp3-graphml.xslt"); final InputStream datafile = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "tinkerpop-classic-tp2.xml"); final ByteArrayOutputStream output = new ByteArrayOutputStream(); final TransformerFactory tFactory = TransformerFactory.newInstance(); final StreamSource stylesource = new StreamSource(stylesheet); final Transformer transformer = tFactory.newTransformer(stylesource); final StreamSource source = new StreamSource(datafile); final StreamResult result = new StreamResult(output); transformer.transform(source, result); final GraphReader reader = GraphMLReader.build().create(); reader.readGraph(new ByteArrayInputStream(output.toByteArray()), graph); assertClassicGraph(graph, false, true); }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_REMOVE_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_VERTICES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_STRING_VALUES) public void shouldReadGraphMLWithCommonVertexAndEdgePropertyNames() throws IOException { final GraphReader reader = GraphMLReader.build().create(); try (final InputStream stream = new ByteArrayInputStream(CLASSIC_GRAPH_WITH_COLOR.getBytes("UTF-8"))) { reader.readGraph(stream, graph); } // there is also a "color" property on this dataset that is on both edges and vertices graph.vertices().forEachRemaining(v -> assertEquals("#6495ed", v.value("color"))); graph.edges().forEachRemaining(e -> assertEquals("#ee0000", e.value("color"))); final GraphWriter writer = GraphMLWriter.build().create(); try (final OutputStream out = new ByteArrayOutputStream()) { writer.writeGraph(out, graph); graph.vertices().forEachRemaining(Element::remove); try (final InputStream stream = new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray())) { reader.readGraph(stream, graph); } // there is also a "color" property on this dataset that is on both edges and vertices graph.vertices().forEachRemaining(v -> assertEquals("#6495ed", v.value("color"))); graph.edges().forEachRemaining(e -> assertEquals("#ee0000", e.value("color"))); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CLASSIC) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteClassic() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = graph.io(ioBuilderToTest).writer().create(); writer.writeGraph(os, graph); final Configuration configuration = graphProvider.newGraphConfiguration("readGraph", this.getClass(), name.getMethodName(), LoadGraphWith.GraphData.CLASSIC); graphProvider.clear(configuration); final Graph g1 = graphProvider.openTestGraph(configuration); final GraphReader reader = graph.io(ioBuilderToTest).reader().create(); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readGraph(bais, g1); } IoTest.assertClassicGraph(g1, assertDouble, lossyForId); graphProvider.clear(g1, configuration); } }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES) public void shouldReadLegacyGraphSON() throws IOException { final GraphReader reader = LegacyGraphSONReader.build().create(); try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphSONResourceAccess.class) + "tinkerpop-classic-legacy.json")) { reader.readGraph(stream, graph); } // the id is lossy in migration because TP2 treated ID as String assertClassicGraph(graph, false, true); } }
try (final InputStream stream = AbstractGraphBenchmark.class. getResourceAsStream(PATH + graphFile)) { reader.readGraph(stream, graph);
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteModern() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = graph.io(ioBuilderToTest).writer().create(); writer.writeGraph(os, graph); final Configuration configuration = graphProvider.newGraphConfiguration("readGraph", this.getClass(), name.getMethodName(), LoadGraphWith.GraphData.MODERN); graphProvider.clear(configuration); final Graph g1 = graphProvider.openTestGraph(configuration); final GraphReader reader = graph.io(ioBuilderToTest).reader().create(); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readGraph(bais, g1); } // modern uses double natively so always assert as such IoTest.assertModernGraph(g1, true, lossyForId); graphProvider.clear(g1, configuration); } }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadGraphMLWithoutEdgeIds() throws IOException { final GraphReader reader = GraphMLReader.build().strict(false).create(); try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "graph-no-edge-ids.xml")) { reader.readGraph(stream, graph); } assertEquals(1, IteratorUtils.count(graph.edges())); assertEquals(2, IteratorUtils.count(graph.vertices())); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteCrew() throws Exception { assumeThat("GraphML does not support multi/metaproperties", ioType, not("graphml")); try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = graph.io(ioBuilderToTest).writer().create(); writer.writeGraph(os, graph); final Configuration configuration = graphProvider.newGraphConfiguration("readGraph", this.getClass(), name.getMethodName(), LoadGraphWith.GraphData.CREW); graphProvider.clear(configuration); final Graph g1 = graphProvider.openTestGraph(configuration); final GraphReader reader = graph.io(ioBuilderToTest).reader().create(); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readGraph(bais, g1); } IoTest.assertCrewGraph(g1, lossyForId); graphProvider.clear(g1, configuration); } } }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_FLOAT_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_BOOLEAN_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_LONG_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_DOUBLE_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES) public void shouldReadGraphMLWithoutStrictOption() throws IOException { final GraphReader reader = GraphMLReader.build().strict(false).create(); try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "graph-types-bad.xml")) { reader.readGraph(stream, graph); } final Vertex v = graph.vertices().next(); assertFalse(v.values("d").hasNext()); assertEquals("some-string", v.<String>value("s")); assertFalse(v.values("i").hasNext()); assertEquals(false, v.<Boolean>value("b")); assertFalse(v.values("f").hasNext()); assertFalse(v.values("l").hasNext()); assertEquals("junk", v.<String>value("n")); }
@Test(expected = NumberFormatException.class) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_FLOAT_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_BOOLEAN_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_LONG_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_DOUBLE_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES) public void shouldReadGraphMLWithStrictOption() throws IOException { final GraphReader reader = GraphMLReader.build().strict(true).create(); try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "graph-types-bad.xml")) { reader.readGraph(stream, graph); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteModernWrappedInJsonObject() throws Exception { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V2_0).create(); try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = graph.io(graphson()).writer().wrapAdjacencyList(true).mapper(mapper).create(); writer.writeGraph(os, graph); final Configuration configuration = graphProvider.newGraphConfiguration("readGraph", this.getClass(), name.getMethodName(), LoadGraphWith.GraphData.MODERN); graphProvider.clear(configuration); final Graph g1 = graphProvider.openTestGraph(configuration); final GraphReader reader = graph.io(graphson()).reader().mapper(mapper).unwrapAdjacencyList(true).create(); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readGraph(bais, g1); } // modern uses double natively so always assert as such IoTest.assertModernGraph(g1, true, true); graphProvider.clear(g1, configuration); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteModernWrappedInJsonObject() throws Exception { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V3_0).create(); try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = graph.io(graphson()).writer().wrapAdjacencyList(true).mapper(mapper).create(); writer.writeGraph(os, graph); final Configuration configuration = graphProvider.newGraphConfiguration("readGraph", this.getClass(), name.getMethodName(), LoadGraphWith.GraphData.MODERN); graphProvider.clear(configuration); final Graph g1 = graphProvider.openTestGraph(configuration); final GraphReader reader = graph.io(graphson()).reader().mapper(mapper).unwrapAdjacencyList(true).create(); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readGraph(bais, g1); } // modern uses double natively so always assert as such IoTest.assertModernGraph(g1, true, true); graphProvider.clear(g1, configuration); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReadWriteModernWrappedInJsonObject() throws Exception { final GraphSONMapper mapper = graph.io(graphson).mapper().version(GraphSONVersion.V1_0).create(); try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = graph.io(graphson()).writer().wrapAdjacencyList(true).mapper(mapper).create(); writer.writeGraph(os, graph); final Configuration configuration = graphProvider.newGraphConfiguration("readGraph", this.getClass(), name.getMethodName(), LoadGraphWith.GraphData.MODERN); graphProvider.clear(configuration); final Graph g1 = graphProvider.openTestGraph(configuration); final GraphReader reader = graph.io(graphson()).reader().mapper(mapper).unwrapAdjacencyList(true).create(); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readGraph(bais, g1); } // modern uses double natively so always assert as such IoTest.assertModernGraph(g1, true, true); graphProvider.clear(g1, configuration); } }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_FLOAT_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_BOOLEAN_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_LONG_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_DOUBLE_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES) public void shouldReadGraphMLWithAllSupportedDataTypes() throws IOException { final GraphReader reader = GraphMLReader.build().create(); try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "graph-types.xml")) { reader.readGraph(stream, graph); } final Vertex v = graph.vertices().next(); assertEquals(123.45d, v.value("d"), 0.000001d); assertEquals("some-string", v.<String>value("s")); assertEquals(29, v.<Integer>value("i").intValue()); assertEquals(true, v.<Boolean>value("b")); assertEquals(123.54f, v.value("f"), 0.000001f); assertEquals(10000000l, v.<Long>value("l").longValue()); assertEquals("junk", v.<String>value("n")); }
final GraphReader reader = graph.io(graphson).reader() .mapper(graph.io(graphson).mapper().version(GraphSONVersion.V3_0).addCustomModule(module).create()).create(); reader.readGraph(is, g2);
final GraphReader reader = graph.io(graphson).reader() .mapper(graph.io(graphson).mapper().version(GraphSONVersion.V2_0).addCustomModule(module).create()).create(); reader.readGraph(is, g2);