Search in sources :

Example 6 with AccessDeniedException

use of org.apache.jena.shared.AccessDeniedException in project jena by apache.

the class SecuredRDFListTest method testSetHead.

@Test
public void testSetHead() {
    try {
        RDFNode result = getSecuredRDFList().setHead(newResource1);
        if (!shouldRead() || !securityEvaluator.evaluate(Action.Update)) {
            fail("Should have thrown AccessDeniedException");
        }
        assertEquals(resource1, result);
        List<RDFNode> lst = ((RDFList) getBaseRDFNode()).asJavaList();
        Iterator<RDFNode> iter = lst.iterator();
        assertEquals(newResource1, iter.next());
        assertEquals(resource2, iter.next());
        assertEquals(resource3, iter.next());
        assertEquals(resource4, iter.next());
        assertFalse(iter.hasNext());
    } catch (final AccessDeniedException e) {
        if (shouldRead() && securityEvaluator.evaluate(Action.Update) && securityEvaluator.evaluate(Action.Delete)) {
            fail("Should not have thrown AccessDeniedException");
        }
    } catch (final EmptyListException e) {
        if (securityEvaluator.evaluate(Action.Read)) {
            fail("Should not have thrown EmptyListException");
        }
    }
}
Also used : AccessDeniedException(org.apache.jena.shared.AccessDeniedException) RDFList(org.apache.jena.rdf.model.RDFList) EmptyListException(org.apache.jena.rdf.model.EmptyListException) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Example 7 with AccessDeniedException

use of org.apache.jena.shared.AccessDeniedException in project jena by apache.

the class SecuredResourceTest method testAddProperty.

@Test
public void testAddProperty() {
    final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { Action.Update, Action.Create });
    final RDFNode rdfNode = ResourceFactory.createResource("http://example.com/newResource");
    try {
        getSecuredResource().addLiteral(SecuredRDFNodeTest.p, rdfNode);
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    }
    try {
        getSecuredResource().addLiteral(SecuredRDFNodeTest.p, "string");
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    }
    final Literal l = ResourceFactory.createTypedLiteral(3.14F);
    try {
        getSecuredResource().addProperty(SecuredRDFNodeTest.p, l.getLexicalForm(), l.getDatatype());
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    }
    try {
        getSecuredResource().addProperty(SecuredRDFNodeTest.p, "dos", "sp");
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    }
}
Also used : Action(org.apache.jena.permissions.SecurityEvaluator.Action) AccessDeniedException(org.apache.jena.shared.AccessDeniedException) Literal(org.apache.jena.rdf.model.Literal) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Example 8 with AccessDeniedException

use of org.apache.jena.shared.AccessDeniedException in project jena by apache.

the class SecuredStatementTest method testCreateReifiedStatement.

private void testCreateReifiedStatement(Supplier<ReifiedStatement> supplier, Resource expected) {
    final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { Action.Update, Action.Create });
    try {
        SecuredReifiedStatement rs = (SecuredReifiedStatement) supplier.get();
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
        ReifiedStatement actual = (ReifiedStatement) rs.getBaseItem();
        if (expected == null) {
            assertTrue(actual.isAnon());
        } else {
            assertEquals(expected, actual);
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail("Should not have thrown AccessDeniedException Exception");
        }
    }
}
Also used : ReifiedStatement(org.apache.jena.rdf.model.ReifiedStatement) Action(org.apache.jena.permissions.SecurityEvaluator.Action) AccessDeniedException(org.apache.jena.shared.AccessDeniedException)

Example 9 with AccessDeniedException

use of org.apache.jena.shared.AccessDeniedException in project jena by apache.

the class SecuredModelTest method testAsStatement_Exists.

@Test
public void testAsStatement_Exists() {
    Triple t = new Triple(s.asNode(), p.asNode(), o.asNode());
    try {
        Statement stmt = securedModel.asStatement(t);
        assertEquals(securedModel, stmt.getModel());
        if (securedModel.canRead(t)) {
            assertEquals(s, stmt.getSubject());
            assertEquals(p, stmt.getPredicate());
            assertEquals(o, stmt.getObject());
        }
        if (!securityEvaluator.evaluate(Action.Read) && !securityEvaluator.evaluate(Action.Update)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(Action.Read) && securityEvaluator.evaluate(Action.Update)) {
            Assert.fail(String.format("Should not have thrown ReadDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    }
}
Also used : Triple(org.apache.jena.graph.Triple) AccessDeniedException(org.apache.jena.shared.AccessDeniedException) ReifiedStatement(org.apache.jena.rdf.model.ReifiedStatement) Statement(org.apache.jena.rdf.model.Statement) Test(org.junit.Test) SecuredPrefixMappingTest(org.apache.jena.permissions.graph.SecuredPrefixMappingTest)

Example 10 with AccessDeniedException

use of org.apache.jena.shared.AccessDeniedException in project jena by apache.

the class SecuredRDFListTest method testAppendRDFList.

@Test
public void testAppendRDFList() {
    final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { Action.Update, Action.Create });
    Model m = ModelFactory.createDefaultModel();
    RDFList lst = m.createList(newResource1, newResource2, newResource3, newResource4);
    try {
        SecuredRDFList actual = (SecuredRDFList) getSecuredRDFList().append(lst);
        if (!securityEvaluator.evaluate(perms) || !shouldRead()) {
            fail("Should have thrown AccessDeniedException");
        }
        Iterator<RDFNode> iter = ((RDFList) actual.getBaseItem()).iterator();
        if (securityEvaluator.evaluate(Action.Read)) {
            assertEquals(resource1, iter.next());
            assertEquals(resource2, iter.next());
            assertEquals(resource3, iter.next());
            assertEquals(resource4, iter.next());
        }
        assertEquals(newResource1, iter.next());
        assertEquals(newResource2, iter.next());
        assertEquals(newResource3, iter.next());
        assertEquals(newResource4, iter.next());
        assertFalse(iter.hasNext());
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms) && shouldRead()) {
            fail("Should not have thrown AccessDeniedException");
        }
    }
}
Also used : Action(org.apache.jena.permissions.SecurityEvaluator.Action) AccessDeniedException(org.apache.jena.shared.AccessDeniedException) RDFList(org.apache.jena.rdf.model.RDFList) Model(org.apache.jena.rdf.model.Model) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Aggregations

AccessDeniedException (org.apache.jena.shared.AccessDeniedException)13 Test (org.junit.Test)11 Action (org.apache.jena.permissions.SecurityEvaluator.Action)8 RDFNode (org.apache.jena.rdf.model.RDFNode)7 RDFList (org.apache.jena.rdf.model.RDFList)4 SecuredPrefixMappingTest (org.apache.jena.permissions.graph.SecuredPrefixMappingTest)3 Literal (org.apache.jena.rdf.model.Literal)3 ReifiedStatement (org.apache.jena.rdf.model.ReifiedStatement)3 Statement (org.apache.jena.rdf.model.Statement)3 Triple (org.apache.jena.graph.Triple)2 Container (org.apache.jena.rdf.model.Container)2 EmptyListException (org.apache.jena.rdf.model.EmptyListException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 ListIndexException (org.apache.jena.rdf.model.ListIndexException)1 Model (org.apache.jena.rdf.model.Model)1