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();
}
}
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();
}
}
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));
}
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);
}
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");
}
Aggregations