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 2016/08/22 20:40:16 UTC
[31/48] tinkerpop git commit: TINKERPOP-1274: GraphSON 2.0.
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV2d0.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV2d0.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV2d0.java
new file mode 100644
index 0000000..a2d29fc
--- /dev/null
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV2d0.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.driver.ser;
+
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONVersion;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.TypeInfo;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Serialize results to JSON with version 2.0.x schema.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public final class GraphSONMessageSerializerGremlinV2d0 extends AbstractGraphSONMessageSerializerV2d0 {
+
+ private static final String MIME_TYPE = SerTokens.MIME_GRAPHSON_V2D0;
+
+ private static byte[] header;
+
+ static {
+ final ByteBuffer buffer = ByteBuffer.allocate(MIME_TYPE.length() + 1);
+ buffer.put((byte) MIME_TYPE.length());
+ buffer.put(MIME_TYPE.getBytes());
+ header = buffer.array();
+ }
+
+ public GraphSONMessageSerializerGremlinV2d0() {
+ super();
+ }
+
+ public GraphSONMessageSerializerGremlinV2d0(final GraphSONMapper mapper) {
+ super(mapper);
+ }
+
+ @Override
+ public String[] mimeTypesSupported() {
+ return new String[]{MIME_TYPE};
+ }
+
+ @Override
+ byte[] obtainHeader() {
+ return header;
+ }
+
+ @Override
+ GraphSONMapper.Builder configureBuilder(final GraphSONMapper.Builder builder) {
+ return builder.version(GraphSONVersion.V2_0)
+ .addCustomModule(new GremlinServerModule())
+ .typeInfo(TypeInfo.PARTIAL_TYPES);
+ }
+}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/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
new file mode 100644
index 0000000..2e8c96e
--- /dev/null
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0.java
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.driver.ser;
+
+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.io.graphson.GraphSONMapper;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONVersion;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.TypeInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.util.Map;
+import java.util.UUID;
+
+/**
+ * Serialize results to JSON with version 2.0.x schema.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public final class GraphSONMessageSerializerV2d0 extends AbstractGraphSONMessageSerializerV2d0 implements MessageTextSerializer {
+ private static final Logger logger = LoggerFactory.getLogger(GraphSONMessageSerializerV2d0.class);
+ private static final String MIME_TYPE = SerTokens.MIME_JSON;
+
+ private static byte[] header;
+
+ static {
+ final ByteBuffer buffer = ByteBuffer.allocate(MIME_TYPE.length() + 1);
+ buffer.put((byte) MIME_TYPE.length());
+ buffer.put(MIME_TYPE.getBytes());
+ header = buffer.array();
+ }
+
+ public GraphSONMessageSerializerV2d0() {
+ super();
+ }
+
+ public GraphSONMessageSerializerV2d0(final GraphSONMapper mapper) {
+ super(mapper);
+ }
+
+ @Override
+ public String[] mimeTypesSupported() {
+ return new String[]{MIME_TYPE};
+ }
+
+ @Override
+ GraphSONMapper.Builder configureBuilder(final GraphSONMapper.Builder builder) {
+ return builder.version(GraphSONVersion.V2_0)
+ .addCustomModule(new GremlinServerModule())
+ .typeInfo(TypeInfo.PARTIAL_TYPES);
+ }
+
+ @Override
+ byte[] obtainHeader() {
+ return header;
+ }
+
+ @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();
+ } catch (Exception ex) {
+ logger.warn("Response [{}] could not be deserialized by {}.", msg, AbstractGraphSONMessageSerializerV2d0.class.getName());
+ throw new SerializationException(ex);
+ }
+ }
+
+ @Override
+ public String serializeResponseAsString(final ResponseMessage responseMessage) throws SerializationException {
+ try {
+ return mapper.writeValueAsString(responseMessage);
+ } catch (Exception ex) {
+ logger.warn("Response [{}] could not be serialized by {}.", responseMessage.toString(), AbstractGraphSONMessageSerializerV2d0.class.getName());
+ throw new SerializationException(ex);
+ }
+ }
+
+ @Override
+ public RequestMessage deserializeRequest(final String msg) throws SerializationException {
+ try {
+ return mapper.readValue(msg, RequestMessage.class);
+ } catch (Exception ex) {
+ logger.warn("Request [{}] could not be deserialized by {}.", msg, AbstractGraphSONMessageSerializerV2d0.class.getName());
+ throw new SerializationException(ex);
+ }
+ }
+
+ @Override
+ public String serializeRequestAsString(final RequestMessage requestMessage) throws SerializationException {
+ try {
+ return mapper.writeValueAsString(requestMessage);
+ } catch (Exception ex) {
+ logger.warn("Request [{}] could not be serialized by {}.", requestMessage.toString(), AbstractGraphSONMessageSerializerV2d0.class.getName());
+ throw new SerializationException(ex);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/SerTokens.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/SerTokens.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/SerTokens.java
index c857116..ecda014 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/SerTokens.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/SerTokens.java
@@ -38,6 +38,7 @@ public final class SerTokens {
public static final String MIME_JSON = "application/json";
public static final String MIME_GRAPHSON_V1D0 = "application/vnd.gremlin-v1.0+json";
+ public static final String MIME_GRAPHSON_V2D0 = "application/vnd.gremlin-v2.0+json";
public static final String MIME_GRYO_V1D0 = "application/vnd.gremlin-v1.0+gryo";
public static final String MIME_GRYO_LITE_V1D0 = "application/vnd.gremlin-v1.0+gryo-lite";
}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTestV1d0.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTestV1d0.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTestV1d0.java
new file mode 100644
index 0000000..95907f9
--- /dev/null
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTestV1d0.java
@@ -0,0 +1,334 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.driver.ser;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.buffer.UnpooledByteBufAllocator;
+import org.apache.tinkerpop.gremlin.driver.MessageSerializer;
+import org.apache.tinkerpop.gremlin.driver.message.ResponseMessage;
+import org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.structure.Property;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONTokens;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
+import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.apache.tinkerpop.shaded.jackson.databind.util.StdDateFormat;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+/**
+ * Serializer tests that cover lossy serialization/deserialization methods.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GraphSONMessageSerializerGremlinTestV1d0 {
+
+ private UUID requestId = UUID.fromString("6457272A-4018-4538-B9AE-08DD5DDC0AA1");
+ private ResponseMessage.Builder responseMessageBuilder = ResponseMessage.build(requestId);
+ private static ByteBufAllocator allocator = UnpooledByteBufAllocator.DEFAULT;
+
+ public MessageSerializer serializer = new GraphSONMessageSerializerGremlinV1d0();
+
+ @Test
+ public void shouldSerializeIterable() throws Exception {
+ final ArrayList<Integer> list = new ArrayList<>();
+ list.add(1);
+ list.add(100);
+
+ final ResponseMessage response = convert(list);
+ assertCommon(response);
+
+ final List<Integer> deserializedFunList = (List<Integer>) response.getResult().getData();
+ assertEquals(2, deserializedFunList.size());
+ assertEquals(new Integer(1), deserializedFunList.get(0));
+ assertEquals(new Integer(100), deserializedFunList.get(1));
+ }
+
+ @Test
+ public void shouldSerializeIterableWithNull() throws Exception {
+ final ArrayList<Integer> list = new ArrayList<>();
+ list.add(1);
+ list.add(null);
+ list.add(100);
+
+ final ResponseMessage response = convert(list);
+ assertCommon(response);
+
+ final List<Integer> deserializedFunList = (List<Integer>) response.getResult().getData();
+ assertEquals(3, deserializedFunList.size());
+ assertEquals(new Integer(1), deserializedFunList.get(0));
+ assertNull(deserializedFunList.get(1));
+ assertEquals(new Integer(100), deserializedFunList.get(2));
+ }
+
+ @Test
+ public void shouldSerializeMap() throws Exception {
+ final Map<String, Object> map = new HashMap<>();
+ final Map<String, String> innerMap = new HashMap<>();
+ innerMap.put("a", "b");
+
+ map.put("x", 1);
+ map.put("y", "some");
+ map.put("z", innerMap);
+
+ final ResponseMessage response = convert(map);
+ assertCommon(response);
+
+ final Map<String, Object> deserializedMap = (Map<String, Object>) response.getResult().getData();
+ assertEquals(3, deserializedMap.size());
+ assertEquals(1, deserializedMap.get("x"));
+ assertEquals("some", deserializedMap.get("y"));
+
+ final Map<String, String> deserializedInnerMap = (Map<String, String>) deserializedMap.get("z");
+ assertEquals(1, deserializedInnerMap.size());
+ assertEquals("b", deserializedInnerMap.get("a"));
+ }
+
+ @Test
+ public void shouldSerializeMapEntries() throws Exception {
+ final Graph graph = TinkerGraph.open();
+ final Vertex v1 = graph.addVertex();
+ final Date d = new Date();
+
+ final Map<Object, Object> map = new HashMap<>();
+ map.put("x", 1);
+ map.put(v1, 100);
+ map.put(d, "test");
+
+ final ResponseMessage response = convert(IteratorUtils.asList(map.entrySet()));
+ assertCommon(response);
+
+ final List<Map<String, Object>> deserializedEntries = (List<Map<String, Object>>) response.getResult().getData();
+ assertEquals(3, deserializedEntries.size());
+ deserializedEntries.forEach(e -> {
+ if (e.containsKey("x"))
+ assertEquals(1, e.get("x"));
+ else if (e.containsKey(v1.id().toString()))
+ assertEquals(100, e.get(v1.id().toString()));
+ else if (e.containsKey(StdDateFormat.instance.format(d)))
+ assertEquals("test", e.get(StdDateFormat.instance.format(d)));
+ else
+ fail("Map entries contains a key that is not part of what was serialized");
+ });
+ }
+
+ @Test
+ public void shouldSerializeEdge() throws Exception {
+ final Graph graph = TinkerGraph.open();
+ final Vertex v1 = graph.addVertex();
+ final Vertex v2 = graph.addVertex();
+ final Edge e = v1.addEdge("test", v2);
+ e.property("abc", 123);
+
+ final Iterable<Edge> iterable = IteratorUtils.list(graph.edges());
+
+ final ResponseMessage response = convert(iterable);
+ assertCommon(response);
+
+ final List<Map<String, Object>> edgeList = (List<Map<String, Object>>) response.getResult().getData();
+ assertEquals(1, edgeList.size());
+
+ final Map<String, Object> deserializedEdge = edgeList.get(0);
+ assertEquals(e.id(), deserializedEdge.get(GraphSONTokens.ID));
+ assertEquals(v1.id(), deserializedEdge.get(GraphSONTokens.OUT));
+ assertEquals(v2.id(), deserializedEdge.get(GraphSONTokens.IN));
+ assertEquals(v1.label(), deserializedEdge.get(GraphSONTokens.OUT_LABEL));
+ assertEquals(v2.label(), deserializedEdge.get(GraphSONTokens.IN_LABEL));
+ assertEquals(e.label(), deserializedEdge.get(GraphSONTokens.LABEL));
+ assertEquals(GraphSONTokens.EDGE, deserializedEdge.get(GraphSONTokens.TYPE));
+
+ final Map<String, Object> properties = (Map<String, Object>) deserializedEdge.get(GraphSONTokens.PROPERTIES);
+ assertNotNull(properties);
+ assertEquals(123, properties.get("abc"));
+
+ }
+
+ @Test
+ public void shouldSerializeEdgeProperty() throws Exception {
+ final Graph graph = TinkerGraph.open();
+ final Vertex v1 = graph.addVertex();
+ final Vertex v2 = graph.addVertex();
+ final Edge e = v1.addEdge("test", v2);
+ e.property("abc", 123);
+
+ final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc"));
+ final ResponseMessage response = convert(iterable);
+ assertCommon(response);
+
+ final List<Map<String, Object>> propertyList = (List<Map<String, Object>>) response.getResult().getData();
+ assertEquals(1, propertyList.size());
+ assertEquals(123, propertyList.get(0).get("value"));
+ }
+
+ @Test
+ public void shouldSerializeVertexWithEmbeddedMap() throws Exception {
+ final Graph graph = TinkerGraph.open();
+ final Vertex v = graph.addVertex();
+ final Map<String, Object> map = new HashMap<>();
+ map.put("x", 500);
+ map.put("y", "some");
+
+ final ArrayList<Object> friends = new ArrayList<>();
+ friends.add("x");
+ friends.add(5);
+ friends.add(map);
+
+ v.property(VertexProperty.Cardinality.single, "friends", friends);
+
+ final List list = IteratorUtils.list(graph.vertices());
+
+ final ResponseMessage response = convert(list);
+ assertCommon(response);
+
+ final List<Map<String, Object>> vertexList = (List<Map<String, Object>>) response.getResult().getData();
+ assertEquals(1, vertexList.size());
+
+ final Map<String, Object> deserializedVertex = vertexList.get(0);
+ assertEquals(v.id(), deserializedVertex.get(GraphSONTokens.ID));
+ assertEquals(Vertex.DEFAULT_LABEL, deserializedVertex.get(GraphSONTokens.LABEL));
+
+ final Map<String, Object> properties = ((Map<String, Object>) deserializedVertex.get(GraphSONTokens.PROPERTIES));
+ assertEquals(1, properties.size());
+
+ final List<Map<String,Object>> friendsProperties = (List<Map<String,Object>>) properties.get("friends");
+ assertEquals(1, friendsProperties.size());
+
+ final List<Object> deserializedInnerList = (List<Object>) friendsProperties.get(0).get(GraphSONTokens.VALUE);
+ assertEquals(3, deserializedInnerList.size());
+ assertEquals("x", deserializedInnerList.get(0));
+ assertEquals(5, deserializedInnerList.get(1));
+
+ final Map<String, Object> deserializedInnerInnerMap = (Map<String, Object>) deserializedInnerList.get(2);
+ assertEquals(2, deserializedInnerInnerMap.size());
+ assertEquals(500, deserializedInnerInnerMap.get("x"));
+ assertEquals("some", deserializedInnerInnerMap.get("y"));
+ }
+
+ @Test
+ public void shouldSerializeToJsonMapWithElementForKey() throws Exception {
+ final TinkerGraph graph = TinkerFactory.createClassic();
+ final GraphTraversalSource g = graph.traversal();
+ final Map<Vertex, Integer> map = new HashMap<>();
+ map.put(g.V().has("name", "marko").next(), 1000);
+
+ final ResponseMessage response = convert(map);
+ assertCommon(response);
+
+ final Map<String, Integer> deserializedMap = (Map<String, Integer>) response.getResult().getData();
+ assertEquals(1, deserializedMap.size());
+
+ // with no embedded types the key (which is a vertex) simply serializes out to an id
+ // {"result":{"1":1000},"code":200,"requestId":"2d62161b-9544-4f39-af44-62ec49f9a595","type":0}
+ assertEquals(new Integer(1000), deserializedMap.get("1"));
+ }
+
+ @Test
+ public void shouldSerializeToTreeJson() throws Exception {
+ final TinkerGraph graph = TinkerFactory.createClassic();
+ final GraphTraversalSource g = graph.traversal();
+ final Map t = g.V(1).out().properties("name").tree().next();
+
+ final ResponseMessage response = convert(t);
+ assertCommon(response);
+
+ final Map<String, Map<String, Map>> deserializedMap = (Map<String, Map<String, Map>>) response.getResult().getData();
+
+ assertEquals(1, deserializedMap.size());
+
+ //check the first object and it's properties
+ Map<String,Object> vertex = deserializedMap.get("1").get("key");
+ Map<String,List<Map>> vertexProperties = (Map<String, List<Map>>)vertex.get("properties");
+ assertEquals(1, (int)vertex.get("id"));
+ assertEquals("marko", vertexProperties.get("name").get(0).get("value"));
+
+ //check objects tree structure
+ //check Vertex property
+ Map<String, Map<String, Map>> subTreeMap = deserializedMap.get("1").get("value");
+ Map<String, Map<String, Map>> subTreeMap2 = subTreeMap.get("2").get("value");
+ Map<String, String> vertexPropertiesDeep = subTreeMap2.get("3").get("key");
+ assertEquals("vadas", vertexPropertiesDeep.get("value"));
+ assertEquals("name", vertexPropertiesDeep.get("label"));
+
+ // check subitem
+ Map<String,Object> vertex2 = subTreeMap.get("3").get("key");
+ Map<String,List<Map>> vertexProperties2 = (Map<String, List<Map>>)vertex2.get("properties");
+
+ assertEquals("lop", vertexProperties2.get("name").get(0).get("value"));
+ }
+
+ @Test
+ public void shouldSerializeFullResponseMessage() throws Exception {
+ final UUID id = UUID.randomUUID();
+
+ final Map<String, Object> metaData = new HashMap<>();
+ metaData.put("test", "this");
+ metaData.put("one", 1);
+
+ final Map<String, Object> attributes = new HashMap<>();
+ attributes.put("test", "that");
+ attributes.put("two", 2);
+
+ final ResponseMessage response = ResponseMessage.build(id)
+ .responseMetaData(metaData)
+ .code(ResponseStatusCode.SUCCESS)
+ .result("some-result")
+ .statusAttributes(attributes)
+ .statusMessage("worked")
+ .create();
+
+ final ByteBuf bb = serializer.serializeResponseAsBinary(response, allocator);
+ final ResponseMessage deserialized = serializer.deserializeResponse(bb);
+
+ assertEquals(id, deserialized.getRequestId());
+ assertEquals("this", deserialized.getResult().getMeta().get("test"));
+ assertEquals(1, deserialized.getResult().getMeta().get("one"));
+ assertEquals("some-result", deserialized.getResult().getData());
+ assertEquals("that", deserialized.getStatus().getAttributes().get("test"));
+ assertEquals(2, deserialized.getStatus().getAttributes().get("two"));
+ assertEquals(ResponseStatusCode.SUCCESS.getValue(), deserialized.getStatus().getCode().getValue());
+ assertEquals("worked", deserialized.getStatus().getMessage());
+ }
+
+ private void assertCommon(final ResponseMessage response) {
+ assertEquals(requestId, response.getRequestId());
+ assertEquals(ResponseStatusCode.SUCCESS, response.getStatus().getCode());
+ }
+
+ private ResponseMessage convert(final Object toSerialize) throws SerializationException {
+ final ByteBuf bb = serializer.serializeResponseAsBinary(responseMessageBuilder.result(toSerialize).create(), allocator);
+ return serializer.deserializeResponse(bb);
+ }
+}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTestV2d0.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTestV2d0.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTestV2d0.java
new file mode 100644
index 0000000..a7f8389
--- /dev/null
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTestV2d0.java
@@ -0,0 +1,341 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.driver.ser;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.buffer.UnpooledByteBufAllocator;
+import org.apache.tinkerpop.gremlin.driver.MessageSerializer;
+import org.apache.tinkerpop.gremlin.driver.message.ResponseMessage;
+import org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.structure.Property;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
+import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.apache.tinkerpop.shaded.jackson.databind.util.StdDateFormat;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+/**
+ * Serializer tests that cover non-lossy serialization/deserialization methods.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GraphSONMessageSerializerGremlinTestV2d0 {
+
+ private UUID requestId = UUID.fromString("6457272A-4018-4538-B9AE-08DD5DDC0AA1");
+ private ResponseMessage.Builder responseMessageBuilder = ResponseMessage.build(requestId);
+ private static ByteBufAllocator allocator = UnpooledByteBufAllocator.DEFAULT;
+
+ public MessageSerializer serializer = new GraphSONMessageSerializerGremlinV2d0();
+
+ @Test
+ public void shouldSerializeIterable() throws Exception {
+ final ArrayList<Integer> list = new ArrayList<>();
+ list.add(1);
+ list.add(100);
+
+ final ResponseMessage response = convert(list);
+ assertCommon(response);
+
+ final List<Integer> deserializedFunList = (List<Integer>) response.getResult().getData();
+ assertEquals(2, deserializedFunList.size());
+ assertEquals(new Integer(1), deserializedFunList.get(0));
+ assertEquals(new Integer(100), deserializedFunList.get(1));
+ }
+
+ @Test
+ public void shouldSerializeIterableWithNull() throws Exception {
+ final ArrayList<Integer> list = new ArrayList<>();
+ list.add(1);
+ list.add(null);
+ list.add(100);
+
+ final ResponseMessage response = convert(list);
+ assertCommon(response);
+
+ final List<Integer> deserializedFunList = (List<Integer>) response.getResult().getData();
+ assertEquals(3, deserializedFunList.size());
+ assertEquals(new Integer(1), deserializedFunList.get(0));
+ assertNull(deserializedFunList.get(1));
+ assertEquals(new Integer(100), deserializedFunList.get(2));
+ }
+
+ @Test
+ public void shouldSerializeMap() throws Exception {
+ final Map<String, Object> map = new HashMap<>();
+ final Map<String, String> innerMap = new HashMap<>();
+ innerMap.put("a", "b");
+
+ map.put("x", 1);
+ map.put("y", "some");
+ map.put("z", innerMap);
+
+ final ResponseMessage response = convert(map);
+ assertCommon(response);
+
+ final Map<String, Object> deserializedMap = (Map<String, Object>) response.getResult().getData();
+ assertEquals(3, deserializedMap.size());
+ assertEquals(1, deserializedMap.get("x"));
+ assertEquals("some", deserializedMap.get("y"));
+
+ final Map<String, String> deserializedInnerMap = (Map<String, String>) deserializedMap.get("z");
+ assertEquals(1, deserializedInnerMap.size());
+ assertEquals("b", deserializedInnerMap.get("a"));
+ }
+
+ @Test
+ public void shouldSerializeMapEntries() throws Exception {
+ final Graph graph = TinkerGraph.open();
+ final Vertex v1 = graph.addVertex();
+ final Date d = new Date();
+
+ final Map<Object, Object> map = new HashMap<>();
+ map.put("x", 1);
+ map.put(v1, 100);
+ map.put(d, "test");
+
+ final ResponseMessage response = convert(IteratorUtils.asList(map.entrySet()));
+ assertCommon(response);
+
+ final List<Map<String, Object>> deserializedEntries = (List<Map<String, Object>>) response.getResult().getData();
+ assertEquals(3, deserializedEntries.size());
+ deserializedEntries.forEach(e -> {
+ if (e.containsKey("x"))
+ assertEquals(1, e.get("x"));
+ else if (e.containsKey(v1.id().toString()))
+ assertEquals(100, e.get(v1.id().toString()));
+ else if (e.containsKey(StdDateFormat.instance.format(d)))
+ assertEquals("test", e.get(StdDateFormat.instance.format(d)));
+ else
+ fail("Map entries contains a key that is not part of what was serialized");
+ });
+ }
+
+ @Test
+ public void shouldSerializeEdge() throws Exception {
+ final Graph graph = TinkerGraph.open();
+ final Vertex v1 = graph.addVertex();
+ final Vertex v2 = graph.addVertex();
+ final Edge e = v1.addEdge("test", v2);
+ e.property("abc", 123);
+
+ final Iterable<Edge> iterable = IteratorUtils.list(graph.edges());
+
+ final ResponseMessage response = convert(iterable);
+ assertCommon(response);
+
+ final List<Edge> edgeList = (List<Edge>) response.getResult().getData();
+ assertEquals(1, edgeList.size());
+
+ final Edge deserializedEdge = edgeList.get(0);
+ assertEquals(e.id(), deserializedEdge.id());
+ assertEquals(v1.id(), deserializedEdge.outVertex().id());
+ assertEquals(v2.id(), deserializedEdge.inVertex().id());
+ assertEquals(v1.label(), deserializedEdge.outVertex().label());
+ assertEquals(v2.label(), deserializedEdge.inVertex().label());
+ assertEquals(e.label(), deserializedEdge.label());
+
+ final List<Property> properties = new ArrayList<>();
+ deserializedEdge.properties().forEachRemaining(properties::add);
+ assertEquals(1, properties.size());
+
+ assertNotNull(properties);
+ assertEquals("abc", properties.get(0).key());
+ assertEquals(123, properties.get(0).value());
+
+ }
+
+ @Test
+ public void shouldSerializeEdgeProperty() throws Exception {
+ final Graph graph = TinkerGraph.open();
+ final Vertex v1 = graph.addVertex();
+ final Vertex v2 = graph.addVertex();
+ final Edge e = v1.addEdge("test", v2);
+ e.property("abc", 123);
+
+ final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc"));
+ final ResponseMessage response = convert(iterable);
+ assertCommon(response);
+
+ final List<Property> propertyList = (List<Property>) response.getResult().getData();
+ assertEquals(1, propertyList.size());
+ assertEquals(123, propertyList.get(0).value());
+ }
+
+ @Test
+ public void shouldSerializeVertexWithEmbeddedMap() throws Exception {
+ final Graph graph = TinkerGraph.open();
+ final Vertex v = graph.addVertex();
+ final Map<String, Object> map = new HashMap<>();
+ map.put("x", 500);
+ map.put("y", "some");
+
+ final ArrayList<Object> friends = new ArrayList<>();
+ friends.add("x");
+ friends.add(5);
+ friends.add(map);
+
+ v.property(VertexProperty.Cardinality.single, "friends", friends);
+
+ final List list = IteratorUtils.list(graph.vertices());
+
+ final ResponseMessage response = convert(list);
+ assertCommon(response);
+
+ final List<Vertex> vertexList = (List<Vertex>) response.getResult().getData();
+ assertEquals(1, vertexList.size());
+
+ final Vertex deserializedVertex = vertexList.get(0);
+ assertEquals(v.id(), deserializedVertex.id());
+ assertEquals(Vertex.DEFAULT_LABEL, deserializedVertex.label());
+
+ final List<VertexProperty> properties = new ArrayList<>();
+ deserializedVertex.properties().forEachRemaining(properties::add);
+ assertEquals(1, properties.size());
+
+ final VertexProperty friendsProperty = properties.get(0);
+ final List<Object> deserializedInnerList = (List<Object>) friendsProperty.value();
+
+ assertEquals(3, deserializedInnerList.size());
+ assertEquals("x", deserializedInnerList.get(0));
+ assertEquals(5, deserializedInnerList.get(1));
+
+ final Map<String, Object> deserializedInnerInnerMap = (Map<String, Object>) deserializedInnerList.get(2);
+ assertEquals(2, deserializedInnerInnerMap.size());
+ assertEquals(500, deserializedInnerInnerMap.get("x"));
+ assertEquals("some", deserializedInnerInnerMap.get("y"));
+ }
+
+ @Test
+ public void shouldSerializeToJsonMapWithElementForKey() throws Exception {
+ final TinkerGraph graph = TinkerFactory.createClassic();
+ final GraphTraversalSource g = graph.traversal();
+ final Map<Vertex, Integer> map = new HashMap<>();
+ map.put(g.V().has("name", "marko").next(), 1000);
+
+ final ResponseMessage response = convert(map);
+ assertCommon(response);
+
+ final Map<String, Integer> deserializedMap = (Map<String, Integer>) response.getResult().getData();
+ assertEquals(1, deserializedMap.size());
+
+ // with no embedded types the key (which is a vertex) simply serializes out to an id
+ // {"result":{"1":1000},"code":200,"requestId":"2d62161b-9544-4f39-af44-62ec49f9a595","type":0}
+ assertEquals(new Integer(1000), deserializedMap.get("1"));
+ }
+
+ @Test
+ public void shouldSerializeToTreeJson() throws Exception {
+ final TinkerGraph graph = TinkerFactory.createClassic();
+ final GraphTraversalSource g = graph.traversal();
+ final Map t = g.V(1).out().properties("name").tree().next();
+
+ final ResponseMessage response = convert(t);
+ assertCommon(response);
+
+ final Tree deserializedTree = (Tree)response.getResult().getData();
+
+ //check the first object and its key's properties
+ assertEquals(1, deserializedTree.size());
+ Vertex v = ((Vertex) deserializedTree.keySet().iterator().next());
+ assertEquals(1, v.id());
+ assertEquals("marko", v.property("name").value());
+
+ Tree firstTree = (Tree)deserializedTree.get(v);
+ assertEquals(3, firstTree.size());
+ Iterator<Vertex> vertexKeys = firstTree.keySet().iterator();
+
+ Tree t2 = (Tree)firstTree.get(vertexKeys.next());
+ VertexProperty vp = (VertexProperty)t2.keySet().iterator().next();
+ assertEquals(3, vp.id());
+ assertEquals("vadas", vp.value());
+
+ t2 = (Tree) firstTree.get(vertexKeys.next());
+ vp = (VertexProperty) t2.keySet().iterator().next();
+ assertEquals(5, vp.id());
+ assertEquals("lop", vp.value());
+
+ t2 = (Tree) firstTree.get(vertexKeys.next());
+ vp = (VertexProperty) t2.keySet().iterator().next();
+ assertEquals(7, vp.id());
+ assertEquals("josh", vp.value());
+ }
+
+ @Test
+ public void shouldSerializeFullResponseMessage() throws Exception {
+ final UUID id = UUID.randomUUID();
+
+ final Map<String, Object> metaData = new HashMap<>();
+ metaData.put("test", "this");
+ metaData.put("one", 1);
+
+ final Map<String, Object> attributes = new HashMap<>();
+ attributes.put("test", "that");
+ attributes.put("two", 2);
+
+ final ResponseMessage response = ResponseMessage.build(id)
+ .responseMetaData(metaData)
+ .code(ResponseStatusCode.SUCCESS)
+ .result("some-result")
+ .statusAttributes(attributes)
+ .statusMessage("worked")
+ .create();
+
+ final ByteBuf bb = serializer.serializeResponseAsBinary(response, allocator);
+ final ResponseMessage deserialized = serializer.deserializeResponse(bb);
+
+ assertEquals(id, deserialized.getRequestId());
+ assertEquals("this", deserialized.getResult().getMeta().get("test"));
+ assertEquals(1, deserialized.getResult().getMeta().get("one"));
+ assertEquals("some-result", deserialized.getResult().getData());
+ assertEquals("that", deserialized.getStatus().getAttributes().get("test"));
+ assertEquals(2, deserialized.getStatus().getAttributes().get("two"));
+ assertEquals(ResponseStatusCode.SUCCESS.getValue(), deserialized.getStatus().getCode().getValue());
+ assertEquals("worked", deserialized.getStatus().getMessage());
+ }
+
+ private void assertCommon(final ResponseMessage response) {
+ assertEquals(requestId, response.getRequestId());
+ assertEquals(ResponseStatusCode.SUCCESS, response.getStatus().getCode());
+ }
+
+ private ResponseMessage convert(final Object toSerialize) throws SerializationException {
+ final ByteBuf bb = serializer.serializeResponseAsBinary(responseMessageBuilder.result(toSerialize).create(), allocator);
+ return serializer.deserializeResponse(bb);
+ }
+}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/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
deleted file mode 100644
index 0091a5f..0000000
--- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV1d0Test.java
+++ /dev/null
@@ -1,334 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tinkerpop.gremlin.driver.ser;
-
-import org.apache.tinkerpop.gremlin.driver.MessageSerializer;
-import org.apache.tinkerpop.gremlin.driver.message.ResponseMessage;
-import org.apache.tinkerpop.gremlin.driver.message.ResponseStatusCode;
-import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
-import org.apache.tinkerpop.gremlin.structure.Edge;
-import org.apache.tinkerpop.gremlin.structure.Graph;
-import org.apache.tinkerpop.gremlin.structure.Property;
-import org.apache.tinkerpop.gremlin.structure.Vertex;
-import org.apache.tinkerpop.gremlin.structure.VertexProperty;
-import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONTokens;
-import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
-import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufAllocator;
-import io.netty.buffer.UnpooledByteBufAllocator;
-import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
-import org.apache.tinkerpop.shaded.jackson.databind.util.StdDateFormat;
-import org.junit.Test;
-
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.fail;
-
-/**
- * Serializer tests that cover non-lossy serialization/deserialization methods.
- *
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-public class GraphSONMessageSerializerGremlinV1d0Test {
- private UUID requestId = UUID.fromString("6457272A-4018-4538-B9AE-08DD5DDC0AA1");
- private ResponseMessage.Builder responseMessageBuilder = ResponseMessage.build(requestId);
- private static ByteBufAllocator allocator = UnpooledByteBufAllocator.DEFAULT;
-
- public MessageSerializer serializer = new GraphSONMessageSerializerGremlinV1d0();
-
- @Test
- public void shouldSerializeIterable() throws Exception {
- final ArrayList<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(100);
-
- final ResponseMessage response = convert(list);
- assertCommon(response);
-
- final List<Integer> deserializedFunList = (List<Integer>) response.getResult().getData();
- assertEquals(2, deserializedFunList.size());
- assertEquals(new Integer(1), deserializedFunList.get(0));
- assertEquals(new Integer(100), deserializedFunList.get(1));
- }
-
- @Test
- public void shouldSerializeIterableWithNull() throws Exception {
- final ArrayList<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(null);
- list.add(100);
-
- final ResponseMessage response = convert(list);
- assertCommon(response);
-
- final List<Integer> deserializedFunList = (List<Integer>) response.getResult().getData();
- assertEquals(3, deserializedFunList.size());
- assertEquals(new Integer(1), deserializedFunList.get(0));
- assertNull(deserializedFunList.get(1));
- assertEquals(new Integer(100), deserializedFunList.get(2));
- }
-
- @Test
- public void shouldSerializeMap() throws Exception {
- final Map<String, Object> map = new HashMap<>();
- final Map<String, String> innerMap = new HashMap<>();
- innerMap.put("a", "b");
-
- map.put("x", 1);
- map.put("y", "some");
- map.put("z", innerMap);
-
- final ResponseMessage response = convert(map);
- assertCommon(response);
-
- final Map<String, Object> deserializedMap = (Map<String, Object>) response.getResult().getData();
- assertEquals(3, deserializedMap.size());
- assertEquals(1, deserializedMap.get("x"));
- assertEquals("some", deserializedMap.get("y"));
-
- final Map<String, String> deserializedInnerMap = (Map<String, String>) deserializedMap.get("z");
- assertEquals(1, deserializedInnerMap.size());
- assertEquals("b", deserializedInnerMap.get("a"));
- }
-
- @Test
- public void shouldSerializeMapEntries() throws Exception {
- final Graph graph = TinkerGraph.open();
- final Vertex v1 = graph.addVertex();
- final Date d = new Date();
-
- final Map<Object, Object> map = new HashMap<>();
- map.put("x", 1);
- map.put(v1, 100);
- map.put(d, "test");
-
- final ResponseMessage response = convert(IteratorUtils.asList(map.entrySet()));
- assertCommon(response);
-
- final List<Map<String, Object>> deserializedEntries = (List<Map<String, Object>>) response.getResult().getData();
- assertEquals(3, deserializedEntries.size());
- deserializedEntries.forEach(e -> {
- if (e.containsKey("x"))
- assertEquals(1, e.get("x"));
- else if (e.containsKey(v1.id().toString()))
- assertEquals(100, e.get(v1.id().toString()));
- else if (e.containsKey(StdDateFormat.instance.format(d)))
- assertEquals("test", e.get(StdDateFormat.instance.format(d)));
- else
- fail("Map entries contains a key that is not part of what was serialized");
- });
- }
-
- @Test
- public void shouldSerializeEdge() throws Exception {
- final Graph graph = TinkerGraph.open();
- final Vertex v1 = graph.addVertex();
- final Vertex v2 = graph.addVertex();
- final Edge e = v1.addEdge("test", v2);
- e.property("abc", 123);
-
- final Iterable<Edge> iterable = IteratorUtils.list(graph.edges());
-
- final ResponseMessage response = convert(iterable);
- assertCommon(response);
-
- final List<Map<String, Object>> edgeList = (List<Map<String, Object>>) response.getResult().getData();
- assertEquals(1, edgeList.size());
-
- final Map<String, Object> deserializedEdge = edgeList.get(0);
- assertEquals(e.id(), deserializedEdge.get(GraphSONTokens.ID));
- assertEquals(v1.id(), deserializedEdge.get(GraphSONTokens.OUT));
- assertEquals(v2.id(), deserializedEdge.get(GraphSONTokens.IN));
- assertEquals(v1.label(), deserializedEdge.get(GraphSONTokens.OUT_LABEL));
- assertEquals(v2.label(), deserializedEdge.get(GraphSONTokens.IN_LABEL));
- assertEquals(e.label(), deserializedEdge.get(GraphSONTokens.LABEL));
- assertEquals(GraphSONTokens.EDGE, deserializedEdge.get(GraphSONTokens.TYPE));
-
- final Map<String, Object> properties = (Map<String, Object>) deserializedEdge.get(GraphSONTokens.PROPERTIES);
- assertNotNull(properties);
- assertEquals(123, properties.get("abc"));
-
- }
-
- @Test
- public void shouldSerializeEdgeProperty() throws Exception {
- final Graph graph = TinkerGraph.open();
- final Vertex v1 = graph.addVertex();
- final Vertex v2 = graph.addVertex();
- final Edge e = v1.addEdge("test", v2);
- e.property("abc", 123);
-
- final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc"));
- final ResponseMessage response = convert(iterable);
- assertCommon(response);
-
- final List<Map<String, Object>> propertyList = (List<Map<String, Object>>) response.getResult().getData();
- assertEquals(1, propertyList.size());
- assertEquals(123, propertyList.get(0).get("value"));
- }
-
- @Test
- public void shouldSerializeVertexWithEmbeddedMap() throws Exception {
- final Graph graph = TinkerGraph.open();
- final Vertex v = graph.addVertex();
- final Map<String, Object> map = new HashMap<>();
- map.put("x", 500);
- map.put("y", "some");
-
- final ArrayList<Object> friends = new ArrayList<>();
- friends.add("x");
- friends.add(5);
- friends.add(map);
-
- v.property(VertexProperty.Cardinality.single, "friends", friends);
-
- final List list = IteratorUtils.list(graph.vertices());
-
- final ResponseMessage response = convert(list);
- assertCommon(response);
-
- final List<Map<String, Object>> vertexList = (List<Map<String, Object>>) response.getResult().getData();
- assertEquals(1, vertexList.size());
-
- final Map<String, Object> deserializedVertex = vertexList.get(0);
- assertEquals(v.id(), deserializedVertex.get(GraphSONTokens.ID));
- assertEquals(Vertex.DEFAULT_LABEL, deserializedVertex.get(GraphSONTokens.LABEL));
-
- final Map<String, Object> properties = ((Map<String, Object>) deserializedVertex.get(GraphSONTokens.PROPERTIES));
- assertEquals(1, properties.size());
-
- final List<Map<String,Object>> friendsProperties = (List<Map<String,Object>>) properties.get("friends");
- assertEquals(1, friendsProperties.size());
-
- final List<Object> deserializedInnerList = (List<Object>) friendsProperties.get(0).get(GraphSONTokens.VALUE);
- assertEquals(3, deserializedInnerList.size());
- assertEquals("x", deserializedInnerList.get(0));
- assertEquals(5, deserializedInnerList.get(1));
-
- final Map<String, Object> deserializedInnerInnerMap = (Map<String, Object>) deserializedInnerList.get(2);
- assertEquals(2, deserializedInnerInnerMap.size());
- assertEquals(500, deserializedInnerInnerMap.get("x"));
- assertEquals("some", deserializedInnerInnerMap.get("y"));
- }
-
- @Test
- public void shouldSerializeToJsonMapWithElementForKey() throws Exception {
- final TinkerGraph graph = TinkerFactory.createClassic();
- final GraphTraversalSource g = graph.traversal();
- final Map<Vertex, Integer> map = new HashMap<>();
- map.put(g.V().has("name", "marko").next(), 1000);
-
- final ResponseMessage response = convert(map);
- assertCommon(response);
-
- final Map<String, Integer> deserializedMap = (Map<String, Integer>) response.getResult().getData();
- assertEquals(1, deserializedMap.size());
-
- // with no embedded types the key (which is a vertex) simply serializes out to an id
- // {"result":{"1":1000},"code":200,"requestId":"2d62161b-9544-4f39-af44-62ec49f9a595","type":0}
- assertEquals(new Integer(1000), deserializedMap.get("1"));
- }
-
- @Test
- public void shouldSerializeToJsonTree() throws Exception {
- final TinkerGraph graph = TinkerFactory.createClassic();
- final GraphTraversalSource g = graph.traversal();
- final Map t = g.V(1).out().properties("name").tree().next();
-
- final ResponseMessage response = convert(t);
- assertCommon(response);
-
- final Map<String, Map<String, Map>> deserializedMap = (Map<String, Map<String, Map>>) response.getResult().getData();
-
- assertEquals(1, deserializedMap.size());
-
- //check the first object and it's properties
- Map<String,Object> vertex = deserializedMap.get("1").get("key");
- Map<String,List<Map>> vertexProperties = (Map<String, List<Map>>)vertex.get("properties");
- assertEquals(1, (int)vertex.get("id"));
- assertEquals("marko", vertexProperties.get("name").get(0).get("value"));
-
- //check objects tree structure
- //check Vertex property
- Map<String, Map<String, Map>> subTreeMap = deserializedMap.get("1").get("value");
- Map<String, Map<String, Map>> subTreeMap2 = subTreeMap.get("2").get("value");
- Map<String, String> vertexPropertiesDeep = subTreeMap2.get("3").get("key");
- assertEquals("vadas", vertexPropertiesDeep.get("value"));
- assertEquals("name", vertexPropertiesDeep.get("label"));
-
- // check subitem
- Map<String,Object> vertex2 = subTreeMap.get("3").get("key");
- Map<String,List<Map>> vertexProperties2 = (Map<String, List<Map>>)vertex2.get("properties");
-
- assertEquals("lop", vertexProperties2.get("name").get(0).get("value"));
- }
-
- @Test
- public void shouldSerializeFullResponseMessage() throws Exception {
- final UUID id = UUID.randomUUID();
-
- final Map<String, Object> metaData = new HashMap<>();
- metaData.put("test", "this");
- metaData.put("one", 1);
-
- final Map<String, Object> attributes = new HashMap<>();
- attributes.put("test", "that");
- attributes.put("two", 2);
-
- final ResponseMessage response = ResponseMessage.build(id)
- .responseMetaData(metaData)
- .code(ResponseStatusCode.SUCCESS)
- .result("some-result")
- .statusAttributes(attributes)
- .statusMessage("worked")
- .create();
-
- final ByteBuf bb = serializer.serializeResponseAsBinary(response, allocator);
- final ResponseMessage deserialized = serializer.deserializeResponse(bb);
-
- assertEquals(id, deserialized.getRequestId());
- assertEquals("this", deserialized.getResult().getMeta().get("test"));
- assertEquals(1, deserialized.getResult().getMeta().get("one"));
- assertEquals("some-result", deserialized.getResult().getData());
- assertEquals("that", deserialized.getStatus().getAttributes().get("test"));
- assertEquals(2, deserialized.getStatus().getAttributes().get("two"));
- assertEquals(ResponseStatusCode.SUCCESS.getValue(), deserialized.getStatus().getCode().getValue());
- assertEquals("worked", deserialized.getStatus().getMessage());
- }
-
- private void assertCommon(final ResponseMessage response) {
- assertEquals(requestId, response.getRequestId());
- assertEquals(ResponseStatusCode.SUCCESS, response.getStatus().getCode());
- }
-
- private ResponseMessage convert(final Object toSerialize) throws SerializationException {
- final ByteBuf bb = serializer.serializeResponseAsBinary(responseMessageBuilder.result(toSerialize).create(), allocator);
- return serializer.deserializeResponse(bb);
- }
-}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/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 a4cee54..bf1ddcb 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
@@ -22,6 +22,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.process.traversal.dsl.graph.GraphTraversalSource;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Property;
@@ -35,21 +36,16 @@ import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.apache.tinkerpop.shaded.jackson.core.JsonGenerationException;
import org.apache.tinkerpop.shaded.jackson.core.JsonGenerator;
-import org.apache.tinkerpop.shaded.jackson.core.JsonParser;
-import org.apache.tinkerpop.shaded.jackson.core.JsonProcessingException;
-import org.apache.tinkerpop.shaded.jackson.databind.DeserializationContext;
import org.apache.tinkerpop.shaded.jackson.databind.JsonNode;
import org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper;
import org.apache.tinkerpop.shaded.jackson.databind.SerializerProvider;
-import org.apache.tinkerpop.shaded.jackson.databind.deser.std.StdDeserializer;
-import org.apache.tinkerpop.shaded.jackson.databind.jsontype.TypeSerializer;
import org.apache.tinkerpop.shaded.jackson.databind.module.SimpleModule;
import org.apache.tinkerpop.shaded.jackson.databind.node.NullNode;
import org.apache.tinkerpop.shaded.jackson.databind.ser.std.StdSerializer;
import org.apache.tinkerpop.shaded.jackson.databind.util.StdDateFormat;
import org.junit.Test;
-import java.awt.*;
+import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -57,7 +53,6 @@ import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
-import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/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
new file mode 100644
index 0000000..de9b493
--- /dev/null
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerV2d0Test.java
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.driver.ser;
+
+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.process.traversal.dsl.graph.GraphTraversalSource;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.structure.Property;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.structure.io.AbstractIoRegistry;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONIo;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONTokens;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
+import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.apache.tinkerpop.shaded.jackson.core.JsonGenerationException;
+import org.apache.tinkerpop.shaded.jackson.core.JsonGenerator;
+import org.apache.tinkerpop.shaded.jackson.databind.JsonNode;
+import org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper;
+import org.apache.tinkerpop.shaded.jackson.databind.SerializerProvider;
+import org.apache.tinkerpop.shaded.jackson.databind.module.SimpleModule;
+import org.apache.tinkerpop.shaded.jackson.databind.node.NullNode;
+import org.apache.tinkerpop.shaded.jackson.databind.ser.std.StdSerializer;
+import org.apache.tinkerpop.shaded.jackson.databind.util.StdDateFormat;
+import org.junit.Test;
+
+import java.awt.Color;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+/**
+ * These tests focus on message serialization and not "result" serialization as test specific to results (e.g.
+ * vertices, edges, annotated values, etc.) are handled in the IO packages.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GraphSONMessageSerializerV2d0Test {
+
+ public static final GraphSONMessageSerializerV2d0 SERIALIZER = new GraphSONMessageSerializerV2d0();
+ private static final RequestMessage msg = RequestMessage.build("op")
+ .overrideRequestId(UUID.fromString("2D62161B-9544-4F39-AF44-62EC49F9A595")).create();
+ private static final ObjectMapper mapper = new ObjectMapper();
+
+ @Test
+ public void shouldConfigureIoRegistry() throws Exception {
+ final GraphSONMessageSerializerV1d0 serializer = new GraphSONMessageSerializerV1d0();
+ final Map<String, Object> config = new HashMap<String, Object>() {{
+ put(GryoMessageSerializerV1d0.TOKEN_IO_REGISTRIES, Arrays.asList(ColorIoRegistry.class.getName()));
+ }};
+
+ serializer.configure(config, null);
+
+ final ResponseMessage toSerialize = ResponseMessage.build(UUID.fromString("2D62161B-9544-4F39-AF44-62EC49F9A595"))
+ .result(Color.RED).create();
+ final String results = serializer.serializeResponseAsString(toSerialize);
+ final JsonNode json = mapper.readTree(results);
+ assertNotNull(json);
+ assertThat(json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA).booleanValue(), is(true));
+ }
+
+ @Test
+ public void shouldSerializeToJsonNullResultReturnsNull() throws Exception {
+ final ResponseMessage message = ResponseMessage.build(msg).create();
+ final String results = SERIALIZER.serializeResponseAsString(message);
+ final JsonNode json = mapper.readTree(results);
+ assertNotNull(json);
+ assertEquals(msg.getRequestId().toString(), json.path(SerTokens.TOKEN_REQUEST).asText());
+ assertEquals(NullNode.getInstance(), json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA));
+ }
+
+ @Test
+ public void shouldSerializeToJsonIterable() throws Exception {
+ final ArrayList<FunObject> funList = new ArrayList<>();
+ funList.add(new FunObject("x"));
+ funList.add(new FunObject("y"));
+
+ final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(funList).create());
+ final JsonNode json = mapper.readTree(results);
+
+ assertNotNull(json);
+ assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
+ final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);
+
+ assertEquals(2, converted.size());
+
+ assertEquals("x", converted.get(0).asText());
+ assertEquals("y", converted.get(1).asText());
+ }
+
+ @Test
+ public void shouldSerializeToJsonIterator() throws Exception {
+ final ArrayList<FunObject> funList = new ArrayList<>();
+ funList.add(new FunObject("x"));
+ funList.add(new FunObject("y"));
+
+ final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(funList).create());
+ final JsonNode json = mapper.readTree(results);
+
+ assertNotNull(json);
+ assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
+ final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);
+
+ assertEquals(2, converted.size());
+
+ assertEquals("x", converted.get(0).asText());
+ assertEquals("y", converted.get(1).asText());
+ }
+
+ @Test
+ public void shouldSerializeToJsonIteratorNullElement() throws Exception {
+
+ final ArrayList<FunObject> funList = new ArrayList<>();
+ funList.add(new FunObject("x"));
+ funList.add(null);
+ funList.add(new FunObject("y"));
+
+ final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(funList.iterator()).create());
+ final JsonNode json = mapper.readTree(results);
+
+ assertNotNull(json);
+ assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
+ final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);
+
+ assertEquals(3, converted.size());
+
+ assertEquals("x", converted.get(0).asText());
+ assertEquals(NullNode.getInstance(), converted.get(1));
+ assertEquals("y", converted.get(2).asText());
+ }
+
+ @Test
+ public void shouldSerializeToJsonMap() throws Exception {
+ final Map<String, Object> map = new HashMap<>();
+ final Map<String, String> innerMap = new HashMap<>();
+ innerMap.put("a", "b");
+
+ map.put("x", new FunObject("x"));
+ map.put("y", "some");
+ map.put("z", innerMap);
+
+ 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);
+
+ assertNotNull(jsonObject);
+ assertEquals("some", jsonObject.get("y").asText());
+ assertEquals("x", jsonObject.get("x").asText());
+
+ final JsonNode innerJsonObject = jsonObject.get("z");
+ assertNotNull(innerJsonObject);
+ assertEquals("b", innerJsonObject.get("a").asText());
+ }
+
+ @Test
+ public void shouldShouldSerializeMapEntries() throws Exception {
+ final Graph graph = TinkerGraph.open();
+ final Vertex v1 = graph.addVertex();
+ final Date d = new Date();
+
+ final Map<Object, Object> map = new HashMap<>();
+ map.put("x", 1);
+ map.put(v1, 100);
+ map.put(d, "test");
+
+ final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(IteratorUtils.asList(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);
+ jsonObject.elements().forEachRemaining(e -> {
+ if (e.has("x"))
+ assertEquals(1, e.get("x").get(GraphSONTokens.VALUEPROP).asInt());
+ else if (e.has(v1.id().toString()))
+ assertEquals(100, e.get(v1.id().toString()).get(GraphSONTokens.VALUEPROP).asInt());
+ else if (e.has(StdDateFormat.instance.format(d)))
+ assertEquals("test", e.get(StdDateFormat.instance.format(d)).asText());
+ else
+ fail("Map entries contains a key that is not part of what was serialized");
+ });
+ }
+
+ @Test
+ public void shouldSerializeEdge() throws Exception {
+ final Graph g = TinkerGraph.open();
+ final Vertex v1 = g.addVertex();
+ final Vertex v2 = g.addVertex();
+ final Edge e = v1.addEdge("test", v2);
+ e.property("abc", 123);
+
+ final Iterable<Edge> iterable = IteratorUtils.list(g.edges());
+ final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());
+
+ final JsonNode json = mapper.readTree(results);
+
+ assertNotNull(json);
+ assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
+ final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);
+
+ assertNotNull(converted);
+ assertEquals(1, converted.size());
+
+ final JsonNode edgeAsJson = converted.get(0).get(GraphSONTokens.VALUEPROP);
+ assertNotNull(edgeAsJson);
+
+ assertEquals(((Long) e.id()).longValue(), edgeAsJson.get(GraphSONTokens.ID).get(GraphSONTokens.VALUEPROP).asLong());
+ assertEquals(((Long) v1.id()).longValue(), edgeAsJson.get(GraphSONTokens.OUT).get(GraphSONTokens.VALUEPROP).asLong());
+ assertEquals(((Long) v2.id()).longValue(), edgeAsJson.get(GraphSONTokens.IN).get(GraphSONTokens.VALUEPROP).asLong());
+ assertEquals(e.label(), edgeAsJson.get(GraphSONTokens.LABEL).asText());
+
+ final JsonNode properties = edgeAsJson.get(GraphSONTokens.PROPERTIES);
+ assertNotNull(properties);
+ assertEquals(123, properties.get("abc").get(GraphSONTokens.VALUEPROP).get("value").get(GraphSONTokens.VALUEPROP).asInt());
+ }
+
+ @Test
+ public void shouldSerializeEdgeProperty() throws Exception {
+ final Graph g = TinkerGraph.open();
+ final Vertex v1 = g.addVertex();
+ final Vertex v2 = g.addVertex();
+ final Edge e = v1.addEdge("test", v2);
+ e.property("abc", 123);
+
+ final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc"));
+ final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());
+
+ final JsonNode json = mapper.readTree(results);
+
+ assertNotNull(json);
+ assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
+ final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);
+
+ assertNotNull(converted);
+ assertEquals(1, converted.size());
+
+ final JsonNode propertyAsJson = converted.get(0);
+ assertNotNull(propertyAsJson);
+
+ assertEquals(123, propertyAsJson.get(GraphSONTokens.VALUEPROP).get("value").get(GraphSONTokens.VALUEPROP).asInt());
+ }
+
+ @Test
+ public void shouldSerializeToJsonIteratorWithEmbeddedMap() throws Exception {
+ final Graph g = TinkerGraph.open();
+ final Vertex v = g.addVertex();
+ final Map<String, Object> map = new HashMap<>();
+ map.put("x", 500);
+ map.put("y", "some");
+
+ final ArrayList<Object> friends = new ArrayList<>();
+ friends.add("x");
+ friends.add(5);
+ friends.add(map);
+
+ v.property(VertexProperty.Cardinality.single, "friends", friends);
+
+ final Iterable iterable = IteratorUtils.list(g.vertices());
+ final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());
+ final JsonNode json = mapper.readTree(results);
+
+ assertNotNull(json);
+ assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
+ final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);
+
+ assertNotNull(converted);
+ assertEquals(1, converted.size());
+
+ final JsonNode vertexAsJson = converted.get(0).get(GraphSONTokens.VALUEPROP);
+ assertNotNull(vertexAsJson);
+
+ final JsonNode properties = vertexAsJson.get(GraphSONTokens.PROPERTIES);
+ assertNotNull(properties);
+ assertEquals(1, properties.size());
+
+ final JsonNode friendProperties = properties.get("friends");
+ assertEquals(1, friendProperties.size());
+ final JsonNode friendsProperty = friendProperties.get(0).get(GraphSONTokens.VALUEPROP);
+ assertNotNull(friendsProperty);
+ assertEquals(3, friendsProperty.size());
+
+ final String object1 = friendsProperty.get(GraphSONTokens.VALUE).get(0).asText();
+ assertEquals("x", object1);
+
+ final int object2 = friendsProperty.get(GraphSONTokens.VALUE).get(1).get(GraphSONTokens.VALUEPROP).asInt();
+ assertEquals(5, object2);
+
+ final JsonNode object3 = friendsProperty.get(GraphSONTokens.VALUE).get(2);
+ assertEquals(500, object3.get("x").get(GraphSONTokens.VALUEPROP).asInt());
+ assertEquals("some", object3.get("y").asText());
+ }
+
+ @Test
+ public void shouldSerializeToJsonMapWithElementForKey() throws Exception {
+ final TinkerGraph graph = TinkerFactory.createClassic();
+ final GraphTraversalSource g = graph.traversal();
+ final Map<Vertex, Integer> map = new HashMap<>();
+ map.put(g.V().has("name", "marko").next(), 1000);
+
+ 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 converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);
+
+ assertNotNull(converted);
+
+ // with no embedded types the key (which is a vertex) simply serializes out to an id
+ // {"result":{"1":1000},"code":200,"requestId":"2d62161b-9544-4f39-af44-62ec49f9a595","type":0}
+ assertEquals(1000, converted.get("1").get(GraphSONTokens.VALUEPROP).asInt());
+ }
+
+ @Test
+ 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());
+ assertEquals("eval", m.getOp());
+ assertNotNull(m.getArgs());
+ assertEquals(0, m.getArgs().size());
+ }
+
+ @Test
+ 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());
+ assertEquals("eval", m.getOp());
+ assertNotNull(m.getArgs());
+ assertEquals("y", m.getArgs().get("x"));
+ }
+
+ @Test(expected = SerializationException.class)
+ public void shouldDeserializeRequestParseMessage() throws Exception {
+ SERIALIZER.deserializeRequest("{\"requestId\":\"%s\",\"op\":\"eval\",\"args\":{\"x\":\"y\"}}");
+ }
+
+ @Test
+ public void shouldSerializeFullResponseMessage() throws Exception {
+ final UUID id = UUID.randomUUID();
+
+ final Map<String, Object> metaData = new HashMap<>();
+ metaData.put("test", "this");
+ metaData.put("one", 1);
+
+ final Map<String, Object> attributes = new HashMap<>();
+ attributes.put("test", "that");
+ attributes.put("two", 2);
+
+ final ResponseMessage response = ResponseMessage.build(id)
+ .responseMetaData(metaData)
+ .code(ResponseStatusCode.SUCCESS)
+ .result("some-result")
+ .statusAttributes(attributes)
+ .statusMessage("worked")
+ .create();
+
+ final String results = SERIALIZER.serializeResponseAsString(response);
+ final ResponseMessage deserialized = SERIALIZER.deserializeResponse(results);
+
+ assertEquals(id, deserialized.getRequestId());
+ assertEquals("this", deserialized.getResult().getMeta().get("test"));
+ assertEquals(1, deserialized.getResult().getMeta().get("one"));
+ assertEquals("some-result", deserialized.getResult().getData());
+ assertEquals("that", deserialized.getStatus().getAttributes().get("test"));
+ assertEquals(2, deserialized.getStatus().getAttributes().get("two"));
+ assertEquals(ResponseStatusCode.SUCCESS.getValue(), deserialized.getStatus().getCode().getValue());
+ assertEquals("worked", deserialized.getStatus().getMessage());
+ }
+
+ @Test
+ public void shouldSerializeToTreeJson() throws Exception {
+ final TinkerGraph graph = TinkerFactory.createClassic();
+ final GraphTraversalSource g = graph.traversal();
+ final Tree t = g.V(1).out().properties("name").tree().next();
+
+
+ final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(t).create());
+
+ final JsonNode json = mapper.readTree(results);
+
+ assertNotNull(json);
+ assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
+ final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);
+ assertNotNull(converted);
+
+ //result is: tree{v1=>tree{vp['name'->'vadas']=>null, vp['name'->'lop']=>null, vp['name'->'josh']=>null}}
+
+ //check the first object and it's properties
+ assertEquals(1, converted.get(GraphSONTokens.VALUEPROP)
+ .get(0)
+ .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP)
+ .get(GraphSONTokens.ID).get(GraphSONTokens.VALUEPROP).asInt());
+
+ assertEquals("marko", converted.get(GraphSONTokens.VALUEPROP)
+ .get(0)
+ .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP)
+ .get(GraphSONTokens.PROPERTIES)
+ .get("name")
+ .get(0).get(GraphSONTokens.VALUEPROP)
+ .get(GraphSONTokens.VALUE).asText());
+
+ //check the leafs
+ assertEquals("vadas", converted.get(GraphSONTokens.VALUEPROP)
+ .get(0)
+ .get(GraphSONTokens.VALUE).get(GraphSONTokens.VALUEPROP)
+ .get(0)
+ .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP)
+ .get(GraphSONTokens.PROPERTIES)
+ .get("name")
+ .get(0).get(GraphSONTokens.VALUEPROP)
+ .get(GraphSONTokens.VALUE).asText());
+
+ assertEquals("lop", converted.get(GraphSONTokens.VALUEPROP)
+ .get(0)
+ .get(GraphSONTokens.VALUE).get(GraphSONTokens.VALUEPROP)
+ .get(1)
+ .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP)
+ .get(GraphSONTokens.PROPERTIES)
+ .get("name")
+ .get(0).get(GraphSONTokens.VALUEPROP)
+ .get(GraphSONTokens.VALUE).asText());
+
+ assertEquals("josh", converted.get(GraphSONTokens.VALUEPROP)
+ .get(0)
+ .get(GraphSONTokens.VALUE).get(GraphSONTokens.VALUEPROP)
+ .get(2)
+ .get(GraphSONTokens.KEY).get(GraphSONTokens.VALUEPROP)
+ .get(GraphSONTokens.PROPERTIES)
+ .get("name")
+ .get(0).get(GraphSONTokens.VALUEPROP)
+ .get(GraphSONTokens.VALUE).asText());
+ }
+
+ private class FunObject {
+ private String val;
+
+ public FunObject(String val) {
+ this.val = val;
+ }
+
+ public String toString() {
+ return this.val;
+ }
+ }
+
+ public static class ColorIoRegistry extends AbstractIoRegistry {
+ public ColorIoRegistry() {
+ register(GraphSONIo.class, null, new ColorSimpleModule());
+ }
+ }
+
+ public static class ColorSimpleModule extends SimpleModule {
+ public ColorSimpleModule() {
+ super("color-fun");
+ addSerializer(Color.class, new ColorSerializer());
+
+ }
+ }
+
+ public static class ColorSerializer extends StdSerializer<Color> {
+ public ColorSerializer() {
+ super(Color.class);
+ }
+
+ @Override
+ public void serialize(final Color color, final JsonGenerator jsonGenerator,
+ final SerializerProvider serializerProvider) throws IOException, JsonGenerationException {
+ jsonGenerator.writeBoolean(color.equals(Color.RED));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/gremlin-server/conf/gremlin-server-classic.yaml
----------------------------------------------------------------------
diff --git a/gremlin-server/conf/gremlin-server-classic.yaml b/gremlin-server/conf/gremlin-server-classic.yaml
index 551e2c0..24923ea 100644
--- a/gremlin-server/conf/gremlin-server-classic.yaml
+++ b/gremlin-server/conf/gremlin-server-classic.yaml
@@ -29,7 +29,7 @@ scriptEngines: {
scripts: [scripts/complex-lifecycle.groovy]}}
serializers:
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v1.0+gryo
- - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoLiteMessageSerializerV1d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v1.0+gryo-lite
+ - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoLiteMessageSerializerV1d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v1.0+gryo-lite
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { serializeResultToString: true }} # application/vnd.gremlin-v1.0+gryo-stringd
metrics: {
slf4jReporter: {enabled: true, interval: 180000}}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/gremlin-server/conf/gremlin-server-min.yaml
----------------------------------------------------------------------
diff --git a/gremlin-server/conf/gremlin-server-min.yaml b/gremlin-server/conf/gremlin-server-min.yaml
index d4471f5..f3e9ddd 100644
--- a/gremlin-server/conf/gremlin-server-min.yaml
+++ b/gremlin-server/conf/gremlin-server-min.yaml
@@ -27,6 +27,7 @@ scriptEngines: {
imports: []}}
serializers:
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v1.0+json
+ - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v2.0+json
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0, config: { useMapperFromGraph: graph }} # application/json
metrics: {
slf4jReporter: {enabled: true, interval: 180000}}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/gremlin-server/conf/gremlin-server-neo4j.yaml
----------------------------------------------------------------------
diff --git a/gremlin-server/conf/gremlin-server-neo4j.yaml b/gremlin-server/conf/gremlin-server-neo4j.yaml
index 8b4f768..7c1544b 100644
--- a/gremlin-server/conf/gremlin-server-neo4j.yaml
+++ b/gremlin-server/conf/gremlin-server-neo4j.yaml
@@ -46,6 +46,7 @@ serializers:
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoLiteMessageSerializerV1d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v1.0+gryo-lite
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { serializeResultToString: true }} # application/vnd.gremlin-v1.0+gryo-stringd
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v1.0+json
+ - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v2.0+json
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0, config: { useMapperFromGraph: graph }} # application/json
processors:
- { className: org.apache.tinkerpop.gremlin.server.op.session.SessionOpProcessor, config: { sessionTimeout: 28800000 }}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/gremlin-server/conf/gremlin-server-rest-modern.yaml
----------------------------------------------------------------------
diff --git a/gremlin-server/conf/gremlin-server-rest-modern.yaml b/gremlin-server/conf/gremlin-server-rest-modern.yaml
index a900204..0fdcb69 100644
--- a/gremlin-server/conf/gremlin-server-rest-modern.yaml
+++ b/gremlin-server/conf/gremlin-server-rest-modern.yaml
@@ -30,6 +30,7 @@ scriptEngines: {
scripts: [scripts/generate-modern.groovy]}}
serializers:
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v1.0+json
+ - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v2.0+json
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0, config: { useMapperFromGraph: graph }} # application/json
metrics: {
slf4jReporter: {enabled: true, interval: 180000}}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/115eb3c7/gremlin-server/conf/gremlin-server-rest-secure.yaml
----------------------------------------------------------------------
diff --git a/gremlin-server/conf/gremlin-server-rest-secure.yaml b/gremlin-server/conf/gremlin-server-rest-secure.yaml
index d017aa4..84c21d7 100644
--- a/gremlin-server/conf/gremlin-server-rest-secure.yaml
+++ b/gremlin-server/conf/gremlin-server-rest-secure.yaml
@@ -43,6 +43,7 @@ scriptEngines: {
"org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.CompileStaticCustomizerProvider":["org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.SimpleSandboxExtension"]}}}}
serializers:
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v1.0+json
+ - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0, config: { useMapperFromGraph: graph }} # application/vnd.gremlin-v2.0+json
- { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0, config: { useMapperFromGraph: graph }} # application/json
processors:
- { className: org.apache.tinkerpop.gremlin.server.op.session.SessionOpProcessor, config: { sessionTimeout: 28800000 }}