use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.
the class GraphSONReaderTest method inputGraphExtendedFullCycle.
@Test
public void inputGraphExtendedFullCycle() throws IOException {
TinkerGraph graph = TinkerGraphFactory.createTinkerGraph();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
GraphSONWriter writer = new GraphSONWriter(graph);
writer.outputGraph(stream, null, null, GraphSONMode.EXTENDED);
stream.flush();
stream.close();
String jsonString = new String(stream.toByteArray());
byte[] bytes = jsonString.getBytes();
InputStream inputStream = new ByteArrayInputStream(bytes);
TinkerGraph emptyGraph = new TinkerGraph();
GraphSONReader.inputGraph(emptyGraph, inputStream);
Assert.assertEquals(6, getIterableCount(emptyGraph.getVertices()));
Assert.assertEquals(6, getIterableCount(emptyGraph.getEdges()));
for (Vertex v : graph.getVertices()) {
Vertex found = emptyGraph.getVertex(v.getId());
Assert.assertNotNull(v);
for (String key : found.getPropertyKeys()) {
Assert.assertEquals(v.getProperty(key), found.getProperty(key));
}
}
for (Edge e : graph.getEdges()) {
Edge found = emptyGraph.getEdge(e.getId());
Assert.assertNotNull(e);
for (String key : found.getPropertyKeys()) {
Assert.assertEquals(e.getProperty(key), found.getProperty(key));
}
}
}
use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.
the class GraphSONUtilityTest method edgeFromJsonStringValid.
@Test
public void edgeFromJsonStringValid() throws IOException, JSONException {
Graph g = new TinkerGraph();
ElementFactory factory = new GraphElementFactory(g);
Vertex v1 = GraphSONUtility.vertexFromJson(vertexJson1, factory, GraphSONMode.NORMAL, null);
Vertex v2 = GraphSONUtility.vertexFromJson(vertexJson2, factory, GraphSONMode.NORMAL, null);
Edge e = GraphSONUtility.edgeFromJson(edgeJson, v1, v2, factory, GraphSONMode.NORMAL, null);
Assert.assertSame(v1, g.getVertex(1));
Assert.assertSame(v2, g.getVertex(2));
Assert.assertSame(e, g.getEdge(7));
// tinkergraph converts id to string
Assert.assertEquals("7", e.getId());
Assert.assertEquals(0.5d, e.getProperty("weight"));
Assert.assertEquals("knows", e.getLabel());
Assert.assertEquals(v1, e.getVertex(Direction.OUT));
Assert.assertEquals(v2, e.getVertex(Direction.IN));
}
use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.
the class GraphSONUtilityTest method edgeFromJsonIgnoreWeightValid.
@Test
public void edgeFromJsonIgnoreWeightValid() throws IOException, JSONException {
Graph g = new TinkerGraph();
ElementFactory factory = new GraphElementFactory(g);
Vertex v1 = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)), factory, GraphSONMode.NORMAL, null);
Vertex v2 = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson2)), factory, GraphSONMode.NORMAL, null);
Set<String> ignoreWeight = new HashSet<String>();
ignoreWeight.add("weight");
ElementPropertyConfig config = ElementPropertyConfig.excludeProperties(null, ignoreWeight);
GraphSONUtility utility = new GraphSONUtility(GraphSONMode.NORMAL, factory, config);
Edge e = utility.edgeFromJson(new JSONObject(new JSONTokener(edgeJson)), v1, v2);
Assert.assertSame(v1, g.getVertex(1));
Assert.assertSame(v2, g.getVertex(2));
Assert.assertSame(e, g.getEdge(7));
// tinkergraph converts id to string
Assert.assertEquals("7", e.getId());
Assert.assertNull(e.getProperty("weight"));
Assert.assertEquals("knows", e.getLabel());
Assert.assertEquals(v1, e.getVertex(Direction.OUT));
Assert.assertEquals(v2, e.getVertex(Direction.IN));
}
use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.
the class GraphSONUtilityTest method vertexFromJsonInputStreamValid.
@Test
public void vertexFromJsonInputStreamValid() throws IOException, JSONException {
Graph g = new TinkerGraph();
ElementFactory factory = new GraphElementFactory(g);
Vertex v = GraphSONUtility.vertexFromJson(inputStreamVertexJson1, factory, GraphSONMode.NORMAL, null);
Assert.assertSame(v, g.getVertex(1));
// tinkergraph converts id to string
Assert.assertEquals("1", v.getId());
Assert.assertEquals("marko", v.getProperty("name"));
Assert.assertEquals(29, v.getProperty("age"));
}
use of com.tinkerpop.blueprints.impls.tg.TinkerGraph in project blueprints by tinkerpop.
the class IdGraphTest method testUnsupportCustomVertexOrEdgeIds.
public void testUnsupportCustomVertexOrEdgeIds() throws Exception {
TinkerGraph baseGraph = new TinkerGraph();
IdGraph<TinkerGraph> graph = new IdGraph<TinkerGraph>(baseGraph, true, false);
IdGraph.IdFactory vFactory = new IdGraph.IdFactory() {
private int count = 0;
public Object createId() {
return "vertex" + ++count;
}
};
IdGraph.IdFactory eFactory = new IdGraph.IdFactory() {
private int count = 0;
public Object createId() {
return "vertex" + ++count;
}
};
graph.setVertexIdFactory(vFactory);
graph.setEdgeIdFactory(eFactory);
Vertex v1 = graph.addVertex(null);
assertEquals("vertex1", v1.getId());
Vertex v2 = graph.addVertex(null);
assertEquals("vertex2", v2.getId());
Edge e1 = graph.addEdge(null, v1, v2, "connected-to");
// the custom id factory for edges is not used, because the base graph's ids are used instead
assertFalse(e1.getId().equals("edge1"));
}
Aggregations