// This test has set response to JSON and pass StringHandle with format as JSON, expectint it to
  // work, logged an issue 82
  @Test
  public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception {
    loadJSONDocuments();
    JSONDocumentManager docMgr = client.newJSONDocumentManager();

    QueryManager queryMgr = client.newQueryManager();
    StructuredQueryBuilder qb = new StructuredQueryBuilder();
    StructuredQueryDefinition qd = qb.and(qb.term("dog1", "dog11"));
    queryMgr.search(qd, new SearchHandle());

    docMgr.setNonDocumentFormat(Format.JSON);
    docMgr.setSearchView(QueryView.METADATA);
    docMgr.setMetadataCategories(Metadata.PERMISSIONS);

    StringHandle results = new StringHandle().withFormat(Format.JSON);
    DocumentPage page = docMgr.search(qd, 1, results);
    DocumentMetadataHandle mh = new DocumentMetadataHandle();
    while (page.hasNext()) {
      DocumentRecord rec = page.next();
      validateRecord(rec, Format.JSON);
      docMgr.readMetadata(rec.getUri(), mh);
      assertTrue("Records has permissions? ", mh.getPermissions().containsKey("flexrep-eval"));
      assertTrue("Record has collections ?", mh.getCollections().isEmpty());
    }
    assertFalse("Search handle contains", results.get().isEmpty());
  }
  // This test is testing SearchView options and search handle
  @Test
  public void testBulkSearchSQDwithJSONResponseFormat() throws Exception {

    loadJSONDocuments();
    JSONDocumentManager docMgr = client.newJSONDocumentManager();

    QueryManager queryMgr = client.newQueryManager();
    StructuredQueryBuilder qb = new StructuredQueryBuilder();
    StructuredQueryDefinition qd = qb.and(qb.term("woof"));
    docMgr.setNonDocumentFormat(Format.JSON);

    docMgr.setSearchView(QueryView.FACETS);
    JacksonHandle jh = new JacksonHandle();
    docMgr.search(qd, 1, jh);

    //		System.out.println(jh.get().toString());
    assertTrue("Searh response has entry for facets", jh.get().has("facets"));
    assertFalse(
        "Searh response has entry for facets",
        jh.get().has("results")); // Issue 84 is tracking this
    assertFalse("Searh response has entry for facets", jh.get().has("metrics"));

    docMgr.setSearchView(QueryView.RESULTS);
    docMgr.search(qd, 1, jh);

    assertFalse("Searh response has entry for facets", jh.get().has("facets"));
    assertTrue("Searh response has entry for facets", jh.get().has("results"));
    assertFalse(
        "Searh response has entry for facets",
        jh.get().has("metrics")); // Issue 84 is tracking this

    docMgr.setSearchView(QueryView.METADATA);
    docMgr.search(qd, 1, jh);

    assertFalse("Searh response has entry for facets", jh.get().has("facets"));
    assertFalse("Searh response has entry for facets", jh.get().has("results"));
    assertTrue("Searh response has entry for facets", jh.get().has("metrics"));

    docMgr.setSearchView(QueryView.ALL);
    docMgr.search(qd, 1, jh);

    assertTrue("Searh response has entry for facets", jh.get().has("facets"));
    assertTrue("Searh response has entry for facets", jh.get().has("results"));
    assertTrue("Searh response has entry for facets", jh.get().has("metrics"));

    queryMgr.setView(QueryView.FACETS);
    queryMgr.search(qd, jh);
    System.out.println(jh.get().toString());
  }
  // This test is to verify the transactions, verifies the search works with transaction before
  // commit, after rollback and after commit
  @Test
  public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception {
    XMLDocumentManager docMgr = client.newXMLDocumentManager();
    DOMHandle results = new DOMHandle();
    StructuredQueryBuilder qb = new StructuredQueryBuilder();
    StructuredQueryDefinition qd = qb.and(qb.term("much", "thought"));
    Transaction t = client.openTransaction();
    try {
      int count = 1;
      XMLDocumentManager xmldocMgr = client.newXMLDocumentManager();
      DocumentWriteSet writeset = xmldocMgr.newWriteSet();
      for (int i = 0; i < 102; i++) {
        writeset.add(
            DIRECTORY + "boo" + i + ".xml",
            new DOMHandle(getDocumentContent("This is so too much thought " + i)));
        if (count % BATCH_SIZE == 0) {
          xmldocMgr.write(writeset, t);
          writeset = xmldocMgr.newWriteSet();
        }
        count++;
      }
      if (count % BATCH_SIZE > 0) {
        xmldocMgr.write(writeset, t);
      }
      count = 0;
      docMgr.setSearchView(QueryView.RESULTS);

      DocumentPage page = docMgr.search(qd, 1, results, t);
      while (page.hasNext()) {
        DocumentRecord rec = page.next();

        validateRecord(rec, Format.XML);
        count++;
      }
      assertTrue("Page has conttent :", page.hasContent());
      assertEquals(
          "Total search results before transaction rollback are ",
          "102",
          results
              .get()
              .getElementsByTagNameNS("*", "response")
              .item(0)
              .getAttributes()
              .getNamedItem("total")
              .getNodeValue());
      //			System.out.println(results.get().getElementsByTagNameNS("*",
      // "response").item(0).getAttributes().getNamedItem("total").getNodeValue());

    } catch (Exception e) {
      throw e;
    } finally {
      t.rollback();
    }

    docMgr.search(qd, 1, results);
    System.out.println(convertXMLDocumentToString(results.get()));
    assertEquals(
        "Total search results after rollback are ",
        results
            .get()
            .getElementsByTagNameNS("*", "response")
            .item(0)
            .getAttributes()
            .getNamedItem("total")
            .getNodeValue(),
        "0");
  }
  @Test
  public void testBulkSearchSQDwithDifferentPageSizes()
      throws IOException, ParserConfigurationException, SAXException, TransformerException {
    int count;
    loadXMLDocuments();
    // Creating a txt document manager for bulk search
    TextDocumentManager docMgr = client.newTextDocumentManager();
    // using QueryManger for query definition and set the search criteria
    StructuredQueryBuilder qb = new StructuredQueryBuilder();
    StructuredQueryDefinition qd = qb.and(qb.term("foo", "bar"));

    // set  document manager level settings for search response
    System.out.println("Default Page length setting on docMgr :" + docMgr.getPageLength());
    docMgr.setPageLength(1);
    docMgr.setSearchView(QueryView.RESULTS);
    docMgr.setNonDocumentFormat(Format.XML);
    assertEquals("format set on document manager", "XML", docMgr.getNonDocumentFormat().toString());
    assertEquals(
        "Queryview set on document manager ", "RESULTS", docMgr.getSearchView().toString());
    assertEquals("Page length ", 1, docMgr.getPageLength());
    // Search for documents where content has bar and get first result record, get search handle on
    // it
    SearchHandle sh = new SearchHandle();
    DocumentPage page = docMgr.search(qd, 0);
    // test for page methods
    assertEquals("Number of records", 1, page.size());
    assertEquals("Starting record in first page ", 1, page.getStart());
    assertEquals("Total number of estimated results:", 102, page.getTotalSize());
    assertEquals("Total number of estimated pages :", 102, page.getTotalPages());
    // till the issue #78 get fixed
    assertTrue("Is this First page :", page.isFirstPage()); // this is bug
    assertFalse("Is this Last page :", page.isLastPage());
    assertTrue("Is this First page has content:", page.hasContent());
    //		Need the Issue #75 to be fixed
    assertFalse("Is first page has previous page ?", page.hasPreviousPage());
    //
    long pageNo = 1;
    do {
      count = 0;
      page = docMgr.search(qd, pageNo, sh);
      if (pageNo > 1) {
        assertFalse("Is this first Page", page.isFirstPage());
        assertTrue("Is page has previous page ?", page.hasPreviousPage());
      }
      while (page.hasNext()) {
        DocumentRecord rec = page.next();
        rec.getFormat();
        validateRecord(rec, Format.XML);
        //		System.out.println(rec.getUri());
        count++;
      }
      MatchDocumentSummary[] mds = sh.getMatchResults();
      assertEquals("Matched document count", 1, mds.length);
      // since we set the query view to get only results, facet count supposed be 0
      assertEquals("Matched Facet count", 0, sh.getFacetNames().length);

      assertEquals("document count", page.size(), count);
      //			assertEquals("Page Number #",pageNo,page.getPageNumber());
      pageNo = pageNo + page.getPageSize();
    } while (!page.isLastPage());
    //		assertTrue("page count is 101 ",pageNo > page.getTotalPages());
    assertTrue("Page has previous page ?", page.hasPreviousPage());
    assertEquals("page size", 1, page.getPageSize());
    assertEquals("document count", 102, page.getTotalSize());
    page = docMgr.search(qd, 103);
    assertFalse("Page has any records ?", page.hasContent());
  }