Search in sources :

Example 1 with Database

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

the class ChangeNotificationsTest method changesCustomParameter.

/**
 * Test that a custom parameter can be added to a changes request.
 */
@Test
public void changesCustomParameter() 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().filter("myFilter").parameter("myParam", "paramValue").getChanges();
    RecordedRequest request = mockWebServer.takeRequest(1, TimeUnit.SECONDS);
    assertNotNull(request, "There should be a changes request");
    assertTrue(request.getPath().contains("filter=myFilter"), "There should be a filter parameter on the request");
    assertTrue(request.getPath().contains("myParam=paramValue"), "There should be a custom 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 2 with Database

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

the class DatabaseTest method permissionsParsing.

@Test
public void permissionsParsing() throws Exception {
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
    Database db = client.database("notarealdb", false);
    // Mock up a request of all permissions
    // for GET _security
    mockWebServer.enqueue(MockWebServerResources.PERMISSIONS);
    // for PUT _security
    mockWebServer.enqueue(MockWebServerResources.JSON_OK);
    db.setPermissions("testUsername", EnumSet.allOf(Permissions.class));
    // Mock up a failing request
    String testError = "test error";
    String testReason = "test reason";
    // for GET _security
    mockWebServer.enqueue(MockWebServerResources.PERMISSIONS);
    mockWebServer.enqueue(new MockResponse().setResponseCode(400).setBody("{\"reason\":\"" + testReason + "\", \"error\":\"" + testError + "\"}"));
    try {
        db.setPermissions("testUsername", EnumSet.allOf(Permissions.class));
    } catch (CouchDbException e) {
        assertEquals(testError, e.getError());
        assertEquals(testReason, e.getReason());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CouchDbException(com.cloudant.client.org.lightcouch.CouchDbException) CloudantClient(com.cloudant.client.api.CloudantClient) Database(com.cloudant.client.api.Database) Permissions(com.cloudant.client.api.model.Permissions) Test(org.junit.jupiter.api.Test)

Example 3 with Database

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

the class DatabaseTest method customGsonDeserializerTest.

// Test case for issue #31
@Test
public void customGsonDeserializerTest() throws MalformedURLException {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    CloudantClient account = CloudantClientHelper.getClientBuilder().gsonBuilder(builder).build();
    Database db = account.database(dbResource.getDatabaseName(), false);
    Map<String, Object> h = new HashMap<String, Object>();
    h.put("_id", "serializertest");
    h.put("date", "2015-01-23T18:25:43.511Z");
    db.save(h);
    // should not throw a JsonSyntaxException
    db.find(Foo.class, "serializertest");
}
Also used : CloudantClient(com.cloudant.client.api.CloudantClient) GsonBuilder(com.google.gson.GsonBuilder) HashMap(java.util.HashMap) Database(com.cloudant.client.api.Database) Test(org.junit.jupiter.api.Test)

Example 4 with Database

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

the class ViewsTest method getIdsAndRevsForDeletedIDsWithAllDocs.

/**
 * <p>
 * Test added for https://github.com/cloudant/java-cloudant/issues/411
 * </p>
 * <p>
 * When _all_docs is used with specified keys deleted documents are also returned. The value of
 * total_rows may represent only the un-deleted documents meaning more rows are returned than
 * total_rows. This total_rows variance doesn't always manifest so we reproduce it using a mock.
 * </p>
 *
 * @throws Exception
 */
@Test
public void getIdsAndRevsForDeletedIDsWithAllDocs() throws Exception {
    Map<String, String> idsAndRevs = new HashMap<String, String>(4);
    idsAndRevs.put("docid0", "1-a00e6463d52d7f167c8ac5c834836c1b");
    idsAndRevs.put("docid1", "1-a00e6463d52d7f167c8ac5c834836c1b");
    idsAndRevs.put("docid2", "2-acbb972b187ec952eae1ca74cfef16a9");
    idsAndRevs.put("docid3", "2-acbb972b187ec952eae1ca74cfef16a9");
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
    Database database = client.database("deletedidsalldocskeysdb", false);
    // _all_docs?keys=["docid0", "docid1", "docid2", "docid3"]
    MockResponse mockResponse = new MockResponse().setResponseCode(200).setBody("{\"total_rows\":2,\"offset\":0,\"rows\":[\n" + "{\"id\":\"docid0\",\"key\":\"docid0\"," + "\"value\":{\"rev\":\"1-a00e6463d52d7f167c8ac5c834836c1b\"}},\n" + "{\"id\":\"docid1\",\"key\":\"docid1\"," + "\"value\":{\"rev\":\"1-a00e6463d52d7f167c8ac5c834836c1b\"}},\n" + "{\"id\":\"docid2\",\"key\":\"docid2\"," + "\"value\":{\"rev\":\"2-acbb972b187ec952eae1ca74cfef16a9\"," + "\"deleted\":true}},\n" + "{\"id\":\"docid3\",\"key\":\"docid3\"," + "\"value\":{\"rev\":\"2-acbb972b187ec952eae1ca74cfef16a9\"," + "\"deleted\":true}}\n" + "]}");
    mockWebServer.enqueue(mockResponse);
    // Do an _all_docs request using the 4 _ids of the generated docs.
    Map<String, String> allDocsIdsAndRevs = database.getAllDocsRequestBuilder().keys(idsAndRevs.keySet().toArray(new String[4])).build().getResponse().getIdsAndRevs();
    assertEquals(idsAndRevs, allDocsIdsAndRevs, "The ids and revs should be equal");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) HashMap(java.util.HashMap) Database(com.cloudant.client.api.Database) TestWithDbPerTest(com.cloudant.tests.base.TestWithDbPerTest) Test(org.junit.jupiter.api.Test)

Example 5 with Database

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

the class Utils method removeReplicatorTestDoc.

public static void removeReplicatorTestDoc(CloudantClient account, String replicatorDocId) throws Exception {
    // Grab replicator doc revision using HTTP HEAD command
    String replicatorDb = "_replicator";
    URI uri = new URIBase(account.getBaseUri()).path(replicatorDb).path(replicatorDocId).build();
    HttpConnection head = Http.HEAD(uri);
    // add a response interceptor to allow us to retrieve the ETag revision header
    final AtomicReference<String> revisionRef = new AtomicReference<String>();
    head.responseInterceptors.add(new HttpConnectionResponseInterceptor() {

        @Override
        public HttpConnectionInterceptorContext interceptResponse(HttpConnectionInterceptorContext context) {
            revisionRef.set(context.connection.getConnection().getHeaderField("ETag"));
            return context;
        }
    });
    account.executeRequest(head);
    String revision = revisionRef.get();
    assertNotNull("The revision should not be null", revision);
    Database replicator = account.database(replicatorDb, false);
    Response removeResponse = replicator.remove(replicatorDocId, revision.replaceAll("\"", ""));
    assertThat(removeResponse.getError(), is(nullValue()));
}
Also used : Response(com.cloudant.client.api.model.Response) HttpConnection(com.cloudant.http.HttpConnection) Database(com.cloudant.client.api.Database) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpConnectionResponseInterceptor(com.cloudant.http.HttpConnectionResponseInterceptor) HttpConnectionInterceptorContext(com.cloudant.http.HttpConnectionInterceptorContext) URI(java.net.URI) URIBase(com.cloudant.client.internal.URIBase)

Aggregations

Database (com.cloudant.client.api.Database)10 CloudantClient (com.cloudant.client.api.CloudantClient)8 Test (org.junit.jupiter.api.Test)8 MockResponse (okhttp3.mockwebserver.MockResponse)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 TestWithDbPerTest (com.cloudant.tests.base.TestWithDbPerTest)2 HashMap (java.util.HashMap)2 Changes (com.cloudant.client.api.Changes)1 Permissions (com.cloudant.client.api.model.Permissions)1 Response (com.cloudant.client.api.model.Response)1 URIBase (com.cloudant.client.internal.URIBase)1 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)1 NoDocumentException (com.cloudant.client.org.lightcouch.NoDocumentException)1 HttpConnection (com.cloudant.http.HttpConnection)1 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)1 HttpConnectionResponseInterceptor (com.cloudant.http.HttpConnectionResponseInterceptor)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1