Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for LDP IsMemberOfRelation
- Loading branch information
Showing
6 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...po-kernel-impl/src/main/java/org/fcrepo/kernel/impl/rdf/impl/LdpIsMemberOfRdfContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* 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.kernel.impl.rdf.impl; | ||
|
||
import com.hp.hpl.jena.rdf.model.Resource; | ||
import com.hp.hpl.jena.rdf.model.ResourceFactory; | ||
import org.fcrepo.kernel.identifiers.IdentifierConverter; | ||
import org.fcrepo.kernel.impl.FedoraBinaryImpl; | ||
|
||
import javax.jcr.Node; | ||
import javax.jcr.Property; | ||
import javax.jcr.RepositoryException; | ||
|
||
import static com.hp.hpl.jena.graph.Triple.create; | ||
import static javax.jcr.PropertyType.PATH; | ||
import static javax.jcr.PropertyType.REFERENCE; | ||
import static javax.jcr.PropertyType.WEAKREFERENCE; | ||
import static org.fcrepo.jcr.FedoraJcrTypes.LDP_IS_MEMBER_OF_RELATION; | ||
import static org.fcrepo.jcr.FedoraJcrTypes.LDP_MEMBER_RESOURCE; | ||
|
||
/** | ||
* @author cabeer | ||
* @since 10/7/14 | ||
*/ | ||
public class LdpIsMemberOfRdfContext extends NodeRdfContext { | ||
/** | ||
* Default constructor. | ||
* | ||
* @param node | ||
* @param graphSubjects | ||
* @throws javax.jcr.RepositoryException | ||
*/ | ||
public LdpIsMemberOfRdfContext(final Node node, | ||
final IdentifierConverter<Resource, Node> graphSubjects) throws RepositoryException { | ||
super(node, graphSubjects); | ||
|
||
if (!isRoot(node)) { | ||
final Node container = getContainer(node); | ||
|
||
if (container.hasProperty(LDP_IS_MEMBER_OF_RELATION)) { | ||
final Property property = container.getProperty(LDP_IS_MEMBER_OF_RELATION); | ||
|
||
final Resource memberRelation = ResourceFactory.createResource(property.getString()); | ||
|
||
final Resource membershipResource = getMemberResource(graphSubjects, container); | ||
|
||
if (membershipResource != null) { | ||
concat(create(subject(), memberRelation.asNode(), membershipResource.asNode())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check if the node is the root resource (and therefore can't be within a container) | ||
* @param node | ||
* @return | ||
* @throws RepositoryException | ||
*/ | ||
private boolean isRoot(final Node node) throws RepositoryException { | ||
return node.getDepth() == 0; | ||
} | ||
|
||
/** | ||
* Get the LDP container for this node | ||
* @param node | ||
* @return | ||
* @throws RepositoryException | ||
*/ | ||
private Node getContainer(final Node node) throws RepositoryException { | ||
final Node parent; | ||
|
||
if (FedoraBinaryImpl.hasMixin(node)) { | ||
parent = node.getParent().getParent(); | ||
} else { | ||
parent = node.getParent(); | ||
} | ||
return parent; | ||
} | ||
|
||
/** | ||
* Get the membership resource relation asserted by the container | ||
* @param graphSubjects | ||
* @param parent | ||
* @return | ||
* @throws RepositoryException | ||
*/ | ||
private Resource getMemberResource(final IdentifierConverter<Resource, Node> graphSubjects, | ||
final Node parent) throws RepositoryException { | ||
final Resource membershipResource; | ||
|
||
if (parent.hasProperty(LDP_MEMBER_RESOURCE)) { | ||
final Property memberResource = parent.getProperty(LDP_MEMBER_RESOURCE); | ||
|
||
final int type = memberResource.getType(); | ||
if ( type == REFERENCE || type == WEAKREFERENCE || type == PATH) { | ||
membershipResource = graphSubjects.reverse().convert(memberResource.getNode()); | ||
} else { | ||
membershipResource = ResourceFactory.createResource(memberResource.getString()); | ||
} | ||
} else { | ||
membershipResource = graphSubjects.reverse().convert(parent); | ||
} | ||
|
||
return membershipResource; | ||
} | ||
} |
178 changes: 178 additions & 0 deletions
178
...ernel-impl/src/test/java/org/fcrepo/kernel/impl/rdf/impl/LdpIsMemberOfRdfContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/** | ||
* 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.kernel.impl.rdf.impl; | ||
|
||
import com.google.common.base.Converter; | ||
import com.hp.hpl.jena.rdf.model.Model; | ||
import com.hp.hpl.jena.rdf.model.Resource; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
import javax.jcr.Node; | ||
import javax.jcr.Property; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
|
||
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty; | ||
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource; | ||
import static javax.jcr.PropertyType.REFERENCE; | ||
import static javax.jcr.PropertyType.URI; | ||
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_BINARY; | ||
import static org.fcrepo.jcr.FedoraJcrTypes.LDP_IS_MEMBER_OF_RELATION; | ||
import static org.fcrepo.jcr.FedoraJcrTypes.LDP_MEMBER_RESOURCE; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
/** | ||
* @author cabeer | ||
*/ | ||
public class LdpIsMemberOfRdfContextTest { | ||
|
||
private LdpIsMemberOfRdfContext testObj; | ||
|
||
@Mock | ||
private Node mockBinary; | ||
|
||
@Mock | ||
private Node mockNode; | ||
|
||
@Mock | ||
private Node mockContainer; | ||
|
||
@Mock | ||
private Node mockResource; | ||
|
||
@Mock | ||
private Property mockRelationProperty; | ||
|
||
@Mock | ||
private Property mockMembershipProperty; | ||
|
||
@Mock | ||
private Session mockSession; | ||
|
||
private DefaultIdentifierTranslator subjects; | ||
|
||
@Before | ||
public void setUp() throws RepositoryException { | ||
initMocks(this); | ||
when(mockNode.getPath()).thenReturn("/a"); | ||
when(mockNode.getParent()).thenReturn(mockContainer); | ||
when(mockNode.getDepth()).thenReturn(1); | ||
|
||
when(mockContainer.getPath()).thenReturn("/"); | ||
|
||
when(mockResource.getPath()).thenReturn("/some/path"); | ||
|
||
when(mockBinary.getParent()).thenReturn(mockNode); | ||
when(mockBinary.getDepth()).thenReturn(2); | ||
when(mockBinary.isNodeType(FEDORA_BINARY)).thenReturn(true); | ||
when(mockBinary.getPath()).thenReturn("/a/jcr:content"); | ||
|
||
subjects = new DefaultIdentifierTranslator(mockSession); | ||
} | ||
|
||
@Test | ||
public void testIsMemberOfRelationWithRootResource() throws RepositoryException { | ||
testObj = new LdpIsMemberOfRdfContext(mockContainer, subjects); | ||
|
||
final Model model = testObj.asModel(); | ||
|
||
assertTrue("Expected stream to be empty", model.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testIsMemberOfRelationWithoutIsMemberOfResource() throws RepositoryException { | ||
testObj = new LdpIsMemberOfRdfContext(mockNode, subjects); | ||
|
||
final Model model = testObj.asModel(); | ||
|
||
assertTrue("Expected stream to be empty", model.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testIsMemberOfRelation() throws RepositoryException { | ||
when(mockContainer.hasProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(true); | ||
when(mockContainer.getProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(mockRelationProperty); | ||
when(mockContainer.hasProperty(LDP_MEMBER_RESOURCE)).thenReturn(true); | ||
when(mockContainer.getProperty(LDP_MEMBER_RESOURCE)).thenReturn(mockMembershipProperty); | ||
when(mockMembershipProperty.getType()).thenReturn(REFERENCE); | ||
when(mockMembershipProperty.getNode()).thenReturn(mockResource); | ||
|
||
final String property = "some:uri"; | ||
when(mockRelationProperty.getString()).thenReturn(property); | ||
testObj = new LdpIsMemberOfRdfContext(mockNode, subjects); | ||
|
||
final Model model = testObj.asModel(); | ||
|
||
final Converter<Node, Resource> nodeSubjects = subjects.reverse(); | ||
|
||
assertTrue("Expected stream to contain triple", | ||
model.contains(nodeSubjects.convert(mockNode), | ||
createProperty(property), | ||
nodeSubjects.convert(mockResource))); | ||
} | ||
|
||
|
||
@Test | ||
public void testIsMemberOfRelationToExternalResource() throws RepositoryException { | ||
when(mockContainer.hasProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(true); | ||
when(mockContainer.getProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(mockRelationProperty); | ||
when(mockContainer.hasProperty(LDP_MEMBER_RESOURCE)).thenReturn(true); | ||
when(mockContainer.getProperty(LDP_MEMBER_RESOURCE)).thenReturn(mockMembershipProperty); | ||
when(mockMembershipProperty.getType()).thenReturn(URI); | ||
when(mockMembershipProperty.getString()).thenReturn("some:resource"); | ||
|
||
final String property = "some:uri"; | ||
when(mockRelationProperty.getString()).thenReturn(property); | ||
testObj = new LdpIsMemberOfRdfContext(mockNode, subjects); | ||
|
||
final Model model = testObj.asModel(); | ||
|
||
final Converter<Node, Resource> nodeSubjects = subjects.reverse(); | ||
|
||
assertTrue("Expected stream to contain triple", | ||
model.contains(nodeSubjects.convert(mockNode), | ||
createProperty(property), | ||
createResource("some:resource"))); | ||
} | ||
|
||
@Test | ||
public void testIsMemberOfRelationForBinary() throws RepositoryException { | ||
when(mockContainer.hasProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(true); | ||
when(mockContainer.getProperty(LDP_IS_MEMBER_OF_RELATION)).thenReturn(mockRelationProperty); | ||
when(mockContainer.hasProperty(LDP_MEMBER_RESOURCE)).thenReturn(true); | ||
when(mockContainer.getProperty(LDP_MEMBER_RESOURCE)).thenReturn(mockMembershipProperty); | ||
when(mockMembershipProperty.getType()).thenReturn(REFERENCE); | ||
when(mockMembershipProperty.getNode()).thenReturn(mockResource); | ||
|
||
final String property = "some:uri"; | ||
|
||
when(mockRelationProperty.getString()).thenReturn(property); | ||
testObj = new LdpIsMemberOfRdfContext(mockBinary, subjects); | ||
|
||
final Model model = testObj.asModel(); | ||
|
||
final Converter<Node, Resource> nodeSubjects = subjects.reverse(); | ||
|
||
assertTrue("Expected stream to contain triple", | ||
model.contains(nodeSubjects.convert(mockBinary), | ||
createProperty(property), | ||
nodeSubjects.convert(mockResource))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters