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