use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method jsonFromElementVertexMapPropertyNoKeysNoTypes.
@Test
public void jsonFromElementVertexMapPropertyNoKeysNoTypes() throws JSONException {
Vertex v = this.graph.addVertex(1);
Map map = new HashMap();
map.put("this", "some");
map.put("that", 1);
v.setProperty("keyMap", map);
JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL);
Assert.assertNotNull(json);
Assert.assertTrue(json.has(GraphSONTokens._ID));
Assert.assertEquals(1, json.optInt(GraphSONTokens._ID));
Assert.assertTrue(json.has("keyMap"));
JSONObject mapAsJSON = json.optJSONObject("keyMap");
Assert.assertNotNull(mapAsJSON);
Assert.assertTrue(mapAsJSON.has("this"));
Assert.assertEquals("some", mapAsJSON.optString("this"));
Assert.assertTrue(mapAsJSON.has("that"));
Assert.assertEquals(1, mapAsJSON.optInt("that"));
}
use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method jsonFromElementCrazyPropertyNoKeysNoTypes.
@Test
public void jsonFromElementCrazyPropertyNoKeysNoTypes() throws JSONException {
Vertex v = this.graph.addVertex(1);
List mix = new ArrayList();
mix.add(new Cat("smithers"));
mix.add(true);
List deepCats = new ArrayList();
deepCats.add(new Cat("mcallister"));
mix.add(deepCats);
Map map = new HashMap();
map.put("crazy", mix);
int[] someInts = new int[] { 1, 2, 3 };
map.put("ints", someInts);
map.put("regular", "stuff");
Map innerMap = new HashMap();
innerMap.put("me", "you");
map.put("inner", innerMap);
v.setProperty("crazy-map", map);
JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL);
Assert.assertNotNull(json);
Assert.assertTrue(json.has(GraphSONTokens._ID));
Assert.assertEquals(1, json.optInt(GraphSONTokens._ID));
Assert.assertTrue(json.has("crazy-map"));
JSONObject mapAsJson = json.optJSONObject("crazy-map");
Assert.assertTrue(mapAsJson.has("regular"));
Assert.assertEquals("stuff", mapAsJson.optString("regular"));
Assert.assertTrue(mapAsJson.has("ints"));
JSONArray intArrayAsJson = mapAsJson.optJSONArray("ints");
Assert.assertNotNull(intArrayAsJson);
Assert.assertEquals(3, intArrayAsJson.length());
Assert.assertTrue(mapAsJson.has("crazy"));
JSONArray deepListAsJSON = mapAsJson.optJSONArray("crazy");
Assert.assertNotNull(deepListAsJSON);
Assert.assertEquals(3, deepListAsJSON.length());
Assert.assertTrue(mapAsJson.has("inner"));
JSONObject mapInMapAsJSON = mapAsJson.optJSONObject("inner");
Assert.assertNotNull(mapInMapAsJSON);
Assert.assertTrue(mapInMapAsJSON.has("me"));
Assert.assertEquals("you", mapInMapAsJSON.optString("me"));
}
use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method jsonFromElementEdgeCompactIdOnlyAsExclude.
@Test
public void jsonFromElementEdgeCompactIdOnlyAsExclude() throws JSONException {
ElementFactory factory = new GraphElementFactory(this.graph);
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);
e.setProperty("x", "y");
Set<String> propertiesToExclude = new HashSet<String>() {
{
add(GraphSONTokens._TYPE);
add(GraphSONTokens._LABEL);
add(GraphSONTokens._IN_V);
add(GraphSONTokens._OUT_V);
add("weight");
}
};
ElementPropertyConfig config = new ElementPropertyConfig(null, propertiesToExclude, ElementPropertyConfig.ElementPropertiesRule.INCLUDE, ElementPropertyConfig.ElementPropertiesRule.EXCLUDE);
GraphSONUtility utility = new GraphSONUtility(GraphSONMode.COMPACT, factory, config);
JSONObject json = utility.jsonFromElement(e);
Assert.assertNotNull(json);
Assert.assertFalse(json.has(GraphSONTokens._TYPE));
Assert.assertFalse(json.has(GraphSONTokens._LABEL));
Assert.assertFalse(json.has(GraphSONTokens._IN_V));
Assert.assertFalse(json.has(GraphSONTokens._OUT_V));
Assert.assertTrue(json.has(GraphSONTokens._ID));
Assert.assertFalse(json.has("weight"));
Assert.assertTrue(json.has("x"));
Assert.assertEquals("y", json.optString("x"));
}
use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method jsonFromElementVertexIntListPropertiesNoKeysWithTypes.
@Test
public void jsonFromElementVertexIntListPropertiesNoKeysWithTypes() throws JSONException {
Vertex v = this.graph.addVertex(1);
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(1);
list.add(1);
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(1, valueAsJson.optInt(GraphSONTokens.VALUE));
}
}
use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.
the class GraphSONUtilityTest method jsonFromElementInTheOddCasePropertyReturnsNullNoKeysNoTypes.
/**
* This test is kinda weird since a Blueprints graph can't have properties set to null, howerver that does not
* mean that a graph may never return null (https://github.com/tinkerpop/blueprints/issues/400).
*/
@Test
public void jsonFromElementInTheOddCasePropertyReturnsNullNoKeysNoTypes() throws Exception {
final Vertex v = new VertexThatReturnsNull();
final JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.NORMAL);
Assert.assertNotNull(json);
Assert.assertTrue(json.has(GraphSONTokens._ID));
Assert.assertEquals(1234l, json.optLong(GraphSONTokens._ID));
Assert.assertFalse(json.has("alwaysNull"));
}
Aggregations