@Override public List<File> importWorkspaces(String user, String... workspaceNames) { if (baseDir == null) { baseDir = getDefaultWorkspacesDir(); } List<File> files = new ArrayList<>(); File userDir = new File(baseDir, user); if (!userDir.exists()) { return files; } final String importQuery = determineImportScriptToUse(); for (String workspace : workspaceNames) { File f = new File(userDir, workspace + ".xml"); if (f.isFile() && f.exists()) { client.newServerEval() .addVariable("user", user) .addVariable("exported-workspace", new FileHandle(f).withFormat(Format.XML)) .xquery(importQuery).eval(); if (logger.isInfoEnabled()) { logger.info(format("Imported workspace from %s", f.getAbsolutePath())); } files.add(f); } } return files; }
private static void installModule() throws Exception { // get a modules manager ExtensionLibrariesManager libsMgr = adminClient.newServerConfigManager().newExtensionLibrariesManager(); // write server-side javascript module file to the modules database libsMgr.write("/ext" + directory + "filterUrisReferencedByHolds.sjs", new FileHandle(new File("src/test/resources/legal_holds/filterUrisReferencedByHolds.sjs")) .withFormat(Format.TEXT)); }
@Test(expected = FailedRequestException.class) public void testWriteMultipleSameBinaryDoc() throws KeyManagementException, NoSuchAlgorithmException, Exception { String docId[] = { "Pandakarlino.jpg", "mlfavicon.png" }; BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); DocumentWriteSet writeset = docMgr.newWriteSet(); File file1 = null; file1 = new File("src/test/java/com/marklogic/client/functionaltest/data/" + docId[0]); FileHandle handle1 = new FileHandle(file1); writeset.add("/1/" + docId[0], handle1.withFormat(Format.BINARY)); writeset.add("/1/" + docId[0], handle1.withFormat(Format.BINARY)); docMgr.write(writeset); }
@BeforeClass public static void beforeClass() { Common.connectAdmin(); // get a manager libsMgr = Common.adminClient .newServerConfigManager().newExtensionLibrariesManager(); // write XQuery file to the modules database libsMgr.write("/ext/my-lib.xqy", new FileHandle( new File("src/test/resources/my-lib.xqy")).withFormat(Format.TEXT)); Common.connect(); } @AfterClass
@BeforeClass public static void beforeClass() { Common.connectAdmin(); // get a manager libsMgr = Common.adminClient .newServerConfigManager().newExtensionLibrariesManager(); // write XQuery file to the modules database libsMgr.write("/ext/my-lib.xqy", new FileHandle( new File("src/test/resources/my-lib.xqy")).withFormat(Format.TEXT)); Common.connect(); }
file1 = new File("src/test/java/com/marklogic/client/functionaltest/data/" + docId[0]); FileHandle handle1 = new FileHandle(file1); writeset.add("/1/" + docId[0], handle1.withFormat(Format.BINARY)); writeset.add("/2/" + docId[0], handle1.withFormat(Format.BINARY)); file2 = new File("src/test/java/com/marklogic/client/functionaltest/data/" + docId[1]); FileHandle handle2 = new FileHandle(file2); writeset.add("/1/" + docId[1], handle2.withFormat(Format.BINARY)); writeset.add("/2/" + docId[1], handle2.withFormat(Format.BINARY));
@Test public void testRawCombinedCtsQueryFromFileHandle() throws Exception { Common.client.newDocumentManager().write("testRawCtsQueryFromFileHandle.xml", new FileHandle(new File("src/test/resources/constraint5.xml")).withFormat(Format.XML)); FileHandle ctsHandle = new FileHandle(new File("src/test/resources/combinedCtsQuery.xml")); QueryDefinition[] queries = new QueryDefinition[] { queryMgr.newRawCombinedQueryDefinition(ctsHandle), queryMgr.newRawCtsQueryDefinition(ctsHandle), }; for ( QueryDefinition query : queries ) { // create result handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(query, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id' or local-name()='highlight'])", resultDoc); } } }
@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 testXQueryModuleCRUDXmlFileNegative() throws KeyManagementException, NoSuchAlgorithmException, IOException { DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // get a manager ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); String Path = "/foo/my/path/to/my/module.xqy"; FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/client/functionaltest/data/all_well.xml")).withFormat(Format.XML); // write XQuery file to the modules database try { libsMgr.write(Path, f); Assert.assertTrue("Exception was not thrown", false); } catch (IllegalArgumentException e) { // Issue 210 logged for meaningful error assertEquals("libraryPath (the modules database path under which you install an asset) must begin with /ext/", e.getMessage()); } // delete it try { libsMgr.delete(Path); Assert.assertTrue("Exception was not thrown", false); } catch (IllegalArgumentException e) { assertEquals("libraryPath (the modules database path under which you install an asset) must begin with /ext/", e.getMessage()); } }
@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 test_issue581_RawStructuredQueryFromFileHandle() throws Exception { Common.client.newDocumentManager().write("test_issue581_RawStructuredQueryFromFileHandle.xml", new FileHandle(new File("src/test/resources/constraint5.xml")).withFormat(Format.XML)); // get the combined query File file = new File("src/test/resources/combinedQueryOption.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); QueryManager queryMgr = Common.client.newQueryManager(); // create a search definition based on the handle RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); // create result 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("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); }
FileHandle f = new FileHandle(new File("test-complete/src/test/java/com/marklogic/client/functionaltest/data/all_well.xml")).withFormat(Format.XML);
@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(); }
FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/client/functionaltest/data/module.xqy")).withFormat(Format.TEXT);
@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(); }
@Test public void testXQueryModuleCRUDBinaryFile() throws KeyManagementException, NoSuchAlgorithmException, IOException { DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // get a manager ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); String Path = "/ext/my/path/to/my/module.xqy"; // write XQuery file to the modules database libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/client/functionaltest/data/binary.jpg")).withFormat(Format.BINARY)); // read it back File f = new File("src/test/java/com/marklogic/client/functionaltest/data/binary.jpg"); assertEquals(f.length(), libsMgr.read(Path, new StringHandle()).getByteLength()); // get the list of descriptors ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); assertEquals("number of modules installed", descriptors.length, 1); for (ExtensionLibraryDescriptor descriptor : descriptors) { assertEquals(descriptor.getPath(), Path); } // delete it libsMgr.delete(Path); try { // read deleted module libsMgr.read(Path, new StringHandle()).get(); } catch (ResourceNotFoundException e) { // pass; } }
@Test public void testRawCtsQueryFromFileHandle() throws Exception { Common.client.newDocumentManager().write("testRawCtsQueryFromFileHandle.xml", new FileHandle(new File("src/test/resources/constraint5.xml")).withFormat(Format.XML)); String ctsQueryFilePath = "ctsQuery1.xml"; String ctsQueryAsString = Common.testFileToString(ctsQueryFilePath); File ctsQueryFile = new File(Common.getResourceUri(ctsQueryFilePath)); FileHandle ctsHandle = new FileHandle(ctsQueryFile); QueryDefinition[] queries = new QueryDefinition[] { queryMgr.newRawCombinedQueryDefinition(ctsHandle), queryMgr.newRawCtsQueryDefinition(ctsHandle), queryMgr.newRawCombinedQueryDefinitionAs(Format.XML, ctsQueryFile), queryMgr.newRawCtsQueryDefinitionAs(Format.XML, ctsQueryFile), queryMgr.newRawCtsQueryDefinition(new InputStreamHandle(new FileInputStream(ctsQueryFile))), queryMgr.newRawCtsQueryDefinition(new StringHandle(ctsQueryAsString)), queryMgr.newRawCtsQueryDefinition(new DOMHandle(Common.testStringToDocument(ctsQueryAsString))) }; for ( QueryDefinition query : queries ) { // create result handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(query, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id' or local-name()='highlight'])", resultDoc); } }
String secondPath = "/ext/my/path/to/my/other/module.xqy"; libsMgr.write(firstPath, new FileHandle(new File("src/test/java/com/marklogic/client/functionaltest/data/module.xqy")).withFormat(Format.TEXT)); libsMgr.write(secondPath, new FileHandle(new File("src/test/java/com/marklogic/client/functionaltest/data/module.xqy")).withFormat(Format.TEXT));
@Test public void testXQueryModuleCRUDTextFile() throws KeyManagementException, NoSuchAlgorithmException, IOException { DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // get a manager ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); String Path = "/ext/my/path/to/my/module.xqy"; // write XQuery file to the modules database libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/client/functionaltest/data/readme.txt")).withFormat(Format.TEXT)); // read it back String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); assertTrue("module read and read back", xqueryModuleAsString.startsWith("Copyright 2017 MarkLogic Corporation")); // get the list of descriptors ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); assertEquals("number of modules installed", descriptors.length, 1); for (ExtensionLibraryDescriptor descriptor : descriptors) { assertEquals(descriptor.getPath(), Path); } // delete it libsMgr.delete(Path); try { // read deleted module xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); } catch (ResourceNotFoundException e) { // pass; } }
@Test public void testXQueryModuleCRUDXmlFile() throws KeyManagementException, NoSuchAlgorithmException, IOException { DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // get a manager ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); String Path = "/ext/my/path/to/my/module.xqy"; FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/client/functionaltest/data/all_well.xml")).withFormat(Format.XML); // write XQuery file to the modules database libsMgr.write(Path, f); // read it back assertEquals(209745, libsMgr.read(Path, new StringHandle()).getByteLength()); // get the list of descriptors ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); assertEquals("number of modules installed", descriptors.length, 1); for (ExtensionLibraryDescriptor descriptor : descriptors) { assertEquals(descriptor.getPath(), Path); } // delete it libsMgr.delete(Path); try { // read deleted module libsMgr.read(Path, new StringHandle()).get(); } catch (ResourceNotFoundException e) { // pass; } }