@Test
  public void getJohnDoeFriendsOrderedByName() throws Exception {
    // Set collection options
    CollectionOptions collectionOptions = new CollectionOptions();
    collectionOptions.setSortBy("name");
    collectionOptions.setSortOrder(SortOrder.ascending);
    collectionOptions.setMax(20);

    // Get all friends of john.doe
    Future<RestfulCollection<Person>> result =
        this.personServiceDb.getPeople(
            SpiTestUtil.buildUserIds("john.doe"),
            new GroupId(GroupId.Type.friends, "@friends"),
            collectionOptions,
            Person.Field.ALL_FIELDS,
            SpiTestUtil.DEFAULT_TEST_SECURITY_TOKEN);

    RestfulCollection<Person> peopleCollection = result.get();
    assertEquals(3, peopleCollection.getTotalResults());
    assertEquals(0, peopleCollection.getStartIndex());
    List<Person> people = peopleCollection.getEntry();
    // The users should be in alphabetical order
    SpiTestUtil.assertPersonEquals(people.get(0), "george.doe", "George Doe");
    SpiTestUtil.assertPersonEquals(people.get(1), "jane.doe", "Jane Doe");
  }
 @Test
 public void getCanonicalPerson() throws Exception {
   Future<Person> person =
       this.personServiceDb.getPerson(
           new UserId(Type.userId, "canonical"),
           Person.Field.ALL_FIELDS,
           SpiTestUtil.DEFAULT_TEST_SECURITY_TOKEN);
   SpiTestUtil.assertPersonEquals(person.get(), canonical);
 }
/** Test the PersonServiceDb implementation. */
public class PersonServiceDbTest {

  private final Person canonical = SpiTestUtil.buildCanonicalPerson();

  private PersonServiceDb personServiceDb;

  /** The bootstrap. */
  private SpiDatabaseBootstrap bootstrap;

  @Before
  public void setup() throws Exception {
    EntityManager entityManager = SpiEntityManagerFactory.getEntityManager();
    this.personServiceDb = new PersonServiceDb(entityManager);

    // Bootstrap hibernate and associated test db, and setup db with test data
    this.bootstrap = new SpiDatabaseBootstrap(entityManager);
    this.bootstrap.init();
  }

  @After
  public void tearDown() throws Exception {
    bootstrap.tearDown();
  }

  @Test
  public void getCanonicalPerson() throws Exception {
    Future<Person> person =
        this.personServiceDb.getPerson(
            new UserId(Type.userId, "canonical"),
            Person.Field.ALL_FIELDS,
            SpiTestUtil.DEFAULT_TEST_SECURITY_TOKEN);
    SpiTestUtil.assertPersonEquals(person.get(), canonical);
  }

  @Test
  public void getJohnDoeFriendsOrderedByName() throws Exception {
    // Set collection options
    CollectionOptions collectionOptions = new CollectionOptions();
    collectionOptions.setSortBy("name");
    collectionOptions.setSortOrder(SortOrder.ascending);
    collectionOptions.setMax(20);

    // Get all friends of john.doe
    Future<RestfulCollection<Person>> result =
        this.personServiceDb.getPeople(
            SpiTestUtil.buildUserIds("john.doe"),
            new GroupId(GroupId.Type.friends, "@friends"),
            collectionOptions,
            Person.Field.ALL_FIELDS,
            SpiTestUtil.DEFAULT_TEST_SECURITY_TOKEN);

    RestfulCollection<Person> peopleCollection = result.get();
    assertEquals(3, peopleCollection.getTotalResults());
    assertEquals(0, peopleCollection.getStartIndex());
    List<Person> people = peopleCollection.getEntry();
    // The users should be in alphabetical order
    SpiTestUtil.assertPersonEquals(people.get(0), "george.doe", "George Doe");
    SpiTestUtil.assertPersonEquals(people.get(1), "jane.doe", "Jane Doe");
  }

  @Test
  public void getJohnDoeFriendsOrderedByNameWithPagination() throws Exception {
    // Set collection options
    CollectionOptions collectionOptions = new CollectionOptions();
    collectionOptions.setSortBy("name");
    collectionOptions.setSortOrder(SortOrder.ascending);
    collectionOptions.setFirst(0);
    collectionOptions.setMax(1);

    // Get first friend of john.doe
    Future<RestfulCollection<Person>> result =
        this.personServiceDb.getPeople(
            SpiTestUtil.buildUserIds("john.doe"),
            new GroupId(GroupId.Type.friends, "@friends"),
            collectionOptions,
            Person.Field.ALL_FIELDS,
            SpiTestUtil.DEFAULT_TEST_SECURITY_TOKEN);
    RestfulCollection<Person> peopleCollection = result.get();
    assertEquals(3, peopleCollection.getTotalResults());
    assertEquals(0, peopleCollection.getStartIndex());
    List<Person> people = peopleCollection.getEntry();
    SpiTestUtil.assertPersonEquals(people.get(0), "george.doe", "George Doe");

    // Get second friend of john.doe
    collectionOptions.setFirst(1);
    result =
        this.personServiceDb.getPeople(
            SpiTestUtil.buildUserIds("john.doe"),
            new GroupId(GroupId.Type.friends, "@friends"),
            collectionOptions,
            Person.Field.ALL_FIELDS,
            SpiTestUtil.DEFAULT_TEST_SECURITY_TOKEN);
    peopleCollection = result.get();
    assertEquals(3, peopleCollection.getTotalResults());
    assertEquals(1, peopleCollection.getStartIndex());
    people = peopleCollection.getEntry();
    SpiTestUtil.assertPersonEquals(people.get(0), "jane.doe", "Jane Doe");
  }
}