public void pagePreviousByIdTest() {
// Skip to last (page 4)
CouchViewQuery viewQuery = CouchViewQuery.builder("pet", "byId").limit(3).build();
GetEntityResponse<PetEntity> er = couchDatabase.get().entity(PetEntity.class, viewQuery).execute();
CouchPageNavigation pageNavigation = er.getCouchPageNavigation();
while (pageNavigation.hasNextPage()) {
er = couchDatabase.get().entity(PetEntity.class, pageNavigation.queryNextPage()).execute();
pageNavigation = er.getCouchPageNavigation();
}
assertEquals(er.getSize(), 1);
List<PetEntity> foundPets = er.getEntityList();
assertEquals(foundPets.get(0), pets[9]);
assertNotNull(pageNavigation);
assertTrue(pageNavigation.hasPreviousPage());
assertFalse(pageNavigation.hasNextPage());
assertTrue(pageNavigation.hasRequestedPage());
assertEquals(pageNavigation.getRequestedPage(), "_design/pet/_view/byId?startkey=9&startkey_docid=Pet:9&skip=1&descending=false");
assertEquals(pageNavigation.getPreviousPage(), "_design/pet/_view/byId?startkey=10&startkey_docid=Pet:10&skip=1&descending=true");
// Previous (page 3)
er = couchDatabase.get().entity(PetEntity.class, pageNavigation.queryPreviousPage()).execute();
assertEquals(er.getSize(), 3);
foundPets = er.getEntityList();
assertEquals(foundPets.get(0), pets[6]);
assertEquals(foundPets.get(1), pets[7]);
assertEquals(foundPets.get(2), pets[8]);
pageNavigation = er.getCouchPageNavigation();
assertNotNull(pageNavigation);
assertTrue(pageNavigation.hasPreviousPage());
assertTrue(pageNavigation.hasNextPage());
assertTrue(pageNavigation.hasRequestedPage());
assertEquals(pageNavigation.getRequestedPage(), "_design/pet/_view/byId?startkey=10&startkey_docid=Pet:10&skip=1&descending=true");
assertEquals(pageNavigation.getPreviousPage(), "_design/pet/_view/byId?startkey=7&startkey_docid=Pet:7&skip=1&descending=true");
assertEquals(pageNavigation.getNextPage(), "_design/pet/_view/byId?startkey=9&startkey_docid=Pet:9&skip=1&descending=false");
// Previous (page 2)
er = couchDatabase.get().entity(PetEntity.class, pageNavigation.queryPreviousPage()).execute();
assertEquals(er.getSize(), 3);
foundPets = er.getEntityList();
assertEquals(foundPets.get(0), pets[3]);
assertEquals(foundPets.get(1), pets[4]);
assertEquals(foundPets.get(2), pets[5]);
pageNavigation = er.getCouchPageNavigation();
assertNotNull(pageNavigation);
assertTrue(pageNavigation.hasPreviousPage());
assertTrue(pageNavigation.hasNextPage());
assertTrue(pageNavigation.hasRequestedPage());
assertEquals(pageNavigation.getRequestedPage(), "_design/pet/_view/byId?startkey=7&startkey_docid=Pet:7&skip=1&descending=true");
assertEquals(pageNavigation.getPreviousPage(), "_design/pet/_view/byId?startkey=4&startkey_docid=Pet:4&skip=1&descending=true");
assertEquals(pageNavigation.getNextPage(), "_design/pet/_view/byId?startkey=6&startkey_docid=Pet:6&skip=1&descending=false");
// Previous (page 1)
er = couchDatabase.get().entity(PetEntity.class, pageNavigation.queryPreviousPage()).execute();
assertEquals(er.getSize(), 3);
foundPets = er.getEntityList();
assertEquals(foundPets.get(0), pets[0]);
assertEquals(foundPets.get(1), pets[1]);
assertEquals(foundPets.get(2), pets[2]);
pageNavigation = er.getCouchPageNavigation();
assertNotNull(pageNavigation);
assertFalse(pageNavigation.hasPreviousPage());
assertTrue(pageNavigation.hasNextPage());
assertTrue(pageNavigation.hasRequestedPage());
assertEquals(pageNavigation.getRequestedPage(), "_design/pet/_view/byId?startkey=4&startkey_docid=Pet:4&skip=1&descending=true");
assertNull(pageNavigation.getPreviousPage());
assertEquals(pageNavigation.getNextPage(), "_design/pet/_view/byId?startkey=3&startkey_docid=Pet:3&skip=1&descending=false");
}