use of org.apache.jena.permissions.SecurityEvaluator in project jena by apache.
the class QueryEngineTest method testOpenQueryType.
@Test
public void testOpenQueryType() {
final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, true, true, true, true);
final SecuredModel model = Factory.getInstance(eval, "http://example.com/securedModel", baseModel);
try {
final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#> " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + " } ";
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
final ResultSet results = qexec.execSelect();
int count = 0;
for (; results.hasNext(); ) {
count++;
results.nextSolution();
}
Assert.assertEquals(8, count);
}
} finally {
model.close();
}
}
use of org.apache.jena.permissions.SecurityEvaluator in project jena by apache.
the class QueryEngineTest method testRestrictedQueryType.
@Test
public void testRestrictedQueryType() {
final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, true, true, true, true) {
@Override
public boolean evaluate(final Object principal, final Action action, final Node graphIRI, final Triple triple) {
if (triple.getSubject().isURI() && triple.getSubject().getURI().equals("http://example.com/resource/1")) {
return false;
}
return super.evaluate(principal, action, graphIRI, triple);
}
};
final SecuredModel model = Factory.getInstance(eval, "http://example.com/securedModel", baseModel);
try {
final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#> " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + " } ";
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
final ResultSet results = qexec.execSelect();
int count = 0;
for (; results.hasNext(); ) {
count++;
results.nextSolution();
}
Assert.assertEquals(4, count);
}
} finally {
model.close();
}
}
use of org.apache.jena.permissions.SecurityEvaluator in project jena by apache.
the class OpRewriterTest method testBGP.
@Test
public void testBGP() {
SecurityEvaluator securityEvaluator = new MockSecurityEvaluator(true, true, true, true, true, true);
rewriter = new OpRewriter(securityEvaluator, "http://example.com/dummy");
rewriter.visit(new OpBGP(BasicPattern.wrap(Arrays.asList(triples))));
Op op = rewriter.getResult();
Assert.assertTrue("Should have been an OpFilter", op instanceof OpFilter);
OpFilter filter = (OpFilter) op;
ExprList eLst = filter.getExprs();
Assert.assertEquals(1, eLst.size());
Assert.assertTrue("Should have been a SecuredFunction", eLst.get(0) instanceof SecuredFunction);
op = filter.getSubOp();
Assert.assertTrue("Should have been a OpBGP", op instanceof OpBGP);
BasicPattern basicPattern = ((OpBGP) op).getPattern();
Assert.assertEquals(3, basicPattern.size());
Triple t = basicPattern.get(0);
Assert.assertEquals(NodeFactory.createVariable("foo"), t.getSubject());
Assert.assertEquals(RDF.type.asNode(), t.getPredicate());
Assert.assertEquals(NodeFactory.createURI("http://example.com/class"), t.getObject());
t = basicPattern.get(1);
Assert.assertEquals(NodeFactory.createVariable("foo"), t.getSubject());
Assert.assertTrue("Should have been blank", t.getPredicate().isBlank());
Assert.assertEquals(NodeFactory.createVariable("bar"), t.getObject());
t = basicPattern.get(2);
Assert.assertEquals(NodeFactory.createVariable("bar"), t.getSubject());
Assert.assertTrue("Should have been blank", t.getPredicate().isBlank());
Assert.assertEquals(NodeFactory.createVariable("baz"), t.getObject());
}
use of org.apache.jena.permissions.SecurityEvaluator in project jena by apache.
the class QueryEngineTest method testSelectAllType.
@Test
public void testSelectAllType() {
final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, true, true, true, true) {
@Override
public boolean evaluate(Object principal, final Action action, final Node graphIRI, final Triple triple) {
if (triple.getSubject().isURI() && triple.getSubject().getURI().equals("http://example.com/resource/1")) {
return false;
}
return super.evaluate(principal, action, graphIRI, triple);
}
};
final SecuredModel model = Factory.getInstance(eval, "http://example.com/securedModel", baseModel);
try {
String query = "SELECT ?s ?p ?o WHERE " + " { ?s ?p ?o } ";
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
final ResultSet results = qexec.execSelect();
int count = 0;
for (; results.hasNext(); ) {
count++;
results.nextSolution();
}
// 2x 3 values + type triple
Assert.assertEquals(8, count);
}
query = "SELECT ?s ?p ?o WHERE " + " { GRAPH ?g {?s ?p ?o } }";
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
final ResultSet results = qexec.execSelect();
int count = 0;
for (; results.hasNext(); ) {
count++;
results.nextSolution();
}
// 2x 3 values + type triple
// no named graphs so no results.
Assert.assertEquals(0, count);
}
} finally {
model.close();
}
}
use of org.apache.jena.permissions.SecurityEvaluator in project jena by apache.
the class OpRewriterTest method testBGPNoReadAccess.
@Test
public void testBGPNoReadAccess() {
SecurityEvaluator securityEvaluator = new MockSecurityEvaluator(true, true, false, true, true, true);
rewriter = new OpRewriter(securityEvaluator, "http://example.com/dummy");
Triple[] triples = { new Triple(NodeFactory.createVariable("foo"), RDF.type.asNode(), NodeFactory.createURI("http://example.com/class")), new Triple(NodeFactory.createVariable("foo"), NodeFactory.createBlankNode(), NodeFactory.createVariable("bar")), new Triple(NodeFactory.createVariable("bar"), NodeFactory.createBlankNode(), NodeFactory.createVariable("baz")) };
try {
rewriter.visit(new OpBGP(BasicPattern.wrap(Arrays.asList(triples))));
Assert.fail("Should have thrown AccessDeniedException");
} catch (ReadDeniedException e) {
// expected
}
}
Aggregations