public void testGetEntity() throws Exception {

    final CountDownLatch latch = new CountDownLatch(1);

    EntityUtils.getEntity(
        getContext(),
        "http://entity1.com",
        new EntityGetListener() {

          @Override
          public void onGet(Entity entity) {
            addResult(0, entity);
            latch.countDown();
          }

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

    latch.await(20, TimeUnit.SECONDS);

    Entity after = getResult(0);

    assertNotNull(after);
    assertEquals("http://entity1.com", after.getKey());

    assertNotNull(after.getEntityStats());
    assertNotNull(after.getUserEntityStats());
  }
  public void testEntityMetaDataWithUrl() throws Exception {

    final String entityKey = "testEntityMetaDataWithUrl" + Math.random();

    Entity entity = Entity.newInstance(entityKey, "testAddEntity");

    String url = "http://www.getsocialize.com";

    JSONObject metaData = new JSONObject();
    metaData.put("url", url);

    String jsonString = metaData.toString();

    entity.setMetaData(jsonString);

    final CountDownLatch latch = new CountDownLatch(1);

    // Force no config
    ConfigUtils.getConfig(getContext())
        .setProperty(SocializeConfig.SOCIALIZE_REQUIRE_AUTH, "false");

    EntityUtils.saveEntity(
        TestUtils.getActivity(this),
        entity,
        new EntityAddListener() {

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

          @Override
          public void onCreate(Entity entity) {
            addResult(0, entity);
            latch.countDown();
          }
        });

    latch.await(20, TimeUnit.SECONDS);

    Object result = getResult(0);

    assertNotNull(result);
    assertTrue("Result is not a entity object", (result instanceof Entity));

    Entity entityAfter = (Entity) result;

    String metaDataAfter = entityAfter.getMetaData();

    assertNotNull(metaDataAfter);

    JSONObject json = new JSONObject(metaDataAfter);
    String urlAfter = json.getString("url");

    assertEquals(url, urlAfter);
  }
  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);
  }
  /**
   * Takes the entity, and if it already has a thumbs up count, increases it, otherwise create it at
   * one
   *
   * @param name - The drink to look at
   * @param cont - The context for the sake of the asynchronous check
   */
  public static void bumpEntityValue(String name, int id, final Context cont) {
    String key = "http://www.boozle.com/" + id + "@%" + name;
    EntityUtils.getEntity(
        (Activity) cont,
        key,
        new EntityGetListener() {
          // The entity was gotten, though an error is still possible
          @Override
          public void onGet(Entity entity) {
            int newVal = 1;
            if (entity.getMetaData() != null && entity.getMetaData().length() != 0) {
              newVal = Integer.parseInt(entity.getMetaData()) + 1;
            }
            entity.setMetaData(String.valueOf(newVal));
            EntityUtils.saveEntity(
                (Activity) cont,
                entity,
                new EntityAddListener() {
                  @Override
                  public void onCreate(Entity result) {
                    // If we want to add some kind of handler, here is where to do so
                  }

                  @Override
                  public void onError(SocializeException error) {
                    // Some kind of error in saving, collision?
                  }
                });
          }

          @Override
          public void onError(SocializeException error) {
            if (isNotFoundError(error)) {
              // No entity found
            } else {
              // Some other kind of error
            }
          }
        });
  }
  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());
  }
  public void testAddEntity() throws Exception {
    final String entityKey = "testAddEntity" + Math.random();
    Entity entity = Entity.newInstance(entityKey, "testAddEntity");

    final CountDownLatch latch = new CountDownLatch(1);

    // Force no config
    ConfigUtils.getConfig(getContext())
        .setProperty(SocializeConfig.SOCIALIZE_REQUIRE_AUTH, "false");

    EntityUtils.saveEntity(
        TestUtils.getActivity(this),
        entity,
        new EntityAddListener() {

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

          @Override
          public void onCreate(Entity entity) {
            addResult(0, entity);
            latch.countDown();
          }
        });

    latch.await(20, TimeUnit.SECONDS);

    Object result = getResult(0);

    assertNotNull(result);
    assertTrue("Result is not a entity object", (result instanceof Entity));

    Entity entityAfter = (Entity) result;
    assertTrue("Entity ID is not greater than 0", entityAfter.getId() > 0);

    // Now use the normal UI to retrieve the entity
    clearResults();
    final CountDownLatch latch2 = new CountDownLatch(1);

    EntityUtils.getEntity(
        TestUtils.getActivity(this),
        entityAfter.getId(),
        new EntityGetListener() {

          @Override
          public void onGet(Entity entity) {
            addResult(0, entity);
            latch2.countDown();
          }

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

    latch2.await(20, TimeUnit.SECONDS);

    Entity after = getResult(0);

    assertNotNull(after);
    assertEquals(entityAfter.getId(), after.getId());
    assertEquals(entityAfter.getKey(), after.getKey());
  }