Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xacmlworkspaceinitializertest #19

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions src/test/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* Copyright 2014 DuraSpace, Inc.
*
* 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.fcrepo.auth.xacml;

import static org.fcrepo.kernel.utils.TestHelpers.setField;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import java.io.File;
import java.io.FileInputStream;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.services.DatastreamService;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mock;

/**
* <p>
* XACMLWorkspaceInitializerTest class.
* </p>
*
* @author mohideen
*/
public class XACMLWorkspaceInitializerTest {

private XACMLWorkspaceInitializer xacmlWI;

@Mock
private SessionFactory mockSessionFactory;

@Mock
private Session mockSession;

@Mock
private Node mockNode;

@Mock
private DatastreamService mockDsService;

@Mock
Datastream mockDatastream;

@Before
public void setUp() throws Exception {
initMocks(this);

when(mockSessionFactory.getInternalSession()).thenReturn(mockSession);
when(mockSession.getRootNode()).thenReturn(mockNode);
when(
mockDsService.createDatastream(eq(mockSession), anyString(), eq("application/xml"), anyString(),
Matchers.any(FileInputStream.class))).thenReturn(mockDatastream);
when(mockDatastream.getPath()).thenReturn("/dummy/test/path");

final File initialPoliciesDirectory = policiesDirectory();
final File initialRootPolicyFile = rootPolicyFile();
xacmlWI = new XACMLWorkspaceInitializer(initialPoliciesDirectory, initialRootPolicyFile);

setField(xacmlWI, "sessionFactory", mockSessionFactory);
setField(xacmlWI, "datastreamService", mockDsService);
}

private File policiesDirectory() {
return new File(this.getClass().getResource("/xacml").getPath());
}

private File rootPolicyFile() {
return new File(this.getClass().getResource("/xacml/testPolicy.xml").getPath());
}

@Test
public void testConstructor() {
assertNotNull(xacmlWI);
}

@Test(expected = IllegalArgumentException.class)
public void testConstructorIllegalArg0() {
xacmlWI = new XACMLWorkspaceInitializer(null, rootPolicyFile());
}

@Test(expected = IllegalArgumentException.class)
public void testConstructorIllegalArg1() {
xacmlWI = new XACMLWorkspaceInitializer(policiesDirectory(), null);
}

@Test(expected = IllegalArgumentException.class)
public void testConstructorEmptyDir() {
final File emptyPoliciesDirectory = new File(this.getClass().getResource("/web.xml").getPath());
xacmlWI = new XACMLWorkspaceInitializer(emptyPoliciesDirectory, rootPolicyFile());
}

@Test
public void testInit() throws Exception {
xacmlWI.init();

final int expectedFiles = policiesDirectory().list().length;
verify(mockDsService, times(expectedFiles)).createDatastream(eq(mockSession),
anyString(),
eq("application/xml"),
anyString(),
Matchers.any(FileInputStream.class));

verify(mockNode).addMixin("authz:xacmlAssignable");
verify(mockNode).setProperty(eq("authz:policy"), any(Node.class));
}

@Test(expected = Error.class)
public void testInitInitialPoliciesException() throws Exception {
when(mockSessionFactory.getInternalSession()).thenThrow(new RepositoryException("expected"));

xacmlWI.init();
}

@Test(expected = Error.class)
public void testInitLinkRootToPolicyException() throws Exception {
when(mockSession.getRootNode()).thenThrow(new RepositoryException("expected"));

xacmlWI.init();
}

}
2 changes: 1 addition & 1 deletion src/test/resources/xacml/adminPermissionPolicySet.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<PolicySet xmlns="urn:oasis:names:tc:xacml:2.0:policy:schema:os"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:fcrepo="http://fedora.info/definitions/v4/repository#"
xsi:schemaLocation = “urn:oasis:names:tc:xacml:2.0:policy:schema:os access_control-xacml-2.0-policyschema-os.xsd
xsi:schemaLocation="urn:oasis:names:tc:xacml:2.0:policy:schema:os access_control-xacml-2.0-policyschema-os.xsd"
PolicySetId="fcrepo:policies/AdminPermissionPolicySet"
PolicyCombiningAlgId="urn:oasis:names:tc:xacml:1.0:policy-combining-algorithm:permit-overrides">
<Policy PolicyId="fcrepo-xacml:AdminPermissionPolicy" RuleCombiningAlgId="urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:first-applicable">
Expand Down