use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method jsonFromElementVertexIntIterablePropertiesNoKeysWithTypes.
@Test
public void jsonFromElementVertexIntIterablePropertiesNoKeysWithTypes() throws JSONException {
Vertex v = this.graph.addVertex(1);
Set<Integer> list = new HashSet<Integer>();
list.add(0);
list.add(1);
list.add(2);
v.setProperty("keyList", list);
JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED);
Assert.assertNotNull(json);
Assert.assertTrue(json.has(GraphSONTokens._ID));
Assert.assertEquals(1, json.optInt(GraphSONTokens._ID));
Assert.assertTrue(json.has("keyList"));
JSONObject listWithTypeAsJson = json.optJSONObject("keyList");
Assert.assertNotNull(listWithTypeAsJson);
Assert.assertTrue(listWithTypeAsJson.has(GraphSONTokens.TYPE));
Assert.assertEquals(GraphSONTokens.TYPE_LIST, listWithTypeAsJson.optString(GraphSONTokens.TYPE));
Assert.assertTrue(listWithTypeAsJson.has(GraphSONTokens.VALUE));
JSONArray listAsJSON = listWithTypeAsJson.optJSONArray(GraphSONTokens.VALUE);
Assert.assertNotNull(listAsJSON);
Assert.assertEquals(3, listAsJSON.length());
for (int ix = 0; ix < listAsJSON.length(); ix++) {
JSONObject valueAsJson = listAsJSON.optJSONObject(ix);
Assert.assertNotNull(valueAsJson);
Assert.assertTrue(valueAsJson.has(GraphSONTokens.TYPE));
Assert.assertEquals(GraphSONTokens.TYPE_INTEGER, valueAsJson.optString(GraphSONTokens.TYPE));
Assert.assertTrue(valueAsJson.has(GraphSONTokens.VALUE));
Assert.assertEquals(ix, valueAsJson.optInt(GraphSONTokens.VALUE));
}
}
use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method edgeFromJsonNormalLabelOrIdOnEdge.
@Test
public void edgeFromJsonNormalLabelOrIdOnEdge() 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);
Edge e = GraphSONUtility.edgeFromJson(new JSONObject(new JSONTokener(edgeJsonLight)), v1, v2, factory, GraphSONMode.NORMAL, null);
Assert.assertSame(v1, g.getVertex(1));
Assert.assertSame(v2, g.getVertex(2));
Assert.assertSame(e, g.getEdge(0));
}
use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method jsonFromElementNullsNoKeysNoTypes.
@Test
public void jsonFromElementNullsNoKeysNoTypes() throws JSONException {
Graph g = new TinkerGraph();
Vertex v = g.addVertex(1);
Map<String, Object> map = new HashMap<String, Object>();
map.put("innerkey", null);
List<String> innerList = new ArrayList<String>();
innerList.add(null);
innerList.add("innerstring");
map.put("list", innerList);
v.setProperty("keyMap", map);
List<String> list = new ArrayList<String>();
list.add(null);
list.add("string");
v.setProperty("keyList", list);
JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL);
Assert.assertNotNull(json);
Assert.assertTrue(json.isNull("key"));
JSONObject jsonMap = json.optJSONObject("keyMap");
Assert.assertNotNull(jsonMap);
Assert.assertTrue(jsonMap.isNull("innerkey"));
JSONArray jsonInnerArray = jsonMap.getJSONArray("list");
Assert.assertNotNull(jsonInnerArray);
Assert.assertTrue(jsonInnerArray.isNull(0));
Assert.assertEquals("innerstring", jsonInnerArray.get(1));
JSONArray jsonArray = json.getJSONArray("keyList");
Assert.assertNotNull(jsonArray);
Assert.assertTrue(jsonArray.isNull(0));
Assert.assertEquals("string", jsonArray.get(1));
}
use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method vertexFromJsonIgnoreKeyValid.
@Test
public void vertexFromJsonIgnoreKeyValid() throws IOException, JSONException {
Graph g = new TinkerGraph();
ElementFactory factory = new GraphElementFactory(g);
Set<String> ignoreAge = new HashSet<String>();
ignoreAge.add("age");
ElementPropertyConfig config = ElementPropertyConfig.excludeProperties(ignoreAge, null);
GraphSONUtility utility = new GraphSONUtility(GraphSONMode.NORMAL, factory, config);
Vertex v = utility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)));
Assert.assertSame(v, g.getVertex(1));
// tinkergraph converts id to string
Assert.assertEquals("1", v.getId());
Assert.assertEquals("marko", v.getProperty("name"));
Assert.assertNull(v.getProperty("age"));
}
use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method jsonFromElementEdgeCompactAllKeys.
@Test
public void jsonFromElementEdgeCompactAllKeys() throws JSONException {
Vertex v1 = this.graph.addVertex(1);
Vertex v2 = this.graph.addVertex(2);
Edge e = this.graph.addEdge(3, v1, v2, "test");
e.setProperty("weight", 0.5f);
JSONObject json = GraphSONUtility.jsonFromElement(e, null, GraphSONMode.COMPACT);
Assert.assertNotNull(json);
Assert.assertTrue(json.has(GraphSONTokens._ID));
Assert.assertTrue(json.has(GraphSONTokens._TYPE));
Assert.assertTrue(json.has(GraphSONTokens._LABEL));
Assert.assertTrue(json.has(GraphSONTokens._IN_V));
Assert.assertTrue(json.has(GraphSONTokens._OUT_V));
Assert.assertEquals(0.5d, json.optDouble("weight"), 0.0d);
}
Aggregations