Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
fcrepo-transform/src/test/java/org/fcrepo/transform/services/LDPathServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |