LemonGraph { public static LemonGraphResponse postToGraph(String graphId, LemonGraphObject graph) throws InvalidGraphException { LemonGraphResponse lgr = new LemonGraphResponse(); ContentResponse res = null; try { openConnection(); String url = restUrl + "graph/" + graphId; Request req = client.POST(url); JSONObject graphContent = graph.get(); req.content(new StringContentProvider(graphContent.toString()), "application/json"); res = req.send(); lgr.parseContentResponse(res); } catch (Exception e) { log.error("Error trying to postToGraph "+e.getMessage()); lgr.setSuccess(false); } return lgr; } LemonGraph(); static void openConnection(); static boolean isConnected(); static void close(); static boolean isApiUp(); static JSONObject queryBasedOnPatterns(String jobId, HashMap<String, String> adapterPatterns, int startId); static JSONObject queryBasedOnPatterns(String jobId, HashMap<String, String> adapterPatterns, int startId, int stopId); static JSONObject queryBasedOnPatterns(String jobId, HashMap<String, String> adapterPatterns); static HashMap parseLemonGraphResult(JSONObject resultdata); static JSONObject resetGraph(String graphId); static JSONObject deleteGraph(String graphId); static String createGraph(JSONObject meta); static String createGraph(String graphId, JSONObject meta); static LemonGraphResponse postToGraph(String graphId, LemonGraphObject graph); static LemonGraphResponse postToGraph(LGPayload payload); static String getContent(String uri); static JSONObject getGraph(String jobId); static JSONObject getGraphCytoscape(String jobId); static JSONObject getGraphD3(String jobId); static JSONObject getGraphD3(String jobId, int start, int stop); static LemonGraphObject buildLemonGraphFromPayloadNodesandEdges(List<JSONObject> inNodes, List<JSONArray> inEdges); static void main(String[] args); final static int NOT_FOUND; static String restUrl; static HttpClient client; }
@Test public void testPostToGraph() { String graphId = ""; try { JSONObject meta = new JSONObject(); graphId = lg.createGraph(meta); } catch(Exception e) { fail("Unable to create graph in lemongraph"); } try { UUID toUuid = UUID.fromString(graphId); assertEquals(toUuid.toString(), graphId); } catch (IllegalArgumentException e) { fail("Invalid graph id in testCreateGraph"); } JSONObject node = new JSONObject(); node.put("type","foo");node.put("value","bar"); JSONObject node2 = new JSONObject(); node2.put("type","foo");node2.put("value","baz"); JSONArray nodes = new JSONArray(); nodes.put(node); nodes.put(node2); JSONObject meta = new JSONObject(); JSONArray edges = new JSONArray(); JSONObject edge1 = new JSONObject(); edge1.put("type","edge"); edge1.put("value","e1"); JSONObject e1src = new JSONObject(); e1src.put("type","foo"); e1src.put("value","bar"); JSONObject e1tgt = new JSONObject(); e1tgt.put("type","foo"); e1tgt.put("value","baz"); edge1.put("src",e1src); edge1.put("tgt",e1tgt); edges.put(edge1); LemonGraphObject lgo = new LemonGraphObject(true,meta,nodes,edges); LemonGraphResponse lgr2 = null; try { lgr2 = lg.postToGraph(graphId, lgo); assertEquals(8, lgr2.getMaxId()); assertEquals(8, lgr2.getUpdateCount()); assertEquals(true, lgr2.didCallSucceed()); assertEquals(204, lgr2.getResponseCode()); } catch(InvalidGraphException e) { fail("Can't lookup graph id "+graphId); } try { lg.deleteGraph(graphId); } catch (Exception e) { System.out.println("Delete failed message: " + e.getMessage()); } }