@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); }
/** * {@inheritDoc} */ @Override public GraphSONReader.Builder reader() { return GraphSONReader.build().mapper(mapper().create()); }
/** * {@inheritDoc} */ @Override public GraphSONWriter.Builder writer() { return GraphSONWriter.build().mapper(mapper().create()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertex() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final Vertex v = graph.vertices(convertToVertexId("marko")).next(); final String json = mapper.writeValueAsString(v); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertEquals(GraphSONTokens.VERTEX, m.get(GraphSONTokens.TYPE)); assertEquals(v.label(), m.get(GraphSONTokens.LABEL)); assertNotNull(m.get(GraphSONTokens.ID)); final Map<String,List<Map<String,Object>>> properties = (Map<String,List<Map<String,Object>>>) m.get(GraphSONTokens.PROPERTIES); assertEquals(v.value("name").toString(), properties.get("name").get(0).get(GraphSONTokens.VALUE).toString()); assertEquals((Integer) v.value("age"), properties.get("age").get(0).get(GraphSONTokens.VALUE)); assertEquals(1, properties.get("name").size()); assertEquals(1, properties.get("age").size()); assertEquals(2, properties.size()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithProperties() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final VertexProperty vp = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final String json = mapper.writeValueAsString(vp); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertEquals(vp.label(), m.get(GraphSONTokens.LABEL)); assertNotNull(m.get(GraphSONTokens.ID)); assertEquals(vp.value(), m.get(GraphSONTokens.VALUE)); assertEquals(vp.values("startTime").next(), ((Map) m.get(GraphSONTokens.PROPERTIES)).get("startTime")); assertEquals(vp.values("endTime").next(), ((Map) m.get(GraphSONTokens.PROPERTIES)).get("endTime")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final TraversalMetrics tm = g.V().both().profile().next(); final String json = mapper.writeValueAsString(tm); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertTrue(m.containsKey(GraphSONTokens.DURATION)); assertTrue(m.containsKey(GraphSONTokens.METRICS)); final List<Map<String, Object>> metrics = (List<Map<String, Object>>) m.get(GraphSONTokens.METRICS); assertEquals(tm.getMetrics().size(), metrics.size()); final Map<String, Object> metrics0 = metrics.get(0); assertTrue(metrics0.containsKey(GraphSONTokens.ID)); assertTrue(metrics0.containsKey(GraphSONTokens.NAME)); assertTrue(metrics0.containsKey(GraphSONTokens.COUNTS)); assertTrue(metrics0.containsKey(GraphSONTokens.DURATION)); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeEdge() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final Edge e = g.E(convertToEdgeId("marko", "knows", "vadas")).next(); final String json = mapper.writeValueAsString(e); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertEquals(GraphSONTokens.EDGE, m.get(GraphSONTokens.TYPE)); assertEquals(e.label(), m.get(GraphSONTokens.LABEL)); assertNotNull(m.get(GraphSONTokens.ID)); assertEquals((Double) e.value("weight"), ((Map) m.get(GraphSONTokens.PROPERTIES)).get("weight")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertexProperty() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final VertexProperty vp = graph.vertices(convertToVertexId("marko")).next().property("name"); final String json = mapper.writeValueAsString(vp); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertEquals(vp.label(), m.get(GraphSONTokens.LABEL)); assertNotNull(m.get(GraphSONTokens.ID)); assertEquals(vp.value(), m.get(GraphSONTokens.VALUE)); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithProperties() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final VertexProperty vp = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final String json = mapper.writeValueAsString(vp); final VertexProperty<?> detached = mapper.readValue(json, VertexProperty.class); assertNotNull(detached); assertEquals(vp.label(), detached.label()); assertEquals(vp.id(), detached.id()); assertEquals(vp.value(), detached.value()); assertEquals(vp.values("startTime").next(), detached.values("startTime").next()); assertEquals(((Property) vp.properties("startTime").next()).key(), ((Property) detached.properties("startTime").next()).key()); assertEquals(vp.values("endTime").next(), detached.values("endTime").next()); assertEquals(((Property) vp.properties("endTime").next()).key(), ((Property) detached.properties("endTime").next()).key()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithProperties() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final VertexProperty vp = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final String json = mapper.writeValueAsString(vp); final VertexProperty<?> detached = mapper.readValue(json, VertexProperty.class); assertNotNull(detached); assertEquals(vp.label(), detached.label()); assertEquals(vp.id(), detached.id()); assertEquals(vp.value(), detached.value()); assertEquals(vp.values("startTime").next(), detached.values("startTime").next()); assertEquals(((Property) vp.properties("startTime").next()).key(), ((Property) detached.properties("startTime").next()).key()); assertEquals(vp.values("endTime").next(), detached.values("endTime").next()); assertEquals(((Property) vp.properties("endTime").next()).key(), ((Property) detached.properties("endTime").next()).key()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeProperty() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final Property p = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); final String json = mapper.writeValueAsString(p); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertEquals(p.value(), m.get(GraphSONTokens.VALUE)); assertEquals(p.key(), m.get(GraphSONTokens.KEY)); }
@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 @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertex() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final Vertex v = graph.vertices(convertToVertexId("marko")).next(); final String json = mapper.writeValueAsString(v); final Vertex detached = mapper.readValue(json, Vertex.class); assertNotNull(detached); assertEquals(v.label(), detached.label()); assertEquals(v.id(), detached.id()); assertEquals(v.value("name").toString(), detached.value("name")); assertEquals((Integer) v.value("age"), detached.value("age")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertex() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final Vertex v = graph.vertices(convertToVertexId("marko")).next(); final String json = mapper.writeValueAsString(v); final Vertex detached = mapper.readValue(json, Vertex.class); assertNotNull(detached); assertEquals(v.label(), detached.label()); assertEquals(v.id(), detached.id()); assertEquals(v.value("name").toString(), detached.value("name")); assertEquals((Integer) v.value("age"), detached.value("age")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertexProperty() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final VertexProperty vp = graph.vertices(convertToVertexId("marko")).next().property("name"); final String json = mapper.writeValueAsString(vp); final VertexProperty detached = mapper.readValue(json, VertexProperty.class); assertNotNull(detached); assertEquals(vp.label(), detached.label()); assertEquals(vp.id(), detached.id()); assertEquals(vp.value(), detached.value()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertexProperty() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final VertexProperty vp = graph.vertices(convertToVertexId("marko")).next().property("name"); final String json = mapper.writeValueAsString(vp); final VertexProperty detached = mapper.readValue(json, VertexProperty.class); assertNotNull(detached); assertEquals(vp.label(), detached.label()); assertEquals(vp.id(), detached.id()); assertEquals(vp.value(), detached.value()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeProperty() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final Property p = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); final String json = mapper.writeValueAsString(p); final Property detached = mapper.readValue(json, Property.class); assertNotNull(detached); assertEquals(p.key(), detached.key()); assertEquals(p.value(), detached.value()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeProperty() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final Property p = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); final String json = mapper.writeValueAsString(p); final Property detached = mapper.readValue(json, Property.class); assertNotNull(detached); assertEquals(p.key(), detached.key()); assertEquals(p.value(), detached.value()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeEdge() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final Edge e = g.E(convertToEdgeId("marko", "knows", "vadas")).next(); final String json = mapper.writeValueAsString(e); final Edge detached = mapper.readValue(json, Edge.class); assertNotNull(detached); assertEquals(e.label(), detached.label()); assertEquals(e.id(), detached.id()); assertEquals((Double) e.value("weight"), detached.value("weight")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeEdge() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final Edge e = g.E(convertToEdgeId("marko", "knows", "vadas")).next(); final String json = mapper.writeValueAsString(e); final Edge detached = mapper.readValue(json, Edge.class); assertNotNull(detached); assertEquals(e.label(), detached.label()); assertEquals(e.id(), detached.id()); assertEquals((Double) e.value("weight"), detached.value("weight")); }