Parameterized some tests, re-wrote the IO tests.
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/a9bf7ea5 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/a9bf7ea5 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/a9bf7ea5 Branch: refs/heads/TINKERPOP-1274 Commit: a9bf7ea55acb9bee1377dcc4a7d1966602493db3 Parents: 50f70a3 Author: Kevin Gallardo <[email protected]> Authored: Thu Jun 30 10:29:00 2016 +0100 Committer: Kevin Gallardo <[email protected]> Committed: Thu Jun 30 10:41:35 2016 +0100 ---------------------------------------------------------------------- .../GraphSONMapperEmbeddedTypeTest.java | 17 +- .../io/graphson/GraphSONMapperTest.java | 17 +- ...aphSONMapperV2d0PartialEmbeddedTypeTest.java | 115 ++---- .../io/graphson/GraphSONMapperV2d0Test.java | 187 ---------- .../GraphSONMessageSerializerGremlinTest.java | 349 +++++++++++++++++++ ...raphSONMessageSerializerGremlinV1d0Test.java | 333 ------------------ ...raphSONMessageSerializerGremlinV2d0Test.java | 333 ------------------ .../structure/IoDataGenerationTest.java | 192 +++++----- .../TinkerGraphGraphSONSerializerV2d0Test.java | 31 -- 9 files changed, 515 insertions(+), 1059 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/a9bf7ea5/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java index db16ea7..1bae35d 100644 --- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java +++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java @@ -20,6 +20,8 @@ package org.apache.tinkerpop.gremlin.structure.io.graphson; import org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -37,11 +39,24 @@ import java.time.Year; import java.time.YearMonth; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.util.Arrays; import static org.junit.Assert.assertEquals; +@RunWith(Parameterized.class) public class GraphSONMapperEmbeddedTypeTest { - private final ObjectMapper mapper = GraphSONMapper.build().embedTypes(true).create().createMapper(); + + @Parameterized.Parameters(name = "{0}") + public static Iterable<Object[]> data() { + return Arrays.asList(new Object[][]{ + {GraphSONMapper.build().version(GraphSONVersion.V1_0).embedTypes(true).create().createMapper()}, + {GraphSONMapper.build().version(GraphSONVersion.V2_0).typeInfo(GraphSONMapper.TypeInfo.PARTIAL_TYPES).create() + .createMapper()}, + }); + } + + @Parameterized.Parameter + public ObjectMapper mapper; @Test public void shouldHandleDuration()throws Exception { http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/a9bf7ea5/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperTest.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperTest.java index 41e24c6..ea5bc7d 100644 --- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperTest.java +++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperTest.java @@ -21,6 +21,8 @@ package org.apache.tinkerpop.gremlin.structure.io.graphson; import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalExplanation; import org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import java.time.Duration; import java.time.Instant; @@ -35,12 +37,25 @@ import java.time.Year; import java.time.YearMonth; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.util.Arrays; import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.__; import static org.junit.Assert.assertEquals; +@RunWith(Parameterized.class) public class GraphSONMapperTest { - private final ObjectMapper mapper = GraphSONMapper.build().embedTypes(false).create().createMapper(); + + @Parameterized.Parameters(name = "{0}") + public static Iterable<Object[]> data() { + return Arrays.asList(new Object[][]{ + {GraphSONMapper.build().version(GraphSONVersion.V1_0).embedTypes(false).create().createMapper()}, + {GraphSONMapper.build().version(GraphSONVersion.V2_0).typeInfo(GraphSONMapper.TypeInfo.NO_TYPES).create().createMapper()}, + }); + } + + @Parameterized.Parameter + public ObjectMapper mapper; + @Test public void shouldHandleTraversalExplanation() throws Exception { http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/a9bf7ea5/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java index 3969c46..5856bae 100644 --- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java +++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java @@ -46,6 +46,7 @@ import java.util.UUID; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.fail; /** @@ -60,83 +61,6 @@ public class GraphSONMapperV2d0PartialEmbeddedTypeTest { .createMapper(); @Test - public void shouldHandleDuration()throws Exception { - final Duration o = Duration.ZERO; - assertEquals(o, serializeDeserialize(o, Duration.class)); - } - @Test - public void shouldHandleInstant()throws Exception { - final Instant o = Instant.ofEpochMilli(System.currentTimeMillis()); - assertEquals(o, serializeDeserialize(o, Instant.class)); - } - - @Test - public void shouldHandleLocalDate()throws Exception { - final LocalDate o = LocalDate.now(); - assertEquals(o, serializeDeserialize(o, LocalDate.class)); - } - - @Test - public void shouldHandleLocalDateTime()throws Exception { - final LocalDateTime o = LocalDateTime.now(); - assertEquals(o, serializeDeserialize(o, LocalDateTime.class)); - } - - @Test - public void shouldHandleLocalTime()throws Exception { - final LocalTime o = LocalTime.now(); - assertEquals(o, serializeDeserialize(o, LocalTime.class)); - } - - @Test - public void shouldHandleMonthDay()throws Exception { - final MonthDay o = MonthDay.now(); - assertEquals(o, serializeDeserialize(o, MonthDay.class)); - } - - @Test - public void shouldHandleOffsetDateTime()throws Exception { - final OffsetDateTime o = OffsetDateTime.now(); - assertEquals(o, serializeDeserialize(o, OffsetDateTime.class)); - } - - @Test - public void shouldHandleOffsetTime()throws Exception { - final OffsetTime o = OffsetTime.now(); - assertEquals(o, serializeDeserialize(o, OffsetTime.class)); - } - - @Test - public void shouldHandlePeriod()throws Exception { - final Period o = Period.ofDays(3); - assertEquals(o, serializeDeserialize(o, Period.class)); - } - - @Test - public void shouldHandleYear()throws Exception { - final Year o = Year.now(); - assertEquals(o, serializeDeserialize(o, Year.class)); - } - - @Test - public void shouldHandleYearMonth()throws Exception { - final YearMonth o = YearMonth.now(); - assertEquals(o, serializeDeserialize(o, YearMonth.class)); - } - - @Test - public void shouldHandleZonedDateTime()throws Exception { - final ZonedDateTime o = ZonedDateTime.now(); - assertEquals(o, serializeDeserialize(o, ZonedDateTime.class)); - } - - @Test - public void shouldHandleZonedOffset()throws Exception { - final ZoneOffset o = ZonedDateTime.now().getOffset(); - assertEquals(o, serializeDeserialize(o, ZoneOffset.class)); - } - - @Test public void shouldHandleDurationAuto() throws Exception { final Duration o = Duration.ZERO; assertEquals(o, serializeDeserializeAuto(o)); @@ -216,6 +140,7 @@ public class GraphSONMapperV2d0PartialEmbeddedTypeTest { @Test public void shouldSerializeDeserializeNestedCollectionsAndMapAndTypedValuesCorrectly() throws Exception { + // Trying to fail the TypeDeserializer type detection UUID uuid = UUID.randomUUID(); List myList = new ArrayList<>(); @@ -263,6 +188,42 @@ public class GraphSONMapperV2d0PartialEmbeddedTypeTest { assertEquals(funObject.getClass(), serializeDeserialize(funObject, FunObject.class).getClass()); } + @Test + public void shouldLooseTypesInfoWithGraphSONNoType() throws Exception { + ObjectMapper mapper = GraphSONMapper.build() + .version(GraphSONVersion.V2_0) + .typeInfo(GraphSONMapper.TypeInfo.NO_TYPES) + .create() + .createMapper(); + + UUID uuid = UUID.randomUUID(); + List myList = new ArrayList<>(); + + List myList2 = new ArrayList<>(); + myList2.add(UUID.randomUUID()); + myList2.add(33L); + myList2.add(84); + Map map2 = new HashMap<>(); + map2.put("eheh", UUID.randomUUID()); + map2.put("normal", "normal"); + myList2.add(map2); + + Map<String, Object> map1 = new HashMap<>(); + map1.put("hello", "world"); + map1.put("test", uuid); + map1.put("hehe", myList2); + myList.add(map1); + + myList.add("kjkj"); + myList.add(UUID.randomUUID()); + + String json = mapper.writeValueAsString(myList); + Object read = mapper.readValue(json, Object.class); + + // Not equals because of type loss + assertNotEquals(myList, read); + } + // Class needs to be defined as statics as it's a nested class. public static class FunObject { http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/a9bf7ea5/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0Test.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0Test.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0Test.java deleted file mode 100644 index e6a5b6f..0000000 --- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0Test.java +++ /dev/null @@ -1,187 +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.structure.io.graphson; - -import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalExplanation; -import org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper; -import org.junit.Test; - -import java.time.Duration; -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.MonthDay; -import java.time.OffsetDateTime; -import java.time.OffsetTime; -import java.time.Period; -import java.time.Year; -import java.time.YearMonth; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.__; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; - -/** - * Tests non-typed serialization/deserialization for GraphSON 2.0. - */ - -public class GraphSONMapperV2d0Test { - private final ObjectMapper mapper = GraphSONMapper.build() - .version(GraphSONVersion.V2_0) - .typeInfo(GraphSONMapper.TypeInfo.NO_TYPES) - .create() - .createMapper(); - - @Test - public void shouldHandleTraversalExplanation() throws Exception { - final TraversalExplanation te = __().out().outV().outE().explain(); - final String json = mapper.writeValueAsString(te); - assertEquals("{\"original\":[\"InjectStep([])\",\"VertexStep(OUT,vertex)\",\"EdgeVertexStep(OUT)\",\"VertexStep(OUT,edge)\"],\"intermediate\":[],\"final\":[\"InjectStep([])\",\"VertexStep(OUT,vertex)\",\"EdgeVertexStep(OUT)\",\"VertexStep(OUT,edge)\"]}", json); - } - - @Test - public void shouldHandleDuration() throws Exception { - final Duration o = Duration.ZERO; - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleInstant() throws Exception { - final Instant o = Instant.ofEpochMilli(System.currentTimeMillis()); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleLocalDate() throws Exception { - final LocalDate o = LocalDate.now(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleLocalDateTime() throws Exception { - final LocalDateTime o = LocalDateTime.now(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleLocalTime() throws Exception { - final LocalTime o = LocalTime.now(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleMonthDay() throws Exception { - final MonthDay o = MonthDay.now(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleOffsetDateTime() throws Exception { - final OffsetDateTime o = OffsetDateTime.now(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleOffsetTime() throws Exception { - final OffsetTime o = OffsetTime.now(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandlePeriod() throws Exception { - final Period o = Period.ofDays(3); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleYear() throws Exception { - final Year o = Year.now(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleYearMonth() throws Exception { - final YearMonth o = YearMonth.now(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleZonedDateTime() throws Exception { - final ZonedDateTime o = ZonedDateTime.now(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldHandleZoneOffset() throws Exception { - final ZoneOffset o = ZonedDateTime.now().getOffset(); - final String json = mapper.writeValueAsString(o); - assertEquals("\"" + o.toString() + "\"", json); - } - - @Test - public void shouldSerializeDeserializeNestedCollectionsAndMapAndTypedCollectionsCorrectly() throws Exception { - UUID uuid = UUID.randomUUID(); - List myList = new ArrayList<>(); - - List myList2 = new ArrayList<>(); - myList2.add(UUID.randomUUID()); - myList2.add(33L); - myList2.add(84); - Map map2 = new HashMap<>(); - map2.put("eheh", UUID.randomUUID()); - map2.put("normal", "normal"); - myList2.add(map2); - - Map<String, Object> map1 = new HashMap<>(); - map1.put("hello", "world"); - map1.put("test", uuid); - map1.put("hehe", myList2); - myList.add(map1); - - myList.add("kjkj"); - myList.add(UUID.randomUUID()); - - String json = mapper.writeValueAsString(myList); - Object read = mapper.readValue(json, Object.class); - - // Not equals because of type loss - assertNotEquals(myList, read); - } - -} http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/a9bf7ea5/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTest.java ---------------------------------------------------------------------- diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTest.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTest.java new file mode 100644 index 0000000..ad69ae4 --- /dev/null +++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinTest.java @@ -0,0 +1,349 @@ +/* + * 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.GraphSONMapper; +import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONTokens; +import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONVersion; +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 org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.ArrayList; +import java.util.Arrays; +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 non-lossy serialization/deserialization methods. + * + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +@RunWith(Parameterized.class) +public class GraphSONMessageSerializerGremlinTest { + + @Parameterized.Parameters(name = "{0}") + public static Iterable<Object[]> data() { + return Arrays.asList(new Object[][]{ + {new GraphSONMessageSerializerGremlinV1d0()}, + {new GraphSONMessageSerializerGremlinV2d0()}, + }); + } + + private UUID requestId = UUID.fromString("6457272A-4018-4538-B9AE-08DD5DDC0AA1"); + private ResponseMessage.Builder responseMessageBuilder = ResponseMessage.build(requestId); + private static ByteBufAllocator allocator = UnpooledByteBufAllocator.DEFAULT; + + @Parameterized.Parameter + public MessageSerializer serializer; + + @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/a9bf7ea5/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 c47b7b6..0000000 --- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV1d0Test.java +++ /dev/null @@ -1,333 +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 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 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/a9bf7ea5/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV2d0Test.java ---------------------------------------------------------------------- diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV2d0Test.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV2d0Test.java deleted file mode 100644 index 2708e7b..0000000 --- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ser/GraphSONMessageSerializerGremlinV2d0Test.java +++ /dev/null @@ -1,333 +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 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 non-lossy serialization/deserialization methods for GraphSONMessage. - * - * @author Stephen Mallette (http://stephen.genoprime.com) - */ -public class GraphSONMessageSerializerGremlinV2d0Test { - 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<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/a9bf7ea5/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/IoDataGenerationTest.java ---------------------------------------------------------------------- diff --git a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/IoDataGenerationTest.java b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/IoDataGenerationTest.java index 71a086c..46d787a 100644 --- a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/IoDataGenerationTest.java +++ b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/IoDataGenerationTest.java @@ -25,14 +25,11 @@ import org.apache.tinkerpop.gremlin.algorithm.generator.DistributionGenerator; import org.apache.tinkerpop.gremlin.algorithm.generator.PowerLawDistribution; 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.T; import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.apache.tinkerpop.gremlin.structure.VertexProperty; import org.apache.tinkerpop.gremlin.structure.io.GraphReader; import org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper; -import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONVersion; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter; import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoReader; @@ -41,16 +38,12 @@ import org.junit.BeforeClass; import org.junit.Test; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.Iterator; import java.util.stream.IntStream; -import static org.junit.Assert.assertEquals; - /** * Less of a test of functionality and more of a tool to help generate data files for TinkerPop. * @@ -181,29 +174,95 @@ public class IoDataGenerationTest { os.close(); } + /** + * No assertions. Just write out the graph for convenience. + */ @Test - public void shouldWriteAndReadClassicGraphAsGraphSONV2d0WithTypes() throws IOException { - Graph g = TinkerFactory.createClassic(); - final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-classic-V2d0-typed.json"); - GraphSONWriter.build().mapper(GraphSONMapper.build().version(GraphSONVersion.V2_0).typeInfo(GraphSONMapper.TypeInfo.PARTIAL_TYPES).create()) - .create().writeGraph(os, g); + public void shouldWriteModernGraphAsGraphSONWithTypes() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-modern-typed.json"); + GraphSONWriter.build().mapper(GraphSONMapper.build().embedTypes(true).create()) + .create().writeGraph(os, TinkerFactory.createModern()); os.close(); + } - Graph readG = TinkerGraph.open(); - final InputStream is = new FileInputStream(tempPath + "tinkerpop-classic-V2d0-typed.json"); - GraphSONReader.build().mapper(GraphSONMapper.build().version(GraphSONVersion.V2_0).typeInfo(GraphSONMapper.TypeInfo.PARTIAL_TYPES).create()).create().readGraph(is, readG); - is.close(); + /** + * No assertions. Just write out the graph for convenience. + */ + @Test + public void shouldWriteCrewGraphAsGraphSONWithTypes() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-crew-typed.json"); + GraphSONWriter.build().mapper(GraphSONMapper.build().embedTypes(true).create()) + .create().writeGraph(os, TinkerFactory.createTheCrew()); + os.close(); + } - assertEquals(approximateGraphsCheck(g, readG), true); + /** + * No assertions. Just write out the graph for convenience. + */ + @Test + public void shouldWriteClassicGraphAsGraphSONV2d0NoTypes() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-classic-v2d0.json"); + GraphSONWriter.build().create().writeGraph(os, TinkerFactory.createClassic()); + os.close(); } + /** + * No assertions. Just write out the graph for convenience. + */ + @Test + public void shouldWriteModernGraphAsGraphSOV2d0NNoTypes() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-modern-v2d0.json"); + GraphSONWriter.build().create().writeGraph(os, TinkerFactory.createModern()); + os.close(); + } /** * No assertions. Just write out the graph for convenience. */ @Test - public void shouldWriteModernGraphAsGraphSONWithTypes() throws IOException { - final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-modern-typed.json"); + public void shouldWriteCrewGraphAsGraphSONV2d0NoTypes() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-crew-v2d0.json"); + GraphSONWriter.build().create().writeGraph(os, TinkerFactory.createTheCrew()); + os.close(); + } + + /** + * No assertions. Just write out the graph for convenience. + */ + @Test + public void shouldWriteClassicGraphNormalizedAsGraphSONV2d0() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-classic-normalized-v2d0.json"); + GraphSONWriter.build().mapper(GraphSONMapper.build().normalize(true).create()).create().writeGraph(os, TinkerFactory.createClassic()); + os.close(); + } + + /** + * No assertions. Just write out the graph for convenience. + */ + @Test + public void shouldWriteModernGraphNormalizedAsGraphSONV2d0() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-modern-normalized-v2d0.json"); + GraphSONWriter.build().mapper(GraphSONMapper.build().normalize(true).create()).create().writeGraph(os, TinkerFactory.createModern()); + os.close(); + } + + /** + * No assertions. Just write out the graph for convenience. + */ + @Test + public void shouldWriteClassicGraphAsGraphSONV2d0WithTypes() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-classic-v2d0-typed.json"); + GraphSONWriter.build().mapper(GraphSONMapper.build().embedTypes(true).create()) + .create().writeGraph(os, TinkerFactory.createClassic()); + os.close(); + } + + /** + * No assertions. Just write out the graph for convenience. + */ + @Test + public void shouldWriteModernGraphAsGraphSONV2d0WithTypes() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-modern-v2d0-typed.json"); GraphSONWriter.build().mapper(GraphSONMapper.build().embedTypes(true).create()) .create().writeGraph(os, TinkerFactory.createModern()); os.close(); @@ -213,8 +272,8 @@ public class IoDataGenerationTest { * No assertions. Just write out the graph for convenience. */ @Test - public void shouldWriteCrewGraphAsGraphSONWithTypes() throws IOException { - final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-crew-typed.json"); + public void shouldWriteCrewGraphAsGraphSONV2d0WithTypes() throws IOException { + final OutputStream os = new FileOutputStream(tempPath + "tinkerpop-crew-v2d0-typed.json"); GraphSONWriter.build().mapper(GraphSONMapper.build().embedTypes(true).create()) .create().writeGraph(os, TinkerFactory.createTheCrew()); os.close(); @@ -292,86 +351,27 @@ public class IoDataGenerationTest { GraphSONWriter.build().mapper(GraphSONMapper.build().create()).create().writeGraph(os2, g); os2.close(); - final OutputStream os3 = new FileOutputStream(tempPath + "grateful-dead.xml"); - GraphMLWriter.build().create().writeGraph(os3, g); + final OutputStream os3 = new FileOutputStream(tempPath + "grateful-dead-v2d0.json"); + GraphSONWriter.build().mapper(GraphSONMapper.build().version(GraphSONVersion.V2_0) + .typeInfo(GraphSONMapper.TypeInfo.NO_TYPES).create()) + .create() + .writeGraph(os3, g); os3.close(); - final OutputStream os4 = new FileOutputStream(tempPath + "grateful-dead-typed.json"); - GraphSONWriter.build().mapper(GraphSONMapper.build().embedTypes(true).create()).create().writeGraph(os4, g); + final OutputStream os4 = new FileOutputStream(tempPath + "grateful-dead.xml"); + GraphMLWriter.build().create().writeGraph(os4, g); os4.close(); - } - @Test - public void shouldWriteGratefulDeadGraphSONV2d0() throws IOException { - final TinkerGraph g = TinkerGraph.open(); - final TinkerGraph readG = TinkerGraph.open(); + final OutputStream os5 = new FileOutputStream(tempPath + "grateful-dead-typed.json"); + GraphSONWriter.build().mapper(GraphSONMapper.build().embedTypes(true).create()).create().writeGraph(os5, g); + os5.close(); - final GraphReader reader = GryoReader.build().create(); - try (final InputStream stream = AbstractGremlinTest.class.getResourceAsStream("/org/apache/tinkerpop/gremlin/structure/io/gryo/grateful-dead.kryo")) { - reader.readGraph(stream, g); - } - final OutputStream os2 = new FileOutputStream(tempPath + "grateful-dead-V2d0-typed.json"); - GraphSONWriter.build().mapper(GraphSONMapper.build().version(GraphSONVersion.V2_0).typeInfo(GraphSONMapper.TypeInfo.PARTIAL_TYPES).create()).create().writeGraph(os2, g); - os2.close(); - - final InputStream is = new FileInputStream(tempPath + "grateful-dead-V2d0-typed.json"); - GraphSONReader.build().mapper(GraphSONMapper.build().version(GraphSONVersion.V2_0).typeInfo(GraphSONMapper.TypeInfo.PARTIAL_TYPES).create()).create().readGraph(is, readG); - is.close(); - - assertEquals(approximateGraphsCheck(g, readG), true); - } - - /** - * Checks sequentially vertices and egdes of both graphs. Will check sequentially Vertex IDs, Vertex Properties IDs - * and values and classes. Then same for edges. To use when serializing a Graph and deserializing the supposedly - * same Graph. - */ - private boolean approximateGraphsCheck(Graph g1, Graph g2) { - Iterator<Vertex> itV = g1.vertices(); - Iterator<Vertex> itVRead = g2.vertices(); - - while (itV.hasNext()) { - Vertex v = itV.next(); - Vertex vRead = itVRead.next(); - // Will only check IDs but that's 'good' enough. - if (!v.equals(vRead)) { - return false; - } - - Iterator itVP = v.properties(); - Iterator itVPRead = vRead.properties(); - while (itVP.hasNext()) { - VertexProperty vp = (VertexProperty) itVP.next(); - VertexProperty vpRead = (VertexProperty) itVPRead.next(); - if (!vp.value().equals(vpRead.value())) { - return false; - } - } - } + final OutputStream os6 = new FileOutputStream(tempPath + "grateful-dead-v2d0-typed.json"); + GraphSONWriter.build().mapper(GraphSONMapper.build().version(GraphSONVersion.V2_0) + .typeInfo(GraphSONMapper.TypeInfo.PARTIAL_TYPES).create()) + .create() + .writeGraph(os6, g); + os6.close(); - - Iterator<Edge> itE = g1.edges(); - Iterator<Edge> itERead = g2.edges(); - - while (itE.hasNext()) { - Edge e = itE.next(); - Edge eRead = itERead.next(); - // Will only check IDs but that's good enough. - if (!e.equals(eRead)) { - return false; - } - - Iterator itEP = e.properties(); - Iterator itEPRead = eRead.properties(); - while (itEP.hasNext()) { - Property ep = (Property) itEP.next(); - Property epRead = (Property) itEPRead.next(); - if (!ep.value().equals(epRead.value()) - || !ep.equals(epRead)) { - return false; - } - } - } - return true; } } http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/a9bf7ea5/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGraphSONSerializerV2d0Test.java ---------------------------------------------------------------------- diff --git a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGraphSONSerializerV2d0Test.java b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGraphSONSerializerV2d0Test.java index 2f87c8d..a4c03cb 100644 --- a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGraphSONSerializerV2d0Test.java +++ b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGraphSONSerializerV2d0Test.java @@ -56,37 +56,6 @@ public class TinkerGraphGraphSONSerializerV2d0Test { .create(); @Test - public void shouldSerializeTinkerGraphToGraphSONWithPartialTypes() throws IOException { - GraphWriter writer = getWriter(defaultMapperV2d0); - try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { - writer.writeGraph(out, baseModern); - String json = out.toString(); - assertEquals(json, "{\"id\":1,\"label\":\"person\",\"outE\":{\"created\":[{\"id\":9,\"inV\":3,\"properties\":{\"weight\":0.4}}],\"knows\":[{\"id\":7,\"inV\":2,\"properties\":{\"weight\":0.5}},{\"id\":8,\"inV\":4,\"properties\":{\"weight\":1.0}}]},\"properties\":{\"name\":[{\"id\":[{\"@class\":\"Long\"},0],\"value\":\"marko\"}],\"age\":[{\"id\":[{\"@class\":\"Long\"},1],\"value\":29}]}}\n" + - "{\"id\":2,\"label\":\"person\",\"inE\":{\"knows\":[{\"id\":7,\"outV\":1,\"properties\":{\"weight\":0.5}}]},\"properties\":{\"name\":[{\"id\":[{\"@class\":\"Long\"},2],\"value\":\"vadas\"}],\"age\":[{\"id\":[{\"@class\":\"Long\"},3],\"value\":27}]}}\n" + - "{\"id\":3,\"label\":\"software\",\"inE\":{\"created\":[{\"id\":9,\"outV\":1,\"properties\":{\"weight\":0.4}},{\"id\":11,\"outV\":4,\"properties\":{\"weight\":0.4}},{\"id\":12,\"outV\":6,\"properties\":{\"weight\":0.2}}]},\"properties\":{\"name\":[{\"id\":[{\"@class\":\"Long\"},4],\"value\":\"lop\"}],\"lang\":[{\"id\":[{\"@class\":\"Long\"},5],\"value\":\"java\"}]}}\n" + - "{\"id\":4,\"label\":\"person\",\"inE\":{\"knows\":[{\"id\":8,\"outV\":1,\"properties\":{\"weight\":1.0}}]},\"outE\":{\"created\":[{\"id\":10,\"inV\":5,\"properties\":{\"weight\":1.0}},{\"id\":11,\"inV\":3,\"properties\":{\"weight\":0.4}}]},\"properties\":{\"name\":[{\"id\":[{\"@class\":\"Long\"},6],\"value\":\"josh\"}],\"age\":[{\"id\":[{\"@class\":\"Long\"},7],\"value\":32}]}}\n" + - "{\"id\":5,\"label\":\"software\",\"inE\":{\"created\":[{\"id\":10,\"outV\":4,\"properties\":{\"weight\":1.0}}]},\"properties\":{\"name\":[{\"id\":[{\"@class\":\"Long\"},8],\"value\":\"ripple\"}],\"lang\":[{\"id\":[{\"@class\":\"Long\"},9],\"value\":\"java\"}]}}\n" + - "{\"id\":6,\"label\":\"person\",\"outE\":{\"created\":[{\"id\":12,\"inV\":3,\"properties\":{\"weight\":0.2}}]},\"properties\":{\"name\":[{\"id\":[{\"@class\":\"Long\"},10],\"value\":\"peter\"}],\"age\":[{\"id\":[{\"@class\":\"Long\"},11],\"value\":35}]}}\n"); - - } - } - - @Test - public void shouldSerializeTinkerGraphToGraphSONWithoutTypes() throws IOException { - GraphWriter writer = getWriter(noTypesMapperV2d0); - try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { - writer.writeGraph(out, baseModern); - String json = out.toString(); - assertEquals(json, "{\"id\":1,\"label\":\"person\",\"outE\":{\"created\":[{\"id\":9,\"inV\":3,\"properties\":{\"weight\":0.4}}],\"knows\":[{\"id\":7,\"inV\":2,\"properties\":{\"weight\":0.5}},{\"id\":8,\"inV\":4,\"properties\":{\"weight\":1.0}}]},\"properties\":{\"name\":[{\"id\":0,\"value\":\"marko\"}],\"age\":[{\"id\":1,\"value\":29}]}}\n" + - "{\"id\":2,\"label\":\"person\",\"inE\":{\"knows\":[{\"id\":7,\"outV\":1,\"properties\":{\"weight\":0.5}}]},\"properties\":{\"name\":[{\"id\":2,\"value\":\"vadas\"}],\"age\":[{\"id\":3,\"value\":27}]}}\n" + - "{\"id\":3,\"label\":\"software\",\"inE\":{\"created\":[{\"id\":9,\"outV\":1,\"properties\":{\"weight\":0.4}},{\"id\":11,\"outV\":4,\"properties\":{\"weight\":0.4}},{\"id\":12,\"outV\":6,\"properties\":{\"weight\":0.2}}]},\"properties\":{\"name\":[{\"id\":4,\"value\":\"lop\"}],\"lang\":[{\"id\":5,\"value\":\"java\"}]}}\n" + - "{\"id\":4,\"label\":\"person\",\"inE\":{\"knows\":[{\"id\":8,\"outV\":1,\"properties\":{\"weight\":1.0}}]},\"outE\":{\"created\":[{\"id\":10,\"inV\":5,\"properties\":{\"weight\":1.0}},{\"id\":11,\"inV\":3,\"properties\":{\"weight\":0.4}}]},\"properties\":{\"name\":[{\"id\":6,\"value\":\"josh\"}],\"age\":[{\"id\":7,\"value\":32}]}}\n" + - "{\"id\":5,\"label\":\"software\",\"inE\":{\"created\":[{\"id\":10,\"outV\":4,\"properties\":{\"weight\":1.0}}]},\"properties\":{\"name\":[{\"id\":8,\"value\":\"ripple\"}],\"lang\":[{\"id\":9,\"value\":\"java\"}]}}\n" + - "{\"id\":6,\"label\":\"person\",\"outE\":{\"created\":[{\"id\":12,\"inV\":3,\"properties\":{\"weight\":0.2}}]},\"properties\":{\"name\":[{\"id\":10,\"value\":\"peter\"}],\"age\":[{\"id\":11,\"value\":35}]}}\n"); - } - } - - @Test public void shouldDeserializeGraphSONIntoTinkerGraphWithPartialTypes() throws IOException { GraphWriter writer = getWriter(defaultMapperV2d0); GraphReader reader = getReader(defaultMapperV2d0);
