Skip to content

Commit 16e26b5

Browse files
author
Kevin Gallardo
committed
Improved typed GraphSON deser tests.
1 parent a9bf7ea commit 16e26b5

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGraphSONSerializerV2d0Test.java

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
*/
1919
package org.apache.tinkerpop.gremlin.tinkergraph.structure;
2020

21+
import org.apache.tinkerpop.gremlin.structure.Edge;
22+
import org.apache.tinkerpop.gremlin.structure.Graph;
23+
import org.apache.tinkerpop.gremlin.structure.Property;
24+
import org.apache.tinkerpop.gremlin.structure.T;
2125
import org.apache.tinkerpop.gremlin.structure.Vertex;
26+
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
2227
import org.apache.tinkerpop.gremlin.structure.io.GraphReader;
2328
import org.apache.tinkerpop.gremlin.structure.io.GraphWriter;
2429
import org.apache.tinkerpop.gremlin.structure.io.IoTest;
@@ -34,9 +39,12 @@
3439
import java.io.IOException;
3540
import java.nio.ByteBuffer;
3641
import java.time.Duration;
42+
import java.util.Iterator;
3743
import java.util.UUID;
3844

3945
import static org.junit.Assert.assertEquals;
46+
import static org.junit.Assert.assertFalse;
47+
import static org.junit.Assert.assertTrue;
4048

4149

4250
public class TinkerGraphGraphSONSerializerV2d0Test {
@@ -55,6 +63,9 @@ public class TinkerGraphGraphSONSerializerV2d0Test {
5563
.addRegistry(TinkerIoRegistryV2d0.getInstance())
5664
.create();
5765

66+
/**
67+
* Checks that the graph has been fully ser/deser with types.
68+
*/
5869
@Test
5970
public void shouldDeserializeGraphSONIntoTinkerGraphWithPartialTypes() throws IOException {
6071
GraphWriter writer = getWriter(defaultMapperV2d0);
@@ -68,6 +79,9 @@ public void shouldDeserializeGraphSONIntoTinkerGraphWithPartialTypes() throws IO
6879
}
6980
}
7081

82+
/**
83+
* Checks that the graph has been fully ser/deser without types.
84+
*/
7185
@Test
7286
public void shouldDeserializeGraphSONIntoTinkerGraphWithoutTypes() throws IOException {
7387
GraphWriter writer = getWriter(noTypesMapperV2d0);
@@ -81,6 +95,104 @@ public void shouldDeserializeGraphSONIntoTinkerGraphWithoutTypes() throws IOExce
8195
}
8296
}
8397

