package com.contentful.java.cda;

import com.contentful.java.cda.lib.Enqueue;
import java.util.List;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

public class EntryTest extends BaseTest {
  @Test
  @Enqueue("entries_nyancat.json")
  public void fetchEntry() throws Exception {
    assertNyanCat(client.fetch(CDAEntry.class).one("nyancat"));
  }

  @Test
  @Enqueue("entries.json")
  public void fetchAllEntries() throws Exception {
    CDAArray array = client.fetch(CDAEntry.class).all();
    assertThat(array.items()).hasSize(15);
    assertThat(array.assets()).hasSize(4);
    assertThat(array.entries()).hasSize(11);

    assertThat(array.total()).isEqualTo(11);
    assertThat(array.skip()).isEqualTo(0);
    assertThat(array.limit()).isEqualTo(100);

    CDAEntry nyanCat = array.entries().get("nyancat");
    assertThat(nyanCat).isNotNull();
    assertNyanCat(nyanCat);
  }

  private void assertNyanCat(CDAEntry entry) {
    assertThat(entry.id()).isEqualTo("nyancat");
    assertThat(entry.getField("name")).isEqualTo("Nyan Cat");
    assertThat(entry.getField("color")).isEqualTo("rainbow");
    assertThat(entry.getField("birthday")).isEqualTo("2011-04-04T22:00:00+00:00");
    assertThat(entry.getField("lives")).isEqualTo(1337.0);

    List<String> likes = entry.getField("likes");
    assertThat(likes).containsExactly("rainbows", "fish");

    Object bestFriend = entry.getField("bestFriend");
    assertThat(bestFriend).isInstanceOf(CDAEntry.class);
    assertThat(entry).isSameAs(((CDAEntry) bestFriend).getField("bestFriend"));
  }
}

@@ -94,16 +94,60 @@
 l.List;%0A
+import java.util.concurrent.CountDownLatch;%0A
 import o
@@ -419,16 +419,604 @@
 );%0A  %7D%0A%0A
+  @Test%0A  @Enqueue(%22entries_nyancat.json%22)%0A  public void fetchEntryAsync() throws Exception %7B%0A    final CountDownLatch latch = new CountDownLatch(1);%0A    final CDAEntry%5B%5D result = %7B null %7D;%0A%0A    client.fetch(CDAEntry.class).one(%22nyancat%22, new CDACallback%3CCDAEntry%3E() %7B%0A      @Override protected void onSuccess(CDAEntry entry) %7B%0A        result%5B0%5D = entry;%0A        latch.countDown();%0A      %7D%0A%0A      @Override protected void onFailure(Throwable error) %7B%0A        latch.countDown();%0A      %7D%0A    %7D);%0A%0A    latch.await();%0A    assertThat(result%5B0%5D).isNotNull();%0A    assertNyanCat(result%5B0%5D);%0A  %7D%0A%0A
   @Test%0A

Test async fetch