use of com.cloudant.http.HttpConnectionInterceptorContext in project java-cloudant by cloudant.
the class ViewsTest method testComplexKeyContainingIntTokenPagination.
/**
* Test written for https://github.com/cloudant/java-cloudant/issues/172 where complex key
* integers were being changed to floats when using pagination tokens. For example ["uuid", 1]
* would become ["uuid", 1.0] when trying to request the second page. 1.0 sorts before 1, so the
* wrong documents would be returned on the second page.
*
* The test creates 10 documents that emit complex keys of e.g. ["uuid", 1000]
*
* We use 5 documents per page and use a start key of ["uuid", 1].
*
* The test gets the first page and then retrieves the second page using a token.
* It captures the request URL and then asserts that the startkey was of the correct integer
* form.
*
* @throws Exception
*/
@Test
public void testComplexKeyContainingIntTokenPagination() throws Exception {
// Use the intercepted client as we want to check the query string in the connection
db = interceptedDB.get();
Utils.putDesignDocs(db);
// Create 10 documents in the database
int nDocs = 10;
for (int i = 0; i < nDocs; i++) {
Foo f = new Foo("" + i);
f.setPosition(i);
multiValueKeyInit(f, i);
db.save(f);
}
// Use the creator_created view (complex keys [String, int])
ViewRequest<Key.ComplexKey, Object> request = db.getViewRequestBuilder("example", "creator_created").newPaginatedRequest(Key.Type.COMPLEX, Object.class).startKey(Key.complex("uuid").add(1)).rowsPerPage(5).build();
// Get the second page response by token
ViewResponse<Key.ComplexKey, Object> response = request.getResponse();
String token = response.getNextPageToken();
request.getResponse(token);
// We want the last context
HttpConnectionInterceptorContext context = cci.contexts.get(cci.contexts.size() - 1);
String query = context.connection.url.getQuery();
assertTrue(query.contains("startkey=%5B%22uuid%22," + "1005%5D"), "The query startkey " + "should match.");
}
Aggregations