@Test public void testBug18993() throws KeyManagementException, NoSuchAlgorithmException, IOException { if (!IsSecurityEnabled()) { // Run it only in non-SSL mode. System.out.println("Running testBug18993"); DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); XMLDocumentManager docMgr = client.newXMLDocumentManager(); StringHandle readHandle = new StringHandle(); String uris[] = { "/a b" }; String expectedXML = "<foo>a space b</foo>"; for (String uri : uris) { System.out.println("uri = " + uri); docMgr.read(uri, readHandle); System.out.println(); String strXML = readHandle.toString(); System.out.print(readHandle.toString()); assertTrue("Document is not returned", strXML.contains(expectedXML)); System.out.println(); } // release client client.release(); } }
@Test public void testWrite_nquads_FileHandle() throws Exception { File file = new File(datasource + "relative2.nq"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/nquads", filehandle.withMimetype("application/n-quads")); StringHandle handle = gmWriter.read("htp://test.sem.graph/nquads", new StringHandle().withMimetype(RDFMimeTypes.NQUADS)); assertTrue("Did not insert document or inserted empty doc", handle.toString().contains("<#electricVehicle2>")); }
@Test public void testJSONConverter() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testJSONConverter"); // String queryOptionName = "jsonConverterOpt.json"; String queryOptionName = "queryValidationOpt.json"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // create a manager for writing query options QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); // create handle FileHandle handle = new FileHandle(new File("src/test/java/com/marklogic/client/functionaltest/queryoptions/" + queryOptionName)); handle.setFormat(Format.JSON); // write the query options to the database optionsMgr.writeOptions(queryOptionName, handle); System.out.println("Write " + queryOptionName + " to database"); // read query option with QueryOptionsHandle StringHandle readHandle = new StringHandle(); optionsMgr.readOptions(queryOptionName, readHandle); String output = readHandle.toString(); System.out.println(output); System.out.println("============================"); client.release(); }
@Test public void testWrite_nTriples_FileHandle() throws Exception { File file = new File(datasource + "5triples.nt"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/n", filehandle.withMimetype("application/n-triples")); StringHandle handle = gmWriter.read("htp://test.sem.graph/n", new StringHandle().withMimetype(RDFMimeTypes.NTRIPLES)); assertTrue("Did not insert document or inserted empty doc", handle.toString().contains("<http://jibbering.com/foaf/santa.rdf#bod>")); }
@Test public void getTrace() throws IOException { TraceService tm = new TraceService(traceClient); TraceQuery traceQuery = new TraceQuery(); traceQuery.start = 1L; traceQuery.count = 1L; StringHandle traces = tm.getTraces(traceQuery); String resultStr = traces.toString(); ObjectMapper mapper = new ObjectMapper(); JsonNode node = mapper.readTree(resultStr); JsonNode results = node.findValue("results"); String traceId = results.get(0).findValue("content").findValue("traceId").asText(); JsonNode trace = tm.getTrace(traceId); assertNotNull(trace); assertEquals(traceId, trace.findValue("traceId").asText()); } }
@Test public void testMergeReplaceAs_Quads() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.NQUADS); File file = new File(datasource + "semantics.nq"); gmWriter.replaceGraphsAs(file); String uri = "http://en.wikipedia.org/wiki/Alexander_I_of_Serbia?oldid=492189987#absolute-line=1"; StringHandle readQuads = gmWriter.read(uri, new StringHandle()); assertTrue("Did not Replace Quads", readQuads.toString().contains("http://dbpedia.org/ontology/Monarch")); file = new File(datasource + "relative2.nq"); gmWriter.mergeGraphsAs(file); uri = "http://originalGraph"; readQuads = gmWriter.read(uri, new StringHandle()); assertTrue("Did not Replace Quads", readQuads.toString().contains("#electricVehicle2")); gmWriter.deleteGraphs(); try { StringHandle readContent = gmWriter.read(uri, new StringHandle()); assertTrue("Unexpected content read, expecting Resource not found exception", readContent.get() == null); } catch (Exception e) { assertTrue("Unexpected Exception Thrown", e.toString().contains("ResourceNotFoundException")); } }
@Test public void testWrite_triplexml_FileHandle() throws Exception { Transaction trx = writerClient.openTransaction(); GraphManager gmWriter = writerClient.newGraphManager(); File file = new File(datasource + "relative5.xml"); FileHandle filehandle = new FileHandle(); filehandle.set(file); // Write Graph using transaction gmWriter.write("htp://test.sem.graph/tripxml", filehandle.withMimetype(RDFMimeTypes.TRIPLEXML), trx); // Validate graph written to DB by reading within the same transaction StringHandle handle = gmWriter.read("htp://test.sem.graph/tripxml", new StringHandle().withMimetype(RDFMimeTypes.TRIPLEXML), trx); assertTrue("Did not insert document or inserted empty doc", handle.toString().contains("Anna's Homepage")); // Delete Graph in the same transaction gmWriter.delete("htp://test.sem.graph/tripxml", trx); // Validate Graph is deleted try { gmWriter.read("htp://test.sem.graph/tripxml", new StringHandle(), trx); trx.commit(); trx = null; } catch (ResourceNotFoundException e) { System.out.println(e); } finally { if (trx != null) { trx.rollback(); trx = null; } } }
@Test public void testMergeReplace_quads() throws FileNotFoundException, InterruptedException { String uri = "http://test.sem.quads/json-quads"; String ntriple6 = "<http://example.org/s6> <http://example.com/mergeQuadP> <http://example.org/o2> <http://test.sem.quads/json-quads>."; File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write(uri, filehandle.withMimetype(RDFMimeTypes.RDFJSON)); gmWriter.mergeGraphs(new StringHandle(ntriple6).withMimetype(RDFMimeTypes.NQUADS)); FileHandle handle = gmWriter.read(uri, new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue("Did not merge Quad", expectedContent.contains("<http://example.com/mergeQuadP")); file = new File(datasource + "relative2.nq"); gmWriter.replaceGraphs(new FileHandle(file).withMimetype(RDFMimeTypes.NQUADS)); uri = "http://originalGraph"; StringHandle readQuads = gmWriter.read(uri, new StringHandle()); assertTrue("Did not Replace Quads", readQuads.toString().contains("#electricVehicle2")); gmWriter.deleteGraphs(); try { StringHandle readContent = gmWriter.read(uri, new StringHandle()); assertTrue("Unexpected content read, expecting Resource not found exception", readContent.get() == null); } catch (Exception e) { assertTrue("Unexpected Exception Thrown", e.toString().contains("ResourceNotFoundException")); } }
@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); }
@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); }