// 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++;
    }
  }
 @Override
 public void write(T entity, Transaction transaction, String... collections) {
   if (entity == null) return;
   JacksonDatabindHandle<T> contentHandle = new JacksonDatabindHandle<T>(entity);
   contentHandle.setMapper(objectMapper);
   DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
   metadataHandle = metadataHandle.withCollections(entityClass.getName());
   if (collections != null && collections.length > 0) {
     metadataHandle = metadataHandle.withCollections(collections);
   }
   DocumentWriteSet writeSet = docMgr.newWriteSet();
   writeSet.add(getDocumentUri(entity), metadataHandle, contentHandle);
   try {
     docMgr.write(writeSet, transaction);
   } catch (ClientHandlerException e) {
     checkForEmptyBeans(e);
     throw e;
   }
 }
  /* Searching for boolean and string in XML element using value query.
   * Purpose: To validate QueryBuilder's new value methods (in 8.0) in XML document using an element.
   *
   * Load a file that has a boolean value in a XML attribute and use query def to search on that boolean value
   *
   * Methods used : value(StructuredQueryBuilder.TextIndex index, boolean)
   *                value(StructuredQueryBuilder.TextIndex index, String)
   */
  @Test
  public void testQueryBuilderValueWithBooleanAndString()
      throws XpathException, SAXException, IOException {

    String docId[] = {"play-persons.xml"};

    TextDocumentManager docMgr = client.newTextDocumentManager();
    QueryManager queryMgr = client.newQueryManager();
    DocumentWriteSet writeset = docMgr.newWriteSet();

    // Put meta-data

    DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
    metadataHandle.getCollections().addAll("my-collection1", "my-collection2");
    metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ);
    metadataHandle.getProperties().put("reviewed", true);
    metadataHandle.getProperties().put("myString", "foo");
    metadataHandle.getProperties().put("myInteger", 10);
    metadataHandle.getProperties().put("myDecimal", 34.56678);
    metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR));
    metadataHandle.setQuality(23);

    writeset.addDefault(metadataHandle);

    // Create a new document using StringHandle
    StringBuffer strBuf = new StringBuffer();

    strBuf.append("<PLAY>");
    strBuf.append("<TITLE>All's Well That Ends Well</TITLE>");
    strBuf.append("<PERSONAE>");
    strBuf.append("<TITLE>Dramatis Personae</TITLE>");

    strBuf.append("<PGROUP>");
    strBuf.append("<subgroup>true</subgroup>");

    strBuf.append("<PERSONA>KING OF FRANCE</PERSONA>");
    strBuf.append("<PERSONA>DUKE OF FLORENCE</PERSONA>");
    strBuf.append("<PERSONA>BERTRAM, Count of Rousillon.</PERSONA>");
    strBuf.append("<PERSONA>LAFEU, an old lord.</PERSONA>");
    strBuf.append("</PGROUP>");

    strBuf.append("<PGROUP>");
    strBuf.append("<subgroup>false</subgroup>");

    strBuf.append("<PERSONA>PAROLLES, a follower of Bertram.</PERSONA>");
    strBuf.append("<PERSONA>A Page. </PERSONA>");
    strBuf.append("</PGROUP>");

    strBuf.append("<PGROUP>");
    strBuf.append("<subgroup>false</subgroup>");
    strBuf.append("<PERSONA>COUNTESS OF ROUSILLON, mother to Bertram. </PERSONA>");
    strBuf.append("<PERSONA>HELENA, a gentlewoman protected by the Countess.</PERSONA>");
    strBuf.append("<PERSONA>An old Widow of Florence. </PERSONA>");
    strBuf.append("<PERSONA>DIANA, daughter to the Widow.</PERSONA>");
    strBuf.append("</PGROUP>");

    strBuf.append("<PGROUP>");
    strBuf.append("<subgroup>false</subgroup>");
    strBuf.append("<PERSONA>VIOLENTA</PERSONA>");
    strBuf.append("<PERSONA>MARIANA</PERSONA>");
    strBuf.append("<GRPDESCR>neighbours and friends to the Widow.</GRPDESCR>");
    strBuf.append("</PGROUP>");

    strBuf.append("<PERSONA>Lords, Officers, Soldiers, &amp;c., French and Florentine.</PERSONA>");
    strBuf.append("</PERSONAE>");
    strBuf.append("</PLAY>");

    writeset.add("/1/" + docId[0], new StringHandle().with(strBuf.toString()));
    docMgr.write(writeset);

    docMgr.write(writeset);

    // Search for the range with attribute value true in rangeRelativeBucketConstraintOpt.xml
    // document.
    StructuredQueryBuilder qb = new StructuredQueryBuilder();

    // Build an object that represents StructuredQueryBuilder.ElementAttribute for use in values
    // method
    // that is of type StructuredQueryBuilder.TextIndex

    QueryDefinition qd = qb.value(qb.element("subgroup"), false);

    // Create handle for the result
    StringHandle resultsHandle = new StringHandle().withFormat(Format.XML);
    queryMgr.search(qd, resultsHandle);

    // Get the result
    String resultDoc = resultsHandle.get();

    System.out.println(resultDoc);
    // Verify that search response has found 1 element attribute
    assertXpathEvaluatesTo(
        "fn:doc(\"/1/play-persons.xml\")",
        "string(//*[local-name()='response']//*[local-name()='result']//@*[local-name()='path'])",
        resultDoc);
    assertXpathEvaluatesTo(
        "3", "count(//*[local-name()='response']//*[local-name()='match'])", resultDoc);

    // Search for the following royal (XML ELEMENT) in all-well.xml document.
    StructuredQueryBuilder qbStr = new StructuredQueryBuilder();
    QueryDefinition qdStr =
        qbStr.value(
            qbStr.element("PERSONA"),
            "KING OF FRANCE",
            "DUKE OF FLORENCE",
            "BERTRAM, Count of Rousillon.",
            "LAFEU, an old lord.");

    // Create handle for the result
    StringHandle resultsHandleStr = new StringHandle().withFormat(Format.XML);
    queryMgr.search(qdStr, resultsHandleStr);

    // Get the result
    String resultDocStr = resultsHandleStr.get();

    System.out.println(resultDocStr);
    // Verify that search response has found 4 PERSONA elements under /PLAY/PERSONAE
    assertXpathEvaluatesTo(
        "fn:doc(\"/1/play-persons.xml\")",
        "string(//*[local-name()='response']//*[local-name()='result']//@*[local-name()='path'])",
        resultDocStr);
    assertXpathEvaluatesTo(
        "4", "count(//*[local-name()='response']//*[local-name()='match'])", resultDocStr);
  }
  // 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());
  }