@Test
 public void read_document_as_stream() throws Exception {
   // content type is really json but it doesn't matter here.
   ResponseOnFileStub rsp = ResponseOnFileStub.newInstance(200, "attachment.txt");
   when(httpClient.get("/test_db/some_doc_id")).thenReturn(rsp);
   InputStream data = dbCon.getAsStream("some_doc_id");
   assertNotNull(data);
   assertFalse(rsp.isConnectionReleased());
   assertEquals("detta är ett påhäng med ett ö i", IOUtils.toString(data, "UTF-8"));
 }
 @Test
 public void return_attachment_with_open_data_stream() throws Exception {
   ResponseOnFileStub rsp = ResponseOnFileStub.newInstance(200, "attachment.txt", "text", 12);
   when(httpClient.get("/test_db/some_doc_id/some_attachment")).thenReturn(rsp);
   AttachmentInputStream a = dbCon.getAttachment("some_doc_id", "some_attachment");
   assertNotNull(a);
   assertFalse(rsp.isConnectionReleased());
   assertEquals("detta är ett påhäng med ett ö i", IOUtils.toString(a, "UTF-8"));
   assertEquals(rsp.getContentType(), a.getContentType());
   assertEquals(rsp.getContentLength(), a.getContentLength());
 }
 @Test
 public void testGetDesignDocInfo() {
   when(httpClient.get("/test_db/_design/exampleDesignDoc/_info"))
       .thenReturn(ResponseOnFileStub.newInstance(200, "../design_doc_info.json"));
   DesignDocInfo info = dbCon.getDesignDocInfo("exampleDesignDoc");
   assertNotNull(info);
 }
 @Test(expected = UpdateConflictException.class)
 public void throw_exception_when_in_conflict() {
   td.setId("some_id");
   td.setRevision("123D123");
   when(httpClient.put(anyString(), anyString()))
       .thenReturn(ResponseOnFileStub.newInstance(409, "update_conflict.json"));
   dbCon.update(td);
 }
 @Test
 public void return_all_revisions() {
   when(httpClient.get("/test_db/some_doc_id?revs_info=true"))
       .thenReturn(ResponseOnFileStub.newInstance(200, "revisions.json"));
   List<Revision> l = dbCon.getRevisions("some_doc_id");
   assertNotNull(l);
   assertEquals(8, l.size());
   assertEquals(new Revision("8-8395fd3a7a2dd04022cc1330a4d20e66", "available"), l.get(0));
 }
 @Test(expected = UpdateConflictException.class)
 public void throw_exception_when_create_attachment_in_conflict() {
   td.setId("some_id");
   td.setRevision("123D123");
   when(httpClient.put(anyString(), any(InputStream.class), anyString(), anyInt()))
       .thenReturn(ResponseOnFileStub.newInstance(409, "update_conflict.json"));
   dbCon.createAttachment(
       "id",
       "rev",
       new AttachmentInputStream("attach_id", IOUtils.toInputStream("data"), "whatever"));
 }
  @Test
  public void should_stream_attachmed_content() {
    when(httpClient.put(anyString(), any(InputStream.class), anyString(), anyInt()))
        .thenReturn(ResponseOnFileStub.newInstance(200, "create_attachment_rsp.json"));

    dbCon.createAttachment(
        "docid",
        new AttachmentInputStream(
            "attachment_id", IOUtils.toInputStream("content"), "text/html", 12l));

    verify(httpClient)
        .put(eq("/test_db/docid/attachment_id"), any(InputStream.class), eq("text/html"), eq(12l));
  }
 @Test
 public void testGetDbInfo() {
   when(httpClient.get(TEST_DB_PATH))
       .thenReturn(ResponseOnFileStub.newInstance(200, "db_info.json"));
   DbInfo info = dbCon.getDbInfo();
   assertNotNull(info);
   assertEquals("dj", info.getDbName());
   assertFalse(info.isCompactRunning());
   assertEquals(12377, info.getDiskSize());
   assertEquals(1267612389906234l, info.getInstanceStartTime());
   assertEquals(1, info.getDocCount());
   assertEquals(1, info.getDocDelCount());
   assertEquals(5, info.getDiskFormatVersion());
   assertEquals(1, info.getPurgeSeq());
   assertEquals(4, info.getUpdateSeq());
 }
  @Test
  public void multiple_query_keys_should_be_posted_3() throws IOException {
    List<Object> keys = new ArrayList<Object>();
    keys.add("key1");
    keys.add("key2");
    keys.add("key3");

    ViewQuery query =
        new ViewQuery()
            .dbPath(TEST_DB_PATH)
            .designDocId("_design/testdoc")
            .viewName("test_view")
            .keys(keys);

    when(httpClient.postUncached(anyString(), anyString()))
        .thenReturn(ResponseOnFileStub.newInstance(200, "view_result.json"));
    dbCon.queryForStream(query).close();
    verify(httpClient).postUncached(query.buildQuery(), query.getKeysAsJson());
  }
  @Test
  public void documents_embedded_in_view_result_should_be_read_directly() {
    ViewQuery query =
        new ViewQuery()
            .dbPath(TEST_DB_PATH)
            .designDocId("_design/testdoc")
            .viewName("test_view")
            .key("key_value");

    when(httpClient.getUncached(query.buildQuery()))
        .thenReturn(ResponseOnFileStub.newInstance(200, "view_result_with_embedded_docs.json"));

    List<TestDoc> result = dbCon.queryView(query, TestDoc.class);

    assertEquals(2, result.size());
    assertEquals(TestDoc.class, result.get(0).getClass());
    assertEquals("doc_id1", result.get(0).getId());
    assertEquals("doc_id2", result.get(1).getId());
    verify(httpClient, times(1)).getUncached(anyString());
  }
  @Test
  public void load_query_result() {
    setupGetDocResponse("doc_id1", "doc_id2");

    ViewQuery query =
        new ViewQuery()
            .dbPath(TEST_DB_PATH)
            .designDocId("_design/testdoc")
            .viewName("test_view")
            .key("key_value");

    when(httpClient.getUncached(query.buildQuery()))
        .thenReturn(ResponseOnFileStub.newInstance(200, "view_result.json"));

    ViewResult result = dbCon.queryView(query);

    assertEquals(2, result.getSize());
    assertEquals("doc_id1", result.getRows().get(0).getId());
    assertEquals("doc_id2", result.getRows().get(1).getId());
  }
  @Test
  public void queries_with_ignore_not_found() {
    ViewQuery query =
        new ViewQuery()
            .dbPath(TEST_DB_PATH)
            .designDocId("_design/testdoc")
            .viewName("test_view")
            .includeDocs(true)
            .keys(Arrays.asList("doc_id1", "doc_id2", "doc_id3", "doc_id4"));
    query.setIgnoreNotFound(true);

    when(httpClient.postUncached(anyString(), anyString()))
        .thenReturn(ResponseOnFileStub.newInstance(200, "view_result_with_ignored_docs.json"));

    List<TestDoc> result = dbCon.queryView(query, TestDoc.class);

    assertEquals(2, result.size());
    assertEquals(TestDoc.class, result.get(0).getClass());
    assertEquals("doc_id1", result.get(0).getId());
    assertEquals("doc_id3", result.get(1).getId());
    verify(httpClient).postUncached(query.buildQuery(), query.getKeysAsJson());
  }