@Test public void testQueryByExampleJSONPayload() throws KeyManagementException, NoSuchAlgorithmException, IOException, Exception { System.out.println("Running testQueryByExampleJSONPayload"); String[] filenames = { "constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json" }; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); } // get the combined query File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe1.json"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.JSON)); String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); System.out.println(resultDoc); assertTrue("Result is not proper", resultDoc.contains("/qbe/constraint1.json")); // release client client.release(); }
@Test public void testQueryByExampleWrongJSON() throws KeyManagementException, NoSuchAlgorithmException, IOException { System.out.println("Running testQueryByExampleJSON"); String filename = "WrongFormat.json"; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); try { // write docs writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); // get the combined query File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe1.json"); String qbeQuery = convertFileToString(file); StringHandle qbeHandle = new StringHandle(qbeQuery); qbeHandle.setFormat(Format.JSON); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); System.out.println(resultDoc); assertTrue("total result is not correct", resultDoc.contains("\"total\":1")); assertTrue("doc returned is not correct", resultDoc.contains("\"metadata\":[{\"title\":\"Vannevar Bush\"},{\"id\":11},{\"p\":\"Vannevar Bush wrote an article for The Atlantic Monthly\"},{\"popularity\":5}]")); } catch (FailedRequestException e) { System.out.println("Negative test passed as JSON with invalid structure gave FailedRequestException "); } // release client client.release(); }
@Test public void testQueryByExampleWrongXML() throws KeyManagementException, NoSuchAlgorithmException, IOException, TransformerException, XpathException { System.out.println("Running testQueryByExampleXMLPayload"); String filename = "WrongFormat.xml"; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); try { // write docs writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); // get the combined query File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe1.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); SearchHandle results = queryMgr.search(rw, new SearchHandle()); for (MatchDocumentSummary result : results.getMatchResults()) { System.out.println(result.getUri() + ": Uri"); // assertEquals("Wrong Document Searched",result.getUri() , // "/qbe/constraint1.xml"); } } catch (FailedRequestException e) { System.out.println("Negative test passed as XML with invalid structure gave FailedRequestException "); } // release client client.release(); }
@Test public void testQueryByExampleJSON() throws KeyManagementException, NoSuchAlgorithmException, IOException { System.out.println("Running testQueryByExampleJSON"); String[] filenames = { "constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json" }; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); } // get the combined query File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe1.json"); String qbeQuery = convertFileToString(file); StringHandle qbeHandle = new StringHandle(qbeQuery); qbeHandle.setFormat(Format.JSON); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); System.out.println("testQueryByExampleJSON Result : " + resultDoc); assertTrue( "doc returned is not correct", resultDoc .contains("<search:result index=\"1\" uri=\"/qbe/constraint1.json\" path=\"fn:doc("/qbe/constraint1.json")\" score=\"28672\" confidence=\"0.6951694\" fitness=\"0.6951694\" href=\"/v1/documents?uri=%2Fqbe%2Fconstraint1.json\" mimetype=\"application/json\" format=\"json\">")); // release client client.release(); }
RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); RawQueryByExampleDefinition newQbyex = newQueryMgr.newRawQueryByExampleDefinition(newQbeHandle); try { String newResultDoc = newQueryMgr.search(newQbyex, new StringHandle()).get();
@Test public void testQueryByExampleXMLPermission() throws KeyManagementException, NoSuchAlgorithmException, IOException, TransformerException, XpathException { System.out.println("Running testQueryByExampleXMLPermission"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); } // get the combined query try { File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe2.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); SearchHandle results = queryMgr.search(rw, new SearchHandle()); for (MatchDocumentSummary result : results.getMatchResults()) { System.out.println(result.getUri() + ": Uri"); assertEquals("Wrong Document Searched", result.getUri(), "/qbe/constraint1.xml"); } } catch (Exception e) { System.out.println("Negative Test Passed of executing nonreadable file"); } // release client client.release(); }
RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle);
@Test public void testQueryByExampleXMLPayload() throws KeyManagementException, NoSuchAlgorithmException, IOException, TransformerException, XpathException { System.out.println("Running testQueryByExampleXMLPayload"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); } // get the combined query File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe1.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); SearchHandle results = queryMgr.search(rw, new SearchHandle()); for (MatchDocumentSummary result : results.getMatchResults()) { System.out.println(result.getUri() + ": Uri"); assertEquals("Wrong Document Searched", result.getUri(), "/qbe/constraint1.xml"); } // release client client.release(); }
QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); SearchHandle results = queryMgr.search(rw, new SearchHandle()); QueryManager newQueryMgr = client.newQueryManager(); RawQueryByExampleDefinition newRw = newQueryMgr.newRawQueryByExampleDefinition(wrongFileHandle.withFormat(Format.XML)); SearchHandle newResults = queryMgr.search(newRw, new SearchHandle());
@Test public void testQueryByExampleXMLnew() throws KeyManagementException, NoSuchAlgorithmException, IOException, TransformerException, XpathException { System.out.println("Running testQueryByExampleXML"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); } // get the combined query File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe1.xml"); String qbeQuery = convertFileToString(file); StringHandle qbeHandle = new StringHandle(qbeQuery); qbeHandle.setFormat(Format.XML); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); Document resultDoc = queryMgr.search(qbyex, new DOMHandle()).get(); System.out.println("XML Result" + convertXMLDocumentToString(resultDoc)); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); // release client client.release(); }
@Test public void testQueryByExampleXML() throws KeyManagementException, NoSuchAlgorithmException, IOException, TransformerException, XpathException { System.out.println("Running testQueryByExampleXML"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); } // get the combined query File file = new File("src/test/java/com/marklogic/client/functionaltest/qbe/qbe1.xml"); String qbeQuery = convertFileToString(file); StringHandle qbeHandle = new StringHandle(qbeQuery); qbeHandle.setFormat(Format.XML); QueryManager queryMgr = client.newQueryManager(); RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); Document resultDoc = queryMgr.search(qbyex, new DOMHandle()).get(); System.out.println("XML Result" + convertXMLDocumentToString(resultDoc)); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); // release client client.release(); }
@Test public void xmlQbe() { XMLDocumentManager xdm = Common.client.newXMLDocumentManager(); QueryManager qm = Common.client.newQueryManager(); String queryAsString = "<q:qbe xmlns:q='http://marklogic.com/appservices/querybyexample'>" + "<q:query>" + "<kind>bird</kind>" + "</q:query>" + "</q:qbe>"; RawQueryByExampleDefinition query = qm.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.XML)); StringHandle report = qm.validate(query, new StringHandle()); System.out.println(report.toString()); SearchHandle results = qm.search(query, new SearchHandle()); assertEquals("No XML results should have matched", results.getTotalResults(), 0); DocumentPage documents = xdm.search(query, 1); assertEquals("No XML documents should have matched", documents.getTotalSize(), 0); documents = xdm.search(query, 1, new DOMHandle()); assertEquals("No XML documents should have matched", documents.getTotalSize(), 0); }
+ ",\"$filtered\": true}}"; System.out.println(queryAsString); PojoQueryDefinition qd = (PojoQueryDefinition) queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); qd.setCollections("even"); SearchHandle results = new SearchHandle();
RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON));
+ "}}"; PojoQueryDefinition qd = (PojoQueryDefinition) queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); qd.setCollections("odd"); products.setPageLength(11);
@Test public void jsonQbe() { JSONDocumentManager jdm = Common.client.newJSONDocumentManager(); QueryManager qm = Common.client.newQueryManager(); String queryAsString = "{ \"$query\": { \"kind\": \"bird\" } }"; StringHandle handle = new StringHandle(); handle.withFormat(Format.JSON).set(queryAsString); RawQueryByExampleDefinition query = qm.newRawQueryByExampleDefinition(handle); StringHandle report = qm.validate(query, new StringHandle()); System.out.println(report.toString()); SearchHandle results = qm.search(query, new SearchHandle()); assertEquals("6 json results should have matched", results.getTotalResults(), 6); DocumentPage documents = jdm.search(query, 1); assertEquals("6 json documents should have matched", documents.getTotalSize(), 6); documents = jdm.search(query, 1, new JacksonHandle()); assertEquals("6 json documents should have matched", documents.getTotalSize(), 6); documents = jdm.search(query, 1, new SearchHandle()); assertEquals("6 json documents should have matched", documents.getTotalSize(), 6); }
RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON));
@Test public void testFailedSearch() throws IOException { StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.term("criteriaThatShouldNotMatchAnyDocument"); RawCombinedQueryDefinition queryDef = queryMgr.newRawCombinedQueryDefinition( new StringHandle(head + t.serialize() + tail) .withMimetype("application/xml") ); SearchHandle results = queryMgr.search(queryDef, new SearchHandle()); assertNotNull(results); MatchDocumentSummary[] summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); StringHandle criteria = new StringHandle().withFormat(Format.XML); criteria.set("<q:query xmlns:q='" + RawQueryByExampleDefinition.QBE_NS + "'>" + "<q:word>criteriaThatShouldNotMatchAnyDocument</q:word>" + "</q:query>"); RawQueryByExampleDefinition qbe = queryMgr.newRawQueryByExampleDefinition(criteria); results = queryMgr.search(qbe, new SearchHandle()); assertNotNull(results); summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); }
String queryAsString = "<q:qbe xmlns:q=\"http://marklogic.com/appservices/querybyexample\"><q:query><foo><q:word>foo</q:word></foo></q:query></q:qbe>"; RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString));