BatchExtension extends AbstractRexsterExtension { @ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH, method = HttpMethod.GET, path = "vertices") @ExtensionDescriptor(description = "get a set of vertices from the graph.", api = { @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.SHOW_TYPES, description = API_SHOW_TYPES), @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.RETURN_KEYS, description = API_RETURN_KEYS), @ExtensionApi(parameterName = "values", description = API_VALUES), @ExtensionApi(parameterName = "type", description = API_TYPE), @ExtensionApi(parameterName = "key", description = API_KEY) }) public ExtensionResponse getVertices(@RexsterContext final RexsterResourceContext context, @RexsterContext final Graph graph) { final JSONObject requestObject = context.getRequestObject(); final JSONArray values = requestObject.optJSONArray("values"); final String type = requestObject.optString("type", "id"); final String key = requestObject.optString("key"); final ExtensionResponse error = checkParameters(context, values, type, key); if (error != null) { return error; } final boolean showTypes = RequestObjectHelper.getShowTypes(requestObject); final GraphSONMode mode = showTypes ? GraphSONMode.EXTENDED : GraphSONMode.NORMAL; final Set<String> returnKeys = RequestObjectHelper.getReturnKeys(requestObject, WILDCARD); try { final JSONArray jsonArray = new JSONArray(); if (type.equals("id")) { for (int ix = 0; ix < values.length(); ix++) { final Vertex vertexFound = graph.getVertex(ElementHelper.getTypedPropertyValue(values.optString(ix))); if (vertexFound != null) { jsonArray.put(GraphSONUtility.jsonFromElement(vertexFound, returnKeys, mode)); } } } else if (type.equals("index")) { Index idx = ((IndexableGraph)graph).getIndex(key, Vertex.class); for (int ix = 0; ix < values.length(); ix++) { CloseableIterable<Vertex> verticesFound = idx.get(key, ElementHelper.getTypedPropertyValue(values.optString(ix))); for (Vertex vertex : verticesFound) { jsonArray.put(GraphSONUtility.jsonFromElement(vertex, returnKeys, mode)); } verticesFound.close(); } } else if (type.equals("keyindex")) { for (int ix = 0; ix < values.length(); ix++) { Iterable<Vertex> verticesFound = graph.getVertices(key, ElementHelper.getTypedPropertyValue(values.optString(ix))); for (Vertex vertex : verticesFound) { jsonArray.put(GraphSONUtility.jsonFromElement(vertex, returnKeys, mode)); } } } final HashMap<String, Object> resultMap = new HashMap<String, Object>(); resultMap.put(Tokens.SUCCESS, true); resultMap.put(Tokens.RESULTS, jsonArray); final JSONObject resultObject = new JSONObject(resultMap); return ExtensionResponse.ok(resultObject); } catch (Exception mqe) { logger.error(mqe); return ExtensionResponse.error( "Error retrieving batch of vertices [" + values + "]", generateErrorJson()); } }  @ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH, method = HttpMethod.GET, path = "vertices") @ExtensionDescriptor(description = "get a set of vertices from the graph.", api = { @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.SHOW_TYPES, description = API_SHOW_TYPES), @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.RETURN_KEYS, description = API_RETURN_KEYS), @ExtensionApi(parameterName = "values", description = API_VALUES), @ExtensionApi(parameterName = "type", description = API_TYPE), @ExtensionApi(parameterName = "key", description = API_KEY) }) ExtensionResponse getVertices(@RexsterContext final RexsterResourceContext context,
                                         @RexsterContext final Graph graph); @ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH, method = HttpMethod.GET, path = "edges") @ExtensionDescriptor(description = "get a set of edges from the graph.", api = { @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.SHOW_TYPES, description = API_SHOW_TYPES), @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.RETURN_KEYS, description = API_RETURN_KEYS), @ExtensionApi(parameterName = "values", description = API_VALUES), @ExtensionApi(parameterName = "type", description = API_TYPE), @ExtensionApi(parameterName = "key", description = API_KEY) }) ExtensionResponse getEdges(@RexsterContext final RexsterResourceContext context,
                                      @RexsterContext final Graph graph); @ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH, method = HttpMethod.POST, path = "tx", autoCommitTransaction = true) @ExtensionDescriptor(description = "post a transaction to the graph.") ExtensionResponse postTx(@RexsterContext RexsterResourceContext context,
                                    @RexsterContext Graph graph,
                                    @RexsterContext RexsterApplicationGraph rag); static final String EXTENSION_NAMESPACE; static final String EXTENSION_NAME; }
