Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
example unit test, with a mocking helper to support continued JCR int…
…eractions at API level
- Loading branch information
Showing
6 changed files
with
105 additions
and
6 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
47 changes: 47 additions & 0 deletions
47
fcrepo-http-api/src/test/java/org/fcrepo/api/FedoraDatastreamsTest.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,47 @@ | ||
package org.fcrepo.api; | ||
|
||
import static org.fcrepo.TestHelpers.mockDatastreamNode; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.jcr.NodeIterator; | ||
import javax.jcr.RepositoryException; | ||
|
||
import org.fcrepo.jaxb.responses.access.ObjectDatastreams; | ||
import org.fcrepo.services.DatastreamService; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class FedoraDatastreamsTest { | ||
|
||
FedoraDatastreams testObj; | ||
|
||
DatastreamService mockDatastreams; | ||
|
||
@Before | ||
public void setUp() { | ||
mockDatastreams = mock(DatastreamService.class); | ||
testObj = new FedoraDatastreams(); | ||
testObj.datastreamService = mockDatastreams; | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
|
||
} | ||
|
||
@Test | ||
public void testGetDatastreams() throws RepositoryException, IOException { | ||
String pid = "FedoraDatastreamsTest1"; | ||
String dsid = "testDS"; | ||
NodeIterator mockIter = mockDatastreamNode(dsid, "asdf"); | ||
when(mockDatastreams.getDatastreamsFor(pid)).thenReturn(mockIter); | ||
ObjectDatastreams actual = testObj.getDatastreams(pid); | ||
verify(mockDatastreams).getDatastreamsFor(pid); | ||
assertEquals(1, actual.datastreams.size()); | ||
assertEquals(dsid, actual.datastreams.iterator().next().dsid); | ||
} | ||
} |
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
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,34 @@ | ||
package org.fcrepo; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
import java.io.IOException; | ||
import java.io.StringBufferInputStream; | ||
|
||
import javax.jcr.Node; | ||
import javax.jcr.NodeIterator; | ||
import javax.jcr.Property; | ||
import javax.jcr.RepositoryException; | ||
|
||
import org.modeshape.jcr.api.Binary; | ||
import org.modeshape.jcr.api.JcrConstants; | ||
|
||
public class TestHelpers { | ||
public static NodeIterator mockDatastreamNode(String dsId, String content) throws RepositoryException, IOException { | ||
Node mockDsNode = mock(Node.class); | ||
Node mockContent = mock(Node.class); | ||
Property mockData = mock(Property.class); | ||
Binary mockBinary = mock(Binary.class); | ||
when(mockDsNode.getName()).thenReturn(dsId); | ||
when(mockDsNode.getNode(JcrConstants.JCR_CONTENT)).thenReturn(mockContent); | ||
when(mockContent.getProperty(JcrConstants.JCR_DATA)).thenReturn(mockData); | ||
when(mockData.getBinary()).thenReturn(mockBinary); | ||
when(mockBinary.getMimeType()).thenReturn("binary/octet-stream"); | ||
when(mockBinary.getStream()).thenReturn(new StringBufferInputStream(content)); | ||
NodeIterator mockIter = mock(NodeIterator.class); | ||
when(mockIter.hasNext()).thenReturn(true, false); | ||
when(mockIter.next()).thenReturn(mockDsNode); | ||
when(mockIter.nextNode()).thenReturn(mockDsNode); | ||
return mockIter; | ||
} | ||
} |