public static void writeNTriplesFromFile(String filename, String graphName) throws KeyManagementException, NoSuchAlgorithmException, Exception { File file = new File(datasource + filename); FileHandle filehandle = new FileHandle(); GraphManager sparqlGmgr = writeclient.newGraphManager(); filehandle.set(file); sparqlGmgr.write(graphName, filehandle.withMimetype(RDFMimeTypes.TURTLE)); }
@Test // GitIssue #319 public void testWrite_rdfxml_WrongTriplesSchema() throws Exception { Exception exp = null; File file = new File(datasource + "relative4.rdf"); FileHandle filehandle = new FileHandle(); filehandle.set(file); try { gmWriter.write("htp://test.sem.graph/rdf", filehandle.withMimetype(RDFMimeTypes.RDFXML)); } catch (Exception e) { exp = e; } assertTrue("Did not throw expected Exception, Expecting ::XDMP-BASEURI (err:FOER0000): Undeclared base URI " + "\n Received::" + exp, exp.toString().contains("XDMP-BASEURI (err:FOER0000): Undeclared base URI")); }
@Test public void testWrite_rdfjson_TripleReadHandle() throws Exception { File file = new File(datasource + "relative6.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/rdfjson", filehandle.withMimetype("application/rdf+json")); TriplesReadHandle handle1 = gmWriter.read("htp://test.sem.graph/rdfjson", new JacksonHandle()); assertTrue("Did not insert document or inserted empty doc", handle1.toString().contains("http://purl.org/dc/elements/1.1/title")); }
@Test public void testMerge_jsonxml_FileHandle() throws Exception { File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); File file2 = new File(datasource + "relative5.xml"); FileHandle filehandle2 = new FileHandle(); filehandle2.set(file2); gmWriter.write("htp://test.sem.graph/trigM", filehandle.withMimetype(RDFMimeTypes.RDFJSON)); gmWriter.merge("htp://test.sem.graph/trigM", filehandle2.withMimetype(RDFMimeTypes.TRIPLEXML)); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigM", new FileHandle().withMimetype(RDFMimeTypes.RDFJSON)); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://example.com/ns/person#firstName") && expectedContent.contains("Anna's Homepage")); }
sparqlGmgr.write(docId, filehandle.withMimetype(sparqlMIMEType)); if (uri != null) System.out.println("Loaded triples in " + filename
@Test public void testWrite_rdfjson_JacksonHandle() throws Exception { File file = new File(datasource + "relative6.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/rdfjson", filehandle.withMimetype("application/rdf+json")); JacksonHandle handle1 = gmWriter.read("htp://test.sem.graph/rdfjson", new JacksonHandle()); JsonNode readFile = handle1.get(); assertTrue("Did not insert document or inserted empty doc", readFile.toString().contains("http://purl.org/dc/elements/1.1/title")); }
@Test public void testWrite_rdfjson_FileHandle() throws Exception { ObjectMapper mapper = new ObjectMapper(); new ObjectMapper(); File file = new File(datasource + "relative6.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/rdfjson", filehandle.withMimetype("application/rdf+json")); FileHandle handle = new FileHandle(); handle.setMimetype(RDFMimeTypes.RDFJSON); handle = gmWriter.read("htp://test.sem.graph/rdfjson", handle); File readFile = handle.get(); JsonNode readContent = mapper.readTree(readFile); // Verify read content with inserted content assertTrue("Did not insert document or inserted empty doc", readContent.toString() .contains("http://purl.org/dc/elements/1.1/title")); }
@Test public void testWrite_trig_FileHandle() throws Exception { File file = new File(datasource + "semantics.trig"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/trig", filehandle.withMimetype("application/trig")); gmWriter.write("htp://test.sem.graph/trig", filehandle.withMimetype("application/trig")); FileHandle handle = gmWriter.read("htp://test.sem.graph/trig", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://www.example.org/exampleDocument#Monica")); }
@Test public void testReadAs_WriteAs() throws Exception { File file = new File(datasource + "semantics.nt"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.writeAs("htp://test.sem.graph/ntAs", filehandle.withMimetype(RDFMimeTypes.NTRIPLES)); File read = gmWriter.readAs("htp://test.sem.graph/ntAs", File.class); String expectedContent = convertFileToString(read); assertTrue("writeAs & readAs test did not return expected content", expectedContent.contains("http://www.w3.org/2001/sw/RDFCore/ntriples/")); }
@Test public void testRead_nonirirdfxml_FileHandle() throws Exception { StringHandle handle = new StringHandle(); File file = new File(datasource + "non-iri.xml"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/tripxmlnoniri", filehandle.withMimetype(RDFMimeTypes.TRIPLEXML)); try { handle = gmWriter.read("htp://test.sem.graph/tripxmlnoniri", new StringHandle()); } catch (Exception e) { assertTrue("Did not receive expected Error message, Expecting ::SEM-NOTRDF \n Received::" + e, e.toString().contains("SEM-NOTRDF") && e != null); } assertTrue("Read content has un-expected content ", handle.get().toString().contains("5.11")); }
@Test public void testRead_nonirin3_FileHandle() throws Exception { StringHandle handle = new StringHandle(); File file = new File(datasource + "non-iri.n3"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/n3noniri", filehandle.withMimetype(RDFMimeTypes.N3)); try { handle = gmWriter.read("htp://test.sem.graph/n3noniri", new StringHandle().withMimetype(RDFMimeTypes.N3)); } catch (Exception e) { assertTrue("Did not receive expected Error message, Expecting ::SEM-NOTRDF \n Received::" + e, e.toString().contains("SEM-NOTRDF") && e != null); } assertTrue("Read content has un-expected content ", handle.get().toString().contains("p0:named_graph")); }
@Test public void testWriteRead_defaultGraph() throws FileNotFoundException { File file = new File(datasource + "semantics.trig"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write(gmWriter.DEFAULT_GRAPH, filehandle.withMimetype(RDFMimeTypes.TRIG)); FileHandle handle = gmWriter.read(gmWriter.DEFAULT_GRAPH, new FileHandle()); GraphPermissions permissions = gmWriter.getPermissions(gmWriter.DEFAULT_GRAPH); System.out.println(permissions); assertEquals(Capability.UPDATE, permissions.get("rest-writer").iterator().next()); assertEquals(Capability.READ, permissions.get("rest-reader").iterator().next()); gmWriter.deletePermissions(gmWriter.DEFAULT_GRAPH); permissions = gmWriter.getPermissions(gmWriter.DEFAULT_GRAPH); System.out.println(permissions); assertEquals(Capability.UPDATE, permissions.get("rest-writer").iterator().next()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); System.out.println(gmWriter.listGraphUris().next().toString()); assertTrue("" + gmWriter.listGraphUris().next().toString(), gmWriter.listGraphUris().next().toString().equals("http://marklogic.com/semantics#default-graph")); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://www.example.org/exampleDocument#Monica")); }
@Test public void testMerge_trig_FileHandle() throws Exception { File file = new File(datasource + "trig.trig"); FileHandle filehandle = new FileHandle(); filehandle.set(file); File file2 = new File(datasource + "semantics.trig"); FileHandle filehandle2 = new FileHandle(); filehandle2.set(file2); gmWriter.write("htp://test.sem.graph/trigMerge", filehandle.withMimetype(RDFMimeTypes.TRIG)); gmWriter.merge("htp://test.sem.graph/trigMerge", filehandle2.withMimetype(RDFMimeTypes.TRIG)); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigMerge", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue( "Did not insert document or inserted empty doc", expectedContent.contains("http://www.example.org/exampleDocument#Monica") && expectedContent.contains("http://purl.org/dc/elements/1.1/publisher")); }
@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 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 testThings_Differentmimetypes() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); String tripleGraphUri = "http://test.things.com/multiple"; File file = new File(datasource + "relative5.xml"); gmWriter.write(tripleGraphUri, new FileHandle(file)); file = new File(datasource + "semantics.ttl"); gmWriter.merge(tripleGraphUri, new FileHandle(file).withMimetype(RDFMimeTypes.TURTLE)); StringHandle things = gmWriter.things(new StringHandle(), "http://dbpedia.org/resource/Hadoop"); assertTrue("Things did not return expected Uri's", things.get().contains("Apache Hadoop")); gmWriter.delete(tripleGraphUri); }
@Test public void testWriteMergeDelete_Trx() throws Exception { Transaction trx = writerClient.openTransaction(); GraphManager gmWriter = writerClient.newGraphManager(); File file = new File(datasource + "triplexml1.xml"); gmWriter.write("htp://test.sem.graph/mergetrx", new FileHandle(file).withMimetype(RDFMimeTypes.TRIPLEXML), trx); file = new File(datasource + "bug25348.json"); gmWriter.merge("htp://test.sem.graph/mergetrx", new FileHandle(file).withMimetype(RDFMimeTypes.RDFJSON), trx); FileHandle handle = gmWriter.read("htp://test.sem.graph/mergetrx", new FileHandle(), trx); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); try { assertTrue("Did not Merge document or inserted empty doc", expectedContent.contains("Michelle") && expectedContent.contains("Anna's Homepage")); gmWriter.delete("htp://test.sem.graph/mergetrx", trx); trx.commit(); trx = null; StringHandle readContent = gmWriter.read("htp://test.sem.graph/mergetrx", new StringHandle(), trx); assertTrue("Unexpected Content from read, expecting null", readContent == null); } catch (Exception e) { assertTrue("Unexpected Exception Thrown", e.toString().contains("ResourceNotFoundException")); } finally { if (trx != null) trx.commit(); 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 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; } } }
filehandle.set(file); gmWriter.writeAs("htp://test.sem.graph/ttlas", filehandle.withMimetype(RDFMimeTypes.TURTLE), trx);