Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jul 15, 2013
1 parent dfad68b commit 2493d13
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 4 deletions.
Expand Up @@ -83,7 +83,7 @@ public List<Map<String, Collection<String>>> evaluateLdpathProgram(
final String path = toPath(pathList);
final FedoraResource object = nodeService.getObject(session, path);

final InputStream ldpathProgram = LDPathService.getLdpathProgram(object.getNode(), program);
final InputStream ldpathProgram = LDPathService.getNodeTypeSpecificLdpathProgram(object.getNode(), program);

return evaluateLdpathProgram(object, ldpathProgram);
} finally {
Expand Down
Expand Up @@ -95,7 +95,7 @@ public void initialize() throws RepositoryException, IOException {
* @return
* @throws RepositoryException
*/
public static InputStream getLdpathProgram(final Node node, final String program) throws RepositoryException {
public static InputStream getNodeTypeSpecificLdpathProgram(final Node node, final String program) throws RepositoryException {

final Node programNode = node.getSession().getNode(CONFIGURATION_FOLDER + program);

Expand Down Expand Up @@ -123,7 +123,7 @@ public static InputStream getLdpathProgram(final Node node, final String program
* @throws LDPathParseException
*/
public static Map<String, Collection<?>> programQuery(final Dataset dataset, final Reader reader) throws RepositoryException, LDPathParseException {
final LDPath<RDFNode> ldpathForResource = getLdpathForResource(dataset);
final LDPath<RDFNode> ldpathForResource = getLdpathResource(dataset);

final Resource context = ResourceFactory.createResource(getDatasetSubject(dataset).getURI());

Expand All @@ -136,7 +136,7 @@ public static Map<String, Collection<?>> programQuery(final Dataset dataset, fin
* @return
* @throws RepositoryException
*/
private static LDPath<RDFNode> getLdpathForResource(final Dataset dataset) throws RepositoryException {
private static LDPath<RDFNode> getLdpathResource(final Dataset dataset) throws RepositoryException {

final Model model = unifyDatasetModel(dataset);

Expand Down
@@ -0,0 +1,121 @@
package org.fcrepo.transform.services;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.util.Symbol;
import org.apache.marmotta.ldpath.exception.LDPathParseException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

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

import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Collection;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

public class LDPathServiceTest {

@Mock
Node mockNode;

@Mock
Session mockSession;

@Before
public void setUp() throws RepositoryException {
initMocks(this);

when(mockNode.getSession()).thenReturn(mockSession);
}

@Test
public void testGetNodeTypeSpecificLdpathProgramForMissingProgram() throws RepositoryException {
final Node mockConfigNode = mock(Node.class);
when(mockSession.getNode(LDPathService.CONFIGURATION_FOLDER + "some-program")).thenReturn(mockConfigNode);

final NodeType mockNodeType = mock(NodeType.class);
final NodeType mockNtBase = mock(NodeType.class);
when(mockNodeType.getSupertypes()).thenReturn(new NodeType[] { mockNtBase });
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
final InputStream nodeTypeSpecificLdpathProgramStream = LDPathService.getNodeTypeSpecificLdpathProgram(mockNode, "some-program");

assertNull(nodeTypeSpecificLdpathProgramStream);
}

@Test
public void testGetNodeTypeSpecificLdpathProgramForNodeTypeProgram() throws RepositoryException {
final Node mockConfigNode = mock(Node.class);
final Node mockTypeConfigNode = mock(Node.class, Mockito.RETURNS_DEEP_STUBS);
when(mockSession.getNode(LDPathService.CONFIGURATION_FOLDER + "some-program")).thenReturn(mockConfigNode);

final NodeType mockNodeType = mock(NodeType.class);
final NodeType mockNtBase = mock(NodeType.class);
when(mockNodeType.getSupertypes()).thenReturn(new NodeType[] { mockNtBase });
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNodeType.toString()).thenReturn("custom:type");
when(mockConfigNode.hasNode("custom:type")).thenReturn(true);
when(mockConfigNode.getNode("custom:type")).thenReturn(mockTypeConfigNode);
final InputStream mockInputStream = mock(InputStream.class);
when(mockTypeConfigNode.getNode("jcr:content").getProperty("jcr:data").getBinary().getStream()).thenReturn(mockInputStream);
final InputStream nodeTypeSpecificLdpathProgramStream = LDPathService.getNodeTypeSpecificLdpathProgram(mockNode, "some-program");

assertEquals(mockInputStream, nodeTypeSpecificLdpathProgramStream);
}

@Test
public void testGetNodeTypeSpecificLdpathProgramForSupertypeProgram() throws RepositoryException {
final Node mockConfigNode = mock(Node.class);
final Node mockTypeConfigNode = mock(Node.class, Mockito.RETURNS_DEEP_STUBS);
when(mockSession.getNode(LDPathService.CONFIGURATION_FOLDER + "some-program")).thenReturn(mockConfigNode);

final NodeType mockNodeType = mock(NodeType.class);
final NodeType mockNtBase = mock(NodeType.class);
when(mockNodeType.getSupertypes()).thenReturn(new NodeType[] { mockNtBase });
when(mockNodeType.toString()).thenReturn("custom:type");
when(mockNtBase.toString()).thenReturn("nt:base");
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);

when(mockConfigNode.hasNode("custom:type")).thenReturn(false);

when(mockConfigNode.hasNode("nt:base")).thenReturn(true);
when(mockConfigNode.getNode("nt:base")).thenReturn(mockTypeConfigNode);

final InputStream mockInputStream = mock(InputStream.class);
when(mockTypeConfigNode.getNode("jcr:content").getProperty("jcr:data").getBinary().getStream()).thenReturn(mockInputStream);

final InputStream nodeTypeSpecificLdpathProgramStream = LDPathService.getNodeTypeSpecificLdpathProgram(mockNode, "some-program");

assertEquals(mockInputStream, nodeTypeSpecificLdpathProgramStream);
}

@Test
public void testProgramQuery() throws LDPathParseException, RepositoryException {

final Model model = ModelFactory.createDefaultModel();
model.add(model.createResource("abc"), model.createProperty("http://purl.org/dc/elements/1.1/title"), model.createLiteral("some-title"));
final Dataset testDataset = DatasetFactory.create(model);
testDataset.getContext().set(Symbol.create("uri"), "abc");
final Reader testReader = new StringReader("title = dc:title :: xsd:string ;");
final Map<String,Collection<?>> stringCollectionMap = LDPathService.programQuery(testDataset, testReader);

assertEquals(1, stringCollectionMap.size());
assertEquals(1, stringCollectionMap.get("title").size());
assertTrue(stringCollectionMap.get("title").contains("some-title"));
}
}

0 comments on commit 2493d13

Please sign in to comment.