// 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());
  }
  public void validateRecord(DocumentRecord record, Format type) {

    assertNotNull("DocumentRecord should never be null", record);
    assertNotNull("Document uri should never be null", record.getUri());
    assertTrue(
        "Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY));
    assertEquals("All records are expected to be in same format", type, record.getFormat());
    //        System.out.println(record.getMimetype());

  }
  // This test is to verify RAW XML structured Query
  @Test
  public void testBulkSearchRawXMLStrucQD() throws Exception {
    //		setAutomaticDirectoryCreation(dbName,"automatic");
    setMaintainLastModified(dbName, true);
    this.loadJSONDocuments();
    this.loadXMLDocuments();
    GenericDocumentManager docMgr = client.newDocumentManager();
    QueryManager queryMgr = client.newQueryManager();
    String rawXMLQuery =
        "<search:query "
            + "xmlns:search='http://marklogic.com/appservices/search'>"
            + " <search:or-query><search:term-query>"
            + "<search:text>bar</search:text>"
            + "</search:term-query>"
            + "<search:term-query>"
            + "<search:text>woof</search:text>"
            + "</search:term-query> </search:or-query>"
            + "</search:query>";
    StringHandle rh = new StringHandle(rawXMLQuery);
    RawStructuredQueryDefinition qd = queryMgr.newRawStructuredQueryDefinition(rh);
    DOMHandle dh = new DOMHandle();
    DocumentPage page = docMgr.search(qd, 1, dh);
    DocumentMetadataHandle mh = new DocumentMetadataHandle();

    int count = 1;
    while (count < 4) {
      page = docMgr.search(qd, count, dh);

      while (page.hasNext()) {
        DocumentRecord rec = page.next();
        if (rec.getMimetype().contains("xml")) {
          validateRecord(rec, Format.XML);
        } else {
          validateRecord(rec, Format.JSON);
        }
        docMgr.readMetadata(rec.getUri(), mh);
        assertTrue("Records has permissions? ", mh.getPermissions().containsKey("flexrep-eval"));
        assertFalse("Record has collections ?", mh.getCollections().isEmpty());
      }
      System.out.println(this.convertXMLDocumentToString(dh.get()));
      assertEquals(
          "Total search results before transaction rollback are ",
          "204",
          dh.get()
              .getElementsByTagNameNS("*", "response")
              .item(0)
              .getAttributes()
              .getNamedItem("total")
              .getNodeValue());
      count++;
    }
  }
  // This test is to verify RAW JSON structured query
  @Test
  public void testBulkSearchRawJSONStrucQD() throws Exception {
    //		setAutomaticDirectoryCreation(dbName,"automatic");
    setMaintainLastModified(dbName, true);
    this.loadJSONDocuments();
    this.loadXMLDocuments();
    GenericDocumentManager docMgr = client.newDocumentManager();
    QueryManager queryMgr = client.newQueryManager();
    JacksonHandle jh = new JacksonHandle();
    ObjectMapper mapper = new ObjectMapper();
    //	constructing JSON representation of Raw JSON Structured Query

    ObjectNode txtNode = mapper.createObjectNode();
    txtNode.putArray("text").add("woof");
    ObjectNode termQNode = mapper.createObjectNode();
    termQNode.set("term-query", txtNode);
    ObjectNode queriesArrayNode = mapper.createObjectNode();
    queriesArrayNode.putArray("queries").add(termQNode);

    ObjectNode txtNode2 = mapper.createObjectNode();
    txtNode2.putArray("text").add("bar");
    ObjectNode termQNode2 = mapper.createObjectNode();
    termQNode2.set("term-query", txtNode2);
    queriesArrayNode.withArray("queries").add(termQNode2);

    ObjectNode orQueryNode = mapper.createObjectNode();
    orQueryNode.set("or-query", queriesArrayNode);

    ObjectNode queryArrayNode = mapper.createObjectNode();
    queryArrayNode.putArray("queries").add(orQueryNode);
    ObjectNode mainNode = mapper.createObjectNode();
    mainNode.set("query", queryArrayNode);
    jh.set(mainNode);
    RawStructuredQueryDefinition qd = queryMgr.newRawStructuredQueryDefinition(jh);
    System.out.println(jh.get().toString());
    docMgr.setNonDocumentFormat(Format.JSON);
    JacksonHandle results = new JacksonHandle();
    DocumentPage page = docMgr.search(qd, 1, results);
    DocumentMetadataHandle mh = new DocumentMetadataHandle();

    int count = 1;
    while (count < 4) {
      page = docMgr.search(qd, count, results);

      while (page.hasNext()) {
        DocumentRecord rec = page.next();
        if (rec.getMimetype().contains("xml")) {
          validateRecord(rec, Format.XML);
        } else {
          validateRecord(rec, Format.JSON);
        }
        docMgr.readMetadata(rec.getUri(), mh);
        assertTrue("Records has permissions? ", mh.getPermissions().containsKey("flexrep-eval"));
        assertFalse("Record has collections ?", mh.getCollections().isEmpty());
      }

      count++;
    }
    System.out.println(results.get().toString());
    assertEquals(
        "Total search results before transaction rollback are ",
        "204",
        results.get().get("total").asText());
  }
  @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());
  }