private boolean validateUriNotFound(String uri, Transaction tx) { try { gmgr.read(uri, new StringHandle(), tx); } catch (ResourceNotFoundException e) { return true; } return false; }
@Test public void testQuads() throws Exception { gmgr.setDefaultMimetype(RDFMimeTypes.NQUADS); String quadGraphUri = "GraphsTest.testQuads"; String quad1 = "<http://example.org/s1> <http://example.org/p1> <http://example.org/o1> <" + quadGraphUri + "> ."; String quad2 = "<http://example.org/s2> <http://example.org/p2> <http://example.org/o2> <" + quadGraphUri + "> ."; String quad3 = "<http://example.org/s3> <http://example.org/p2> <http://example.org/o2> <" + quadGraphUri + "> ."; String quad4 = "<http://example.org/s4> <http://example.org/p2> <http://example.org/o2> <" + quadGraphUri + "> ."; gmgr.replaceGraphs(new StringHandle(quad1)); StringHandle quadsHandle = gmgr.read(quadGraphUri, new StringHandle()); assertEquals(quad1, quadsHandle.get()); gmgr.mergeGraphs(new StringHandle(quad2)); StringHandle quads1and2 = gmgr.read(quadGraphUri, new StringHandle()); assertEquals(quad1 + "\n" + quad2, quads1and2.get()); gmgr.replaceGraphsAs(quad3); quadsHandle = gmgr.read(quadGraphUri, new StringHandle()); assertEquals(quad3, quadsHandle.get()); gmgr.mergeGraphsAs(quad4); String quads3and4 = gmgr.readAs(quadGraphUri, String.class); assertEquals(quad3 + "\n" + quad4, quads3and4); gmgr.delete(quadGraphUri); }
public Graph readGraph(String uri) { InputStreamHandle handle = new InputStreamHandle(); Graph graph = GraphFactory.createDefaultGraph(); try { this.graphManager.read(uri, handle, currentTransaction); RDFDataMgr.read(graph, handle.get(), Lang.NTRIPLES); } catch (NullPointerException e) { log.debug("RDF Manager Throws execption for empty results"); } catch (ResourceNotFoundException e) { log.debug("No graph returned by graph manage. Returning empty graph", e.getMessage()); } // close handle? return graph; }
@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 testWrite_ttl_FileHandle() throws Exception { File file = new File(datasource + "relative3.ttl"); FileInputStream fileinputstream = new FileInputStream(file); ByteArrayOutputStream byteoutputstream = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; // Get triples into bytes from file for (int readNum; (readNum = fileinputstream.read(buf)) != -1;) { byteoutputstream.write(buf, 0, readNum); } byte[] bytes = byteoutputstream.toByteArray(); fileinputstream.close(); byteoutputstream.close(); BytesHandle contentHandle = new BytesHandle(); contentHandle.set(bytes); // write triples in bytes to DB gmWriter.write("http://test.turtle.com/byteHandle", contentHandle.withMimetype(RDFMimeTypes.TURTLE)); BytesHandle byteHandle = gmWriter.read("http://test.turtle.com/byteHandle", new BytesHandle().withMimetype(RDFMimeTypes.TURTLE)); byte[] readInBytes = byteHandle.get(); String readInString = new String(readInBytes); assertTrue("Did not insert document or inserted empty doc", readInString.contains("#relativeIRI")); }
@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_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 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 testWrite_n3_BytesHandle() throws Exception { File file = new File(datasource + "relative7.n3"); FileInputStream fileinputstream = new FileInputStream(file); ByteArrayOutputStream byteoutputstream = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; // Get triples into bytes from file for (int readNum; (readNum = fileinputstream.read(buf)) != -1;) { byteoutputstream.write(buf, 0, readNum); } byte[] bytes = byteoutputstream.toByteArray(); fileinputstream.close(); byteoutputstream.close(); BytesHandle contentHandle = new BytesHandle(); contentHandle.set(bytes); // write triples in bytes to DB gmWriter.write("http://test.n3.com/byte", contentHandle.withMimetype(RDFMimeTypes.N3)); InputStreamHandle read = gmWriter.read("http://test.n3.com/byte", new InputStreamHandle().withMimetype(RDFMimeTypes.N3)); InputStream fileRead = read.get(); String readContent = convertInputStreamToString(fileRead); assertTrue("Did not find expected content after inserting the triples:: Found: " + readContent, readContent.contains("/publications/journals/Journal1/1940")); read.close(); }
@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 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 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 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_rdfxml_FileHandle() throws Exception { BufferedReader buffreader = new BufferedReader(new FileReader(datasource + "semantics.rdf")); ReaderHandle handle = new ReaderHandle(); handle.set(buffreader); gmWriter.write("http://test.reader.handle/bufferreadtrig", handle.withMimetype(RDFMimeTypes.RDFXML)); buffreader.close(); ReaderHandle read = gmWriter.read("http://test.reader.handle/bufferreadtrig", new ReaderHandle().withMimetype(RDFMimeTypes.RDFXML)); Reader readFile = read.get(); String readContent = convertReaderToString(readFile); assertTrue("Did not get receive expected string content, Received:: " + readContent, readContent.contains("http://www.daml.org/2001/12/factbook/vi#A113932")); handle.close(); read.close(); }
@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 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 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 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")); }
@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 testMergeSameDoc_jsonxml_FileHandle() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.RDFJSON); File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/trigMSameDoc", filehandle); gmWriter.merge("htp://test.sem.graph/trigMSameDoc", filehandle); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigMSameDoc", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue( "Did not insert document or inserted empty doc", expectedContent .equals("{\"http://example.com/ns/directory#m\":{\"http://example.com/ns/person#firstName\":[{\"value\":\"Michelle\", \"" + "type\":\"literal\", \"datatype\":\"http://www.w3.org/2001/XMLSchema#string\"}]}}")); }