@Test public void getVerticesNoValuesInvalid() throws Exception { BatchExtension batchExtension = new BatchExtension(); JSONObject requestObject = new JSONObject(); this.ctx = new RexsterResourceContext(null, null, null, requestObject, null, null, null, null); ExtensionResponse response = batchExtension.getVertices(this.ctx, graph); Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getJerseyResponse().getStatus()); }
@Test public void getVerticesDefaultTypeValid() throws Exception { BatchExtension batchExtension = new BatchExtension(); JSONObject requestObject = new JSONObject(); JSONArray values = new JSONArray(); values.put(1); values.put(2); values.put(100000); requestObject.put("values", values); this.ctx = new RexsterResourceContext(null, null, null, requestObject, null, null, null, null); ExtensionResponse response = batchExtension.getVertices(this.ctx, graph); Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getJerseyResponse().getStatus()); JSONObject entity = (JSONObject) response.getJerseyResponse().getEntity(); Assert.assertNotNull(entity); Assert.assertTrue(entity.has(Tokens.RESULTS)); JSONArray results = entity.optJSONArray(Tokens.RESULTS); Assert.assertNotNull(results); Assert.assertEquals(2, results.length()); }
@Test public void getVerticesTypeIdValid() throws Exception { BatchExtension batchExtension = new BatchExtension(); JSONObject requestObject = new JSONObject(); JSONArray values = new JSONArray(); values.put(1); values.put(2); values.put(100000); requestObject.put("values", values); requestObject.put("type", "id"); this.ctx = new RexsterResourceContext(null, null, null, requestObject, null, null, null, null); ExtensionResponse response = batchExtension.getVertices(this.ctx, graph); Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getJerseyResponse().getStatus()); JSONObject entity = (JSONObject) response.getJerseyResponse().getEntity(); Assert.assertNotNull(entity); Assert.assertTrue(entity.has(Tokens.RESULTS)); JSONArray results = entity.optJSONArray(Tokens.RESULTS); Assert.assertNotNull(results); Assert.assertEquals(2, results.length()); }
@Test public void getVerticesTypeIndexNoKeyInvalid() throws Exception { BatchExtension batchExtension = new BatchExtension(); createManualIndices((IndexableGraph) graph); JSONObject requestObject = new JSONObject(); JSONArray values = new JSONArray(); values.put("(i,27)"); values.put("(i,29)"); values.put("(i,32)"); requestObject.put("values", values); requestObject.put("type", "index"); this.ctx = new RexsterResourceContext(null, null, null, requestObject, null, null, null, null); ExtensionResponse response = batchExtension.getVertices(this.ctx, graph); Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getJerseyResponse().getStatus()); }
@Test public void getVerticesTypeIndexValid() throws Exception { BatchExtension batchExtension = new BatchExtension(); createManualIndices((IndexableGraph) graph); JSONObject requestObject = new JSONObject(); JSONArray values = new JSONArray(); values.put("(i,27)"); values.put("(i,29)"); values.put("(i,32)"); requestObject.put("values", values); requestObject.put("type", "index"); requestObject.put("key", "age"); this.ctx = new RexsterResourceContext(null, null, null, requestObject, null, null, null, null); ExtensionResponse response = batchExtension.getVertices(this.ctx, graph); Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getJerseyResponse().getStatus()); JSONObject entity = (JSONObject) response.getJerseyResponse().getEntity(); Assert.assertNotNull(entity); Assert.assertTrue(entity.has(Tokens.RESULTS)); JSONArray results = entity.optJSONArray(Tokens.RESULTS); Assert.assertNotNull(results); Assert.assertEquals(2, results.length()); Assert.assertTrue(contains(results, "age", 27)); Assert.assertTrue(contains(results, "age", 29)); Assert.assertFalse(contains(results, "age", 32)); }
@Test public void getVerticesTypeKeyIndexNoKeyInvalid() throws Exception { BatchExtension batchExtension = new BatchExtension(); createKeyIndices((KeyIndexableGraph) graph); JSONObject requestObject = new JSONObject(); JSONArray values = new JSONArray(); values.put("(i,27)"); values.put("(i,29)"); values.put("(i,200)"); requestObject.put("values", values); requestObject.put("type", "keyindex"); this.ctx = new RexsterResourceContext(null, null, null, requestObject, null, null, null, null); ExtensionResponse response = batchExtension.getVertices(this.ctx, graph); Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getJerseyResponse().getStatus()); }
@Test public void getVerticesTypeKeyIndexValid() throws Exception { BatchExtension batchExtension = new BatchExtension(); createKeyIndices((KeyIndexableGraph) graph); JSONObject requestObject = new JSONObject(); JSONArray values = new JSONArray(); values.put("(i,27)"); values.put("(i,29)"); values.put("(i,200)"); requestObject.put("values", values); requestObject.put("type", "keyindex"); requestObject.put("key", "age"); this.ctx = new RexsterResourceContext(null, null, null, requestObject, null, null, null, null); ExtensionResponse response = batchExtension.getVertices(this.ctx, graph); Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getJerseyResponse().getStatus()); JSONObject entity = (JSONObject) response.getJerseyResponse().getEntity(); Assert.assertNotNull(entity); Assert.assertTrue(entity.has(Tokens.RESULTS)); JSONArray results = entity.optJSONArray(Tokens.RESULTS); Assert.assertNotNull(results); Assert.assertEquals(2, results.length()); Assert.assertTrue(contains(results, "age", 27)); Assert.assertTrue(contains(results, "age", 29)); Assert.assertFalse(contains(results, "age", 200)); }