Search in sources :

Example 1 with AccessDeniedException

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

the class SecuredModelTest method testAsStatement_NotExists.

@Test
public void testAsStatement_NotExists() {
    // check a triple that does not exist -- must have update to add it.
    Triple t = new Triple(s.asNode(), p2.asNode(), o.asNode());
    try {
        Statement stmt = securedModel.asStatement(t);
        assertEquals(securedModel, stmt.getModel());
        if (securedModel.canRead(t)) {
            assertEquals(s, stmt.getSubject());
            assertEquals(p2, 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 (securedModel.canUpdate() && securedModel.canCreate(t)) {
            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 2 with AccessDeniedException

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

the class SecuredModelTest method testReadEmpty.

@Test
public void testReadEmpty() throws Exception {
    final Set<Action> createAndUpdate = SecurityEvaluator.Util.asSet(new Action[] { Action.Update, Action.Create });
    final String XML_INPUT = "<rdf:RDF" + "   xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' " + "   xmlns:rt='http://example.com/readTest#' " + "   xmlns:j.0='http://example.com/readTest#3' > " + "  <rdf:Description rdf:about='http://example.com/readTest#1'> " + "    <rdf:type rdf:resource='http://example.com/readTest#3'/>" + "  </rdf:Description>" + "</rdf:RDF>";
    final String TTL_INPUT = "@prefix rt: <http://example.com/readTest#> . rt:1 a rt:3 .";
    final String base = "http://example.com/test";
    final String lang = "TURTLE";
    try {
        final URL url = SecuredModelTest.class.getResource("./test.xml");
        securedModel.read(url.toString());
        if (!securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    } finally {
        baseModel.removeAll();
    }
    try {
        final InputStream in = new ByteArrayInputStream(XML_INPUT.getBytes());
        securedModel.read(in, base);
        if (!securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    } finally {
        baseModel.removeAll();
    }
    try {
        final Reader reader = new StringReader(XML_INPUT);
        securedModel.read(reader, base);
        if (!securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    } finally {
        baseModel.removeAll();
    }
    try {
        final URL url = SecuredModelTest.class.getResource("./test.ttl");
        securedModel.read(url.toString(), lang);
        if (!securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    } finally {
        baseModel.removeAll();
    }
    try {
        final InputStream in = new ByteArrayInputStream(TTL_INPUT.getBytes());
        securedModel.read(in, base, lang);
        if (!securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    } finally {
        baseModel.removeAll();
    }
    try {
        final Reader reader = new StringReader(TTL_INPUT);
        securedModel.read(reader, base, lang);
        if (!securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    } finally {
        baseModel.removeAll();
    }
    try {
        final URL url = SecuredModelTest.class.getResource("./test.ttl");
        securedModel.read(url.toString(), base, lang);
        if (!securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail("Should have thrown AccessDeniedException Exception");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(createAndUpdate)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    } finally {
        baseModel.removeAll();
    }
}
Also used : Action(org.apache.jena.permissions.SecurityEvaluator.Action) AccessDeniedException(org.apache.jena.shared.AccessDeniedException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) URL(java.net.URL) Test(org.junit.Test) SecuredPrefixMappingTest(org.apache.jena.permissions.graph.SecuredPrefixMappingTest)

Example 3 with AccessDeniedException

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

the class SecuredAltTest method testSetDefaultObject.

@Test
public void testSetDefaultObject() {
    Object o = 2;
    Literal expected = ResourceFactory.createTypedLiteral(o);
    final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { Action.Update, Action.Create });
    try {
        getSecuredAlt().setDefault(o);
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException");
        }
        RDFNode actual = alt.getDefault();
        assertEquals(expected, actual);
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    }
    try {
        getSecuredAlt().setDefault(o);
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException on update");
        }
        RDFNode actual = alt.getDefault();
        assertEquals(expected, actual);
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail("Should not have thrown AccessDeniedException on Update");
        }
    }
}
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 4 with AccessDeniedException

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

the class SecuredAltTest method testSetDefaultStringAndLang.

@Test
public void testSetDefaultStringAndLang() {
    final Literal expected = ResourceFactory.createLangLiteral("dos", "es");
    final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { Action.Update, Action.Create });
    try {
        getSecuredAlt().setDefault("dos", "es");
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException");
        }
        Literal actual = alt.getDefaultLiteral();
        assertEquals(expected, actual);
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException Exception: %s - %s", e, e.getTriple()));
        }
    }
    try {
        getSecuredAlt().setDefault("dos", "es");
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException on update");
        }
        Literal actual = alt.getDefaultLiteral();
        assertEquals(expected, actual);
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail("Should not have thrown AccessDeniedException on Update");
        }
    }
}
Also used : Action(org.apache.jena.permissions.SecurityEvaluator.Action) AccessDeniedException(org.apache.jena.shared.AccessDeniedException) Literal(org.apache.jena.rdf.model.Literal) Test(org.junit.Test)

Example 5 with AccessDeniedException

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

the class SecuredContainerTest method testRemove.

@Test
public void testRemove() {
    getBaseRDFNode().as(Container.class).add("SomeItem");
    final Set<Action> perms = SecurityEvaluator.Util.asSet(new Action[] { Action.Update, Action.Delete });
    final Statement s = baseModel.listStatements().next();
    try {
        getSecuredContainer().remove(s);
        if (!securityEvaluator.evaluate(perms)) {
            Assert.fail("Should have thrown AccessDeniedException");
        }
    } catch (final AccessDeniedException e) {
        if (securityEvaluator.evaluate(perms)) {
            Assert.fail(String.format("Should not have thrown AccessDeniedException: %s - %s", e, e.getTriple()));
        }
    }
}
Also used : Container(org.apache.jena.rdf.model.Container) Action(org.apache.jena.permissions.SecurityEvaluator.Action) AccessDeniedException(org.apache.jena.shared.AccessDeniedException) Statement(org.apache.jena.rdf.model.Statement) 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