tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spmalle...@apache.org
Subject [1/4] tinkerpop git commit: Add ResponseMessageDeserializer for GraphSON2.
Date Mon, 09 Jan 2017 12:33:28 GMT
Repository: tinkerpop
Updated Branches:
  refs/heads/master e248da06e -> 0819a686a


Add ResponseMessageDeserializer for GraphSON2.


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

Branch: refs/heads/master
Commit: 5ba605bf67fe6c46cdb874b63703cf65aa25fa6a
Parents: 3064b93
Author: Kevin Gallardo <kevin.gallardo@datastax.com>
Authored: Thu Jan 5 11:57:32 2017 -0500
Committer: Kevin Gallardo <kevin.gallardo@datastax.com>
Committed: Thu Jan 5 11:57:32 2017 -0500

----------------------------------------------------------------------
 .../AbstractGraphSONMessageSerializerV2d0.java  | 36 ++++++++++----
 .../ser/GraphSONMessageSerializerV2d0.java      | 11 +----
 .../ser/GraphSONMessageSerializerV2d0Test.java  | 49 ++++++++++++++++++++
 3 files changed, 76 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/5ba605bf/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/AbstractGraphSONMessageSerializerV2d0.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/AbstractGraphSONMessageSerializerV2d0.java
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/AbstractGraphSONMessageSerializerV2d0.java
index 52cbda2..5cd5198 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/AbstractGraphSONMessageSerializerV2d0.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/AbstractGraphSONMessageSerializerV2d0.java
@@ -26,6 +26,7 @@ import org.apache.tinkerpop.gremlin.driver.message.RequestMessage;
 import org.apache.tinkerpop.gremlin.driver.message.ResponseMessage;
 import org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode;
 import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.AbstractObjectDeserializer;
 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONIo;
 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONUtil;
@@ -154,16 +155,7 @@ public abstract class AbstractGraphSONMessageSerializerV2d0 extends AbstractMess
         try {
             final byte[] payload = new byte[msg.readableBytes()];
             msg.readBytes(payload);
-            final Map<String, Object> responseData = mapper.readValue(payload, mapTypeReference);
-            final Map<String, Object> status = (Map<String, Object>) responseData.get(SerTokens.TOKEN_STATUS);
-            final Map<String, Object> result = (Map<String, Object>) responseData.get(SerTokens.TOKEN_RESULT);
-            return ResponseMessage.build(UUID.fromString(responseData.get(SerTokens.TOKEN_REQUEST).toString()))
-                    .code(ResponseStatusCode.getFromValue((Integer) status.get(SerTokens.TOKEN_CODE)))
-                    .statusMessage(status.get(SerTokens.TOKEN_MESSAGE).toString())
-                    .statusAttributes((Map<String, Object>) status.get(SerTokens.TOKEN_ATTRIBUTES))
-                    .result(result.get(SerTokens.TOKEN_DATA))
-                    .responseMetaData((Map<String, Object>) result.get(SerTokens.TOKEN_META))
-                    .create();
+            return mapper.readValue(payload, ResponseMessage.class);
         } catch (Exception ex) {
             logger.warn("Response [{}] could not be deserialized by {}.", msg, AbstractGraphSONMessageSerializerV2d0.class.getName());
             throw new SerializationException(ex);
@@ -180,8 +172,13 @@ public abstract class AbstractGraphSONMessageSerializerV2d0 extends AbstractMess
     public final static class GremlinServerModule extends SimpleModule {
         public GremlinServerModule() {
             super("graphson-gremlin-server");
+
+            // SERIALIZERS
             addSerializer(JsonBuilder.class, new JsonBuilderJacksonSerializer());
             addSerializer(ResponseMessage.class, new ResponseMessageSerializer());
+
+            //DESERIALIZERS
+            addDeserializer(ResponseMessage.class, new ResponseMessageDeserializer());
         }
     }
 
@@ -251,4 +248,23 @@ public abstract class AbstractGraphSONMessageSerializerV2d0 extends AbstractMess
             GraphSONUtil.writeEndObject(responseMessage, jsonGenerator, typeSerializer);
         }
     }
