public void testGetEntities() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);

    EntityUtils.getEntities(
        getContext(),
        0,
        100,
        new EntityListListener() {

          @Override
          public void onList(ListResult<Entity> entities) {
            addResult(0, entities);
            latch.countDown();
          }

          @Override
          public void onError(SocializeException error) {
            error.printStackTrace();
            latch.countDown();
          }
        });

    latch.await(20, TimeUnit.SECONDS);

    ListResult<Entity> after = getResult(0);

    assertNotNull(after);
    assertTrue(after.size() >= 2);

    // Make sure it contains what we expect.
    JSONObject json = TestUtils.getJSON(getContext(), "entities.json");
    JSONArray jsonArray = json.getJSONArray("items");

    JSONObject jsonObject0 = (JSONObject) jsonArray.get(0);
    JSONObject jsonObject1 = (JSONObject) jsonArray.get(1);

    String key0 = jsonObject0.getString("key");
    String key1 = jsonObject1.getString("key");

    List<Entity> items = after.getItems();

    int found = 0;

    for (Entity entity : items) {
      if (entity.getKey().equals(key0) || entity.getKey().equals(key1)) {
        found++;
      }

      if (found >= 2) {
        break;
      }
    }

    assertEquals(2, found);
  }
  public void testGetEntitiesWithSort() throws Exception {

    final Activity context = getContext();

    // 3 entities (these are created from python script)
    String[] keys = {"http://entityA.com", "http://entityB.com", "http://entityC.com"};

    // Get by normal means
    final CountDownLatch getLatch = new CountDownLatch(1);

    EntityUtils.getEntities(
        context,
        SortOrder.CREATION_DATE,
        new EntityListListener() {

          @Override
          public void onList(ListResult<Entity> entities) {
            addResult(0, entities);
            getLatch.countDown();
          }

          @Override
          public void onError(SocializeException error) {
            error.printStackTrace();
            getLatch.countDown();
          }
        },
        keys);

    assertTrue(getLatch.await(20, TimeUnit.SECONDS));

    ListResult<Entity> after = getResult(0);

    assertNotNull(after);
    assertEquals(3, after.size());

    // Check the order.. should be C/B/A
    List<Entity> items = after.getItems();

    assertEquals(keys[2], items.get(0).getKey());
    assertEquals(keys[1], items.get(1).getKey());
    assertEquals(keys[0], items.get(2).getKey());

    // Now get by activity
    final CountDownLatch activityLatch = new CountDownLatch(1);

    EntityUtils.getEntities(
        context,
        SortOrder.TOTAL_ACTIVITY,
        new EntityListListener() {

          @Override
          public void onList(ListResult<Entity> entities) {
            addResult(1, entities);
            activityLatch.countDown();
          }

          @Override
          public void onError(SocializeException error) {
            error.printStackTrace();
            activityLatch.countDown();
          }
        },
        keys);

    assertTrue(activityLatch.await(20, TimeUnit.SECONDS));

    ListResult<Entity> sorted = getResult(1);

    assertNotNull(sorted);
    assertEquals(3, sorted.size());

    // Check the order.. should be A/B/C
    List<Entity> sortedItems = sorted.getItems();

    assertEquals(keys[0], sortedItems.get(0).getKey());
    assertEquals(keys[1], sortedItems.get(1).getKey());
    assertEquals(keys[2], sortedItems.get(2).getKey());
  }