/**
   * Generic function to list security events based on a NeutralQuery.
   *
   * <p>This is mostly the same as the corresponding method in BasicService, except that it does
   * security checking differently, based on the SecurityEventContextResolver.
   *
   * @param neutralQuery
   * @return
   */
  @Override
  public Iterable<EntityBody> list(NeutralQuery neutralQuery) {
    neutralQuery.setSortBy("timeStamp");
    neutralQuery.setSortOrder(NeutralQuery.SortOrder.descending);

    return super.list(neutralQuery);
  }
  @SuppressWarnings("unchecked")
  @Test
  public void testSort() {

    // clean up the existing student data
    repository.deleteAll("student", null);

    // create new student entity
    Map<String, Object> body1 = buildTestStudentEntity();
    Map<String, Object> body2 = buildTestStudentEntity();
    Map<String, Object> body3 = buildTestStudentEntity();
    Map<String, Object> body4 = buildTestStudentEntity();

    body1.put("firstName", "Austin");
    body2.put("firstName", "Jane");
    body3.put("firstName", "Mary");
    body4.put("firstName", "Suzy");

    body1.put("performanceLevels", new String[] {"1"});
    body2.put("performanceLevels", new String[] {"2"});
    body3.put("performanceLevels", new String[] {"3"});
    body4.put("performanceLevels", new String[] {"4"});

    // save entities
    repository.create("student", body1);
    repository.create("student", body2);
    repository.create("student", body3);
    repository.create("student", body4);

    // sort entities by firstName with ascending order
    NeutralQuery sortQuery1 = new NeutralQuery();
    sortQuery1.setSortBy("firstName");
    sortQuery1.setSortOrder(NeutralQuery.SortOrder.ascending);
    sortQuery1.setOffset(0);
    sortQuery1.setLimit(100);

    Iterable<Entity> entities = repository.findAll("student", sortQuery1);
    assertNotNull(entities);
    Iterator<Entity> it = entities.iterator();
    assertEquals("Austin", it.next().getBody().get("firstName"));
    assertEquals("Jane", it.next().getBody().get("firstName"));
    assertEquals("Mary", it.next().getBody().get("firstName"));
    assertEquals("Suzy", it.next().getBody().get("firstName"));

    // sort entities by firstName with descending order
    NeutralQuery sortQuery2 = new NeutralQuery();
    sortQuery2.setSortBy("firstName");
    sortQuery2.setSortOrder(NeutralQuery.SortOrder.descending);
    sortQuery2.setOffset(0);
    sortQuery2.setLimit(100);
    entities = repository.findAll("student", sortQuery2);
    assertNotNull(entities);
    it = entities.iterator();
    assertEquals("Suzy", it.next().getBody().get("firstName"));
    assertEquals("Mary", it.next().getBody().get("firstName"));
    assertEquals("Jane", it.next().getBody().get("firstName"));
    assertEquals("Austin", it.next().getBody().get("firstName"));

    // sort entities by performanceLevels which is an array with ascending
    // order
    NeutralQuery sortQuery3 = new NeutralQuery();
    sortQuery3.setSortBy("performanceLevels");
    sortQuery3.setSortOrder(NeutralQuery.SortOrder.ascending);
    sortQuery3.setOffset(0);
    sortQuery3.setLimit(100);
    entities = repository.findAll("student", sortQuery3);
    assertNotNull(entities);
    it = entities.iterator();
    assertEquals("1", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0));
    assertEquals("2", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0));
    assertEquals("3", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0));
    assertEquals("4", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0));

    // sort entities by performanceLevels which is an array with descending
    // order
    NeutralQuery sortQuery4 = new NeutralQuery();
    sortQuery4.setSortBy("performanceLevels");
    sortQuery4.setSortOrder(NeutralQuery.SortOrder.descending);
    sortQuery4.setOffset(0);
    sortQuery4.setLimit(100);
    entities = repository.findAll("student", sortQuery4);
    assertNotNull(entities);
    it = entities.iterator();
    assertEquals("4", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0));
    assertEquals("3", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0));
    assertEquals("2", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0));
    assertEquals("1", ((List<String>) (it.next().getBody().get("performanceLevels"))).get(0));
  }