Skip to content

Commit

Permalink
unit testing LegacyMethod in fcrepo-jms
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Apr 29, 2013
1 parent c42bb5c commit e83b481
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 26 deletions.
Expand Up @@ -53,6 +53,12 @@ public class LegacyMethod {

//TODO get this out of the build properties
public static final String SERVER_VERSION = "4.0.0-SNAPSHOT";

public static final String FEDORA_ID_SCHEME = "xsd:string";

public static final String DSID_CATEGORY_LABEL = "fedora-types:dsID";

public static final String PID_CATEGORY_LABEL = "fedora-types:pid";

private static final String TYPES_NS =
"http://www.fedora.info/definitions/1/0/types/";
Expand Down Expand Up @@ -158,51 +164,54 @@ public void setMethodName(final String val) {
public String getMethodName() {
return delegate.getTitle();
}

public void setPid(final String val) {
final List<Category> vals = delegate.getCategories("fedora-types:pid");
if (vals == null || vals.isEmpty()) {
delegate.addCategory("xsd:string", val, "fedora-types:pid");
} else {
vals.get(0).setTerm(val);

private void setLabelledCategory(String label, String val) {
final List<Category> vals = delegate.getCategories(FEDORA_ID_SCHEME);
Category found = null;
if (vals != null && !vals.isEmpty()) {
for(Category c: vals) {
if (label.equals(c.getLabel())) {
found = c.setTerm(val);
}
}
}
if (found == null) {
delegate.addCategory(FEDORA_ID_SCHEME, val, label);
}
delegate.setSummary(val);
}

public String getPid() {
final List<Category> categories = delegate.getCategories("xsd:string");
private String getLabelledCategory(String label) {
final List<Category> categories = delegate.getCategories(FEDORA_ID_SCHEME);
for (final Category c : categories) {
if ("fedora-types:pid".equals(c.getLabel())) {
if (label.equals(c.getLabel())) {
return c.getTerm();
}
}
return null;
}

public void setPid(final String val) {
setLabelledCategory(PID_CATEGORY_LABEL, val);
delegate.setSummary(val);
}

public String getPid() {
return getLabelledCategory(PID_CATEGORY_LABEL);
}

public void setDsId(final String val) {
final List<Category> vals = delegate.getCategories("fedora-types:dsID");
if (vals == null || vals.isEmpty()) {
delegate.addCategory("xsd:string", val, "fedora-types:dsID");
} else {
vals.get(0).setTerm(val);
}
setLabelledCategory(DSID_CATEGORY_LABEL, val);
}

public String getDsId() {
final List<Category> categories = delegate.getCategories("xsd:string");
for (final Category c : categories) {
if ("fedora-types:dsID".equals(c.getLabel())) {
return c.getTerm();
}
}
return null;
return getLabelledCategory(DSID_CATEGORY_LABEL);
}

public void writeTo(final Writer writer) throws IOException {
delegate.writeTo(writer);
}

private static Entry newEntry() {
static Entry newEntry() {
final Entry entry = abdera.newEntry();
entry.declareNS(XSD_NS, "xsd");
entry.declareNS(TYPES_NS, "fedora-types");
Expand Down
@@ -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");
}
}
@@ -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");
}
}

0 comments on commit e83b481

Please sign in to comment.