// 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++;
    }
  }
  @Test
  public void verifyExtractionTempGenNoRequired()
      throws TestEvalException, SAXException, IOException, TransformerException {

    String entityType = "no-primary-required.json";
    DOMHandle res = new DOMHandle();
    logger.info("Validating extraction template for:" + entityType);
    try {
      res = evalOneResult("fn:doc( '" + entityType + "')=>es:extraction-template-generate()", res);
    } catch (TestEvalException e) {
      throw new RuntimeException(e);
    }
    // logger.info(docMgr.read(entityType.replaceAll("\\.(xml|json)", ".tdex"), new
    // StringHandle()).get());
    // DOMHandle handle = docMgr.read(entityType.replaceAll("\\.(xml|json)", ".tdex"), new
    // DOMHandle());
    Document template = res.get();

    InputStream is =
        this.getClass()
            .getResourceAsStream(
                "/test-extraction-template/" + entityType.replace(".json", ".xml"));
    Document filesystemXML = builder.parse(is);

    // debugOutput(template);

    XMLUnit.setIgnoreWhitespace(true);
    XMLAssert.assertXMLEqual(
        "Must be no validation errors for schema " + entityType + ".", filesystemXML, template);
  }
  @Test
  public void verifyExtractionTemplateGenerate()
      throws TestEvalException, SAXException, IOException, TransformerException {

    for (String entityType : entityTypes) {
      if (entityType.contains(".json")
          || entityType.contains(".jpg")
          || entityType.contains("primary-key-as-a-ref")) {
        continue;
      }

      logger.info("Validating extraction template for:" + entityType);
      // logger.info(docMgr.read(entityType.replaceAll("\\.(xml|json)", ".tdex"), new
      // StringHandle()).get());
      DOMHandle handle =
          docMgr.read(entityType.replaceAll("\\.(xml|json)", ".tdex"), new DOMHandle());
      Document template = handle.get();

      InputStream is =
          this.getClass().getResourceAsStream("/test-extraction-template/" + entityType);
      Document filesystemXML = builder.parse(is);

      // debugOutput(template);

      XMLUnit.setIgnoreWhitespace(true);
      XMLAssert.assertXMLEqual(
          "Must be no validation errors for schema " + entityType + ".", filesystemXML, template);
    }
  }
    public Document[] checkDictionaries(String... uris) {
      RequestParameters params = new RequestParameters();
      params.add("service", "check-dictionary");
      params.add("uris", uris);

      // specify the mime type for each expected document returned
      String[] mimetypes = new String[uris.length];
      for (int i = 0; i < uris.length; i++) {
        mimetypes[i] = "application/xml";
      }

      // call the service
      ServiceResultIterator resultItr = getServices().get(params, mimetypes);

      // iterate over the results
      List<Document> documents = new ArrayList<Document>();
      DOMHandle readHandle = new DOMHandle();
      while (resultItr.hasNext()) {
        ServiceResult result = resultItr.next();

        // get the result content
        result.getContent(readHandle);
        documents.add(readHandle.get());
      }

      // release the iterator resources
      resultItr.close();

      return documents.toArray(new Document[documents.size()]);
    }
  // 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 testWithVariousGrammarAndWordQuery()
      throws IOException, ParserConfigurationException, SAXException, XpathException {
    System.out.println("Running testWithVariousGrammarAndWordQuery");

    String[] filenames = {
      "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"
    };
    String queryOptionName = "absRangeConstraintWithVariousGrammarAndWordQueryOpt.xml";

    DatabaseClient client =
        DatabaseClientFactory.newClient(
            "localhost", 8011, "rest-admin", "x", Authentication.DIGEST);

    // write docs
    for (String filename : filenames) {
      writeDocumentUsingInputStreamHandle(client, filename, "/abs-range-constraint/", "XML");
    }

    setQueryOption(client, queryOptionName);

    QueryManager queryMgr = client.newQueryManager();

    // create query def
    StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName);
    querydef.setCriteria("(pop:high OR pop:medium) AND price:medium AND intitle:served");

    // create handle
    DOMHandle resultsHandle = new DOMHandle();
    queryMgr.search(querydef, resultsHandle);

    // get the result
    Document resultDoc = resultsHandle.get();

    assertXpathEvaluatesTo(
        "1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc);
    assertXpathEvaluatesTo(
        "Vannevar served",
        "string(//*[local-name()='result'][1]//*[local-name()='title'])",
        resultDoc);
    assertXpathEvaluatesTo(
        "12.34", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc);
    assertXpathEvaluatesTo(
        "5", "string(//*[local-name()='result'][1]//*[local-name()='popularity'])", resultDoc);
    assertXpathEvaluatesTo(
        "1", "string(//*[local-name()='facet-value']//@*[local-name()='count'])", resultDoc);
    assertXpathEvaluatesTo("High", "string(//*[local-name()='facet-value'])", resultDoc);

    // String expectedSearchReport = "(cts:search(fn:collection(),
    // cts:and-query((cts:or-query((cts:element-range-query(fn:QName(\"\", \"popularity\"),
    // \"&gt;=\", xs:int(\"5\"), (), 1), cts:and-query((cts:element-range-query(fn:QName(\"\",
    // \"popularity\"), \"&gt;=\", xs:int(\"3\"), (), 1), cts:element-range-query(fn:QName(\"\",
    // \"popularity\"), \"&lt;\", xs:int(\"5\"), (), 1)), ()))),
    // cts:element-attribute-range-query(fn:QName(\"http://cloudbank.com\", \"price\"),
    // fn:QName(\"\", \"amt\"), \"&gt;=\", 3.0, (), 1),
    // cts:element-attribute-range-query(fn:QName(\"http://cloudbank.com\", \"price\"),
    // fn:QName(\"\", \"amt\"), \"&lt;\", 14.0, (), 1), cts:element-word-query(fn:QName(\"\",
    // \"title\"), \"served\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]";

    // assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])",
    // resultDoc);

    // release client
    client.release();
  }