18
18
*/
19
19
package org .apache .tinkerpop .gremlin .tinkergraph .structure ;
20
20
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 ;
21
25
import org .apache .tinkerpop .gremlin .structure .Vertex ;
26
+ import org .apache .tinkerpop .gremlin .structure .VertexProperty ;
22
27
import org .apache .tinkerpop .gremlin .structure .io .GraphReader ;
23
28
import org .apache .tinkerpop .gremlin .structure .io .GraphWriter ;
24
29
import org .apache .tinkerpop .gremlin .structure .io .IoTest ;
34
39
import java .io .IOException ;
35
40
import java .nio .ByteBuffer ;
36
41
import java .time .Duration ;
42
+ import java .util .Iterator ;
37
43
import java .util .UUID ;
38
44
39
45
import static org .junit .Assert .assertEquals ;
46
+ import static org .junit .Assert .assertFalse ;
47
+ import static org .junit .Assert .assertTrue ;
40
48
41
49
42
50
public class TinkerGraphGraphSONSerializerV2d0Test {
@@ -55,6 +63,9 @@ public class TinkerGraphGraphSONSerializerV2d0Test {
55
63
.addRegistry (TinkerIoRegistryV2d0 .getInstance ())
56
64
.create ();
57
65
66
+ /**
67
+ * Checks that the graph has been fully ser/deser with types.
68
+ */
58
69
@ Test
59
70
public void shouldDeserializeGraphSONIntoTinkerGraphWithPartialTypes () throws IOException {
60
71
GraphWriter writer = getWriter (defaultMapperV2d0 );
@@ -68,6 +79,9 @@ public void shouldDeserializeGraphSONIntoTinkerGraphWithPartialTypes() throws IO
68
79
}
69
80
}
70
81
82
+ /**
83
+ * Checks that the graph has been fully ser/deser without types.
84
+ */
71
85
@ Test
72
86
public void shouldDeserializeGraphSONIntoTinkerGraphWithoutTypes () throws IOException {
73
87
GraphWriter writer = getWriter (noTypesMapperV2d0 );
@@ -81,6 +95,104 @@ public void shouldDeserializeGraphSONIntoTinkerGraphWithoutTypes() throws IOExce
81
95
}
82
96
}
83
97
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
+ */
84
196
@ Test
85
197
public void shouldKeepTypesWhenDeserializingSerializedTinkerGraph () throws IOException {
86
198
TinkerGraph tg = TinkerGraph .open ();
@@ -122,4 +234,59 @@ private GraphWriter getWriter(Mapper paramMapper) {
122
234
private GraphReader getReader (Mapper paramMapper ) {
123
235
return GraphSONReader .build ().mapper (paramMapper ).create ();
124
236
}
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
+ }
125
292
}
0 commit comments