Search in sources :

Example 96 with Vertex

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

Example 97 with Vertex

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

Example 98 with Vertex

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

Example 99 with Vertex

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

Example 100 with Vertex

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