public static void writeSPARQLDataFromString(String content, String graphName) throws KeyManagementException, NoSuchAlgorithmException, Exception { // Create Grapgh manager GraphManager sparqlGmgr = writeclient.newGraphManager(); StringHandle handle = new StringHandle(); handle.set(content.toString()); // write the triples in the doc into named graph sparqlGmgr.write(graphName, handle.withMimetype(RDFMimeTypes.TURTLE)); // Wait for index. Need a better way to handle this. try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.print("Exception from writeSPARQLDataFromString " + e.getMessage()); e.printStackTrace(); } }
@Test public void testWriteTriples_ReadUser() throws Exception { Exception exp = null; String ntriple5 = "<http://example.org/s33> <http://example.com/p33> <http://example.org/o33> ."; try { gmReader.write("htp://test.sem.graph/G3", new StringHandle(ntriple5).withMimetype("application/n-triples")); } catch (Exception e) { exp = e; } assertTrue( "Unexpected Error Message, \n Expecting:: com.marklogic.client.ForbiddenUserException: Local message: User is not allowed to write 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 write resource at graphs. Server Message: " + "You do not have permission to this method and URL")); }
@Test public void testListUris_writeUser() throws Exception { String ntriple5 = "<http://example.org/s22> <http://example.com/p22> <http://example.org/o22> ."; gmWriter.write("htp://test.sem.graph/G2", new StringHandle(ntriple5) .withMimetype("application/n-triples")); try { Iterator<String> iter = gmWriter.listGraphUris(); while (iter.hasNext()) { String uri = iter.next(); assertNotNull(uri); assertNotEquals("", uri); System.out.println("DEBUG: [GraphsTest] uri =[" + uri + "]"); } } catch (Exception e) { } }
@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 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")); }
public void writeXSLTransform(TransformExtensionsManager extensionMgr) throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException { extensionMgr.writeXSLTransform( XSLT_NAME, new StringHandle().withFormat(Format.XML).withMimetype("application/xslt+xml").with(xslTransform), makeXSLTMetadata() ); } }
@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 B_testWritePermission() throws Exception { // a negative test to ensure a user without update privilege can't write try { GraphManager readPrivilegedGmgr = readPrivilegedClient.newGraphManager(); readPrivilegedGmgr.writeAs(graphUri, new StringHandle("").withMimetype(RDFMimeTypes.NTRIPLES)); fail("User read-privileged should see ForbiddenUserException when trying to write"); } catch (ForbiddenUserException e) { // passed negative test } }
@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 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")); }
@BeforeClass public static void beforeClass() { Common.connect(); gmgr = Common.client.newGraphManager(); gmgr.mergeGraphs(new StringHandle(TEST_TRIG) .withMimetype("text/trig")); smgr = Common.client.newSPARQLQueryManager(); }
@BeforeClass public static void beforeClass() { Common.connect(); gmgr = Common.client.newGraphManager(); String triple = "<s> <p> <o>."; GraphPermissions perms = gmgr.permission("read-privileged", Capability.READ) .permission("write-privileged", Capability.UPDATE); gmgr.write(graphUri, new StringHandle(triple).withMimetype(RDFMimeTypes.NTRIPLES), perms); }
@Test public void testTuples() { String str = head + lexiconOptions + tail; RawCombinedQueryDefinition rawCombinedQueryDefinition; rawCombinedQueryDefinition = queryMgr .newRawCombinedQueryDefinition(new StringHandle(str).withMimetype("application/xml")); StringHandle stringResults = null; ValuesDefinition vdef = queryMgr.newValuesDefinition("n-way"); vdef.setQueryDefinition(rawCombinedQueryDefinition); stringResults = queryMgr.tuples(vdef, new StringHandle()); System.out.println(stringResults.get()); TuplesHandle tuplesResults = queryMgr.tuples(vdef, new TuplesHandle()); Tuple[] tuples = tuplesResults.getTuples(); assertNotNull(tuples); }
@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; } } }
@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 testValues() { String str = head + lexiconOptions + tail; RawCombinedQueryDefinition rawCombinedQueryDefinition; rawCombinedQueryDefinition = queryMgr .newRawCombinedQueryDefinition(new StringHandle(str).withMimetype("application/xml")); StringHandle stringResults = null; ValuesDefinition vdef = queryMgr.newValuesDefinition("grandchild"); vdef.setQueryDefinition(rawCombinedQueryDefinition); stringResults = queryMgr.tuples(vdef, new StringHandle()); System.out.println(stringResults.get()); ValuesHandle valuesResults = queryMgr.values(vdef, new ValuesHandle()); assertFalse(valuesResults.getMetrics().getTotalTime() == -1); CountedDistinctValue[] values = valuesResults.getValues(); assertNotNull(values); }
@Test public void testConvertedSearches() throws IOException { // Structured Query, No Options StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(null); StructuredQueryDefinition t = qb.term("leaf3"); check(new StringHandle(head + t.serialize() + returnQueryOption + tail)); // String query no options String str = head + qtext1 + returnQueryOption + tail; check(new StringHandle(str).withMimetype("application/xml")); // String query plus options str = head + qtext2 + optionsString + tail; check(new StringHandle(str)); // Structured query plus options str = head + t.serialize() + optionsString + tail; check(new StringHandle(str)); // Structured query plus options str = head + t.serialize() + optionsString + tail; check(new StringHandle(str), "alerts"); // All three str = head + qtext3 + t.serialize() + optionsString + tail; check(new StringHandle(str)); }
@Test public void testCombinedSearches() throws IOException { // Structured Query, No Options StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(null); StructuredQueryDefinition t = qb.term("leaf3"); check(new StringHandle(head + t.serialize() + tail)); // String query no options String str = head + qtext1 + tail; check(new StringHandle(str).withMimetype("application/xml")); // String query plus options str = head + qtext2 + optionsString + tail; check(new StringHandle(str)); // Structured query plus options str = head + t.serialize() + optionsString + tail; check(new StringHandle(str)); // Structured query plus options str = head + t.serialize() + optionsString + tail; check(new StringHandle(str), "alerts"); // All three str = head + qtext3 + t.serialize() + optionsString + tail; check(new StringHandle(str)); }
@Test public void testFailedSearch() throws IOException { StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.term("criteriaThatShouldNotMatchAnyDocument"); RawCombinedQueryDefinition queryDef = queryMgr.newRawCombinedQueryDefinition( new StringHandle(head + t.serialize() + tail) .withMimetype("application/xml") ); SearchHandle results = queryMgr.search(queryDef, new SearchHandle()); assertNotNull(results); MatchDocumentSummary[] summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); StringHandle criteria = new StringHandle().withFormat(Format.XML); criteria.set("<q:query xmlns:q='" + RawQueryByExampleDefinition.QBE_NS + "'>" + "<q:word>criteriaThatShouldNotMatchAnyDocument</q:word>" + "</q:query>"); RawQueryByExampleDefinition qbe = queryMgr.newRawQueryByExampleDefinition(criteria); results = queryMgr.search(qbe, new SearchHandle()); assertNotNull(results); summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); }