Search in sources :

Example 41 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class SslAuthenticationTest method remoteSslAuthenticationDisabledTest.

/**
 * Connect to the remote Cloudant server with SSL Authentication disabled.
 */
@TestTemplate
@RequiresCloudantService
public void remoteSslAuthenticationDisabledTest() {
    CloudantClient dbClient = CloudantClientHelper.getClientBuilder().disableSSLAuthentication().build();
    // Make an arbitrary connection to the DB.
    dbClient.getAllDbs();
// Test is successful if no exception is thrown, so no explicit check is needed.
}
Also used : CloudantClient(com.cloudant.client.api.CloudantClient) TestTemplate(org.junit.jupiter.api.TestTemplate) RequiresCloudantService(com.cloudant.test.main.RequiresCloudantService)

Example 42 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class ViewsTest method staleParameterValues.

/**
 * We can't test the server behaviour of stale, but we can test the URL values are what we
 * expect. This test uses the various values and checks the stale parameter in the URL, it makes
 * a request using getSingleValue() as it is easier to mock the responses.
 *
 * @throws Exception
 */
@Test
public void staleParameterValues() throws Exception {
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
    Database database = client.database("notarealdb", false);
    UnpaginatedRequestBuilder<String, Integer> viewBuilder = database.getViewRequestBuilder("testDDoc", "testView").newRequest(Key.Type.STRING, Integer.class);
    // Regex patterns for stale parameter cases
    Pattern noStaleParameter = Pattern.compile(".*/notarealdb/_design/testDDoc/_view/testView");
    Pattern staleParameterOK = Pattern.compile(".*/notarealdb/_design/testDDoc/_view" + "/testView\\?stale=ok");
    Pattern staleParameterUpdate = Pattern.compile(".*/notarealdb/_design/testDDoc/_view" + "/testView\\?stale=update_after");
    // Test the no stale argument supplied case
    assertStaleParameter(viewBuilder.build(), noStaleParameter);
    // Test the OK stale argument supplied case
    assertStaleParameter(viewBuilder.stale(SettableViewParameters.STALE_OK).build(), staleParameterOK);
    // Test the update_after stale argument supplied case
    assertStaleParameter(viewBuilder.stale(SettableViewParameters.STALE_UPDATE_AFTER).build(), staleParameterUpdate);
    // Test the NO stale argument supplied case
    assertStaleParameter(viewBuilder.stale(SettableViewParameters.STALE_NO).build(), noStaleParameter);
}
Also used : Pattern(java.util.regex.Pattern) CloudantClient(com.cloudant.client.api.CloudantClient) Database(com.cloudant.client.api.Database) TestWithDbPerTest(com.cloudant.tests.base.TestWithDbPerTest) Test(org.junit.jupiter.api.Test)

Example 43 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class ChangeNotificationsTest method changesDescending.

/**
 * Test that the descending true parameter is applied and the results are returned in reverse
 * order.
 */
@Test
public void changesDescending() throws Exception {
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
    Database db = client.database("notreal", false);
    // Mock up an empty set of changes
    mockWebServer.enqueue(new MockResponse().setBody("{\"results\": []}"));
    db.changes().descending(true).getChanges();
    RecordedRequest request = mockWebServer.takeRequest(1, TimeUnit.SECONDS);
    assertNotNull(request, "There should be a changes request");
    assertTrue(request.getPath().contains("descending=true"), "There should be a descending parameter on the " + "request");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) Database(com.cloudant.client.api.Database) Test(org.junit.jupiter.api.Test)

Example 44 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class ChangeNotificationsTest method changesFeedLastSeq.

/**
 * Test that the continuous changes feed iterator correctly reads the last_seq line
 */
