You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/11/11 22:48:25 UTC

incubator-tinkerpop git commit: Added more tests around Map.Entry serialization.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master 1c575eb76 -> e35e6c86d


Added more tests around Map.Entry serialization.

Uncovered that GraphSON with embedded types would not serialize it.  Added a serializer to coerce it to a Map representation.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/e35e6c86
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/e35e6c86
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/e35e6c86

Branch: refs/heads/master
Commit: e35e6c86d93cbb7e8c6618212d63eb3020eed96d
Parents: 1c575eb
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Nov 11 16:47:36 2015 -0500
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Nov 11 16:47:36 2015 -0500

----------------------------------------------------------------------
 CHANGELOG.asciidoc                              |  2 +-
 .../structure/io/graphson/GraphSONMapper.java   |  2 +-
 .../structure/io/graphson/GraphSONModule.java   |  3 ++
 .../io/graphson/GraphSONSerializers.java        | 26 +++++++++++++
 ...raphSONMessageSerializerGremlinV1d0Test.java | 29 ++++++++++----
 .../ser/GraphSONMessageSerializerV1d0Test.java  | 41 +++++++++++++-------
 .../ser/GryoMessageSerializerV1d0Test.java      | 14 +++++++
 7 files changed, 94 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e35e6c86/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index a42e1a6..eee27e9 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -25,7 +25,7 @@ image::https://raw.githubusercontent.com/apache/incubator-tinkerpop/master/docs/
 TinkerPop 3.1.0 (NOT OFFICIALLY RELEASED YET)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-* Fixed bug in Gryo serialization for serialization of results returned from `Map.entrySet()`.
