Search in sources :

Example 11 with Response

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

the class UpdateHandlerTest method updateHandler_queryParams.

@Test
public void updateHandler_queryParams() {
    final String oldValue = "foo";
    final String newValue = "foo bar+plus=equals&ampersand";
    Response response = db.save(new Foo(null, oldValue));
    Params params = new Params().addParam("field", "title").addParam("value", newValue);
    String output = db.invokeUpdateHandler("example/example_update", response.getId(), params);
    // retrieve from db to verify
    Foo foo = db.find(Foo.class, response.getId());
    assertNotNull(output);
    assertEquals(foo.getTitle(), newValue);
}
Also used : Response(com.cloudant.client.api.model.Response) Params(com.cloudant.client.api.model.Params) Test(org.junit.jupiter.api.Test)

Example 12 with Response

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

the class UpdateHandlerTest method updateHandler_queryString.

@Test
public void updateHandler_queryString() {
    final String oldValue = "foo";
    final String newValue = "foo bar+plus=equals&ampersand";
    Response response = db.save(new Foo(null, oldValue));
    Params params = new Params().addParam("field", "title").addParam("value", newValue);
    String output = db.invokeUpdateHandler("example/example_update", response.getId(), params);
    // retrieve from db to verify
    Foo foo = db.find(Foo.class, response.getId());
    assertNotNull(output);
    assertEquals(foo.getTitle(), newValue);
}
Also used : Response(com.cloudant.client.api.model.Response) Params(com.cloudant.client.api.model.Params) Test(org.junit.jupiter.api.Test)

Example 13 with Response

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

the class AttachmentsTest method attachmentStandaloneGivenId.

@Test
public void attachmentStandaloneGivenId() throws IOException, URISyntaxException {
    String docId = Utils.generateUUID();
    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", docId, null);
    assertEquals(docId, response.getId(), "The saved document ID should match");
    InputStream in = db.getAttachment(docId, "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) Test(org.junit.jupiter.api.Test)

Example 14 with Response

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

the class AttachmentsTest method removeAttachment.

@Test
public void removeAttachment() {
    String attachmentName = "txt_1.txt";
    Attachment attachment1 = new Attachment("VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=", "text/plain");
    // Bar extends Document
    Bar bar = new Bar();
    bar.addAttachment(attachmentName, attachment1);
    Response response = db.save(bar);
    Bar bar2 = db.find(Bar.class, response.getId(), new Params().attachments());
    String base64Data = bar2.getAttachments().get("txt_1.txt").getData();
    assertNotNull(base64Data);
    response = db.removeAttachment(bar2, attachmentName);
    Bar bar3 = db.find(Bar.class, response.getId(), new Params().attachments());
    assertNull(bar3.getAttachments());
}
Also used : Response(com.cloudant.client.api.model.Response) Params(com.cloudant.client.api.model.Params) Attachment(com.cloudant.client.api.model.Attachment) Test(org.junit.jupiter.api.Test)

Example 15 with Response

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

the class AttachmentsTest method attachmentInline_getWithDocument.

@Test
public void attachmentInline_getWithDocument() {
    Attachment attachment = new Attachment("VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=", "text/plain");
    Bar bar = new Bar();
    bar.addAttachment("txt_1.txt", attachment);
    Response response = db.save(bar);
    Bar bar2 = db.find(Bar.class, response.getId(), new Params().attachments());
    String base64Data = bar2.getAttachments().get("txt_1.txt").getData();
    assertNotNull(base64Data);
}
Also used : Response(com.cloudant.client.api.model.Response) Params(com.cloudant.client.api.model.Params) Attachment(com.cloudant.client.api.model.Attachment) 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