Navigation Menu

Skip to content

Commit

Permalink
Add XACMLWorkspaceInitializerTest
Browse files Browse the repository at this point in the history
- Replace “ and ” quote characters with default " character

Resovles: https://www.pivotaltracker.com/story/show/73580804
  • Loading branch information
mohideen authored and Andrew Woods committed Jul 9, 2014
1 parent ff1f836 commit 1e12d5b
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
147 changes: 147 additions & 0 deletions src/test/java/org/fcrepo/auth/xacml/XACMLWorkspaceInitializerTest.java
@@ -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
@@ -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

0 comments on commit 1e12d5b

Please sign in to comment.