+* Fixed bug in Gryo and GraphSON (with embedded types) serialization for serialization of results returned from `Map.entrySet()`.
 * `Transaction` settings for `onReadWrite` and `onClose` are now `ThreadLocal` in nature of standard transactions.
 * Optimized `BulkLoaderVertexProgram`. It now uses `EventStrategy` to monitor what the underlying `BulkLoader` implementation does (e.g. whether it creates a new vertex or returns an existing).
 * Integrated `NumberHelper` in `SumStep`, `MinStep`, `MaxStep` and `MeanStep` (local and global step variants).

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e35e6c86/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java
index ef27488..73e9552 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java
@@ -158,7 +158,7 @@ public class GraphSONMapper implements Mapper<ObjectMapper> {
 
         /**
          * Try to load {@code SimpleModule} instances from the current classpath.  These are loaded in addition to
-         * the one supplied to the {@link #addCustomModule(com.fasterxml.jackson.databind.module.SimpleModule)};
+         * the one supplied to the {@link #addCustomModule(SimpleModule)};
          */
         public Builder loadCustomModules(final boolean loadCustomModules) {
             this.loadCustomModules = loadCustomModules;

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e35e6c86/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
index ebe31cc..dba774e 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
@@ -27,6 +27,8 @@ import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGraphSONSerializer;
 import org.apache.tinkerpop.shaded.jackson.databind.module.SimpleModule;
 
+import java.util.Map;
+
 /**
  * The set of serializers that handle the core graph interfaces.  These serializers support normalization which
  * ensures that generated GraphSON will be compatible with line-based versioning tools. This setting comes with
@@ -60,6 +62,7 @@ abstract class GraphSONModule extends SimpleModule {
             addSerializer(TraversalMetrics.class, new GraphSONSerializers.TraversalMetricsJacksonSerializer());
             addSerializer(Path.class, new GraphSONSerializers.PathJacksonSerializer());
             addSerializer(StarGraphGraphSONSerializer.DirectionalStarGraph.class, new StarGraphGraphSONSerializer(normalize));
+            addSerializer(Map.Entry.class, new GraphSONSerializers.MapEntryJacksonSerializer());
         }
 
         public static Builder build() {

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e35e6c86/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java
index 1708ced..9e818b9 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java
@@ -324,7 +324,33 @@ final class GraphSONSerializers {
             }
             return m;
         }
+    }
+
+    final static class MapEntryJacksonSerializer extends StdSerializer<Map.Entry> {
+
+        public MapEntryJacksonSerializer() {
+            super(Map.Entry.class);
+        }
+
+        @Override
+        public void serialize(final Map.Entry entry, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
+                throws IOException {
+            ser(entry, jsonGenerator, serializerProvider, null);
+        }
+        @Override
+        public void serializeWithType(final Map.Entry entry, final JsonGenerator jsonGenerator,
+                                      final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException {
+            ser(entry, jsonGenerator, serializerProvider, typeSerializer);
+        }
 
+        private static void ser(final Map.Entry entry, final JsonGenerator jsonGenerator,
+                                final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException {
+            jsonGenerator.writeStartObject();
+            if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName());
+            serializerProvider.defaultSerializeField(GraphSONTokens.KEY, entry.getKey(), jsonGenerator);
+            serializerProvider.defaultSerializeField(GraphSONTokens.VALUE, entry.getValue(), jsonGenerator);
+            jsonGenerator.writeEndObject();
+        }
     }
 
     private static void serializerVertexProperty(final VertexProperty property, final JsonGenerator jsonGenerator,

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e35e6c86/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV1d0Test.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV1d0Test.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV1d0Test.java
index 018d6da..99e1325 100644
--- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV1d0Test.java
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV1d0Test.java
@@ -57,7 +57,7 @@ public class GraphSONMessageSerializerGremlinV1d0Test {
     public MessageSerializer serializer = new GraphSONMessageSerializerGremlinV1d0();
 
     @Test
-    public void serializeIterable() throws Exception {
+    public void shouldSerializeIterable() throws Exception {
         final ArrayList<Integer> list = new ArrayList<>();
         list.add(1);
         list.add(100);
@@ -72,7 +72,7 @@ public class GraphSONMessageSerializerGremlinV1d0Test {
     }
 
     @Test
-    public void serializeIterableWithNull() throws Exception {
+    public void shouldSerializeIterableWithNull() throws Exception {
         final ArrayList<Integer> list = new ArrayList<>();
         list.add(1);
         list.add(null);
@@ -89,7 +89,7 @@ public class GraphSONMessageSerializerGremlinV1d0Test {
     }
 
     @Test
-    public void serializeMap() throws Exception {
+    public void shouldSerializeMap() throws Exception {
         final Map<String, Object> map = new HashMap<>();
         final Map<String, String> innerMap = new HashMap<>();
         innerMap.put("a", "b");
@@ -112,7 +112,20 @@ public class GraphSONMessageSerializerGremlinV1d0Test {
     }
 
     @Test
-    public void serializeEdge() throws Exception {
+    public void shouldSerializeMapEntry() throws Exception {
+        final Map<String, Object> map = new HashMap<>();
+        map.put("x", 1);
+
+        final ResponseMessage response = convert(map.entrySet().toArray()[0]);
+        assertCommon(response);
+
+        final Map<String, Object> deserializedEntry = (Map<String, Object>) response.getResult().getData();
+        assertEquals(1, deserializedEntry.get(GraphSONTokens.VALUE));
+        assertEquals("x", deserializedEntry.get(GraphSONTokens.KEY));
+    }
+
+    @Test
+    public void shouldSerializeEdge() throws Exception {
         final Graph graph = TinkerGraph.open();
         final Vertex v1 = graph.addVertex();
         final Vertex v2 = graph.addVertex();
@@ -143,7 +156,7 @@ public class GraphSONMessageSerializerGremlinV1d0Test {
     }
 
     @Test
-    public void serializeEdgeProperty() throws Exception {
+    public void shouldSerializeEdgeProperty() throws Exception {
         final Graph graph = TinkerGraph.open();
         final Vertex v1 = graph.addVertex();
         final Vertex v2 = graph.addVertex();
@@ -160,7 +173,7 @@ public class GraphSONMessageSerializerGremlinV1d0Test {
     }
 
     @Test
-    public void serializeVertexWithEmbeddedMap() throws Exception {
+    public void shouldSerializeVertexWithEmbeddedMap() throws Exception {
         final Graph graph = TinkerGraph.open();
         final Vertex v = graph.addVertex();
         final Map<String, Object> map = new HashMap<>();
@@ -204,7 +217,7 @@ public class GraphSONMessageSerializerGremlinV1d0Test {
     }
 
     @Test
-    public void serializeToJsonMapWithElementForKey() throws Exception {
+    public void shouldSerializeToJsonMapWithElementForKey() throws Exception {
         final TinkerGraph graph = TinkerFactory.createClassic();
         final GraphTraversalSource g = graph.traversal();
         final Map<Vertex, Integer> map = new HashMap<>();
@@ -223,7 +236,7 @@ public class GraphSONMessageSerializerGremlinV1d0Test {
 
 
     @Test
-    public void serializeFullResponseMessage() throws Exception {
+    public void shouldSerializeFullResponseMessage() throws Exception {
         final UUID id = UUID.randomUUID();
 
         final Map<String, Object> metaData = new HashMap<>();

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e35e6c86/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV1d0Test.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV1d0Test.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV1d0Test.java
index b030704..bddeaab 100644
--- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV1d0Test.java
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV1d0Test.java
@@ -58,7 +58,7 @@ public class GraphSONMessageSerializerV1d0Test {
     private static final ObjectMapper mapper = new ObjectMapper();
 
     @Test
-    public void serializeToJsonNullResultReturnsNull() throws Exception {
+    public void shouldSerializeToJsonNullResultReturnsNull() throws Exception {
         final ResponseMessage message = ResponseMessage.build(msg).create();
         final String results = SERIALIZER.serializeResponseAsString(message);
         final JsonNode json = mapper.readTree(results);
@@ -68,7 +68,7 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void serializeToJsonIterable() throws Exception {
+    public void shouldSerializeToJsonIterable() throws Exception {
         final ArrayList<FunObject> funList = new ArrayList<>();
         funList.add(new FunObject("x"));
         funList.add(new FunObject("y"));
@@ -87,7 +87,7 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void serializeToJsonIterator() throws Exception {
+    public void shouldSerializeToJsonIterator() throws Exception {
         final ArrayList<FunObject> funList = new ArrayList<>();
         funList.add(new FunObject("x"));
         funList.add(new FunObject("y"));
@@ -106,7 +106,7 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void serializeToJsonIteratorNullElement() throws Exception {
+    public void shouldSerializeToJsonIteratorNullElement() throws Exception {
 
         final ArrayList<FunObject> funList = new ArrayList<>();
         funList.add(new FunObject("x"));
@@ -128,7 +128,7 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void serializeToJsonMap() throws Exception {
+    public void shouldSerializeToJsonMap() throws Exception {
         final Map<String, Object> map = new HashMap<>();
         final Map<String, String> innerMap = new HashMap<>();
         innerMap.put("a", "b");
@@ -154,7 +154,22 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void serializeEdge() throws Exception {
+    public void shouldShouldSerializeMapEntry() throws Exception {
+        final Map<String, Object> map = new HashMap<>();
+        map.put("x", 1);
+
+        final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(map).create());
+        final JsonNode json = mapper.readTree(results);
+
+        assertNotNull(json);
+        assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
+        final JsonNode jsonObject = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);
+
+        assertEquals(1, jsonObject.get("x").asInt());
+    }
+
+    @Test
+    public void shouldSerializeEdge() throws Exception {
         final Graph g = TinkerGraph.open();
         final Vertex v1 = g.addVertex();
         final Vertex v2 = g.addVertex();
@@ -188,7 +203,7 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void serializeEdgeProperty() throws Exception {
+    public void shouldSerializeEdgeProperty() throws Exception {
         final Graph g = TinkerGraph.open();
         final Vertex v1 = g.addVertex();
         final Vertex v2 = g.addVertex();
@@ -214,7 +229,7 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void serializeToJsonIteratorWithEmbeddedMap() throws Exception {
+    public void shouldSerializeToJsonIteratorWithEmbeddedMap() throws Exception {
         final Graph g = TinkerGraph.open();
         final Vertex v = g.addVertex();
         final Map<String, Object> map = new HashMap<>();
@@ -264,7 +279,7 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void serializeToJsonMapWithElementForKey() throws Exception {
+    public void shouldSerializeToJsonMapWithElementForKey() throws Exception {
         final TinkerGraph graph = TinkerFactory.createClassic();
         final GraphTraversalSource g = graph.traversal();
         final Map<Vertex, Integer> map = new HashMap<>();
@@ -285,7 +300,7 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void deserializeRequestNicelyWithNoArgs() throws Exception {
+    public void shouldDeserializeRequestNicelyWithNoArgs() throws Exception {
         final UUID request = UUID.fromString("011CFEE9-F640-4844-AC93-034448AC0E80");
         final RequestMessage m = SERIALIZER.deserializeRequest(String.format("{\"requestId\":\"%s\",\"op\":\"eval\"}", request));
         assertEquals(request, m.getRequestId());
@@ -295,7 +310,7 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test
-    public void deserializeRequestNicelyWithArgs() throws Exception {
+    public void shouldDeserializeRequestNicelyWithArgs() throws Exception {
         final UUID request = UUID.fromString("011CFEE9-F640-4844-AC93-034448AC0E80");
         final RequestMessage m = SERIALIZER.deserializeRequest(String.format("{\"requestId\":\"%s\",\"op\":\"eval\",\"args\":{\"x\":\"y\"}}", request));
         assertEquals(request, m.getRequestId());
@@ -305,12 +320,12 @@ public class GraphSONMessageSerializerV1d0Test {
     }
 
     @Test(expected = SerializationException.class)
-    public void deserializeRequestParseMessage() throws Exception {
+    public void shouldDeserializeRequestParseMessage() throws Exception {
         SERIALIZER.deserializeRequest("{\"requestId\":\"%s\",\"op\":\"eval\",\"args\":{\"x\":\"y\"}}");
     }
 
     @Test
-    public void serializeFullResponseMessage() throws Exception {
+    public void shouldSerializeFullResponseMessage() throws Exception {
         final UUID id = UUID.randomUUID();
 
         final Map<String, Object> metaData = new HashMap<>();

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e35e6c86/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GryoMessageSerializerV1d0Test.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GryoMessageSerializerV1d0Test.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GryoMessageSerializerV1d0Test.java
index baf3bd4..20853b2 100644
--- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GryoMessageSerializerV1d0Test.java
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GryoMessageSerializerV1d0Test.java
@@ -44,6 +44,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.UUID;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
@@ -164,6 +165,19 @@ public class GryoMessageSerializerV1d0Test {
     }
 
     @Test
+    public void shouldSerializeMapEntry() throws Exception {
+        final Map<String, Object> map = new HashMap<>();
+        map.put("x", 1);
+
+        final ResponseMessage response = convertBinary(map.entrySet().toArray()[0]);
+        assertCommon(response);
+
+        final Map.Entry<String, Object> deserializedEntry = (Map.Entry<String, Object>) response.getResult().getData();
+        assertEquals(1, deserializedEntry.getValue());
+        assertEquals("x", deserializedEntry.getKey());
+    }
+
+    @Test
     public void shouldSerializeEdge() throws Exception {
         final Graph g = TinkerGraph.open();
         final Vertex v1 = g.addVertex();