public void deleteGraph(String uri) { this.graphManager.delete(uri, currentTransaction); }
@AfterClass public static void afterClass() { Common.connect(); gmgr = Common.client.newGraphManager(); gmgr.delete(GraphManager.DEFAULT_GRAPH); gmgr.delete("http://marklogic.com/SPARQLQDefTest/g1"); gmgr.delete("http://marklogic.com/SPARQLQDefTest/g2"); gmgr.delete("http://marklogic.com/SPARQLQDefTest/g4"); gmgr.delete("http://marklogic.com/SPARQLQDefTest/g65"); gmgr.delete("http://marklogic.com/SPARQLQDefTest/o1"); gmgr.delete("http://marklogic.com/SPARQLQDefTest/o2"); gmgr.delete("http://marklogic.com/SPARQLQDefTest/o3"); gmgr.delete("http://marklogic.com/SPARQLQDefTest/o4"); }
@AfterClass public static void afterClass() { gmgr.delete(graphUri); }
@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); }
@AfterClass public static void afterClass() { GraphManager gmgr = Common.client.newGraphManager(); gmgr.delete(graphUri); readPrivilegedClient.release(); writePrivilegedClient.release(); }
@Test public void testWriteTriples_DeletereadUser() throws Exception { Exception exp = null; boolean exceptionThrown = false; String ntriple5 = "<http://example.org/s44> <http://example.com/p44> <http://example.org/o44> ."; try { gmWriter.write("htp://test.sem.graph/G4", new StringHandle(ntriple5).withMimetype("application/n-triples")); gmReader.delete("htp://test.sem.graph/G4"); } catch (Exception e) { exp = e; exceptionThrown = true; } if (exceptionThrown) assertTrue( "Unexpected Error Message, \n Expecting:: com.marklogic.client.ForbiddenUserException: Local message: User is not allowed to delete resource at graphs. Server Message: " + "You do not have permission to this method and URL \n BUT Received :: " + exp.toString(), exp.toString() .contains( "com.marklogic.client.ForbiddenUserException: Local message: User is not allowed to delete resource at graphs. Server Message: " + "You do not have permission to this method and URL")); assertTrue("Expected Exception but Received NullPointer", exceptionThrown); }
@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 testThings_fileNomatch() 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)); Exception exp = null; try { TriplesReadHandle things = gmWriter.things(new StringHandle(), "noMatch"); assertTrue("Things did not return expected Uri's", things == null); } catch (Exception e) { exp = e; } assertTrue(exp.toString().contains("ResourceNotFoundException")); gmWriter.delete(tripleGraphUri); }
@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 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 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; } } }
assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://www.w3.org/2004/02/skos/core#Concept")); gmWriter.delete("htp://test.sem.graph/ttlas", trx); trx.commit();
@Test public void G_testSPARQLInsertPermissions() throws Exception { String localGraphUri = graphUri + ".SPARQLPermissions"; String sparql = "INSERT DATA { GRAPH <" + localGraphUri + "> { <s2> <p2> <o2> } }"; SPARQLQueryManager sparqlMgr = Common.client.newSPARQLQueryManager(); SPARQLQueryDefinition qdef = sparqlMgr.newQueryDefinition(sparql) .withUpdatePermission("write-privileged", Capability.READ) .withUpdatePermission("write-privileged", Capability.UPDATE); sparqlMgr.executeUpdate(qdef); GraphPermissions getPermissions = gmgr.getPermissions(localGraphUri); assertEquals(5, getPermissions.size()); assertNotNull(getPermissions.get("write-privileged")); assertEquals(2, getPermissions.get("write-privileged").size()); for ( Capability capability : getPermissions.get("write-privileged") ) { if ( capability == null ) fail("capability should not be null"); if ( capability != Capability.READ && capability != Capability.UPDATE ) { fail("capabilities should be read or update, not [" + capability + "]"); } } gmgr.delete(localGraphUri); } }
@Test public void testInference() throws Exception { gmgr.write("/ontology", new StringHandle(ontology).withMimetype("application/n-triples")); SPARQLQueryDefinition qdef = smgr.newQueryDefinition( "SELECT ?s { ?s a <http://example.org/C1> }"); qdef.setIncludeDefaultRulesets(false); StringHandle handle = new StringHandle().withMimetype(SPARQLMimeTypes.SPARQL_CSV); String results = smgr.executeSelect(qdef, handle).get(); assertEquals("%0D%0A", URLEncoder.encode(results, "utf8")); qdef.setRulesets(SPARQLRuleset.RANGE); results = smgr.executeSelect(qdef, handle).get(); assertEquals(1, countLines(parseCsv(results))); qdef.setRulesets(SPARQLRuleset.RANGE, SPARQLRuleset.DOMAIN); results = smgr.executeSelect(qdef, handle).get(); MappingIterator<Map<String,String>> csvRows = parseCsv(results); assertTrue(csvRows.hasNext()); Map<String,String> row = csvRows.next(); assertEquals("http://example.org/o1", row.get("s")); assertTrue(csvRows.hasNext()); row = csvRows.next(); assertEquals("http://example.org/s2", row.get("s")); assertFalse(csvRows.hasNext()); gmgr.delete("/ontology"); }
@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); }
assertEquals(ntriple7 + "\n" + ntriple8, triples7and8); gmgr.delete(tripleGraphUri);
gmgr.delete(quadGraphUri, tx);
graphManagerWriter.delete( "newGraph"); } catch (Exception e) {