@Test
public void testFindDataObjectsWithExplicitPagination() {

    for (int i = 0; i < 8; ++i) {
        DXRecord.newRecord().setProject(testProject).setName("foo" + Integer.toString(i)).build();
    }
    List<DXRecord> outputRecords =
            DXSearch.findDataObjects().inProject(testProject).nameMatchesGlob("foo*")
                    .withClassRecord().execute().asList();
    Assert.assertEquals(8, outputRecords.size());
    DXSearch.FindDataObjectsResult<DXRecord> result =
            DXSearch.findDataObjects().inProject(testProject).nameMatchesGlob("foo*").withClassRecord().execute();
    FindResultPage<DXRecord> page = result.getFirstPage(3);
    Assert.assertEquals(3, page.size());
    Assert.assertEquals(true, page.hasNextPage());
    Iterator<DXRecord> iter = page.iterator();
    for (int i = 0; i < 3; i++) {
        Assert.assertEquals(true, iter.hasNext());
        DXRecord r = iter.next();
        Assert.assertEquals(outputRecords.get(i).getId(), r.getId());
    }
    Assert.assertEquals(false, iter.hasNext());
    page = result.getSubsequentPage(page.getNext(), 3);
    Assert.assertEquals(3, page.size());
    Assert.assertEquals(true, page.hasNextPage());
    iter = page.iterator();
    for (int i = 3; i < 6; i++) {
        Assert.assertEquals(true, iter.hasNext());
        DXRecord r = iter.next();
        Assert.assertEquals(outputRecords.get(i).getId(), r.getId());
    }
    Assert.assertEquals(false, iter.hasNext());
    page = result.getSubsequentPage(page.getNext(), 3);
    Assert.assertEquals(2, page.size());
    Assert.assertEquals(false, page.hasNextPage());
    iter = page.iterator();
    for (int i = 6; i < 8; i++) {
        Assert.assertEquals(true, iter.hasNext());
        DXRecord r = iter.next();
        Assert.assertEquals(outputRecords.get(i).getId(), r.getId());
    }
    Assert.assertEquals(false, iter.hasNext());
    // Checking when the requested page size is greater than amount of items
    page = result.getFirstPage(100);
    Assert.assertEquals(8, page.size());
    Assert.assertEquals(false, page.hasNextPage());
    int i = 0;
    for (DXRecord r : page) {
        Assert.assertEquals(outputRecords.get(i++).getId(), r.getId());
    }
    // Checking invalid arguments
    try {
        result.getFirstPage(0);
        Assert.fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        result.getFirstPage(-2);
        Assert.fail();
    } catch (IllegalArgumentException e) {
    }
    ObjectMapper mapper = new ObjectMapper();
    try {
        result.getSubsequentPage(mapper.createObjectNode(), 0);
        Assert.fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        result.getSubsequentPage(mapper.createObjectNode(), -1);
        Assert.fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        result.getSubsequentPage(null, 10);
        Assert.fail();
    } catch (NullPointerException e) {
    }
}