Search in sources :

Example 81 with Vertex

use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.

the class GraphSONUtilityTest method jsonFromElementNullsNoKeysWithTypes.

@Test
public void jsonFromElementNullsNoKeysWithTypes() 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.EXTENDED);
    Assert.assertNotNull(json);
    JSONObject jsonMap = json.optJSONObject("keyMap").optJSONObject(GraphSONTokens.VALUE);
    Assert.assertNotNull(jsonMap);
    JSONObject jsonObjectMap = jsonMap.optJSONObject("innerkey");
    Assert.assertTrue(jsonObjectMap.isNull(GraphSONTokens.VALUE));
    Assert.assertEquals(GraphSONTokens.TYPE_UNKNOWN, jsonObjectMap.optString(GraphSONTokens.TYPE));
    JSONArray jsonInnerArray = jsonMap.getJSONObject("list").getJSONArray(GraphSONTokens.VALUE);
    Assert.assertNotNull(jsonInnerArray);
    JSONObject jsonObjectInnerListFirst = jsonInnerArray.getJSONObject(0);
    Assert.assertTrue(jsonObjectInnerListFirst.isNull(GraphSONTokens.VALUE));
    Assert.assertEquals(GraphSONTokens.TYPE_UNKNOWN, jsonObjectInnerListFirst.optString(GraphSONTokens.TYPE));
    JSONArray jsonArray = json.getJSONObject("keyList").getJSONArray(GraphSONTokens.VALUE);
    Assert.assertNotNull(jsonArray);
    JSONObject jsonObjectListFirst = jsonArray.getJSONObject(0);
    Assert.assertTrue(jsonObjectListFirst.isNull(GraphSONTokens.VALUE));
    Assert.assertEquals(GraphSONTokens.TYPE_UNKNOWN, jsonObjectListFirst.optString(GraphSONTokens.TYPE));
}
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 82 with Vertex

use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.

the class GraphSONUtilityTest method jsonFromElementVertexCompactIdOnlyAsInclude.

@Test
public void jsonFromElementVertexCompactIdOnlyAsInclude() throws JSONException {
    Vertex v = this.graph.addVertex(1);
    v.setProperty("name", "marko");
    Set<String> propertiesToInclude = new HashSet<String>() {

        {
            add(GraphSONTokens._ID);
        }
    };
    JSONObject json = GraphSONUtility.jsonFromElement(v, propertiesToInclude, GraphSONMode.COMPACT);
    Assert.assertNotNull(json);
    Assert.assertFalse(json.has(GraphSONTokens._TYPE));
    Assert.assertTrue(json.has(GraphSONTokens._ID));
    Assert.assertFalse(json.has("name"));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 83 with Vertex

use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.

the class GraphSONUtilityTest method jsonFromElementVertexCompactAllOnly.

@Test
public void jsonFromElementVertexCompactAllOnly() throws JSONException {
    Vertex v = this.graph.addVertex(1);
    v.setProperty("name", "marko");
    JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.COMPACT);
    Assert.assertNotNull(json);
    Assert.assertTrue(json.has(GraphSONTokens._TYPE));
    Assert.assertTrue(json.has(GraphSONTokens._ID));
    Assert.assertTrue(json.has("name"));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) Test(org.junit.Test)

Example 84 with Vertex

use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.

the class GraphSONUtilityTest method edgeFromJsonValid.

@Test
public void edgeFromJsonValid() 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(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));
}
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 85 with Vertex

use of com.tinkerpop.blueprints.Vertex in project blueprints by tinkerpop.

the class GraphSONUtilityTest method jsonFromElementFloatArrayPropertyNoKeysNoTypes.

@Test
public void jsonFromElementFloatArrayPropertyNoKeysNoTypes() throws JSONException {
    Vertex v = this.graph.addVertex(1);
    float[] floatArray = new float[] { 1.0f, 2.0f, 3.0f };
    v.setProperty("keyFloatArray", floatArray);
    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("keyFloatArray"));
    JSONArray floatArrayAsJSON = json.optJSONArray("keyFloatArray");
    Assert.assertNotNull(floatArrayAsJSON);
    Assert.assertEquals(3, floatArrayAsJSON.length());
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) 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