From 6c9c59a1229002b5a97e8ec6d117702bf765cbf6 Mon Sep 17 00:00:00 2001 From: Aaron Coburn Date: Sun, 10 Feb 2019 19:39:19 -0500 Subject: [PATCH] Add a no-op AccessControlService (#355) Resolves #354 --- .../api/NoopAccessControlService.java | 40 +++++++++++++++++ .../api/NoopAccessControlServiceTest.java | 45 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 core/api/src/main/java/org/trellisldp/api/NoopAccessControlService.java create mode 100644 core/api/src/test/java/org/trellisldp/api/NoopAccessControlServiceTest.java diff --git a/core/api/src/main/java/org/trellisldp/api/NoopAccessControlService.java b/core/api/src/main/java/org/trellisldp/api/NoopAccessControlService.java new file mode 100644 index 000000000..62c59ca45 --- /dev/null +++ b/core/api/src/main/java/org/trellisldp/api/NoopAccessControlService.java @@ -0,0 +1,40 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.trellisldp.api; + +import static java.util.Arrays.asList; +import static java.util.Collections.unmodifiableSet; +import static java.util.stream.Collectors.toSet; +import static org.trellisldp.api.TrellisUtils.getInstance; + +import java.util.Set; + +import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.RDF; + +/** + * A no-op AccessControlService implementation. + */ +public class NoopAccessControlService implements AccessControlService { + + private static final RDF rdf = getInstance(); + private static final String URI = "http://www.w3.org/ns/auth/acl#"; + private static final Set modes = unmodifiableSet(asList("Control", "Read", "Write", "Append") + .stream().map(mode -> URI + mode).map(rdf::createIRI).collect(toSet())); + + @Override + public Set getAccessModes(final IRI identifier, final Session session) { + return modes; + } +} diff --git a/core/api/src/test/java/org/trellisldp/api/NoopAccessControlServiceTest.java b/core/api/src/test/java/org/trellisldp/api/NoopAccessControlServiceTest.java new file mode 100644 index 000000000..0fb852b59 --- /dev/null +++ b/core/api/src/test/java/org/trellisldp/api/NoopAccessControlServiceTest.java @@ -0,0 +1,45 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.trellisldp.api; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.MockitoAnnotations.initMocks; + +import org.apache.commons.rdf.api.IRI; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.trellisldp.vocabulary.ACL; + +public class NoopAccessControlServiceTest { + + @Mock + private Session mockSession; + + @BeforeEach + public void setUp() { + initMocks(this); + } + + @Test + public void testAccessControl() { + final IRI resource = TrellisUtils.getInstance().createIRI("trellis:data/resource"); + final AccessControlService svc = new NoopAccessControlService(); + assertTrue(svc.getAccessModes(resource, mockSession).contains(ACL.Control)); + assertTrue(svc.getAccessModes(resource, mockSession).contains(ACL.Read)); + assertTrue(svc.getAccessModes(resource, mockSession).contains(ACL.Write)); + assertTrue(svc.getAccessModes(resource, mockSession).contains(ACL.Append)); + } +}