Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit testing LegacyMethod in fcrepo-jms
- Loading branch information
Showing
3 changed files
with
254 additions
and
26 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
57 changes: 57 additions & 0 deletions
57
fcrepo-jms/src/test/java/org/fcrepo/messaging/legacy/LegacyMethodEventFactoryTest.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,57 @@ | ||
package org.fcrepo.messaging.legacy; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.powermock.api.mockito.PowerMockito.whenNew; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.jcr.nodetype.NodeType; | ||
import javax.jcr.observation.Event; | ||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.jms.JMSException; | ||
import javax.jms.Message; | ||
import javax.jms.TextMessage; | ||
|
||
import org.fcrepo.utils.FedoraJcrTypes; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({LegacyMethodEventFactory.class}) | ||
public class LegacyMethodEventFactoryTest { | ||
|
||
private LegacyMethodEventFactory testObj; | ||
|
||
@Before | ||
public void setUp() { | ||
testObj = new LegacyMethodEventFactory(); | ||
} | ||
|
||
@Test | ||
public void testGetMessage() throws Exception { | ||
|
||
String testPath = "/foo/bar"; | ||
javax.jms.Session mockJMS = mock(javax.jms.Session.class); | ||
TextMessage mockText = mock(TextMessage.class); | ||
when(mockJMS.createTextMessage(anyString())).thenReturn(mockText); | ||
Event mockEvent = mock(Event.class); | ||
when(mockEvent.getPath()).thenReturn(testPath); | ||
Session mockJCR = mock(Session.class); | ||
Node mockSource = mock(Node.class); | ||
NodeType mockType = mock(NodeType.class); | ||
when(mockType.getName()).thenReturn(FedoraJcrTypes.FEDORA_OBJECT); | ||
NodeType[] mockTypes = new NodeType[]{mockType}; | ||
when(mockSource.getMixinNodeTypes()).thenReturn(mockTypes); | ||
when(mockJCR.getNode(testPath)).thenReturn(mockSource); | ||
LegacyMethod mockMethod = mock(LegacyMethod.class); | ||
whenNew(LegacyMethod.class).withArguments(mockEvent, mockSource).thenReturn(mockMethod); | ||
when(mockMethod.getMethodName()).thenReturn("foo-method"); | ||
Message actual = testObj.getMessage(mockEvent, mockJCR, mockJMS); | ||
verify(mockText).setStringProperty("methodName", "foo-method"); | ||
} | ||
} |
162 changes: 162 additions & 0 deletions
162
fcrepo-jms/src/test/java/org/fcrepo/messaging/legacy/LegacyMethodTest.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,162 @@ | ||
package org.fcrepo.messaging.legacy; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.nodetype.NodeType; | ||
import javax.jcr.observation.Event; | ||
|
||
import javax.jms.JMSException; | ||
import javax.jms.Message; | ||
|
||
import org.apache.abdera.model.Category; | ||
import org.apache.abdera.model.Entry; | ||
import org.apache.abdera.model.Person; | ||
import org.apache.abdera.model.Text; | ||
import org.fcrepo.utils.FedoraJcrTypes; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({LegacyMethod.class}) | ||
public class LegacyMethodTest { | ||
|
||
private LegacyMethod testObj; | ||
|
||
private Entry mockDelegate; | ||
|
||
private Event mockEvent; | ||
|
||
private Node mockSource; | ||
|
||
private Category mockPidCategory; | ||
|
||
private Category mockDsidCategory; | ||
|
||
private static final String SOURCE_DSID = "sourceDsid"; | ||
|
||
private static final String SOURCE_PID = "sourcePid"; | ||
|
||
@Before | ||
public void setUp() throws RepositoryException { | ||
// set up the supporting mocks for the constructor | ||
mockEvent = mock(Event.class); | ||
Node mockParent = mock(Node.class); | ||
when(mockParent.getName()).thenReturn(SOURCE_PID); | ||
mockSource = mock(Node.class); | ||
when(mockSource.getName()).thenReturn(SOURCE_DSID); | ||
when(mockSource.getParent()).thenReturn(mockParent); | ||
NodeType mockDSType = mock(NodeType.class); | ||
when(mockDSType.getName()).thenReturn(FedoraJcrTypes.FEDORA_DATASTREAM); | ||
NodeType[] mockTypes = new NodeType[]{mockDSType}; | ||
when(mockSource.getMixinNodeTypes()).thenReturn(mockTypes); | ||
mockDelegate = mock(Entry.class); | ||
Text mockText = mock(Text.class); | ||
when(mockDelegate.setTitle(anyString())).thenReturn(mockText); | ||
// make sure the delegate Entry can be instrumented for tests | ||
PowerMockito.mockStatic(LegacyMethod.class); | ||
when(LegacyMethod.newEntry()).thenReturn(mockDelegate); | ||
mockPidCategory = mock(Category.class); | ||
when(mockPidCategory.getLabel()).thenReturn(LegacyMethod.PID_CATEGORY_LABEL); | ||
when(mockPidCategory.getTerm()).thenReturn(SOURCE_PID); | ||
mockDsidCategory = mock(Category.class); | ||
when(mockDsidCategory.getLabel()).thenReturn(LegacyMethod.DSID_CATEGORY_LABEL); | ||
when(mockDsidCategory.getTerm()).thenReturn(SOURCE_DSID); | ||
List<Category> categories = Arrays.asList(new Category[]{mockPidCategory, mockDsidCategory}); | ||
when(mockDelegate.getCategories(LegacyMethod.FEDORA_ID_SCHEME)).thenReturn(categories); | ||
// construct the test object | ||
testObj = new LegacyMethod(mockEvent, mockSource); | ||
} | ||
|
||
public void testCanParse() throws JMSException { | ||
// Should the static tests be broken out into a separate test class, so we can use PowerMock with better scope? | ||
Message mockYes = mock(Message.class); | ||
when(mockYes.getJMSType()).thenReturn(LegacyMethod.FORMAT); | ||
when(mockYes.getStringProperty("methodName")).thenReturn("ingest"); | ||
assertEquals(true, LegacyMethod.canParse(mockYes)); | ||
Message mockNoFormat = mock(Message.class); | ||
when(mockYes.getJMSType()).thenReturn("crazyType"); | ||
when(mockYes.getStringProperty("methodName")).thenReturn("ingest"); | ||
assertEquals(false, LegacyMethod.canParse(mockNoFormat)); | ||
Message mockNoMessage = mock(Message.class); | ||
when(mockYes.getJMSType()).thenReturn(LegacyMethod.FORMAT); | ||
when(mockYes.getStringProperty("methodName")).thenReturn("destroyEverything"); | ||
assertEquals(false, LegacyMethod.canParse(mockNoMessage)); | ||
} | ||
|
||
@Test | ||
public void testPidAccessors() { | ||
String newPid = "newPid"; | ||
assertEquals(SOURCE_PID, testObj.getPid()); | ||
testObj.setPid(newPid); | ||
verify(mockPidCategory).setTerm(SOURCE_PID); | ||
verify(mockPidCategory).setTerm(newPid); | ||
} | ||
|
||
@Test | ||
public void testDsidAccessors() { | ||
String newDsid = "newDsid"; | ||
assertEquals(SOURCE_DSID, testObj.getDsId()); | ||
testObj.setDsId(newDsid); | ||
verify(mockDsidCategory).setTerm(SOURCE_DSID); | ||
verify(mockDsidCategory).setTerm(newDsid); | ||
} | ||
|
||
@Test | ||
public void textGetEntry() { | ||
Entry mockEntry = mock(Entry.class); | ||
LegacyMethod to = new LegacyMethod(mockEntry); | ||
assertEquals(mockEntry, to.getEntry()); | ||
} | ||
|
||
@Test | ||
public void testMethodNameAccessors() { | ||
when(mockDelegate.getTitle()).thenReturn("foo"); | ||
testObj.getMethodName(); | ||
// called once in the constructor, once in the accessor | ||
verify(mockDelegate, times(2)).getTitle(); | ||
testObj.setMethodName("foo"); | ||
Text mockText = mock(Text.class); | ||
when(mockDelegate.setTitle(anyString())).thenReturn(mockText); | ||
String newTitle = "bar"; | ||
testObj.setMethodName(newTitle); | ||
verify(mockDelegate.setTitle(newTitle)); | ||
} | ||
|
||
@Test | ||
public void testModifiedAccesors() { | ||
testObj.getModified(); | ||
verify(mockDelegate).getUpdated(); | ||
testObj.setModified(new Date()); | ||
// called once in the constructor, once in the accessor | ||
verify(mockDelegate, times(2)).setUpdated(any(Date.class)); | ||
} | ||
|
||
@Test | ||
public void testUserIdAccessors() { | ||
Person mockPerson = mock(Person.class); | ||
when(mockDelegate.getAuthor()).thenReturn(mockPerson); | ||
testObj.getUserID(); | ||
testObj.setUserId("foo"); | ||
verify(mockDelegate).addAuthor(eq("foo"), anyString(), anyString()); | ||
testObj.setUserId(null); | ||
// called once in the constructor, once in the accessor | ||
verify(mockDelegate, times(2)).addAuthor(eq("unknown"), anyString(), anyString()); | ||
} | ||
|
||
@Test | ||
public void testSetContent() { | ||
testObj.setContent("foo"); | ||
verify(mockDelegate).setContent("foo"); | ||
} | ||
} |