Skip to content

Commit

Permalink
Polishing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 30, 2013
1 parent a2d6a57 commit d964360
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 156 deletions.
Expand Up @@ -16,13 +16,16 @@

package org.fcrepo.kernel.rdf.impl;

import static com.google.common.base.Predicates.not;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterators.forArray;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static com.hp.hpl.jena.vocabulary.RDFS.Class;
import static com.hp.hpl.jena.vocabulary.RDFS.label;
import static com.hp.hpl.jena.vocabulary.RDFS.subClassOf;
import static org.fcrepo.kernel.utils.JcrRdfTools.getRDFNamespaceForJcrNamespace;
import static org.fcrepo.kernel.rdf.impl.mappings.ItemDefinitionToTriples.getResource;
import static org.slf4j.LoggerFactory.getLogger;

import com.google.common.base.Function;
Expand All @@ -32,9 +35,6 @@
import com.google.common.collect.Iterators;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import org.fcrepo.kernel.rdf.impl.mappings.NodeDefinitionToTriples;
import org.fcrepo.kernel.rdf.impl.mappings.PropertyDefinitionToTriples;
import org.fcrepo.kernel.utils.iterators.NodeTypeIterator;
Expand All @@ -56,12 +56,12 @@ public class NodeTypeRdfContext extends RdfStream {

private static Logger LOGGER = getLogger(NodeTypeRdfContext.class);

private static final Predicate<ItemDefinition> ignoreWildcardResidualDefinitions =
private static final Predicate<ItemDefinition> isWildcardResidualDefinition =
new Predicate<ItemDefinition>() {

@Override
public boolean apply(final ItemDefinition input) {
return !input.getName().equals("*");
return input.getName().equals("*");
}
};

Expand Down Expand Up @@ -113,15 +113,15 @@ public NodeTypeRdfContext(final NodeType nodeType)
ImmutableSet.builder();

final Node nodeTypeResource =
getResource((Namespaced) nodeType).asNode();
getResource((Namespaced) nodeType).asNode();
final String nodeTypeName = nodeType.getName();

LOGGER.debug("Adding triples for nodeType {} with URI {}",
LOGGER.debug("Adding triples for nodeType: {} with URI: {}",
nodeTypeName, nodeTypeResource.getURI());

nsTriples.add(create(nodeTypeResource, RDF.type.asNode(), RDFS.Class
nsTriples.add(create(nodeTypeResource, type.asNode(), Class
.asNode()));
nsTriples.add(create(nodeTypeResource, RDFS.label.asNode(),
nsTriples.add(create(nodeTypeResource, label.asNode(),
createLiteral(nodeTypeName)));

concat(Iterators.transform(forArray(nodeType.getDeclaredSupertypes()),
Expand All @@ -133,7 +133,7 @@ public Triple apply(final NodeType input) {
try {
supertypeNode = getResource(input).asNode();
LOGGER.trace(
"Adding triple for nodeType {} subclass {}",
"Adding triple for nodeType: {} with subclass: {}",
nodeTypeName, supertypeNode.getURI());
return create(nodeTypeResource,
subClassOf.asNode(), supertypeNode);
Expand All @@ -147,36 +147,17 @@ public Triple apply(final NodeType input) {
concat(Iterators.concat(
Iterators.transform(Iterators.filter(
forArray(nodeType.getDeclaredChildNodeDefinitions()),
ignoreWildcardResidualDefinitions),
not(isWildcardResidualDefinition)),
new NodeDefinitionToTriples(nodeTypeResource))));

concat(Iterators.concat(
Iterators.transform(Iterators.filter(
forArray(nodeType.getDeclaredPropertyDefinitions()),
ignoreWildcardResidualDefinitions),
not(isWildcardResidualDefinition)),
new PropertyDefinitionToTriples(nodeTypeResource))));

concat(nsTriples.build());
}

/**
* Get a RDF Resource for a Namespaced JCR object
*
* @param namespacedObject
* @return
* @throws RepositoryException
*/
private Resource getResource(final Namespaced namespacedObject)
throws RepositoryException {
return createProperty(
getRDFNamespaceForJcrNamespace(namespacedObject
.getNamespaceURI()), namespacedObject.getLocalName())
.asResource();
}

private Resource getResource(final NodeType nodeType)
throws RepositoryException {
return getResource((Namespaced) nodeType);
}

}
Expand Up @@ -20,8 +20,6 @@
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDFS;

import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.modeshape.jcr.api.Namespaced;
import org.slf4j.Logger;
Expand All @@ -37,6 +35,7 @@
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
import static com.hp.hpl.jena.vocabulary.RDF.Property;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static com.hp.hpl.jena.vocabulary.RDFS.domain;
import static com.hp.hpl.jena.vocabulary.RDFS.label;
import static org.fcrepo.kernel.utils.JcrRdfTools.getRDFNamespaceForJcrNamespace;
import static org.slf4j.LoggerFactory.getLogger;
Expand All @@ -55,57 +54,58 @@ public class ItemDefinitionToTriples<T extends ItemDefinition> implements Functi

private static Logger LOGGER = getLogger(ItemDefinitionToTriples.class);

private Node domain;
private Node context;

/**
* Translate ItemDefinitions into triples. The definitions will hang off
* the provided RDF Node
* @param domain
* @param context
*/
public ItemDefinitionToTriples(final Node domain) {
this.domain = domain;
public ItemDefinitionToTriples(final Node context) {
this.context = context;
}

@Override
public Iterator<Triple> apply(final T input) {

try {

final Node propertyDefinitionNode = getResource(input).asNode();

LOGGER.trace("Adding triples for nodeType: {} with child nodes: {}", domain.getURI(), propertyDefinitionNode.getURI());
LOGGER.trace("Adding triples for nodeType: {} with child nodes: {}", context.getURI(), propertyDefinitionNode.getURI());

return new RdfStream(
create(propertyDefinitionNode, type.asNode(), Property.asNode()),
create(propertyDefinitionNode, RDFS.domain.asNode(), domain),
create(propertyDefinitionNode, domain.asNode(), context),
create(propertyDefinitionNode, label.asNode(), createLiteral(input.getName())));

} catch (final RepositoryException e) {
throw propagate(e);
}

}

/**
* Get a RDF Resource for a Namespaced JCR object
* Get a RDF {@link Resource} for a {@link Namespaced} JCR object.
* {@link Namespaced} is a Modeshape API type which is implemented by types
* that fulfill the JCR interfaces that represent definitions.
*
* @param namespacedObject
* @return
* @throws javax.jcr.RepositoryException
*/
protected Resource getResource(final Namespaced namespacedObject)
public static Resource getResource(final Namespaced namespacedObject)
throws RepositoryException {
// TODO find a better way to create an explicitly-namespaced resource
// if Jena offers one, since this isn't actually a Property
return createProperty(
getRDFNamespaceForJcrNamespace(namespacedObject
.getNamespaceURI()), namespacedObject.getLocalName())
.asResource();
}

protected Resource getResource(final NodeType nodeType) throws RepositoryException {
public static Resource getResource(final NodeType nodeType) throws RepositoryException {
return getResource((Namespaced) nodeType);
}

protected Resource getResource(final ItemDefinition nodeType) throws RepositoryException {
public static Resource getResource(final ItemDefinition nodeType) throws RepositoryException {
return getResource((Namespaced) nodeType);
}
}
Expand Up @@ -16,7 +16,6 @@

package org.fcrepo.kernel.rdf.impl.mappings;

import com.google.common.collect.Iterators;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import org.fcrepo.kernel.utils.iterators.RdfStream;
Expand Down Expand Up @@ -51,7 +50,6 @@ public NodeDefinitionToTriples(final Node domain) {

@Override
public Iterator<Triple> apply(final NodeDefinition input) {
final Iterator<Triple> itemDefinition = super.apply(input);

try {

Expand All @@ -67,16 +65,15 @@ public Iterator<Triple> apply(final NodeDefinition input) {
final Triple nsTriple =
create(propertyDefinitionNode, range.asNode(), getResource(
requiredPrimaryTypes[0]).asNode());
return Iterators.concat(itemDefinition, new RdfStream(nsTriple));
return new RdfStream(nsTriple).concat(super.apply(input));
} else {
LOGGER.trace("Skipping RDFS:range for {} with no required primary types");
}
return itemDefinition;
return super.apply(input);

} catch (final RepositoryException e) {
throw propagate(e);
}


}
}
Expand Up @@ -17,14 +17,13 @@
package org.fcrepo.kernel.rdf.impl.mappings;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;

import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;

import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.PropertyDefinition;
import java.util.Iterator;
Expand Down Expand Up @@ -52,6 +51,7 @@
import static javax.jcr.PropertyType.URI;
import static javax.jcr.PropertyType.WEAKREFERENCE;
import static javax.jcr.PropertyType.nameFromValue;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.slf4j.LoggerFactory.getLogger;

/**
Expand All @@ -62,7 +62,16 @@ public class PropertyDefinitionToTriples extends ItemDefinitionToTriples<Propert

private static Logger LOGGER = getLogger(PropertyDefinitionToTriples.class);

static final Map<Integer, XSDDatatype> JCR_TYPE_TO_XSD_DATATYPE =
/**
* A JCR type for which we know no RDF equivalent.
*/
private static Node UNMAPPED_TYPE = createURI(REPOSITORY_NAMESPACE
+ "UNMAPPED_TYPE");

/**
* A map from JCR types to RDF types.
*/
private static final Map<Integer, XSDDatatype> JCR_TYPE_TO_XSD_DATATYPE =
ImmutableMap.<Integer, XSDDatatype>builder()
.put(BOOLEAN, XSDboolean)
.put(DATE, XSDdate)
Expand All @@ -87,27 +96,26 @@ public PropertyDefinitionToTriples(final Node domain) {

@Override
public Iterator<Triple> apply(final PropertyDefinition input) {
final Iterator<Triple> itemDefinition = super.apply(input);

try {
// skip range declaration for unknown types
final int requiredType = input.getRequiredType();

final Node rangeForJcrType = getRangeForJcrType(requiredType);

if (rangeForJcrType != null) {
if (rangeForJcrType != UNMAPPED_TYPE) {
LOGGER.trace("Adding RDFS:range for property: {} with required type: {} as: {}",
input.getName(), nameFromValue(requiredType), rangeForJcrType.getURI());
final Triple propertyTriple =
create(getResource(input).asNode(), range.asNode(),
rangeForJcrType);
return Iterators.concat(itemDefinition, new RdfStream(propertyTriple));
return new RdfStream(propertyTriple).concat(super.apply(input));
} else {
LOGGER.trace("Skipping RDFS:range for property {} with unmapped type {}",
input.getName(),
PropertyType.nameFromValue(requiredType));
LOGGER.trace(
"Skipping RDFS:range for property: {} with unmappable type: {}",
input.getName(), nameFromValue(requiredType));
}
return itemDefinition;
return super.apply(input);
} catch (final RepositoryException e) {
throw propagate(e);
}
Expand All @@ -120,13 +128,8 @@ public Iterator<Triple> apply(final PropertyDefinition input) {
* @return
*/
private Node getRangeForJcrType(final int requiredType) {
final XSDDatatype type = JCR_TYPE_TO_XSD_DATATYPE.get(requiredType);

if (type == null) {
return null;
} else {
return createURI(type.getURI());
}

return JCR_TYPE_TO_XSD_DATATYPE.containsKey(requiredType)
? createURI(JCR_TYPE_TO_XSD_DATATYPE.get(requiredType).getURI())
: UNMAPPED_TYPE;
}
}
Expand Up @@ -18,6 +18,7 @@

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static org.fcrepo.kernel.RdfLexicon.JCR_NAMESPACE;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -80,8 +81,6 @@ public class NodeRdfContextTest {

private static final Resource mockNodeSubject = createResource();

private static final String jcrNamespace = "http://www.jcp.org/jcr/1.0";

@Before
public void setUp() throws RepositoryException {
initMocks(this);
Expand All @@ -95,7 +94,7 @@ public void setUp() throws RepositoryException {
when(mockSession.getRepository()).thenReturn(mockRepository);
when(mockSession.getWorkspace()).thenReturn(mockWorkspace);
when(mockWorkspace.getNamespaceRegistry()).thenReturn(mockNamespaceRegistry);
when(mockNamespaceRegistry.getURI("jcr")).thenReturn(jcrNamespace);
when(mockNamespaceRegistry.getURI("jcr")).thenReturn(JCR_NAMESPACE);
when(mockGraphSubjects.getGraphSubject(mockNode)).thenReturn(
mockNodeSubject);
}
Expand Down

0 comments on commit d964360

Please sign in to comment.