@Test public void testThings_file() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); String tripleGraphUri = "http://test.things.com/file"; File file = new File(datasource + "relative5.xml"); gmWriter.write(tripleGraphUri, new FileHandle(file)); TriplesReadHandle things = gmWriter.things(new StringHandle(), "about"); assertTrue("Things did not return expected Uri's", things.toString().contains("<about> <http://purl.org/dc/elements/1.1/title> \"Anna's Homepage\" .")); gmWriter.delete(tripleGraphUri); }
public void deletePermissions(String uri) { this.graphManager.deletePermissions(uri, currentTransaction); }
public GraphPermissions getGraphPermissions(String uri) { return this.graphManager.getPermissions(uri, currentTransaction); }
@Test public void testThings() throws Exception { gmgr.setDefaultMimetype(RDFMimeTypes.NTRIPLES); String tripleGraphUri = "GraphsTest.testTriples"; String triplesAboutS5 = "<http://example.org/s5> <http://example.org/p1> <1> .\n" + "<http://example.org/s5> <http://example.org/p2> <2> .\n" + "<http://example.org/s5> <http://example.org/p3> <3> .\n"; String triplesAboutS6 = "<http://example.org/s6> <http://example.org/p4> <http://example.org/s5> ."; gmgr.write(tripleGraphUri, new StringHandle(triplesAboutS5)); gmgr.merge(tripleGraphUri, new StringHandle(triplesAboutS6)); // since all triples reference http://example.org/s5, all triples should be returned // in NTRIPLES format since that's the default mimetype on gmgr String things = gmgr.thingsAs(String.class, "http://example.org/s5"); assertEquals(triplesAboutS5 + triplesAboutS6, things); gmgr.delete(tripleGraphUri); } }
@Test public void testThingsAs() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.NTRIPLES); File file = new File(datasource + "relative1.nt"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("http://test.things.com/", filehandle); String things = gmWriter.thingsAs(String.class, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); assertTrue( "Did not return Expected graph Uri's", things.equals("<#electricVehicle2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://people.aifb.kit.edu/awa/2011/smartgrid/schema/smartgrid#ElectricVehicle> .")); }
GraphPermissions perms = gmTestPerm.permission("test-perm", Capability.UPDATE); gmTestPerm.write(uri, handle.withMimetype(RDFMimeTypes.RDFXML), perms, trx); System.out.println("Permissions after create , Shold not see Execute" + gmTestPerm.getPermissions(uri, trx)); perms = gmTestPerm.getPermissions(uri, trx); for (Capability capability : perms.get("test-perm")) { assertTrue("capability should be UPDATE, not [" + capability + "]", capability == Capability.UPDATE); perms = gmTestPerm.permission("test-perm", Capability.EXECUTE); gmTestPerm.mergePermissions(uri, perms, trx); perms = gmTestPerm.getPermissions(uri, trx); System.out.println("Permissions after setting execute , Should see Execute & Update" + gmTestPerm.getPermissions(uri, trx)); for (Capability capability : perms.get("test-perm")) { assertTrue("capability should be UPDATE && Execute, not [" + capability + "]", capability == Capability.UPDATE gmTestPerm.write(uri, handle.withMimetype(RDFMimeTypes.RDFXML), perms, trx); } catch (Exception e) { System.out.println("Tried to Write Here ::" + e);// System.out.println("Permissions after setting execute , Should see Execute & Update" + gmTestPerm.getPermissions(uri, trx)); gmTestPerm.deletePermissions(uri, trx); try { perms = gmTestPerm.getPermissions(uri, trx); } catch (Exception e) { System.out
@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); }
@Test public void testTriples() throws Exception { gmgr.setDefaultMimetype(RDFMimeTypes.NTRIPLES); String tripleGraphUri = "GraphsTest.testTriples"; String ntriple5 = "<http://example.org/s5> <http://example.org/p2> <http://example.org/o2> ."; String ntriple7 = "<http://example.org/s7> <http://example.org/p2> <http://example.org/o2> ."; String ntriple8 = "<http://example.org/s8> <http://example.org/p2> <http://example.org/o2> ."; gmgr.write(tripleGraphUri, new StringHandle(ntriple5)); StringHandle triplesHandle = gmgr.read(tripleGraphUri, new StringHandle()); assertEquals(ntriple5, triplesHandle.get()); gmgr.write(tripleGraphUri, new StringHandle(ntriple5 + "\n" + ntriple6) ); String triples5and6 = gmgr.readAs(tripleGraphUri, String.class); String expected = ntriple5 + "\n" + ntriple6; assertEquals(expected, triples5and6); gmgr.merge(tripleGraphUri, new StringHandle(ntriple7)); StringHandle triples5and6and7 = gmgr.read(tripleGraphUri, new StringHandle()); assertEquals(ntriple5 + "\n" + ntriple6 + "\n" + ntriple7, triples5and6and7.get()); gmgr.writeAs(tripleGraphUri, ntriple7); String triples7 = gmgr.readAs(tripleGraphUri, String.class); assertEquals(ntriple7, triples7); gmgr.mergeAs(tripleGraphUri, ntriple8); String triples7and8 = gmgr.readAs(tripleGraphUri, String.class); assertEquals(ntriple7 + "\n" + ntriple8, triples7and8);
@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 testTransactions() { GraphManager graphManagerWriter = Common.client.newGraphManager(); graphManagerWriter.setDefaultMimetype(RDFMimeTypes.NTRIPLES); DatabaseClient readOnlyClient = Common.connectReadOnly(); GraphManager graphManagerReader = readOnlyClient.newGraphManager(); graphManagerReader.setDefaultMimetype(RDFMimeTypes.NTRIPLES); String t1 = "<s1> <p1> <o1> ."; String t2 = "<s2> <p2> <o2> ."; try { graphManagerReader.write("thisFails", new StringHandle(t1)); fail("reader could write a graph."); } catch (ForbiddenUserException e) { try { tx = Common.client.openTransaction(); graphManagerWriter.write("newGraph", new StringHandle(t1), tx); graphManagerReader.read( "newGraph", new StringHandle()); graphManagerWriter.read( "newGraph", new StringHandle()); graphManagerWriter.write("newGraph", new StringHandle(t1), tx); graphManagerWriter.merge("newGraph", new StringHandle(t2), tx);
@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")); }
gmWriter.write(uri, filehandle.withMimetype(RDFMimeTypes.RDFJSON), trxIn); gmWriter.mergeGraphs(new StringHandle(ntriple6).withMimetype(RDFMimeTypes.NQUADS), trxIn); trxIn.commit(); FileHandle handle = gmWriter.read(uri, new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); gmWriter.delete(uri, trxDelIn); trxDelIn.commit(); trxDelIn = null; gmWriter.write(uri, filehandle.withMimetype(RDFMimeTypes.RDFJSON), trxIn); trxIn.commit(); handle = gmWriter.read(uri, new FileHandle()); readFile = handle.get(); expectedContent = convertFileToString(readFile); gmWriter.mergeGraphs(new StringHandle(ntriple6).withMimetype(RDFMimeTypes.NQUADS), trxInMergeGraph); trxInMergeGraph.commit(); handle = gmWriter.read(uri, new FileHandle()); readFile = handle.get(); expectedContent = convertFileToString(readFile); gmWriter.delete(uri, trxDelIn); trxDelIn.commit(); trxDelIn = null;
@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\"}]}}")); }
@Test public void testListUris_readUser() throws Exception { Exception exp = null; String ntriple5 = "<http://example.org/s5> <http://example.com/p2> <http://example.org/o2> ."; gmWriter.write("htp://test.sem.graph/G1", new StringHandle(ntriple5).withMimetype("application/n-triples")); Iterator<String> iter = gmReader.listGraphUris(); while (iter.hasNext()) { String uri = iter.next(); assertNotNull(uri); assertNotEquals("", uri); System.out.println("DEBUG: [GraphsTest] uri =[" + uri + "]"); } try { gmWriter.delete("htp://test.sem.graph/G1"); gmWriter.read("htp://test.sem.graph/G1", new StringHandle()); } catch (Exception e) { exp = e; } assertTrue("Read after detele did not throw expected Exception, Received ::" + exp, exp.toString().contains("Could not read resource at graphs.")); // Delete non existing graph try { gmWriter.delete("htp://test.sem.graph/G1"); } catch (Exception e) { exp = e; } assertTrue( "Deleting non-existing Graph did not throw expected Exception:: http://bugtrack.marklogic.com/35064 , Received ::" + exp, exp.toString().contains("Could not delete resource at graphs")); }
@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 testMergeAs_jsonxml_FileHandle() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.RDFJSON); File file = new File(datasource + "bug25348.json"); FileInputStream fis = new FileInputStream(file); Object graphData = convertInputStreamToString(fis); fis.close(); gmWriter.writeAs("htp://test.sem.graph/jsonMergeAs", graphData); gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); File file2 = new File(datasource + "relative5.xml"); Object graphData2 = convertFileToString(file2); gmWriter.mergeAs("htp://test.sem.graph/jsonMergeAs", graphData2); File readFile = gmWriter.readAs("htp://test.sem.graph/jsonMergeAs", File.class); 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 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 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 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")); } }
GraphManager gmTestPerm = permUser.newGraphManager(); GraphPermissions perms = gmTestPerm.permission("test-perm", Capability.UPDATE); gmTestPerm.write(uri, handle.withMimetype(RDFMimeTypes.RDFXML), trx); trx.commit(); gmTestPerm.mergePermissions(uri, perms, trx); GraphPermissions perm = gmTestPerm.getPermissions(uri); System.out.println("OUTSIDE TRX , SHOULD NOT SEE test-perm EXECUTE" + perm); assertNull(perm.get("test-perm")); perms = gmTestPerm.getPermissions(uri, trx); assertTrue("Permission within trx should have Update capability", perms.get("test-perm").contains(Capability.UPDATE)); trx.rollback(); trx = null; perms = gmTestPerm.getPermissions(uri); assertNull(perm.get("test-perm")); } catch (Exception e) {