IndexResource extends AbstractSubResource { @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) public Response getIndexCount(@PathParam("graphname") final String graphName, @PathParam("indexName") final String indexName) { final Index index = this.getIndexFromGraph(graphName, indexName); final RexsterApplicationGraph rag = this.getRexsterApplicationGraph(graphName); String key = null; Object value = null; final JSONObject theRequestObject = this.getRequestObject(); Object temp = theRequestObject.opt(Tokens.KEY); if (temp != null) { key = temp.toString(); } temp = theRequestObject.opt(Tokens.VALUE); if (temp != null) { value = ElementHelper.getTypedPropertyValue(temp.toString()); } if (index != null && key != null && value != null) { try { final long count = index.count(key, value); this.resultObject.put(Tokens.TOTAL_SIZE, count); 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(); } } else if (null == index) { final String msg = "Could not find index [" + indexName + "] on graph [" + graphName + "]"; logger.info(msg); final JSONObject error = generateErrorObject(msg); throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).entity(error).build()); } else { final String msg = "A key and value must be provided to lookup elements in an index"; logger.info(msg); final JSONObject error = generateErrorObject(msg); throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(error).build()); } 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 getIndexCountIndexNotFound() { final IndexResource resource = constructIndexResourceWithToyGraph().getResource(); resource.getIndexCount(graphName, "index-name-20"); }
@Test(expected = WebApplicationException.class) public void getIndexCountIndexBadRequestNoKey() { final HashMap<String, Object> parameters = new HashMap<String, Object>(); parameters.put("value", "name"); final IndexResource resource = constructIndexResource(true, parameters).getResource(); resource.getIndexCount(graphName, "index-name-0"); }
@Test(expected = WebApplicationException.class) public void getIndexCountIndexBadRequestNoValue() { final HashMap<String, Object> parameters = new HashMap<String, Object>(); parameters.put("key", "name"); final IndexResource resource = constructIndexResource(true, parameters).getResource(); resource.getIndexCount(graphName, "index-name-0"); }
@Test public void getIndexCountIndexValid() { final HashMap<String, Object> parameters = new HashMap<String, Object>(); parameters.put("key", "name"); parameters.put("value", "marko"); final IndexResource resource = constructIndexResource(true, parameters).getResource(); final Response response = resource.getIndexCount(graphName, "index-name-0"); Assert.assertNotNull(response); Assert.assertEquals(Status.OK.getStatusCode(), response.getStatus()); Assert.assertNotNull(response.getEntity()); Assert.assertTrue(response.getEntity() instanceof JSONObject); final JSONObject json = (JSONObject) response.getEntity(); Assert.assertTrue(json.has(Tokens.TOTAL_SIZE)); Assert.assertEquals(1, json.optInt(Tokens.TOTAL_SIZE)); Assert.assertTrue(json.has(Tokens.QUERY_TIME)); }