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());
  }
Ejemplo n.º 2
0
  /**
   * 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 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());
  }