Search in sources :

Example 61 with Vertex

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));
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 62 with Vertex

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));
}
Also used : JSONTokener(org.codehaus.jettison.json.JSONTokener) Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) JSONObject(org.codehaus.jettison.json.JSONObject) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 63 with Vertex

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));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.codehaus.jettison.json.JSONArray) JSONObject(org.codehaus.jettison.json.JSONObject) Test(org.junit.Test)

Example 64 with Vertex

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"));
}
Also used : JSONTokener(org.codehaus.jettison.json.JSONTokener) Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) JSONObject(org.codehaus.jettison.json.JSONObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 65 with Vertex

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);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Aggregations

Vertex (com.tinkerpop.blueprints.Vertex)406 Test (org.junit.Test)119 Edge (com.tinkerpop.blueprints.Edge)111 Graph (com.tinkerpop.blueprints.Graph)85 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)84 JSONObject (org.codehaus.jettison.json.JSONObject)51 HashSet (java.util.HashSet)49 ArrayList (java.util.ArrayList)40 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)37 GremlinPipeline (com.tinkerpop.gremlin.java.GremlinPipeline)28 HashMap (java.util.HashMap)25 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)22 JSONArray (org.codehaus.jettison.json.JSONArray)20 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)19 Test (org.testng.annotations.Test)16 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)15 Map (java.util.Map)15 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)14 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)13 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)11