+    
+    public final static class ResponseMessageDeserializer extends AbstractObjectDeserializer<ResponseMessage>
{
+        protected ResponseMessageDeserializer() {
+            super(ResponseMessage.class);
+        }
+        
+        @Override
+        public ResponseMessage createObject(Map<String, Object> data) {
+            final Map<String, Object> status = (Map<String, Object>) data.get(SerTokens.TOKEN_STATUS);
+            final Map<String, Object> result = (Map<String, Object>) data.get(SerTokens.TOKEN_RESULT);
+            return ResponseMessage.build(UUID.fromString(data.get(SerTokens.TOKEN_REQUEST).toString()))
+                    .code(ResponseStatusCode.getFromValue((Integer) status.get(SerTokens.TOKEN_CODE)))
+                    .statusMessage(status.get(SerTokens.TOKEN_MESSAGE).toString())
+                    .statusAttributes((Map<String, Object>) status.get(SerTokens.TOKEN_ATTRIBUTES))
+                    .result(result.get(SerTokens.TOKEN_DATA))
+                    .responseMetaData((Map<String, Object>) result.get(SerTokens.TOKEN_META))
+                    .create();
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/5ba605bf/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0.java
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0.java
index 89432d9..251b5c1 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0.java
@@ -77,16 +77,7 @@ public final class GraphSONMessageSerializerV2d0 extends AbstractGraphSONMessage
     @Override
     public ResponseMessage deserializeResponse(final String msg) throws SerializationException
{
         try {
-            final Map<String, Object> responseData = mapper.readValue(msg, mapTypeReference);
-            final Map<String, Object> status = (Map<String, Object>) responseData.get(SerTokens.TOKEN_STATUS);
-            final Map<String, Object> result = (Map<String, Object>) responseData.get(SerTokens.TOKEN_RESULT);
-            return ResponseMessage.build(UUID.fromString(responseData.get(SerTokens.TOKEN_REQUEST).toString()))
-                    .code(ResponseStatusCode.getFromValue((Integer) status.get(SerTokens.TOKEN_CODE)))
-                    .statusMessage(status.get(SerTokens.TOKEN_MESSAGE).toString())
-                    .statusAttributes((Map<String, Object>) status.get(SerTokens.TOKEN_ATTRIBUTES))
-                    .result(result.get(SerTokens.TOKEN_DATA))
-                    .responseMetaData((Map<String, Object>) result.get(SerTokens.TOKEN_META))
-                    .create();
+            return mapper.readValue(msg, ResponseMessage.class);
         } catch (Exception ex) {
             logger.warn("Response [{}] could not be deserialized by {}.", msg, AbstractGraphSONMessageSerializerV2d0.class.getName());
             throw new SerializationException(ex);

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/5ba605bf/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0Test.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0Test.java
b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0Test.java
index d3dbecc..460e935 100644
--- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0Test.java
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0Test.java
@@ -54,6 +54,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
@@ -491,6 +492,54 @@ public class GraphSONMessageSerializerV2d0Test {
         }
     }
 
+    @Test
+    public void shouldSerializeAndDeserializeResponseAndRequestFromObjectMapper() throws
IOException {
+        ObjectMapper om = GraphSONMapper.build().version(GraphSONVersion.V2_0)
+                .addCustomModule(new GraphSONMessageSerializerGremlinV2d0.GremlinServerModule())
+                .create().createMapper();
+
+        final Map<String, Object> requestBindings = new HashMap<>();
+        requestBindings.put("x", 1);
+
+        final Map<String, Object> requestAliases = new HashMap<>();
+        requestAliases.put("g", "social");
+
+        RequestMessage requestMessage = RequestMessage.build("eval").processor("session").
+                overrideRequestId(UUID.fromString("cb682578-9d92-4499-9ebc-5c6aa73c5397")).
+                add("gremlin", "social.V(x)", "bindings", requestBindings, "language", "gremlin-groovy",
"aliases", requestAliases, "session", UUID.fromString("41d2e28a-20a4-4ab0-b379-d810dede3786")).create();
+
+        String json = om.writeValueAsString(requestMessage);
+        RequestMessage readRequestMessage = om.readValue(json, RequestMessage.class);
+
+        assertEquals(requestMessage.getOp(), readRequestMessage.getOp());
+        assertEquals(requestMessage.getProcessor(), readRequestMessage.getProcessor());
+        assertEquals(requestMessage.getRequestId(), readRequestMessage.getRequestId());
+        assertEquals(requestMessage.getArgs(), readRequestMessage.getArgs());
+    }
+
+    @Test
+    public void shouldSerializeAndDeserializeResponseFromObjectMapper() throws IOException
{
+        ObjectMapper om = GraphSONMapper.build().version(GraphSONVersion.V2_0)
+                .addCustomModule(new GraphSONMessageSerializerGremlinV2d0.GremlinServerModule())
+                .create().createMapper();
+        Graph graph = TinkerFactory.createModern();
+
+        ResponseMessage responseMessage = ResponseMessage.build(UUID.fromString("41d2e28a-20a4-4ab0-b379-d810dede3786")).
+                code(org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode.SUCCESS).
+                result(Collections.singletonList(graph.vertices().next())).create();
+
+        String respJson = om.writeValueAsString(responseMessage);
+        ResponseMessage responseMessageRead = om.readValue(respJson, ResponseMessage.class);
+
+        assertEquals(responseMessage.getRequestId(), responseMessageRead.getRequestId());
+        assertEquals(responseMessage.getResult().getMeta(), responseMessageRead.getResult().getMeta());
+        assertEquals(responseMessage.getResult().getData(), responseMessageRead.getResult().getData());
+        assertEquals(responseMessage.getStatus().getAttributes(), responseMessageRead.getStatus().getAttributes());
+        assertEquals(responseMessage.getStatus().getCode().getValue(), responseMessageRead.getStatus().getCode().getValue());
+        assertEquals(responseMessage.getStatus().getCode().isSuccess(), responseMessageRead.getStatus().getCode().isSuccess());
+        assertEquals(responseMessage.getStatus().getMessage(), responseMessageRead.getStatus().getMessage());
+    }
+
     private class FunObject {
         private String val;
 


Mime
View raw message