Search in sources :

Example 36 with Response

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();
    }
}
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 37 with Response

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();
    }
}
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 38 with Response

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

Example 39 with Response

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();
    }
}
Also used : Changes(com.cloudant.client.api.Changes) Response(com.cloudant.client.api.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Row(com.cloudant.client.api.model.ChangesResult.Row) JsonObject(com.google.gson.JsonObject) ChangesResult(com.cloudant.client.api.model.ChangesResult) DbInfo(com.cloudant.client.api.model.DbInfo) Test(org.junit.jupiter.api.Test)

Example 40 with Response

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 ");
}
Also used : Response(com.cloudant.client.api.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) DesignDocument(com.cloudant.client.api.model.DesignDocument) Test(org.junit.jupiter.api.Test)

Aggregations

Response (com.cloudant.client.api.model.Response)41 Test (org.junit.jupiter.api.Test)40 InputStream (java.io.InputStream)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Document (com.cloudant.client.api.model.Document)5 Params (com.cloudant.client.api.model.Params)5 DesignDocument (com.cloudant.client.api.model.DesignDocument)4 JsonObject (com.google.gson.JsonObject)4 ReplicatorDocument (com.cloudant.client.api.model.ReplicatorDocument)3 Attachment (com.cloudant.client.api.model.Attachment)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Changes (com.cloudant.client.api.Changes)1 Database (com.cloudant.client.api.Database)1 ChangesResult (com.cloudant.client.api.model.ChangesResult)1 Row (com.cloudant.client.api.model.ChangesResult.Row)1 DbInfo (com.cloudant.client.api.model.DbInfo)1 URIBase (com.cloudant.client.internal.URIBase)1