/** * Specifies the format of the content and returns the handle * as a fluent convenience. * @param format the format of the content * @return this handle */ public StringHandle withFormat(Format format) { setFormat(format); return this; } /**
/** * Specifies the format of the content and returns the handle * as a fluent convenience. * @param format the format of the content * @return this handle */ public StringHandle withFormat(Format format) { setFormat(format); return this; } /**
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (isFlowPropertiesFile(file) && modulesManager.hasFileBeenModifiedSinceLastLoaded(file.toFile())) { Flow flow = flowManager.getFlowFromProperties(file); StringHandle handle = new StringHandle(flow.serialize()); handle.setFormat(Format.XML); documentWriteSet.add(flow.getFlowDbPath(), handle); modulesManager.saveLastLoadedTimestamp(file.toFile(), new Date()); } return FileVisitResult.CONTINUE; } });
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (isFlowPropertiesFile(file) && modulesManager.hasFileBeenModifiedSinceLastLoaded(file.toFile())) { Flow flow = flowManager.getFlowFromProperties(file); StringHandle handle = new StringHandle(flow.serialize()); handle.setFormat(Format.XML); documentWriteSet.add(flow.getFlowDbPath(), handle); modulesManager.saveLastLoadedTimestamp(file.toFile(), new Date()); } return FileVisitResult.CONTINUE; } });
readHandle.setFormat(Format.JSON); optionsMgr.readOptions(queryOptionName, readHandle); StringHandle writeHandle = new StringHandle(); writeHandle.set(jsonQueryOption); writeHandle.setFormat(Format.JSON); optionsMgr.writeOptions(queryOptionNameJson, writeHandle); System.out.println("Write " + queryOptionNameJson + " to database");
@Test public void testBug19092() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testBug19092"); DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // create query options manager QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); // create query options handle String xmlOptions = "<search:options xmlns:search='http://marklogic.com/appservices/search'>" + "<search:term>" + "<search:term-option>case-sensitive</search:term-option>" + "</search:term>" + "</search:options>"; StringHandle handle = new StringHandle(xmlOptions); // write query options optionsMgr.writeOptions("DefaultTermOpt", handle); // read query option StringHandle readHandle = new StringHandle(); readHandle.setFormat(Format.XML); optionsMgr.readOptions("DefaultTermOpt", readHandle); String output = readHandle.get(); System.out.println(output); assertTrue("Default term is not correct", output.contains("<ns2:term-option>case-sensitive</ns2:term-option>") || output.contains("<search:term-option>case-sensitive</search:term-option>")); assertFalse("Weight element exists", output.contains("<ns2:weight>0.0</ns2:weight>") || output.contains("<search:weight>0.0</search:weight>")); assertFalse("Default element exists", output.contains("<ns2:default/>") || output.contains("<search:default/>")); // 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 testJSONOptions() throws JAXBException, ResourceNotFoundException, ForbiddenUserException, FailedRequestException, ResourceNotResendableException { QueryOptionsManager mgr = Common.adminClient.newServerConfigManager().newQueryOptionsManager(); assertNotNull("Client could not create query options manager", mgr); FileHandle jsonHandle = new FileHandle(new File("src/test/resources/json-config.json")); jsonHandle.setFormat(Format.JSON); mgr.writeOptions("jsonoptions", jsonHandle); JsonNode options = mgr.readOptions("jsonoptions", new JacksonHandle()).get(); assertEquals("JSON options came back incorrectly", options.findPath("constraint").get(0).get("name").textValue(), "decade"); StringHandle jsonStringHandle = new StringHandle(); jsonStringHandle.setFormat(Format.JSON); mgr.readOptions("jsonoptions", jsonStringHandle); assertTrue("JSON String from QueryManager must start with json options", jsonStringHandle.get().startsWith("{\"options\":")); mgr.deleteOptions("jsonoptions"); }; }
@Override public JsonNode validate(String entity, String flow, String plugin, String type, String content) { RequestParameters params = new RequestParameters(); params.add("entity", entity); params.add("flow", flow); params.add("plugin", plugin); params.add("type", type); StringHandle handle = new StringHandle(content); handle.setFormat(Format.TEXT); ResourceServices.ServiceResultIterator resultItr = this.getServices().post(params, handle ); if (resultItr == null || ! resultItr.hasNext()) { return null; } ResourceServices.ServiceResult res = resultItr.next(); return res.getContent(new JacksonHandle()).get(); } }
@Test public void testBug19140() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testBug19140"); DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // create query options manager QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); // create query options handle String xmlOptions = "<search:options xmlns:search='http://marklogic.com/appservices/search'>" + "<search:transform-results apply='raw'/>" + "</search:options>"; StringHandle handle = new StringHandle(xmlOptions); // write query options optionsMgr.writeOptions("RawResultsOpt", handle); // read query option StringHandle readHandle = new StringHandle(); readHandle.setFormat(Format.XML); optionsMgr.readOptions("RawResultsOpt", readHandle); String output = readHandle.get(); System.out.println(output); assertTrue("transform-results is incorrect", output.contains("transform-results apply=\"raw\"/")); assertFalse("preferred-elements is exist", output.contains("preferred-elements/")); // 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(); }
@Test public void testBug19092WithJson() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testBug19092WithJson"); DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // create query options manager QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); // create query options handle String xmlOptions = "<search:options xmlns:search='http://marklogic.com/appservices/search'>" + "<search:term>" + "<search:term-option>case-sensitive</search:term-option>" + "</search:term>" + "</search:options>"; StringHandle handle = new StringHandle(xmlOptions); // write query options optionsMgr.writeOptions("DefaultTermOpt", handle); // read query option StringHandle readHandle = new StringHandle(); readHandle.setFormat(Format.JSON); optionsMgr.readOptions("DefaultTermOpt", readHandle); String output = readHandle.get(); System.out.println(output); assertTrue("Default term is not correct", output.contains("{\"options\":{\"term\":{\"term-option\":[\"case-sensitive\"]}}}")); // release client client.release(); }
qbeHandle.setFormat(Format.XML);
rawHandle.setFormat(Format.JSON);
rawHandle.setFormat(Format.JSON);
@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 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(); }
rawHandle.setFormat(Format.JSON);
rawHandle.setFormat(Format.JSON);
@Test public void evalAndInvokeJavascript() throws DatatypeConfigurationException, JsonProcessingException, IOException { String javascript = "var myString;" + "var myArray;" + "var myObject;" + "var myBool;" + "var myInteger;" + "var myDouble;" + "var myDate;" + "var myNull;" + "xdmp.arrayValues([myString, myArray, myObject, myBool, myInteger, myDouble, myDate, myNull])"; // first run it as ad-hoc eval runAndTestJavascript( Common.evalClient.newServerEval().javascript(javascript) ); // run the same code, this time as a module we'll invoke StringHandle javascriptModule = new StringHandle( javascript ); javascriptModule.setFormat(Format.TEXT); // libMgr is connected with admin privileges (as rest-admin user) libMgr.write("/ext/test/evaltest.sjs", javascriptModule); // now module is installed, let's invoke it runAndTestJavascript( Common.evalClient.newServerEval().modulePath("/ext/test/evaltest.sjs") ); // clean up module we no longer need libMgr.delete("/ext/test/evaltest.sjs"); }