@Test
  public void testPOJOSearchWithoutSearchHandle() {
    PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class);
    PojoPage<Artifact> p;
    this.loadSimplePojos(products);
    QueryManager queryMgr = client.newQueryManager();
    StringQueryDefinition qd = queryMgr.newStringDefinition();
    qd.setCriteria("Widgets");

    products.setPageLength(11);
    p = products.search(qd, 1);
    assertEquals("total no of pages", 5, p.getTotalPages());
    System.out.println(p.getTotalPages());
    long pageNo = 1, count = 0;
    do {
      count = 0;

      p = products.search(qd, pageNo);

      while (p.iterator().hasNext()) {
        Artifact a = p.iterator().next();
        validateArtifact(a);
        assertTrue("Artifact Id is odd", a.getId() % 2 != 0);
        assertTrue(
            "Company name contains widgets", a.getManufacturer().getName().contains("Widgets"));
        count++;
        //				System.out.println(a.getId()+" "+a.getManufacturer().getName() +"  "+count);
      }
      assertEquals("Page size", count, p.size());
      pageNo = pageNo + p.getPageSize();
    } while (!p.isLastPage() && pageNo < p.getTotalSize());
    assertEquals("page number after the loop", 5, p.getPageNumber());
    assertEquals("total no of pages", 5, p.getTotalPages());
  }
  @Test
  public void testPOJOSearchWithJacksonHandle() {
    PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class);
    PojoPage<Artifact> p;
    this.loadSimplePojos(products);
    QueryManager queryMgr = client.newQueryManager();
    StringQueryDefinition qd = queryMgr.newStringDefinition();
    qd.setCriteria("cogs");
    JacksonHandle results = new JacksonHandle();
    p = products.search(qd, 1, results);
    products.setPageLength(11);
    assertEquals("total no of pages", 3, p.getTotalPages());
    //		System.out.println(p.getTotalPages()+results.get().toString());
    long pageNo = 1, count = 0;
    do {
      count = 0;
      p = products.search(qd, pageNo, results);

      while (p.iterator().hasNext()) {
        Artifact a = p.iterator().next();
        validateArtifact(a);
        count++;
        //				System.out.println(a.getId()+" "+a.getManufacturer().getName() +"  "+count);
      }
      assertEquals("Page size", count, p.size());
      pageNo = pageNo + p.getPageSize();

      assertEquals(
          "Page start from search handls vs page methods",
          results.get().get("start").asLong(),
          p.getStart());
      assertEquals(
          "Format in the search handle",
          "json",
          results.get().withArray("results").get(1).path("format").asText());
      assertTrue(
          "Uri in search handle contains Artifact",
          results.get().withArray("results").get(1).path("uri").asText().contains("Artifact"));
      //			System.out.println(results.get().toString());
    } while (!p.isLastPage() && pageNo < p.getTotalSize());
    //		assertTrue("search handle has metrics",results.get().has("metrics"));
    assertEquals("Search text is", "cogs", results.get().path("qtext").asText());
    assertEquals("Total from search handle", 110, results.get().get("total").asInt());
    assertEquals("page number after the loop", 10, p.getPageNumber());
    assertEquals("total no of pages", 10, p.getTotalPages());
  }
  @Test
  public void testPOJOSearchWithSearchHandle() {
    PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class);
    PojoPage<Artifact> p;
    this.loadSimplePojos(products);
    QueryManager queryMgr = client.newQueryManager();
    StringQueryDefinition qd = queryMgr.newStringDefinition();
    qd.setCriteria("Acme");
    SearchHandle results = new SearchHandle();
    products.setPageLength(11);
    p = products.search(qd, 1, results);
    assertEquals("total no of pages", 5, p.getTotalPages());
    System.out.println(p.getTotalPages());
    long pageNo = 1, count = 0;
    do {
      count = 0;
      p = products.search(qd, pageNo, results);

      while (p.iterator().hasNext()) {
        Artifact a = p.iterator().next();
        validateArtifact(a);
        assertTrue("Artifact Id is even", a.getId() % 2 == 0);
        assertTrue("Company name contains Acme", a.getManufacturer().getName().contains("Acme"));
        count++;
        //				System.out.println(a.getId()+" "+a.getManufacturer().getName() +"  "+count);
      }
      assertEquals("Page size", count, p.size());
      pageNo = pageNo + p.getPageSize();
      MatchDocumentSummary[] mds = results.getMatchResults();
      assertEquals("Size of the results summary", 11, mds.length);
      for (MatchDocumentSummary md : mds) {
        assertTrue("every uri should contain the class name", md.getUri().contains("Artifact"));
      }
      String[] facetNames = results.getFacetNames();
      for (String fname : facetNames) {
        System.out.println(fname);
      }
      assertEquals("Total resulr from search handle ", 55, results.getTotalResults());
      //			assertTrue("Search Handle metric results ",results.getMetrics().getTotalTime()>0);
    } while (!p.isLastPage() && pageNo < p.getTotalSize());
    assertEquals("Page start check", 45, p.getStart());
    assertEquals("page number after the loop", 5, p.getPageNumber());
    assertEquals("total no of pages", 5, p.getTotalPages());
  }
  // Searching for Id as Number in JSON using string should not return any results
  @Test
  public void testPOJOSearchWithStringHandle() throws JsonProcessingException, IOException {
    PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class);
    PojoPage<Artifact> p;
    this.loadSimplePojos(products);
    QueryManager queryMgr = client.newQueryManager();
    StringQueryDefinition qd = queryMgr.newStringDefinition();
    qd.setCriteria("5");
    StringHandle results = new StringHandle();
    JacksonHandle jh = new JacksonHandle();
    p = products.search(qd, 1, jh);

    long pageNo = 1, count = 0;
    do {
      count = 0;
      p = products.search(qd, pageNo, results.withFormat(Format.JSON));

      while (p.iterator().hasNext()) {
        Artifact a = p.iterator().next();
        validateArtifact(a);
        count++;
        //				System.out.println(a.getId()+" "+a.getManufacturer().getName() +"  "+count);
      }
      assertEquals("Page total results", 0, p.getTotalSize());
      pageNo = pageNo + p.getPageSize();
      //				System.out.println(results.get().toString());
    } while (!p.isLastPage() && pageNo < p.getTotalSize());
    assertFalse("String handle is not empty", results.get().isEmpty());
    assertTrue("String handle contains results", results.get().contains("results"));
    assertFalse("String handle contains format", results.get().contains("\"format\":\"json\""));

    ObjectMapper mapper = new ObjectMapper();
    JsonNode actNode = mapper.readTree(results.get()).get("total");
    //		System.out.println(expNode.equals(actNode)+"\n"+
    // expNode.toString()+"\n"+actNode.toString());

    assertEquals("Total search results resulted are ", actNode.asInt(), 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();
  }