Navigation Menu

Skip to content

Commit

Permalink
More and better unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 17, 2013
1 parent a3b51fa commit 1cd1c9a
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 35 deletions.
Expand Up @@ -16,6 +16,7 @@

package org.fcrepo.kernel.rdf.impl;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.graph.Triple.create;
Expand All @@ -32,6 +33,7 @@

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

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -60,8 +62,9 @@ public NamespaceContext(final Session session) throws RepositoryException {
super();
final NamespaceRegistry namespaceRegistry =
session.getWorkspace().getNamespaceRegistry();
assert (namespaceRegistry != null) : new RepositoryException(
checkNotNull(namespaceRegistry,
"Couldn't find namespace registry in repository!");

final ImmutableMap.Builder<String, String> namespaces =
ImmutableMap.builder();
final ImmutableCollection.Builder<Triple> nsTriples =
Expand Down
Expand Up @@ -93,10 +93,8 @@ public class PropertiesRdfContext extends NodeRdfContext {
* @throws RepositoryException
*/

public PropertiesRdfContext(final javax.jcr.Node node,
final GraphSubjects graphSubjects,
final LowLevelStorageService lowLevelStorageService)
throws RepositoryException {
public PropertiesRdfContext(final javax.jcr.Node node, final GraphSubjects graphSubjects,
final LowLevelStorageService lowLevelStorageService) throws RepositoryException {
super(node, graphSubjects, lowLevelStorageService);
property2triple = new PropertyToTriple(graphSubjects);
putPropertiesIntoContext();
Expand All @@ -109,7 +107,9 @@ private void putPropertiesIntoContext() throws RepositoryException {
node());

// this node's own properties
concat(triplesFromProperties(node()));
if (node().hasProperties()) {
concat(triplesFromProperties(node()));
}

// if there's a jcr:content node, include information about it
if (node().hasNode(JCR_CONTENT)) {
Expand All @@ -119,31 +119,26 @@ private void putPropertiesIntoContext() throws RepositoryException {
final Node subject =
graphSubjects().getGraphSubject(node()).asNode();
// add triples representing parent-to-content-child relationship
concat(
Iterators.forArray(new Triple[] {
create(subject, HAS_CONTENT.asNode(),
contentSubject),
create(contentSubject, IS_CONTENT_OF.asNode(),
subject)}));
concat(Iterators.forArray(new Triple[] {
create(subject, HAS_CONTENT.asNode(), contentSubject),
create(contentSubject, IS_CONTENT_OF.asNode(), subject)}));
// add properties from content child
concat(triplesFromProperties(node().getNode(JCR_CONTENT)));

// add triples describing storage of content child
lowLevelStorageService().setRepository(
node().getSession().getRepository());
concat(
transform(lowLevelStorageService().getLowLevelCacheEntries(
contentNode).iterator(),
new Function<LowLevelCacheEntry, Triple>() {

@Override
public Triple apply(
final LowLevelCacheEntry llce) {
return create(contentSubject, HAS_LOCATION
.asNode(), createLiteral(llce
concat(transform(lowLevelStorageService().getLowLevelCacheEntries(
contentNode).iterator(),
new Function<LowLevelCacheEntry, Triple>() {

@Override
public Triple apply(final LowLevelCacheEntry llce) {
return create(contentSubject,
HAS_LOCATION.asNode(), createLiteral(llce
.getExternalIdentifier()));
}
}));
}
}));

}

Expand Down Expand Up @@ -183,7 +178,8 @@ private Set<Triple> triplesForRootNode() throws RepositoryException {
b.add(create(subject, HAS_OBJECT_SIZE.asNode(), createLiteral(String
.valueOf(getRepositorySize(repository)))));
// Get the cluster configuration for the RDF response, if available
// this ugly test checks to see whether this is an ordinary JCR repository
// this ugly test checks to see whether this is an ordinary JCR
// repository
// or a ModeShape repo, which will possess the extra info
if (JcrRepository.class.isAssignableFrom(repository.getClass())) {
final Map<String, String> config =
Expand Down Expand Up @@ -221,8 +217,7 @@ private Set<Triple> triplesForRootNode() throws RepositoryException {
return b.build();
}

private Iterator<Triple> triplesFromProperties(final javax.jcr.Node n)
throws RepositoryException {
private Iterator<Triple> triplesFromProperties(final javax.jcr.Node n) throws RepositoryException {
LOGGER.debug("Creating triples for node: {}", n);
final UnmodifiableIterator<Property> nonBinaryProperties =
filter(new PropertyIterator(n.getProperties()),
Expand All @@ -233,9 +228,9 @@ private Iterator<Triple> triplesFromProperties(final javax.jcr.Node n)
not(isBinaryProperty));

return Iterators.concat(new ZippingIterator<>(
transform(
transform(
nonBinaryProperties, property2values),
transform(
transform(
nonBinaryPropertiesCopy, property2triple)));

}
Expand Down
@@ -0,0 +1,129 @@
/**
* Copyright 2013 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.rdf.impl;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;

import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.services.LowLevelStorageService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.slf4j.Logger;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;

public class HierarchyRdfContextTest {

// for mocks see bottom of this class

@Before
public void setUp() throws RepositoryException {
initMocks(this);
when(mockNode.getSession()).thenReturn(mockSession);
when(mockGraphSubjects.getContext()).thenReturn(testPage);
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.getMixinNodeTypes()).thenReturn(new NodeType[] {});
when(mockNodeType.getChildNodeDefinitions()).thenReturn(
new NodeDefinition[] {mock(NodeDefinition.class)});
when(mockNode.getPath()).thenReturn(MOCK_NODE_PATH);
when(mockParentNode.getPath()).thenReturn(MOCK_PARENT_NODE_PATH);
when(mockNode.getParent()).thenReturn(mockParentNode);
when(mockNode.getNodes()).thenReturn(mockNodes);
when(mockParentNode.hasProperties()).thenReturn(false);
when(mockNode.hasProperties()).thenReturn(false);
when(mockGraphSubjects.getGraphSubject(mockNode)).thenReturn(
testSubject);
when(mockGraphSubjects.getGraphSubject(mockParentNode)).thenReturn(
testParentSubject);
}

@Test
public void testOneNodeAndParent() throws RepositoryException, IOException {
when(mockNode.hasNodes()).thenReturn(false);
final Model results =
new HierarchyRdfContext(mockNode, mockGraphSubjects,
mockLowLevelStorageService).asModel();
logRdf("Created RDF for testOneNodeAndParent()", results);

// assertTrue("Didn't find node described!", results.list);

}

private void
logRdf(final String message, final Model model) throws IOException {
LOGGER.debug(message);
try (Writer w = new StringWriter()) {
model.write(w);
LOGGER.debug("\n" + w.toString());
}
}

private static final String MOCK_PARENT_NODE_PATH = "/mockNodeParent";

private static final String MOCK_NODE_PATH = MOCK_PARENT_NODE_PATH
+ "/mockNode";

private static final String RESOURCE_PREFIX = "http://example.com";

private static final Resource testPage = createResource(RESOURCE_PREFIX
+ "/page");

private static final Resource testSubject = createResource(RESOURCE_PREFIX
+ MOCK_NODE_PATH);

private static final Resource testParentSubject =
createResource(RESOURCE_PREFIX + MOCK_PARENT_NODE_PATH);

@Mock
private Session mockSession;

@Mock
private Node mockNode, mockParentNode, mockChildNode;

@Mock
private NodeIterator mockNodes;

@Mock
private NodeType mockNodeType;

@Mock
private GraphSubjects mockGraphSubjects;

@Mock
private LowLevelStorageService mockLowLevelStorageService;

private static final Logger LOGGER =
getLogger(HierarchyRdfContextTest.class);

}
Expand Up @@ -26,6 +26,7 @@
import javax.jcr.Workspace;

import org.fcrepo.kernel.rdf.impl.NamespaceContext;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.modeshape.jcr.api.NamespaceRegistry;
Expand All @@ -41,21 +42,33 @@ public class NamespaceContextTest {
private Session mockSession;

@Mock
Workspace mockWorkspace;
private Workspace mockWorkspace;

private final static String testUri = "http://example.com";

private final static String prefix = "jcr";

@Test
public void testConstructor() throws RepositoryException {
@Before
public void setUp() throws RepositoryException {
initMocks(this);
when(mockNamespaceRegistry.getPrefixes()).thenReturn(
new String[] {prefix});
when(mockNamespaceRegistry.getURI(prefix)).thenReturn(testUri);
when(mockSession.getWorkspace()).thenReturn(mockWorkspace);
when(mockWorkspace.getNamespaceRegistry()).thenReturn(
mockNamespaceRegistry);
}

@Test(expected = NullPointerException.class)
public void testBadNamespaceRegistry() throws RepositoryException {
mockNamespaceRegistry = null;
new NamespaceContext(mockSession);
}

@Test
public void testConstructor() throws RepositoryException {
when(mockNamespaceRegistry.getPrefixes()).thenReturn(
new String[] {prefix, ""});
when(mockNamespaceRegistry.getURI("")).thenReturn(
"GARBAGE URI FOR FAKE NAMESPACE, SHOULD NEVER BE PARSED");
when(mockNamespaceRegistry.getURI(prefix)).thenReturn(testUri);
assertTrue(any(new NamespaceContext(mockSession), hasTestUriAsObject));
}

Expand Down

0 comments on commit 1cd1c9a

Please sign in to comment.