@Test
public void changesFeedLastSeq() throws Exception {
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
    Database db = client.database("notreal", false);
    // Mock up a set of changes with a last_seq
    String body = "{\"seq\":\"1" + "-g1AAAAETeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YiA6oWI9xakhyAZFI9SFcGcyJjLpDHbmxukmponkjYBKIdlscCJBkagBTQov0Y7jMmpPMARCfIZ1kA23taSg\",\"id\":\"llama\",\"changes\":[{\"rev\":\"6-77acefc448de2129bb6427ebdeb021df\"}]}\n" + "{\"seq\":\"2-g1AAAAFDeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YiA6oWI9xakhyAZFI9SFcGcyJjLpDHbmxukmponkjYBKIdlscCJBkagBTQov0Y7jMmpPMARCeyG41NTZPSTAmbkgUA-4RpJw\",\"id\":\"kookaburra\",\"changes\":[{\"rev\":\"4-6038cf35dfe1211f85484dec951142c7\"}]}\n" + "{\"seq\":\"3-g1AAAAFDeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YiA6oWI9xakhyAZFI9SFcGcyJjLpDHbmxukmponkjYBKIdlscCJBkagBTQov0Y7jMmpPMARCfYjUwQNxqbmialmRI2JQsA-7RpKA\",\"id\":\"panda\",\"changes\":[{\"rev\":\"2-f578490963b0bd266f6c5bbf92302977\"}]}\n" + "{\"seq\":\"4-g1AAAAFDeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YiA6oWI9xakhyAZFI9SFcGcyJjLpDHbmxukmponkjYBKIdlscCJBkagBTQov0Y7jMmpPMARCfYjcwQNxqbmialmRI2JQsA--RpKQ\",\"id\":\"snipe\",\"changes\":[{\"rev\":\"3-4b2fb3b7d6a226b13951612d6ca15a6b\"}]}\n" + "{\"seq\":\"5-g1AAAAFzeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJjLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqUWwxNjdJNTRPJGwC0R7JYwGSDA1ACmjR_qxEBlSdxoR0HoDoBLuRGeJGY1PTpDRTwqZkAQAv9XgS\",\"id\":\"badger\",\"changes\":[{\"rev\":\"4-51aa94e4b0ef37271082033bba52b850\"}]}\n" + "{\"seq\":\"6-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJjLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqUWwxNjdJNTRPJGwC0R7JYwGSDA1ACmjRfoRNhubGZqaWlqT6x5iQTQcgNoH9xAzxk7GpaVKaKWFTsgBvlIad\",\"id\":\"870908b66ac0ed114512e6fb6d00260f\",\"changes\":[{\"rev\":\"2-eec205a9d413992850a6e32678485900\"}],\"deleted\":true}\n" + "{\"seq\":\"7-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJTLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqobYwgm0xNjdJNTRPJGwC0R7JYwGSDA1ACmjRfoRNhubGZqaWlqT6x5iQTQcgNoH9xAzxk7GpaVKaKWFTsgBw_oae\",\"id\":\"_design/validation\",\"changes\":[{\"rev\":\"2-97e93126a6337d173f9b2810c0b9c0b6\"}],\"deleted\":true}\n" + "{\"seq\":\"8-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJzLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqobYwgm0xNjdJNTRPJGwC0R7JYwGSDA1ACmjRfoRNhubGZqaWlqT6x5iQTQcgNiGFnLGxqWlSmilhU7IAcmiGnw\",\"id\":\"elephant\",\"changes\":[{\"rev\":\"3-f812228f45b5f4e496250556195372b2\"}]}\n" + "{\"seq\":\"9-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJzLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqobYwgm0xNjdJNTRPJGwC0R7JYwGSDA1ACmjRfpBNTGCbDM2NzUwtLUn1jzEhmw5AbEIKOWNjU9OkNFPCpmQBAHMChqA\",\"id\":\"_design/views101\",\"changes\":[{\"rev\":\"1-a918dd4f11704143b535f0ab3af4bf75\"}]}\n" + "{\"seq\":\"10-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJLLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqobYwgm0xNjdJNTRPJGwC0R7JYwGSDA1ACmjRfpBNTGCbDM2NzUwtLUn1jzEhmw5AbAL7iRniJ2NT06Q0U8KmZAEAdGyGoQ\",\"id\":\"lemur\",\"changes\":[{\"rev\":\"3-552d9dbf91fa914a07756e69b9ceaafa\"}]}\n" + "{\"seq\":\"11-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJLLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqobYwgm0xNjdJNTRPJGwC0R7JYwGSDA1ACmjRfpBNzGCbDM2NzUwtLUn1jzEhmw5AbPqPsMnY2NQ0Kc2UsClZAHUGhqI\",\"id\":\"aardvark\",\"changes\":[{\"rev\":\"3-fe45a3e06244adbe7ba145e74e57aba5\"}]}\n" + "{\"seq\":\"12-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJrLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqobYwgm0xNjdJNTRPJGwC0R7JYwGSDA1ACmjRfpBNzGCbDM2NzUwtLUn1jzEhmw5AbPqPsMnY2NQ0Kc2UsClZAHZwhqM\",\"id\":\"zebra\",\"changes\":[{\"rev\":\"3-750dac460a6cc41e6999f8943b8e603e\"}]}\n" + "{\"seq\":\"13-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJrLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqobYwgm0xNjdJNTRPJGwC0R7JYwGSDA1ACmjRfpBNLGCbDM2NzUwtLUn1jzEhmw5AbAL7iRniJ2NT06Q0U8KmZAEAdwqGpA\",\"id\":\"cat\",\"changes\":[{\"rev\":\"2-eec205a9d413992850a6e32678485900\"}],\"deleted\":true}\n" + "{\"seq\":\"14-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJrLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqobYwgm0xNjdJNTRPJGwC0R7JYwGSDA1ACmjRfoR_DM2NzUwtLUn1jzEhmw5AbAL7iRniJ2NT06Q0U8KmZAEAd6SGpQ\",\"id\":\"giraffe\",\"changes\":[{\"rev\":\"3-7665c3e66315ff40616cceef62886bd8\"}]}\n" + "{\"last_seq\":\"14-g1AAAAGjeJzLYWBgYMlgTmGQT0lKzi9KdUhJMtNLykxPyilN1UvOyS9NScwr0ctLLckBKmRKZEiy____f1YGcyJrLlCA3dwkLcXczISwdlQrjHBbkeQAJJPqobYwgm0xNjdJNTRPJGwC0R7JYwGSDA1ACmjRfoR_DM2NzUwtLUn1jzEhmw5AbAL7iRniJ2NT06Q0U8KmZAEAd6SGpQ\",\"pending\":0}";
    mockWebServer.enqueue(new MockResponse().setBody(body));
    Changes c = db.changes().continuousChanges();
    int nChanges = 0;
    // check against regression where hasNext() will hang
    while (c.hasNext()) {
        nChanges++;
        c.next();
    }
    RecordedRequest request = mockWebServer.takeRequest(1, TimeUnit.SECONDS);
    assertNotNull(request, "There should be a changes request");
    assertEquals(14, nChanges, "There should be 14 changes");
}
Also used : Changes(com.cloudant.client.api.Changes) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) Database(com.cloudant.client.api.Database) Test(org.junit.jupiter.api.Test)

