Search in sources :

Example 6 with HttpConnectionInterceptorContext

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.");
}
Also used : JsonObject(com.google.gson.JsonObject) HttpConnectionInterceptorContext(com.cloudant.http.HttpConnectionInterceptorContext) TestWithDbPerTest(com.cloudant.tests.base.TestWithDbPerTest) Test(org.junit.jupiter.api.Test)

Aggregations

HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)6 CloudantClient (com.cloudant.client.api.CloudantClient)3 HttpConnection (com.cloudant.http.HttpConnection)3 HttpConnectionResponseInterceptor (com.cloudant.http.HttpConnectionResponseInterceptor)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 HttpConnectionRequestInterceptor (com.cloudant.http.HttpConnectionRequestInterceptor)2 IOException (java.io.IOException)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Test (org.junit.jupiter.api.Test)2 TestTemplate (org.junit.jupiter.api.TestTemplate)2 Database (com.cloudant.client.api.Database)1 Response (com.cloudant.client.api.model.Response)1 URIBase (com.cloudant.client.internal.URIBase)1 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)1 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)1 TestWithDbPerTest (com.cloudant.tests.base.TestWithDbPerTest)1 MockWebServerResources (com.cloudant.tests.util.MockWebServerResources)1 JsonObject (com.google.gson.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1