Skip to content

Commit

Permalink
Unit tests for negative persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 25, 2013
1 parent 6c5f91a commit f63bf3c
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 52 deletions.
Expand Up @@ -16,19 +16,25 @@

package org.fcrepo.kernel.utils.iterators;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Iterators.filter;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static org.fcrepo.kernel.utils.JcrRdfTools.getJcrNamespaceForRDFNamespace;
import static org.fcrepo.kernel.utils.iterators.UnmanagedRdfStream.isManagedTriple;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Map;

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

import org.fcrepo.kernel.exception.MalformedRdfException;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.utils.JcrRdfTools;
import org.slf4j.Logger;
Expand Down Expand Up @@ -143,6 +149,44 @@ protected Value createValue(final Node subjectNode, final RDFNode object,
return jcrRdfTools().createValue(subjectNode, object, propertyType);
}

protected String jcrMixinNameFromRdfResource(final Resource mixinResource) throws RepositoryException {

final String namespace = getJcrNamespaceForRDFNamespace(mixinResource.getNameSpace());
String namespacePrefix = null;
final Map<String, String> streamNSMap =
checkNotNull(stream().namespaces(),
"Use an empty map of namespaces, not null!");
if (streamNSMap.containsValue(namespace)) {
LOGGER.debug("Found namespace: {} in stream namespace mapping.",
namespace);
for (final String prefix : streamNSMap.keySet()) {
final String streamNamespace = streamNSMap.get(prefix);
if (namespace.equals(streamNamespace)) {
LOGGER.debug(
"Found namespace: {} in stream namespace mapping with prefix: {}.",
namespace, namespacePrefix);
namespacePrefix = prefix;
}
}
} else {
try {
namespacePrefix = session().getNamespacePrefix(namespace);
LOGGER.debug(
"Found namespace: {} in repository namespace mapping with prefix: {}.",
namespace, namespacePrefix);
} catch (final NamespaceException e) {
throw new MalformedRdfException(
"Unable to resolve registered namespace for resource "
+ mixinResource.toString());

}
}
final String mixinName =
namespacePrefix + ":" + mixinResource.getLocalName();
LOGGER.debug("Constructed JCR mixin name: {}", mixinName);
return mixinName;
}

protected abstract void operateOnProperty(final Statement t,
final Node subjectNode) throws RepositoryException;

Expand Down
Expand Up @@ -16,15 +16,10 @@

package org.fcrepo.kernel.utils.iterators;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.fcrepo.kernel.utils.JcrRdfTools.getJcrNamespaceForRDFNamespace;
import static org.fcrepo.kernel.utils.NodePropertiesTools.appendOrReplaceNodeProperty;
import static org.fcrepo.kernel.utils.NodePropertiesTools.getPropertyType;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Map;

import javax.jcr.NamespaceException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand Down Expand Up @@ -64,39 +59,8 @@ public RdfAdder(final GraphSubjects graphSubjects, final Session session,
@Override
protected void operateOnMixin(final Resource mixinResource,
final Node subjectNode) throws RepositoryException {
final String namespace = getJcrNamespaceForRDFNamespace(mixinResource.getNameSpace());
String namespacePrefix = null;
final Map<String, String> streamNSMap =
checkNotNull(stream().namespaces(),
"Use an empty map of namespaces, not null!");
if (streamNSMap.containsValue(namespace)) {
LOGGER.debug("Found namespace: {} in stream namespace mapping.",
namespace);
for (final String prefix : streamNSMap.keySet()) {
final String streamNamespace = streamNSMap.get(prefix);
if (namespace.equals(streamNamespace)) {
LOGGER.debug(
"Found namespace: {} in stream namespace mapping with prefix: {}.",
namespace, namespacePrefix);
namespacePrefix = prefix;
}
}
} else {
try {
namespacePrefix = session().getNamespacePrefix(namespace);
LOGGER.debug(
"Found namespace: {} in repository namespace mapping with prefix: {}.",
namespace, namespacePrefix);
} catch (final NamespaceException e) {
throw new MalformedRdfException(
"Unable to resolve registered namespace for resource "
+ mixinResource.toString());

}
}
final String mixinName =
namespacePrefix + ":" + mixinResource.getLocalName();
LOGGER.debug("Constructed JCR mixin name: {}", mixinName);
final String mixinName = jcrMixinNameFromRdfResource(mixinResource);
if (session().getWorkspace().getNodeTypeManager()
.hasNodeType(mixinName)) {
if (subjectNode.canAddMixin(mixinName)) {
Expand Down
Expand Up @@ -20,13 +20,12 @@
import static org.fcrepo.kernel.utils.NodePropertiesTools.removeNodeProperty;
import static org.slf4j.LoggerFactory.getLogger;

import javax.jcr.NamespaceException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.nodetype.NoSuchNodeTypeException;

import org.fcrepo.kernel.exception.MalformedRdfException;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.slf4j.Logger;

Expand Down Expand Up @@ -60,28 +59,23 @@ public RdfRemover(final GraphSubjects graphSubjects, final Session session,
@Override
protected void operateOnMixin(final Resource mixinResource,
final Node subjectNode) throws RepositoryException {
final String namespacePrefix;
try {
namespacePrefix =
session().getNamespacePrefix(mixinResource.getNameSpace());
} catch (final NamespaceException e) {
throw new MalformedRdfException(
"Unable to resolve registered namespace for resource "
+ mixinResource.toString());
}
final String mixinName =
namespacePrefix + ":" + mixinResource.getLocalName();

final String mixinName = jcrMixinNameFromRdfResource(mixinResource);
if (session().getWorkspace().getNodeTypeManager().hasNodeType(mixinName)) {
LOGGER.debug("Removing mixin: {} from node: {}.", mixinName,
subjectNode.getPath());
subjectNode.removeMixin(mixinName);
try {
subjectNode.removeMixin(mixinName);
} catch (final NoSuchNodeTypeException e) {
LOGGER.debug("which that node turned out not to have.");
}
}
}

@Override
protected void operateOnProperty(final Statement t, final Node n)
throws RepositoryException {
LOGGER.debug("Removing property from triple: {} on node: {}.", t, n
LOGGER.debug("Trying to remove property from triple: {} on node: {}.", t, n
.getPath());
final String propertyName =
getPropertyNameFromPredicate(n, t.getPredicate());
Expand Down
Expand Up @@ -227,6 +227,8 @@ public void testGetBinary() throws RepositoryException {
public void testGetDefinitionForPropertyName() throws RepositoryException {
final String mockPropertyName = "mock:property";
when(mockNode.getSession()).thenReturn(mockSession);
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.getMixinNodeTypes()).thenReturn(new NodeType[]{});
when(mockSession.getWorkspace()).thenReturn(mockWS);
when(mockWS.getNodeTypeManager()).thenReturn(mockNTM);
when(mockNTM.getNodeType(anyString())).thenReturn(mockNodeType);
Expand Down

0 comments on commit f63bf3c

Please sign in to comment.