98+
/**
99+
* Thorough types verification for Vertex ids, Vertex props, Edge ids, Edge props
100+
*/
101+
@Test
102+
public void shouldDeserializeGraphSONIntoTinkerGraphKeepingTypes() throws IOException {
103+
GraphWriter writer = getWriter(defaultMapperV2d0);
104+
GraphReader reader = getReader(defaultMapperV2d0);
105+
Vertex v1 = baseModern.addVertex(T.id, 100L, "name", "kevin", "uuid", UUID.randomUUID());
106+
v1.addEdge("hello", baseModern.traversal().V().has("name", "marko").next(), T.id, 101L,
107+
"uuid", UUID.randomUUID());
108+
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
109+
writer.writeGraph(out, baseModern);
110+
String json = out.toString();
111+
TinkerGraph read = TinkerGraph.open();
112+
reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
113+
assertTrue(approximateGraphsCheck(baseModern, read));
114+
}
115+
}
116+
117+
/**
118+
* Asserts the approximateGraphsChecks function fails when expected. Vertex ids.
119+
*/
120+
@Test
121+
public void shouldLooseTypesWithGraphSONNoTypesForVertexIds() throws IOException {
122+
GraphWriter writer = getWriter(noTypesMapperV2d0);
123+
GraphReader reader = getReader(noTypesMapperV2d0);
124+
baseModern.addVertex(T.id, 100L, "name", "kevin");
125+
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
126+
writer.writeGraph(out, baseModern);
127+
String json = out.toString();
128+
TinkerGraph read = TinkerGraph.open();
129+
reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
130+
// Should fail on deserialized vertex Id.
131+
assertFalse(approximateGraphsCheck(baseModern, read));
132+
}
133+
}
134+
135+
/**
136+
* Asserts the approximateGraphsChecks function fails when expected. Vertex props.
137+
*/
138+
@Test
139+
public void shouldLooseTypesWithGraphSONNoTypesForVertexProps() throws IOException {
140+
GraphWriter writer = getWriter(noTypesMapperV2d0);
141+
GraphReader reader = getReader(noTypesMapperV2d0);
142+
baseModern.addVertex(T.id, 100, "name", "kevin", "uuid", UUID.randomUUID());
143+
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
144+
writer.writeGraph(out, baseModern);
145+
String json = out.toString();
146+
TinkerGraph read = TinkerGraph.open();
147+
reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
148+
// Should fail on deserialized vertex prop.
149+
assertFalse(approximateGraphsCheck(baseModern, read));
150+
}
151+
}
152+
153+
/**
154+
* Asserts the approximateGraphsChecks function fails when expected. Edge ids.
155+
*/
156+
@Test
157+
public void shouldLooseTypesWithGraphSONNoTypesForEdgeIds() throws IOException {
158+
GraphWriter writer = getWriter(noTypesMapperV2d0);
159+
GraphReader reader = getReader(noTypesMapperV2d0);
160+
Vertex v1 = baseModern.addVertex(T.id, 100, "name", "kevin");
161+
v1.addEdge("hello", baseModern.traversal().V().has("name", "marko").next(), T.id, 101L);
162+
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
163+
writer.writeGraph(out, baseModern);
164+
String json = out.toString();
165+
TinkerGraph read = TinkerGraph.open();
166+
reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
167+
// Should fail on deserialized edge Id.
168+
assertFalse(approximateGraphsCheck(baseModern, read));
169+
}
170+
}
171+
172+
/**
173+
* Asserts the approximateGraphsChecks function fails when expected. Edge props.
174+
*/
175+
@Test
176+
public void shouldLooseTypesWithGraphSONNoTypesForEdgeProps() throws IOException {
177+
GraphWriter writer = getWriter(noTypesMapperV2d0);
178+
GraphReader reader = getReader(noTypesMapperV2d0);
179+
Vertex v1 = baseModern.addVertex(T.id, 100, "name", "kevin");
180+
v1.addEdge("hello", baseModern.traversal().V().has("name", "marko").next(), T.id, 101,
181+
"uuid", UUID.randomUUID());
182+
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
183+
writer.writeGraph(out, baseModern);
184+
String json = out.toString();
185+
TinkerGraph read = TinkerGraph.open();
186+
reader.readGraph(new ByteArrayInputStream(json.getBytes()), read);
187+
// Should fail on deserialized edge prop.
188+
assertFalse(approximateGraphsCheck(baseModern, read));
189+
}
190+
}
191+
192+
/**
193+
* Those kinds of types are declared differently in the GraphSON type deserializer, check that all are handled
194+
* properly.
195+
*/
84196
@Test
85197
public void shouldKeepTypesWhenDeserializingSerializedTinkerGraph() throws IOException {
86198
TinkerGraph tg = TinkerGraph.open();
@@ -122,4 +234,59 @@ private GraphWriter getWriter(Mapper paramMapper) {
122234
private GraphReader getReader(Mapper paramMapper) {
123235
return GraphSONReader.build().mapper(paramMapper).create();
124236
}
237+
238+
/**
239+
* Checks sequentially vertices and egdes of both graphs. Will check sequentially Vertex IDs, Vertex Properties IDs
240+
* and values and classes. Then same for edges. To use when serializing a Graph and deserializing the supposedly
241+
* same Graph.
242+
*/
243+
private boolean approximateGraphsCheck(Graph g1, Graph g2) {
244+
Iterator<Vertex> itV = g1.vertices();
245+
Iterator<Vertex> itVRead = g2.vertices();
246+
247+
while (itV.hasNext()) {
248+
Vertex v = itV.next();
249+
Vertex vRead = itVRead.next();
250+
251+
// Will only check IDs but that's 'good' enough.
252+
if (!v.equals(vRead)) {
253+
return false;
254+
}
255+
256+
Iterator itVP = v.properties();
257+
Iterator itVPRead = vRead.properties();
258+
while (itVP.hasNext()) {
259+
VertexProperty vp = (VertexProperty) itVP.next();
260+
VertexProperty vpRead = (VertexProperty) itVPRead.next();
261+
if (!vp.value().equals(vpRead.value())
262+
|| !vp.equals(vpRead)) {
263+
return false;
264+
}
265+
}
266+
}
267+
268+
Iterator<Edge> itE = g1.edges();
269+
Iterator<Edge> itERead = g2.edges();
270+
271+
while (itE.hasNext()) {
272+
Edge e = itE.next();
273+
Edge eRead = itERead.next();
274+
// Will only check IDs but that's good enough.
275+
if (!e.equals(eRead)) {
276+
return false;
277+
}
278+
279+
Iterator itEP = e.properties();
280+
Iterator itEPRead = eRead.properties();
281+
while (itEP.hasNext()) {
282+
Property ep = (Property) itEP.next();
283+
Property epRead = (Property) itEPRead.next();
284+
if (!ep.value().equals(epRead.value())
285+
|| !ep.equals(epRead)) {
286+
return false;
287+
}
288+
}
289+
}
290+
return true;
291+
}
125292
}

0 commit comments

Comments
 (0)