Search in sources :

Example 1 with SecuredModel

use of org.apache.jena.permissions.model.SecuredModel 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();
    }
}
Also used : MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) ResultSet(org.apache.jena.query.ResultSet) SecurityEvaluator(org.apache.jena.permissions.SecurityEvaluator) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) SecuredModel(org.apache.jena.permissions.model.SecuredModel) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 2 with SecuredModel

use of org.apache.jena.permissions.model.SecuredModel 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();
    }
}
Also used : Triple(org.apache.jena.graph.Triple) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) Node(org.apache.jena.graph.Node) ResultSet(org.apache.jena.query.ResultSet) SecurityEvaluator(org.apache.jena.permissions.SecurityEvaluator) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) SecuredModel(org.apache.jena.permissions.model.SecuredModel) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 3 with SecuredModel

use of org.apache.jena.permissions.model.SecuredModel in project jena by apache.

the class SecuredAssemblerTest method testCreation.

@Test
public void testCreation() throws Exception {
    Resource r = model.createResource("http://apache.org/jena/permissions/test#secModel");
    Object o = assembler.open(r);
    Assert.assertTrue(o instanceof Model);
    Assert.assertTrue(o instanceof SecuredModel);
}
Also used : Resource(org.apache.jena.rdf.model.Resource) Model(org.apache.jena.rdf.model.Model) SecuredModel(org.apache.jena.permissions.model.SecuredModel) SecuredModel(org.apache.jena.permissions.model.SecuredModel) Test(org.junit.Test)

Example 4 with SecuredModel

use of org.apache.jena.permissions.model.SecuredModel in project jena by apache.

the class SecuredRDFNodeTest method testInModel.

@Test
public void testInModel() {
    final Model m2 = ModelFactory.createDefaultModel();
    try {
        final RDFNode n2 = securedRDFNode.inModel(m2);
        if (!securityEvaluator.evaluate(Action.Read)) {
            Assert.fail("Should have thrown ReadDeniedException Exception");
        }
        Assert.assertFalse("RDFNode should not have been secured", n2 instanceof SecuredRDFNode);
        Assert.assertEquals("Wrong securedModel returned", n2.getModel(), m2);
    } catch (final ReadDeniedException e) {
        if (securityEvaluator.evaluate(Action.Read)) {
            Assert.fail(String.format("Should not have thrown ReadDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    }
    m2.removeAll();
    final SecuredModel m3 = Factory.getInstance(securityEvaluator, "http://example.com/securedGraph2", m2);
    try {
        final RDFNode n2 = securedRDFNode.inModel(m3);
        if (!securityEvaluator.evaluate(Action.Read)) {
            Assert.fail("Should have thrown ReadDeniedException Exception");
        }
        Assert.assertTrue("RDFNode should have been secured", n2 instanceof SecuredRDFNode);
        Assert.assertEquals("Wrong securedModel returned", n2.getModel(), m3);
    } catch (final ReadDeniedException e) {
        if (securityEvaluator.evaluate(Action.Read)) {
            Assert.fail(String.format("Should not have thrown ReadDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    }
}
Also used : ReadDeniedException(org.apache.jena.shared.ReadDeniedException) SecuredModel(org.apache.jena.permissions.model.SecuredModel) SecuredRDFNode(org.apache.jena.permissions.model.SecuredRDFNode) SecuredModel(org.apache.jena.permissions.model.SecuredModel) SecuredRDFNode(org.apache.jena.permissions.model.SecuredRDFNode) Test(org.junit.Test)

Example 5 with SecuredModel

use of org.apache.jena.permissions.model.SecuredModel in project jena by apache.

the class SecuredRDFNodeTest method testGetModel.

@Test
public void testGetModel() {
    final Model m2 = securedRDFNode.getModel();
    Assert.assertTrue("Model should have been secured", m2 instanceof SecuredModel);
}
Also used : SecuredModel(org.apache.jena.permissions.model.SecuredModel) SecuredModel(org.apache.jena.permissions.model.SecuredModel) Test(org.junit.Test)

Aggregations

SecuredModel (org.apache.jena.permissions.model.SecuredModel)7 Test (org.junit.Test)7 MockSecurityEvaluator (org.apache.jena.permissions.MockSecurityEvaluator)3 SecurityEvaluator (org.apache.jena.permissions.SecurityEvaluator)3 QueryExecution (org.apache.jena.query.QueryExecution)3 ResultSet (org.apache.jena.query.ResultSet)3 Node (org.apache.jena.graph.Node)2 Triple (org.apache.jena.graph.Triple)2 Model (org.apache.jena.rdf.model.Model)2 Resource (org.apache.jena.rdf.model.Resource)2 SecuredRDFNode (org.apache.jena.permissions.model.SecuredRDFNode)1 ReadDeniedException (org.apache.jena.shared.ReadDeniedException)1