use of com.cloudant.client.api.model.Document in project java-cloudant by cloudant.
the class AttachmentsTest method attachmentStandaloneUpdate.
@Test
public void attachmentStandaloneUpdate() throws Exception {
byte[] bytesToDB = "binary data".getBytes("UTF-8");
ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytesToDB);
Response response = db.saveAttachment(bytesIn, "foo.txt", "text/plain");
bytesToDB = "updated binary data".getBytes("UTF-8");
bytesIn = new ByteArrayInputStream(bytesToDB);
response = db.saveAttachment(bytesIn, "foo.txt", "text/plain", response.getId(), response.getRev());
Document doc = db.find(Document.class, response.getId(), response.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.Document in project java-cloudant by cloudant.
the class AttachmentsTest method getAttachmentStandaloneWithRev.
@Test
public void getAttachmentStandaloneWithRev() 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", response.getRev());
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.Document in project java-cloudant by cloudant.
the class AttachmentsTest method attachmentStandaloneNullIdNullRev.
@Test
public void attachmentStandaloneNullIdNullRev() throws IOException, URISyntaxException {
byte[] bytesToDB = "binary data".getBytes();
ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytesToDB);
// Save the attachment to a doc with the given ID
Response response = db.saveAttachment(bytesIn, "foo.txt", "text/plain", null, null);
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.Document 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.Document 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();
}
}
Aggregations