@Test
  public void pagingWithBoundsCriteria() throws Exception {

    UUID applicationId = createApplication("testOrganization", "pagingWithBoundsCriteria");
    assertNotNull(applicationId);

    EntityManager em = emf.getEntityManager(applicationId);
    assertNotNull(em);

    int size = 40;
    List<UUID> entityIds = new ArrayList<UUID>();

    for (int i = 0; i < size; i++) {
      Map<String, Object> properties = new LinkedHashMap<String, Object>();
      properties.put("index", i);
      Entity created = em.create("page", properties);

      entityIds.add(created.getUuid());
    }

    int pageSize = 10;

    Query query = new Query();
    query.setLimit(pageSize);
    query.addFilter("index >= 10");
    query.addFilter("index <= 29");

    Results r = null;

    // check they're all the same before deletion
    for (int i = 1; i < 3; i++) {

      r = em.searchCollection(em.getApplicationRef(), "pages", query);

      logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities()));

      assertEquals(pageSize, r.size());

      for (int j = 0; j < pageSize; j++) {
        assertEquals(entityIds.get(i * pageSize + j), r.getEntities().get(j).getUuid());
      }

      query.setCursor(r.getCursor());
    }

    r = em.searchCollection(em.getApplicationRef(), "pages", query);

    assertEquals(0, r.size());

    assertNull(r.getCursor());
  }
  @Test
  public void testCollectionOrdering() throws Exception {
    logger.info("testCollectionOrdering");

    UUID applicationId = createApplication("testOrganization", "testCollectionOrdering");
    assertNotNull(applicationId);

    EntityManager em = emf.getEntityManager(applicationId);
    assertNotNull(em);

    for (int i = alphabet.length - 1; i >= 0; i--) {
      String name = alphabet[i];
      Map<String, Object> properties = new LinkedHashMap<String, Object>();
      properties.put("name", name);

      em.create("item", properties);
    }

    int i = 0;

    Query query = Query.fromQL("order by name");
    Results r = em.searchCollection(em.getApplicationRef(), "items", query);
    for (Entity entity : r.getEntities()) {
      assertEquals(alphabet[i], entity.getProperty("name"));
      i++;
    }

    query = Query.fromQL("order by name").withCursor(r.getCursor());
    r = em.searchCollection(em.getApplicationRef(), "items", query);
    for (Entity entity : r.getEntities()) {
      assertEquals(alphabet[i], entity.getProperty("name"));
      i++;
    }

    query = Query.fromQL("order by name").withCursor(r.getCursor());
    r = em.searchCollection(em.getApplicationRef(), "items", query);
    for (Entity entity : r.getEntities()) {
      assertEquals(alphabet[i], entity.getProperty("name"));
      i++;
    }

    assertEquals(alphabet.length, i);

    i = alphabet.length;

    query = Query.fromQL("order by name desc");
    r = em.searchCollection(em.getApplicationRef(), "items", query);
    for (Entity entity : r.getEntities()) {
      i--;
      assertEquals(alphabet[i], entity.getProperty("name"));
    }

    query = Query.fromQL("order by name desc").withCursor(r.getCursor());
    r = em.searchCollection(em.getApplicationRef(), "items", query);
    // logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities()));
    for (Entity entity : r.getEntities()) {
      i--;
      assertEquals(alphabet[i], entity.getProperty("name"));
    }

    query = Query.fromQL("order by name desc").withCursor(r.getCursor());
    r = em.searchCollection(em.getApplicationRef(), "items", query);
    for (Entity entity : r.getEntities()) {
      i--;
      assertEquals(alphabet[i], entity.getProperty("name"));
    }

    assertEquals(0, i);
  }
  @Test
  public void pagingAfterDelete() throws Exception {

    UUID applicationId = createApplication("testOrganization", "pagingAfterDelete");
    assertNotNull(applicationId);

    EntityManager em = emf.getEntityManager(applicationId);
    assertNotNull(em);

    int size = 20;
    List<UUID> entityIds = new ArrayList<UUID>();

    for (int i = 0; i < size; i++) {
      Map<String, Object> properties = new LinkedHashMap<String, Object>();
      properties.put("name", "object" + i);
      Entity created = em.create("objects", properties);

      entityIds.add(created.getUuid());
    }

    Query query = new Query();
    query.setLimit(50);

    Results r = em.searchCollection(em.getApplicationRef(), "objects", query);

    logger.info(JsonUtils.mapToFormattedJsonString(r.getEntities()));

    assertEquals(size, r.size());

    // check they're all the same before deletion
    for (int i = 0; i < size; i++) {
      assertEquals(entityIds.get(i), r.getEntities().get(i).getUuid());
    }

    // now delete 5 items that will span the 10 pages
    for (int i = 5; i < 10; i++) {
      Entity entity = r.getEntities().get(i);
      em.delete(entity);
      entityIds.remove(entity.getUuid());
    }

    // now query with paging
    query = new Query();

    r = em.searchCollection(em.getApplicationRef(), "objects", query);

    assertEquals(10, r.size());

    for (int i = 0; i < 10; i++) {
      assertEquals(entityIds.get(i), r.getEntities().get(i).getUuid());
    }

    // try the next page, set our cursor, it should be the last 5 entities
    query = new Query();
    query.setCursor(r.getCursor());

    r = em.searchCollection(em.getApplicationRef(), "objects", query);

    assertEquals(5, r.size());
    for (int i = 10; i < 15; i++) {
      assertEquals(entityIds.get(i), r.getEntities().get(i - 10).getUuid());
    }
  }