public static void removeReplicatorTestDoc(CloudantClient account, String replicatorDocId) throws Exception { // Grab replicator doc revision using HTTP HEAD command String replicatorDb = "_replicator"; URI uri = URI.create(account.getBaseUri() + replicatorDb + "/" + replicatorDocId); 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())); }
// Test case for issue #31 @Test public void customGsonDeserializerTest() { Map<String, Object> h = new HashMap<String, Object>(); h.put("_id", "serializertest"); h.put("date", "2015-01-23T18:25:43.511Z"); db.save(h); GsonBuilder builder = new GsonBuilder(); builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss"); account.setGsonBuilder(builder); db.find(Foo.class, "serializertest"); // should not throw a JsonSyntaxException }
public static void main(String[] args) { CloudantClient client = new CloudantClient("http://username.cloudant.com", "username", "password"); Database db = client.database("java-searchindex", false); Map<String, Object> animals = new HashMap<>(); animals.put("index", "function(doc){ index(\"default\", doc._id); }"); Map<String, Object> indexes = new HashMap<>(); indexes.put("animals", animals); Map<String, Object> ddoc = new HashMap<>(); ddoc.put("_id", "_design/searchindex"); ddoc.put("indexes", indexes); db.save(ddoc); }
@Test @Category(RequiresCloudant.class) public void shard() { Shard s = db.getShard("snipe"); assertNotNull(s); assertNotNull(s.getRange()); assertNotNull(s.getNodes()); assert (s.getNodes().hasNext()); }
@Test @Category(RequiresCloudant.class) public void shards() { List<Shard> shards = db.getShards(); assert (shards.size() > 0); for (Shard s : shards) { assertNotNull(s.getRange()); assertNotNull(s.getNodes()); assertNotNull(s.getNodes().hasNext()); } }
@Test @Category(RequiresCloudantService.class) public void permissions() { Map<String, EnumSet<Permissions>> userPerms = db.getPermissions(); assertNotNull(userPerms); ApiKey key = account.generateApiKey(); EnumSet<Permissions> p = EnumSet.<Permissions>of(Permissions._reader, Permissions._writer); db.setPermissions(key.getKey(), p); userPerms = db.getPermissions(); assertNotNull(userPerms); assertEquals(userPerms.size(), 1); assertEquals(userPerms.get(key.getKey()), p); p = EnumSet.noneOf(Permissions.class); db.setPermissions(key.getKey(), p); userPerms = db.getPermissions(); assertNotNull(userPerms); assertEquals(userPerms.size(), 1); assertEquals(userPerms.get(key.getKey()), p); }
/** * When testing against a cluster requests can be routed to different nodes. This means after a * save a document may not appear for all subsequent requests until an internal replication has * finished between all the nodes. This method tries to find a document up to maxRetries times, * with retries separated by 0.5 s. Retrying may route the request to a node that already has the * document and the delay between retries also gives an opportunity for internal replication to * complete between nodes increasing the chance of retrieving the document. * * <p>It should be noted that trying to read back a document immediately after writing to a * cluster is not a best practice, but is needed in some test cases to reproduce, for example, * conflict conditions. It is also worth noting that a document being returned from this method is * not a guarantee that it has reached all nodes, only that it has reached a node that received * one of the requests. * * @param db * @param docId */ public static <T> T findDocumentWithRetries( Database db, String docId, Class<T> clazz, int maxRetries) throws Exception { for (int i = 1; i <= maxRetries; i++) { try { return db.find(clazz, docId); } catch (NoDocumentException e) { if (i == maxRetries) { throw e; } else { // sleep for 0.5 s before trying again Thread.sleep(500); } } } throw new Exception("Retries exceeded"); }
@Test @Category(RequiresCloudant.class) public void QuorumTests() { db.save(new Animal("human"), 2); Animal h = db.find(Animal.class, "human", new com.cloudant.client.api.model.Params().readQuorum(2)); assertNotNull(h); assertEquals("human", h.getId()); db.update(h.setClass("inhuman"), 2); h = db.find(Animal.class, "human", new com.cloudant.client.api.model.Params().readQuorum(2)); assertEquals("inhuman", h.getclass()); db.post(new Animal("test"), 2); h = db.find(Animal.class, "test", new com.cloudant.client.api.model.Params().readQuorum(3)); assertEquals("test", h.getId()); }