Search in sources :

Example 1 with Document

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();
    }
}
Also used : Response(com.cloudant.client.api.model.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.cloudant.client.api.model.Document) Test(org.junit.jupiter.api.Test)

Example 2 with Document

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();
    }
}
Also used : Response(com.cloudant.client.api.model.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.cloudant.client.api.model.Document) Test(org.junit.jupiter.api.Test)

Example 3 with Document

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();
    }
}
Also used : Response(com.cloudant.client.api.model.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.cloudant.client.api.model.Document) Test(org.junit.jupiter.api.Test)

Example 4 with Document

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();
    }
}
Also used : Response(com.cloudant.client.api.model.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.cloudant.client.api.model.Document) Test(org.junit.jupiter.api.Test)

Example 5 with Document

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();
    }
}
Also used : Response(com.cloudant.client.api.model.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.cloudant.client.api.model.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (com.cloudant.client.api.model.Document)5 Response (com.cloudant.client.api.model.Response)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStream (java.io.InputStream)5 Test (org.junit.jupiter.api.Test)5