@Test
  public void buildRevisionFromMapValidMapWithAttachments() throws Exception {

    // lets the get the attachment encoded

    File file = TestUtils.loadFixture("fixture/bonsai-boston.jpg");

    byte[] unencodedAttachment = FileUtils.readFileToByteArray(file);
    byte[] encodedAttachment = this.encodeAttachment(unencodedAttachment);

    Map<String, Map<String, Object>> attachments = new HashMap<String, Map<String, Object>>();
    Map<String, Object> bonsai = createAttachmentMap(encodedAttachment, "image/jpeg", false);

    attachments.put("bonsai-boston.jpg", bonsai);
    documentRev.put("_attachments", attachments);

    DocumentRevision revision =
        DocumentRevisionBuilder.buildRevisionFromMap(documentURI, documentRev);

    Assert.assertNotNull(revision);
    Assert.assertEquals(body, revision.getBody().asMap());
    Assert.assertEquals(revision.getId(), "someIdHere");
    Assert.assertEquals(revision.getRevision(), "3-750dac460a6cc41e6999f8943b8e603e");
    Assert.assertEquals(revision.getAttachments().size(), 1);
    ByteArrayInputStream expected = new ByteArrayInputStream(unencodedAttachment);
    InputStream actual = revision.getAttachments().get("bonsai-boston.jpg").getInputStream();
    Assert.assertTrue(TestUtils.streamsEqual(expected, actual));
  }
  @Test
  public void buildRevisionFromMapValidMapWithAttachmentsDataExcluded() throws Exception {

    Assume.assumeFalse(
        "Not running test as 'buildRevisionFromMap' can't retrieve the "
            + "attachment with cookie authentication enabled",
        TestOptions.COOKIE_AUTH);

    // lets the get the attachment encoded

    File file = TestUtils.loadFixture("fixture/bonsai-boston.jpg");

    byte[] unencodedAttachment = FileUtils.readFileToByteArray(file);
    byte[] encodedAttachment = this.encodeAttachment(unencodedAttachment);

    // create a revision on a couchDB instance so we can test the download
    // of attachments
    Map<String, Object> remoteDoc = new HashMap<String, Object>();
    remoteDoc.put("_id", "someIdHere");
    Map<String, Map<String, Object>> remoteAttachments = new HashMap<String, Map<String, Object>>();
    Map<String, Object> remoteBonsai = createAttachmentMap(encodedAttachment, "image/jpeg", false);

    remoteAttachments.put("bonsai-boston.jpg", remoteBonsai);
    remoteDoc.put("_attachments", remoteAttachments);

    remoteDb.create(remoteDoc);

    // build up the test json map

    Map<String, Map<String, Object>> attachments = new HashMap<String, Map<String, Object>>();
    Map<String, Object> bonsai = createAttachmentMap(encodedAttachment, "image/jpeg", true);

    attachments.put("bonsai-boston.jpg", bonsai);
    documentRev.put("_attachments", attachments);

    URI uri = new URI(remoteDb.getCouchClient().getRootUri().toString() + "/" + "someIdHere");

    // create the document revision and test

    DocumentRevision revision = DocumentRevisionBuilder.buildRevisionFromMap(uri, documentRev);

    Assert.assertNotNull(revision);
    Assert.assertEquals(body, revision.getBody().asMap());
    Assert.assertEquals(revision.getId(), "someIdHere");
    Assert.assertEquals(revision.getRevision(), "3-750dac460a6cc41e6999f8943b8e603e");
    Assert.assertEquals(revision.getAttachments().size(), 1);
    InputStream attachmentInputStream =
        revision.getAttachments().get("bonsai-boston.jpg").getInputStream();
    InputStream expectedInputStream = new ByteArrayInputStream(unencodedAttachment);
    Assert.assertTrue(TestUtils.streamsEqual(expectedInputStream, attachmentInputStream));
  }
  @Before
  public void setup() throws IOException {
    CouchConfig config = super.getCouchConfig(CLOUDANT_TEST_DB_NAME + System.currentTimeMillis());
    remoteDb =
        new CouchClientWrapper(
            config.getRootUri(), config.getRequestInterceptors(), config.getResponseInterceptors());
    bodyOne =
        DocumentBodyFactory.create(
            FileUtils.readFileToByteArray(TestUtils.loadFixture(documentOneFile)));
    bodyTwo =
        DocumentBodyFactory.create(
            FileUtils.readFileToByteArray(TestUtils.loadFixture(documentTwoFile)));

    CouchClientWrapperDbUtils.deleteDbQuietly(remoteDb);
    remoteDb.createDatabase();
  }
  @Test
  public void buildRevisionFromMapValidMapWithTextAttachmentsDataExcluded() throws Exception {

    Assume.assumeFalse(
        "Not running test as 'buildRevisionFromMap' can't retrieve the "
            + "attachment with cookie authentication enabled",
        TestOptions.COOKIE_AUTH);

    // lets the get the attachment encoded

    String attachmentText = "Hello World";

    byte[] unencodedAttachment = attachmentText.getBytes();
    byte[] encodedAttachment = this.encodeAttachment(unencodedAttachment);

    // create a revision on a couchDB instance so we can test the download
    // of attachments
    Map<String, Object> remoteDoc = new HashMap<String, Object>();
    remoteDoc.put("_id", "someIdHere");
    Map<String, Map<String, Object>> remoteAttachments = new HashMap<String, Map<String, Object>>();

    remoteAttachments.put(
        "hello.txt", this.createAttachmentMap(encodedAttachment, "text/plain", false));
    remoteDoc.put("_attachments", remoteAttachments);

    remoteDb.create(remoteDoc);
    documentURI = new URI(remoteDb.getCouchClient().getRootUri().toString() + "/" + "someIdHere");

    Map<String, Map<String, Object>> attachments = new HashMap<String, Map<String, Object>>();
    Map<String, Object> bonsai =
        createAttachmentMap(encodedAttachment, "text/plain", true); // new HashMap<String,Object>();
    bonsai.put("encoding", "gzip");

    attachments.put("hello.txt", bonsai);
    documentRev.put("_attachments", attachments);

    // create the document revision and test

    DocumentRevision revision =
        DocumentRevisionBuilder.buildRevisionFromMap(documentURI, documentRev);

    Assert.assertNotNull(revision);
    Assert.assertEquals(body, revision.getBody().asMap());
    Assert.assertEquals(revision.getId(), "someIdHere");
    Assert.assertEquals(revision.getRevision(), "3-750dac460a6cc41e6999f8943b8e603e");
    Assert.assertEquals(revision.getAttachments().size(), 1);
    InputStream attachmentInputStream = revision.getAttachments().get("hello.txt").getInputStream();
    InputStream expectedInputStream = new ByteArrayInputStream(unencodedAttachment);
    Assert.assertTrue(TestUtils.streamsEqual(expectedInputStream, attachmentInputStream));
  }
  @Test
  public void asMap_differentNumberTypes_jacksonPicksNaturalMapping() throws IOException {
    byte[] d =
        FileUtils.readFileToByteArray(
            TestUtils.loadFixture("fixture/basic_bdbody_test_as_map.json"));
    DocumentBody body = new BasicDocumentBody(d);
    Assert.assertEquals("-101", body.asMap().get("StringValue"));

    Map<String, Object> m = body.asMap();

    Assert.assertTrue(m.get("LongValue") instanceof Long);
    Assert.assertTrue(m.get("LongValue").equals(2147483648l)); // Integer.MAX_VALUE + 1

    Assert.assertTrue(m.get("IntegerValue") instanceof Integer);
    Assert.assertTrue(m.get("IntegerValue").equals(2147483647)); // Integer.MAX_VALUE
  }
 @Before
 public void setUp() throws Exception {
   jsonData = FileUtils.readFileToByteArray(TestUtils.loadFixture(documentOneFile));
 }