Skip to content

Commit

Permalink
Address impacts from fcrepo DatastreamService API refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Woods committed Sep 22, 2014
1 parent ba0eeee commit 9922d93
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
Expand Up @@ -30,6 +30,7 @@

import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.services.DatastreamService;
import org.fcrepo.kernel.services.NodeService;
Expand Down Expand Up @@ -101,16 +102,16 @@ public final boolean isIdReferenceSupported() {
* @return
*/
private AbstractPolicy getPolicy(final Datastream policyDatastream) {
return loadPolicy(policyDatastream);
return loadPolicy(policyDatastream.getBinary());
}

/**
* Creates a new policy or policy set object from the given policy node
*
* @param policyDatastream
* @param policyBinary
* @return
*/
private AbstractPolicy loadPolicy(final Datastream policyDatastream) {
private AbstractPolicy loadPolicy(final FedoraBinary policyBinary) {
String policyName = "unparsed";
try {
// create the factory
Expand All @@ -122,7 +123,7 @@ private AbstractPolicy loadPolicy(final Datastream policyDatastream) {
final DocumentBuilder db = factory.newDocumentBuilder();

// Parse the policy content
final Document doc = db.parse(policyDatastream.getContent());
final Document doc = db.parse(policyBinary.getContent());

// handle the policy, if it's a known type
final Element root = doc.getDocumentElement();
Expand Down Expand Up @@ -227,7 +228,7 @@ public final PolicyFinderResult findPolicy(final URI idReference,

final String path = PolicyUtil.getPathForId(id);
final Session internalSession = sessionFactory.getInternalSession();
final Datastream policyDatastream = datastreamService.getDatastream(internalSession, path);
final Datastream policyDatastream = datastreamService.findOrCreateDatastream(internalSession, path);
final AbstractPolicy policy = getPolicy(policyDatastream);

return new PolicyFinderResult(policy);
Expand Down
Expand Up @@ -30,7 +30,7 @@
import com.google.common.collect.ImmutableList;
import org.apache.commons.io.FileUtils;
import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.exception.InvalidChecksumException;
import org.fcrepo.kernel.services.DatastreamService;
import org.modeshape.jcr.api.nodetype.NodeTypeManager;
Expand Down Expand Up @@ -154,12 +154,14 @@ private void loadInitialPolicies() {
for (final File p : initialPoliciesDirectory.listFiles()) {
final String id = PolicyUtil.getID(FileUtils.openInputStream(p));
final String repoPath = PolicyUtil.getPathForId(id);
final Datastream d = datastreamService.createDatastream(session,
repoPath,
"application/xml",
p.getName(),
new FileInputStream(p));
LOGGER.info("Add initial policy {} at {}", p.getAbsolutePath(), d.getPath());
final FedoraBinary binary = datastreamService.getBinary(session, repoPath);
binary.setContent(new FileInputStream(p),
"application/xml",
null,
p.getName(),
datastreamService.getStoragePolicyDecisionPoint());

LOGGER.info("Add initial policy {} at {}", p.getAbsolutePath(), binary.getPath());
}
session.save();
} catch (final RepositoryException | InvalidChecksumException | IOException e) {
Expand Down
Expand Up @@ -40,6 +40,7 @@

import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.services.DatastreamService;
import org.jboss.security.xacml.sunxacml.EvaluationCtx;
import org.jboss.security.xacml.sunxacml.PolicyReference;
Expand Down Expand Up @@ -79,6 +80,9 @@ public class FedoraPolicyFinderModuleTest {
@Mock
private Datastream mockPolicyDs;

@Mock
private FedoraBinary mockBinary;

@Mock
private DatastreamService mockDsService;

Expand Down Expand Up @@ -134,7 +138,8 @@ public void testFindPolicyOnTargetNode() throws Exception {
when(mockNode.hasProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(true);
when(mockNode.getProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(mockPolicyProperty);

when(mockPolicyDs.getContent()).thenReturn(this.getClass().getResourceAsStream("/xacml/testPolicy.xml"));
when(mockPolicyDs.getBinary()).thenReturn(mockBinary);
when(mockBinary.getContent()).thenReturn(this.getClass().getResourceAsStream("/xacml/testPolicy.xml"));

final FedoraEvaluationCtxBuilder ctxBuilder = new FedoraEvaluationCtxBuilder();
ctxBuilder.addResourceID("/{}myPath");
Expand All @@ -156,8 +161,9 @@ public void testFindPolicyByIdReference() throws Exception {
final String idPath = POLICY_URI_PREFIX + policyPath;
final URI idReference = new URI(idPath);

when(mockPolicyDs.getContent()).thenReturn(this.getClass().getResourceAsStream("/xacml/testPolicy.xml"));
when(mockDsService.getDatastream(any(Session.class), eq(policyPath))).thenReturn(mockPolicyDs);
when(mockPolicyDs.getBinary()).thenReturn(mockBinary);
when(mockBinary.getContent()).thenReturn(this.getClass().getResourceAsStream("/xacml/testPolicy.xml"));
when(mockDsService.findOrCreateDatastream(any(Session.class), eq(policyPath))).thenReturn(mockPolicyDs);

final PolicyFinderResult result = finderModule.findPolicy(idReference, 0, null, null);

Expand All @@ -172,14 +178,17 @@ public void testFindPolicySet() throws Exception {
when(mockNode.hasProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(true);
when(mockNode.getProperty(eq(XACML_POLICY_PROPERTY))).thenReturn(mockPolicyProperty);

when(mockPolicyDs.getContent())
when(mockPolicyDs.getBinary()).thenReturn(mockBinary);
when(mockBinary.getContent())
.thenReturn(this.getClass().getResourceAsStream("/xacml/adminRolePolicySet.xml"));

final String referencedId = "fcrepo:policies/AdminPermissionPolicySet";
final Datastream referencedPolicyDs = mock(Datastream.class);
when(referencedPolicyDs.getContent()).thenReturn(
final FedoraBinary referencedPolicyBinary = mock(FedoraBinary.class);
when(referencedPolicyDs.getBinary()).thenReturn(referencedPolicyBinary);
when(referencedPolicyBinary.getContent()).thenReturn(
this.getClass().getResourceAsStream("/xacml/adminPermissionPolicySet.xml"));
when(mockDsService.getDatastream(any(Session.class), eq(referencedId))).thenReturn(referencedPolicyDs);
when(mockDsService.findOrCreateDatastream(any(Session.class), eq(referencedId))).thenReturn(referencedPolicyDs);

final FedoraEvaluationCtxBuilder ctxBuilder = new FedoraEvaluationCtxBuilder();
ctxBuilder.addResourceID("/{}myPath");
Expand Down
Expand Up @@ -26,20 +26,19 @@
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.FedoraBinary;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.services.DatastreamService;

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

/**
Expand Down Expand Up @@ -68,15 +67,16 @@ public class XACMLWorkspaceInitializerTest {
@Mock
Datastream mockDatastream;

@Mock
FedoraBinary mockBinary;

@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(mockDsService.findOrCreateDatastream(eq(mockSession), anyString())).thenReturn(mockDatastream);
when(mockDatastream.getPath()).thenReturn("/dummy/test/path");

final File initialPoliciesDirectory = policiesDirectory();
Expand Down Expand Up @@ -118,14 +118,12 @@ public void testConstructorEmptyDir() {

@Test
public void testInit() throws Exception {
when(mockDsService.getBinary(eq(mockSession), anyString())).thenReturn(mockBinary);

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(mockDsService, times(expectedFiles)).getBinary(eq(mockSession), anyString());

verify(mockNode).addMixin("authz:xacmlAssignable");
verify(mockNode).setProperty(eq("authz:policy"), any(Node.class));
Expand All @@ -140,6 +138,7 @@ public void testInitInitialPoliciesException() throws Exception {

@Test(expected = Error.class)
public void testInitLinkRootToPolicyException() throws Exception {
when(mockDsService.getBinary(eq(mockSession), anyString())).thenReturn(mockBinary);
when(mockSession.getRootNode()).thenThrow(new RepositoryException("expected"));

xacmlWI.init();
Expand Down

0 comments on commit 9922d93

Please sign in to comment.