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");
}
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());
}
}
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");
}
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");
}
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()));
}
Aggregations