Skip to content

Commit

Permalink
Yet more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 18, 2013
1 parent 257f298 commit 58a7a1f
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 24 deletions.
Expand Up @@ -123,7 +123,8 @@ private void putPropertiesIntoContext() throws RepositoryException {
create(subject, HAS_CONTENT.asNode(), contentSubject),
create(contentSubject, IS_CONTENT_OF.asNode(), subject)}));
// add properties from content child
concat(triplesFromProperties(node().getNode(JCR_CONTENT)));
concat(new PropertiesRdfContext(node().getNode(JCR_CONTENT),
graphSubjects(), lowLevelStorageService()));

// add triples describing storage of content child
lowLevelStorageService().setRepository(
Expand Down Expand Up @@ -217,7 +218,8 @@ 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 @@ -228,10 +230,10 @@ private Iterator<Triple> triplesFromProperties(final javax.jcr.Node n) throws Re
not(isBinaryProperty));

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

}

Expand Down
@@ -0,0 +1,134 @@
/**
* 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.fcrepo.kernel.RdfLexicon.HAS_CONTENT;
import static org.fcrepo.kernel.RdfLexicon.HAS_LOCATION;
import static org.fcrepo.kernel.RdfLexicon.IS_CONTENT_OF;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
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.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;

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

import com.google.common.collect.ImmutableSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;

public class PropertiesRdfContextTest {

@Test
public void testForLowLevelStorageTriples() throws RepositoryException,
IOException {
final Model results =
new PropertiesRdfContext(mockNode, mockGraphSubjects,
mockLowLevelStorageService).asModel();
logRdf("Retrieved RDF for testForLowLevelStorageTriples():", results);
assertTrue("Didn't find triple showing node has content!", results
.contains(mockSubject, HAS_CONTENT, mockContentSubject));
assertTrue("Didn't find triple showing content has node!", results
.contains(mockContentSubject, IS_CONTENT_OF, mockSubject));
assertTrue("Didn't find triple showing content has location!", results
.contains(mockContentSubject, HAS_LOCATION,
MOCK_EXTERNAL_IDENTIFIER));
}

@Before
public void setUp() throws RepositoryException {
initMocks(this);
when(mockNode.getSession()).thenReturn(mockSession);
when(mockSession.getRepository()).thenReturn(mockRepository);
when(mockNode.hasNode(JCR_CONTENT)).thenReturn(true);
when(mockNode.getNode(JCR_CONTENT)).thenReturn(mockContentNode);
when(mockNode.hasProperties()).thenReturn(false);
when(mockContentNode.hasProperties()).thenReturn(false);
when(
mockLowLevelStorageService
.getLowLevelCacheEntries(mockContentNode)).thenReturn(
ImmutableSet.of(mockLowLevelCacheEntry));
when(mockLowLevelCacheEntry.getExternalIdentifier()).thenReturn(
MOCK_EXTERNAL_IDENTIFIER);
when(mockGraphSubjects.getGraphSubject(mockNode)).thenReturn(
mockSubject);
when(mockGraphSubjects.getGraphSubject(mockContentNode)).thenReturn(
mockContentSubject);
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockContentNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNodeType.getName()).thenReturn("not:root");
}

private static final String MOCK_EXTERNAL_IDENTIFIER =
"external-identifier";

private static final Resource mockContentSubject =
createResource("http://example.com/node/jcr:content");;

private static final Resource mockSubject =
createResource("http://example.com/node");

@Mock
private Node mockNode, mockContentNode;

@Mock
private NodeType mockNodeType;

@Mock
private GraphSubjects mockGraphSubjects;

@Mock
private LowLevelStorageService mockLowLevelStorageService;

@Mock
private Session mockSession;

@Mock
private Repository mockRepository;

@Mock
private LowLevelCacheEntry mockLowLevelCacheEntry;

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 Logger LOGGER =
getLogger(PropertiesRdfContextTest.class);
}
Expand Up @@ -38,6 +38,8 @@
import java.util.Calendar;
import java.util.Iterator;

import javax.jcr.AccessDeniedException;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand Down Expand Up @@ -234,6 +236,15 @@ public void testSingleValuedUriLiteralTriple() throws RepositoryException {
.getSubject());
}

@Test(expected = RuntimeException.class)
public void testBadSingleValuedTriple() throws RepositoryException {
when(mockProperty.getType()).thenReturn(URI);
when(mockValue.getType()).thenReturn(URI);
when(mockValue.getString()).thenThrow(
new RepositoryException("Bad value!"));
createSingleValuedLiteralTriple();
}

@Test
public void testMultiValuedResourceTriple() throws RepositoryException {

Expand Down Expand Up @@ -318,6 +329,17 @@ public void testMultiValuedResourceTriple() throws RepositoryException {
.getSubject());
}

@Test(expected = RepositoryException.class)
public void badProperty() throws AccessDeniedException,
ItemNotFoundException, RepositoryException {
when(mockProperty.getParent()).thenThrow(
new RepositoryException("Bad property!"));
// we exhaust the mock of mockProperty.getParent() to replace it with an
// exception
mockProperty.getParent();
createSingleValuedLiteralTriple();
}

private Triple createSingleValuedLiteralTriple() throws RepositoryException {

when(mockProperty.isMultiple()).thenReturn(false);
Expand All @@ -329,6 +351,24 @@ private Triple createSingleValuedLiteralTriple() throws RepositoryException {
return t;
}

@Before
public void setUp() throws ValueFormatException, RepositoryException {
initMocks(this);
testPropertyToTriple = new PropertyToTriple(mockGraphSubjects);
when(mockProperty.getValue()).thenReturn(mockValue);
when(mockProperty.getParent()).thenReturn(mockNode);
when(mockProperty.getName()).thenReturn(TEST_PROPERTY_NAME);
when(mockProperty.getSession()).thenReturn(mockSession);
when(mockNode.getPath()).thenReturn(TEST_NODE_PATH);
when(mockGraphSubjects.getGraphSubject(mockNode)).thenReturn(
TEST_NODE_SUBJECT);
when(mockNode.getNode(TEST_NODE_PATH)).thenReturn(mockNode);
}

private <T> Iterator<T> twoValueIterator(final T t, final T t2) {
return ImmutableList.of(t, t2).iterator();
}

private PropertyToTriple testPropertyToTriple;

@Mock
Expand Down Expand Up @@ -357,22 +397,4 @@ private Triple createSingleValuedLiteralTriple() throws RepositoryException {

private static final String TEST_PROPERTY_NAME = "info:predicate";

@Before
public void setUp() throws ValueFormatException, RepositoryException {
initMocks(this);
testPropertyToTriple = new PropertyToTriple(mockGraphSubjects);
when(mockProperty.getValue()).thenReturn(mockValue);
when(mockProperty.getParent()).thenReturn(mockNode);
when(mockProperty.getName()).thenReturn(TEST_PROPERTY_NAME);
when(mockProperty.getSession()).thenReturn(mockSession);
when(mockNode.getPath()).thenReturn(TEST_NODE_PATH);
when(mockGraphSubjects.getGraphSubject(mockNode)).thenReturn(
TEST_NODE_SUBJECT);
when(mockNode.getNode(TEST_NODE_PATH)).thenReturn(mockNode);
}

private <T> Iterator<T> twoValueIterator(final T t, final T t2) {
return ImmutableList.of(t, t2).iterator();
}

}
@@ -0,0 +1,81 @@
/**
* 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.mappings;

import static org.junit.Assert.assertEquals;

import java.util.Iterator;

import org.junit.Test;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;

public class ZippingIteratorTest {

private ZippingIterator<String, String> zip;

private static final String from = "from";

private static final String to = "to";

/*
* We test to see that a ZippingIterator will return results until one or
* the other source iterator is exhausted.
*/

@Test
public void testMoreValuesThanFunctions() {
final Iterator<String> values =
Iterators.forArray(new String[] {from, from});
final Iterator<Function<String, String>> functions =
ImmutableList.of(f).iterator();
zip = new ZippingIterator<String, String>(values, functions);
while (zip.hasNext()) {
assertEquals("Got wrong value!", to, zip.next());
}

}

@Test
public void testMoreFunctionsThanValues() {
final Iterator<String> values = Iterators.forArray(new String[] {from});
final Iterator<Function<String, String>> functions =
ImmutableList.of(f, f).iterator();
zip = new ZippingIterator<String, String>(values, functions);
while (zip.hasNext()) {
assertEquals("Got wrong value!", to, zip.next());
}

}

private static final Function<String, String> f =
new Function<String, String>() {

@Override
public String apply(final String input) {
if (input == from) {
return to;
} else {
throw new AssertionError("Received 'impossible' input!");
}
}

};

}

0 comments on commit 58a7a1f

Please sign in to comment.