Example 45 with CloudantClient

use of com.cloudant.client.api.CloudantClient in project java-cloudant by cloudant.

the class CloudantClientTests method sessionDeleteOnShutdown.

@Test
public void sessionDeleteOnShutdown() throws Exception {
    // Mock a 200 OK for the _session DELETE request
    server.enqueue(new MockResponse().setResponseCode(200).setBody("{\"ok\":\"true\"}"));
    CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(server).build();
    c.shutdown();
    RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
    assertEquals("DELETE", request.getMethod(), "The request method should be DELETE");
    assertEquals("/_session", request.getPath(), "The request should be to the _session path");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) Test(org.junit.jupiter.api.Test)

Aggregations

CloudantClient (com.cloudant.client.api.CloudantClient)47 MockResponse (okhttp3.mockwebserver.MockResponse)32 TestTemplate (org.junit.jupiter.api.TestTemplate)23 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)21 Test (org.junit.jupiter.api.Test)20 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 Database (com.cloudant.client.api.Database)8 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)7 TestTimer (com.cloudant.tests.util.TestTimer)4 URL (java.net.URL)4 TooManyRequestsException (com.cloudant.client.org.lightcouch.TooManyRequestsException)3 HttpConnection (com.cloudant.http.HttpConnection)3 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)3 Replay429Interceptor (com.cloudant.http.interceptors.Replay429Interceptor)3 Dispatcher (okhttp3.mockwebserver.Dispatcher)3 HttpConnectionRequestInterceptor (com.cloudant.http.HttpConnectionRequestInterceptor)2 BasicAuthInterceptor (com.cloudant.http.interceptors.BasicAuthInterceptor)2 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)2 RequiresCloudantService (com.cloudant.test.main.RequiresCloudantService)2 TestWithDbPerTest (com.cloudant.tests.base.TestWithDbPerTest)2