IndexResource extends AbstractSubResource { @GET @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.collection.get", absolute = true) public Response getAllIndices(@PathParam("graphname") final String graphName) { final RexsterApplicationGraph rag = this.getRexsterApplicationGraph(graphName); final Graph graph = rag.getGraph(); final JSONObject theRequestObject = this.getRequestObject(); final IndexableGraph idxGraph = graph instanceof IndexableGraph ? (IndexableGraph) graph : null; if (idxGraph == null) { final JSONObject error = this.generateErrorObject("The requested graph is not of type " + IndexableGraph.class.getName() + "."); throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build()); } final Long start = RequestObjectHelper.getStartOffset(theRequestObject); final Long end = RequestObjectHelper.getEndOffset(theRequestObject); long counter = 0l; try { final JSONArray indexArray = new JSONArray(); for (Index index : idxGraph.getIndices()) { if (counter >= start && counter < end) { indexArray.put(createJSONObject(index)); } counter++; } this.resultObject.put(Tokens.RESULTS, indexArray); this.resultObject.put(Tokens.TOTAL_SIZE, counter); this.resultObject.put(Tokens.QUERY_TIME, this.sh.stopWatch()); } catch (JSONException ex) { logger.error(ex); final JSONObject error = generateErrorObjectJsonFail(ex); throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build()); } finally { rag.tryCommit(); } return Response.ok(this.resultObject).build(); } IndexResource();  IndexResource(final UriInfo ui, final HttpServletRequest req, final RexsterApplication ra); @OPTIONS Response optionsAllIndices(); @GET @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.collection.get", absolute = true) Response getAllIndices(@PathParam("graphname") final String graphName); @OPTIONS @Path("/{indexName}") Response optionsElementsFromIndex(); @GET @Path("/{indexName}") @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON}) @Timed(name = "http.rest.indices.object.get", absolute = true) Response getElementsFromIndex(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName); @GET @Path("/{indexName}") @Produces({RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.object.get", absolute = true) Response getElementsFromIndexRexsterTypedJson(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName); @OPTIONS @Path("/{indexName}/count") @Timed(name = "http.rest.indices.count.get", absolute = true) Response optionsIndexCount(); @GET @Path("/{indexName}/count") @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.count.get", absolute = true) Response getIndexCount(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName); @DELETE @Path("/{indexName}") @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Consumes({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.object.delete", absolute = true) Response deleteIndex(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName, final JSONObject json); @DELETE @Path("/{indexName}") @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.object.delete", absolute = true) Response deleteIndex(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName); @POST @Path("/{indexName}") @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Consumes({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.object.post", absolute = true) Response postIndex(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName, final JSONObject json); @POST @Path("/{indexName}") @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.object.post", absolute = true) Response postIndex(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName); @PUT @Path("/{indexName}") @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Consumes({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.object.put", absolute = true) Response putElementInIndex(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName, final JSONObject json); @PUT @Path("/{indexName}") @Produces({MediaType.APPLICATION_JSON, RexsterMediaType.APPLICATION_REXSTER_JSON, RexsterMediaType.APPLICATION_REXSTER_TYPED_JSON}) @Timed(name = "http.rest.indices.object.put", absolute = true) Response putElementInIndex(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName);  }
@Test(expected = WebApplicationException.class) public void getAllIndicesNonIndexableGraph() { final Graph graph = this.mockery.mock(Graph.class); final RexsterApplicationGraph rag = new RexsterApplicationGraph("graph", graph); final RexsterApplication ra = this.mockery.mock(RexsterApplication.class); final UriInfo uri = this.mockery.mock(UriInfo.class); final HttpServletRequest httpServletRequest = this.mockery.mock(HttpServletRequest.class); this.mockery.checking(new Expectations() {{ allowing(httpServletRequest).getParameterMap(); will(returnValue(new HashMap<String, String>())); allowing(httpServletRequest).getParameterNames(); will(returnValue(new Hashtable().keys())); allowing(ra).getApplicationGraph(with(any(String.class))); will(returnValue(rag)); }}); final IndexResource resource = new IndexResource(uri, httpServletRequest, ra); resource.getAllIndices(graphName); }
@Test public void getAllIndicesNoOffset() { final IndexResource resource = constructIndexResourceWithToyGraph().getResource(); final Response response = resource.getAllIndices(graphName); this.assertIndexOkResponseJsonStructure(10, 10, response); }
@Test public void getAllIndicesWithValidOffset() { final HashMap<String, Object> parameters = new HashMap<String, Object>(); parameters.put(Tokens.REXSTER + "." + Tokens.OFFSET_START, "1"); parameters.put(Tokens.REXSTER + "." + Tokens.OFFSET_END, "2"); final IndexResource resource = constructIndexResource(true, parameters).getResource(); final Response response = resource.getAllIndices(graphName); this.assertIndexOkResponseJsonStructure(1, 10, response); }
@Test public void getAllIndicesWithOffsetNotEnoughResults() { final HashMap<String, Object> parameters = new HashMap<String, Object>(); parameters.put(Tokens.REXSTER + "." + Tokens.OFFSET_START, "10"); parameters.put(Tokens.REXSTER + "." + Tokens.OFFSET_END, "20"); final IndexResource resource = constructIndexResource(true, parameters).getResource(); final Response response = resource.getAllIndices(graphName); this.assertIndexOkResponseJsonStructure(0, 10, response); }
@Test public void getAllIndicesWithOffsetStartAfterEnd() { final HashMap<String, Object> parameters = new HashMap<String, Object>(); parameters.put(Tokens.REXSTER + "." + Tokens.OFFSET_START, "3"); parameters.put(Tokens.REXSTER + "." + Tokens.OFFSET_END, "2"); final IndexResource resource = constructIndexResource(true, parameters).getResource(); final Response response = resource.getAllIndices(graphName); this.assertIndexOkResponseJsonStructure(0, 10, response); }