Search in sources :

Example 16 with Response

use of com.cloudant.client.api.model.Response 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 17 with Response

use of com.cloudant.client.api.model.Response 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 18 with Response

use of com.cloudant.client.api.model.Response in project java-cloudant by cloudant.

the class BulkDocumentTest method bulkModifyDocs.

@Test
public void bulkModifyDocs() {
    List<Object> newDocs = new ArrayList<Object>();
    newDocs.add(new Foo());
    newDocs.add(new JsonObject());
    List<Response> responses = db.bulk(newDocs);
    assertThat(responses.size(), is(2));
}
Also used : Response(com.cloudant.client.api.model.Response) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) Test(org.junit.jupiter.api.Test)

Example 19 with Response

use of com.cloudant.client.api.model.Response in project java-cloudant by cloudant.

the class DesignDocumentsTest method updateDesignDocIndex.

@Test
public void updateDesignDocIndex() throws Exception {
    DesignDocument designDoc1 = DesignDocumentManager.fromFile(new File(String.format("%s/views101_design_doc.js", rootDesignDir)));
    designDoc1.setId("_design/MyAmazingDdoc");
    JsonObject indexes = designDoc1.getIndexes();
    designDoc1.setIndexes(null);
    Response response = designManager.put(designDoc1);
    designDoc1.setRevision(response.getRev());
    designDoc1.setIndexes(indexes);
    response = designManager.put(designDoc1);
    Utils.assertOKResponse(response);
}
Also used : Response(com.cloudant.client.api.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) DesignDocument(com.cloudant.client.api.model.DesignDocument) JsonObject(com.google.gson.JsonObject) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 20 with Response

use of com.cloudant.client.api.model.Response in project java-cloudant by cloudant.

the class DesignDocumentsTest method serializeJavascriptView.

/**
 * Test that when setting a javascript map function it is correctly serialized and deserialized.
 *
 * @throws Exception
 * @see #deserializeJavascriptView()
 * @see #serializeQueryDesignDoc()
 * @see #deserializeQueryDesignDoc()
 */
@Test
public void serializeJavascriptView() throws Exception {
    // Create and write a design document with a javascript lang map function
    String testDDocName = "testJSMapFn";
    String mapFunction = "function(doc){emit([doc.contentArray[0].boolean,doc.contentArray[0]" + ".creator,doc.contentArray[0].created],doc);}";
    DesignDocument ddoc = new DesignDocument();
    ddoc.setId(testDDocName);
    Map<String, DesignDocument.MapReduce> views = new HashMap<String, DesignDocument.MapReduce>();
    DesignDocument.MapReduce mr = new DesignDocument.MapReduce();
    mr.setMap(mapFunction);
    mr.setReduce("_count");
    views.put("testView", mr);
    ddoc.setViews(views);
    Response r = designManager.put(ddoc);
    // Retrieve the doc and check that the javascript function is correct
    DesignDocument retrievedDDoc = designManager.get(testDDocName, r.getRev());
    assertNotNull(retrievedDDoc, "There should be a retrieved design doc");
    Map<String, DesignDocument.MapReduce> retrievedViews = retrievedDDoc.getViews();
    assertNotNull(retrievedViews, "There should be views defined on the design doc");
    DesignDocument.MapReduce mrView = retrievedViews.get("testView");
    assertNotNull(mrView, "There should be a testView in the retrieved design doc");
    assertEquals(mapFunction, mrView.getMap(), "The map function string should be the " + "expected string");
}
Also used : Response(com.cloudant.client.api.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) 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