// Searching for Id as Number in JSON using range query @Test(expected = ClassCastException.class) public void testPOJOcombinedSearchforNumberWithStringHandle() throws JsonProcessingException, IOException { PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class); PojoPage<Artifact> p; this.loadSimplePojos(products); QueryManager queryMgr = client.newQueryManager(); String queryAsString = "{\"search\":{\"query\":{" + "\"range-constraint-query\":{\"constraint-name\":\"id\", \"value\":[5,10,15,20,25,30]}}," + "\"options\":{\"return-metrics\":false, \"constraint\":{\"name\":\"id\", \"range\":{\"type\": \"xs:long\",\"json-property\":\"id\"}}}" + "}}"; PojoQueryDefinition qd = (PojoQueryDefinition) queryMgr.newRawCombinedQueryDefinition( new StringHandle(queryAsString).withFormat(Format.JSON)); 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++; } assertEquals("Page total results", count, 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")); assertTrue("String handle contains format", results.get().contains("\"format\":\"json\"")); ObjectMapper mapper = new ObjectMapper(); JsonNode actNode = mapper.readTree(results.get()).get("total"); assertEquals("Total search results resulted are ", 6, actNode.asInt()); }
// 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); }