use of com.cloudant.client.api.model.Response in project java-cloudant by cloudant.
the class AttachmentsTest method getAttachmentStandaloneWithoutRev.
@Test
public void getAttachmentStandaloneWithoutRev() throws IOException, URISyntaxException {
byte[] bytesToDB = "binary data".getBytes();
ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytesToDB);
Response response = db.saveAttachment(bytesIn, "foo.txt", "text/plain");
Document doc = db.find(Document.class, response.getId());
assertTrue(doc.getAttachments().containsKey("foo.txt"));
InputStream in = db.getAttachment(response.getId(), "foo.txt");
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
IOUtils.copy(in, bytesOut);
byte[] bytesFromDB = bytesOut.toByteArray();
assertArrayEquals(bytesToDB, bytesFromDB);
} finally {
in.close();
}
}
use of com.cloudant.client.api.model.Response in project java-cloudant by cloudant.
the class AttachmentsTest method addNewAttachmentToExistingDocument.
@Test
public void addNewAttachmentToExistingDocument() throws Exception {
// Save a new document
Bar bar = new Bar();
Response response = db.save(bar);
// Create an attachment and save it to the existing document
byte[] bytesToDB = "binary data".getBytes("UTF-8");
ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytesToDB);
Response attResponse = db.saveAttachment(bytesIn, "foo.txt", "text/plain", response.getId(), response.getRev());
assertEquals(response.getId(), attResponse.getId(), "The document ID should be the same");
assertTrue(attResponse.getStatusCode() / 100 == 2, "The response code should be a 20x");
assertNull(attResponse.getError(), "There should be no error saving the attachment");
// Assert the attachment is correct
Document doc = db.find(Document.class, response.getId(), attResponse.getRev());
assertTrue(doc.getAttachments().containsKey("foo.txt"));
InputStream in = db.getAttachment(response.getId(), "foo.txt");
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
IOUtils.copy(in, bytesOut);
byte[] bytesFromDB = bytesOut.toByteArray();
assertArrayEquals(bytesToDB, bytesFromDB);
} finally {
in.close();
}
}
use of com.cloudant.client.api.model.Response in project java-cloudant by cloudant.
the class BulkDocumentTest method bulkDocsRetrieve.
@Test
public void bulkDocsRetrieve() throws Exception {
Response r1 = db.save(new Foo());
Response r2 = db.save(new Foo());
List<Foo> docs = db.getAllDocsRequestBuilder().includeDocs(true).keys(r1.getId(), r2.getId()).build().getResponse().getDocsAs(Foo.class);
assertThat(docs.size(), is(2));
}
use of com.cloudant.client.api.model.Response in project java-cloudant by cloudant.
the class ChangeNotificationsTest method changes_continuousFeed.
@Test
public void changes_continuousFeed() {
db.save(new Foo());
DbInfo dbInfo = db.info();
String since = dbInfo.getUpdateSeq();
Changes changes = db.changes().includeDocs(true).since(since).heartBeat(30000).continuousChanges();
Response response = db.save(new Foo());
while (changes.hasNext()) {
ChangesResult.Row feed = changes.next();
final JsonObject feedObject = feed.getDoc();
final String docId = feed.getId();
assertEquals(response.getId(), docId);
assertNotNull(feedObject);
changes.stop();
}
}
use of com.cloudant.client.api.model.Response in project java-cloudant by cloudant.
the class DesignDocumentsTest method designDocCompare.
@Test
public void designDocCompare() throws Exception {
DesignDocument exampleDoc = fileToDesignDocument("example");
Response response = designManager.put(exampleDoc);
// Assign the revision to our local DesignDocument object (needed for equality)
exampleDoc.setRevision(response.getRev());
DesignDocument designDoc11 = db.getDesignDocumentManager().get("_design/example");
assertEquals(exampleDoc, designDoc11, "The design document retrieved should equal ");
}
Aggregations