Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
PowerMockito to clean up the object test
  • Loading branch information
barmintor committed Apr 1, 2013
1 parent 37a7327 commit 038e8e8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 113 deletions.
56 changes: 4 additions & 52 deletions fcrepo-kernel/src/main/java/org/fcrepo/FedoraObject.java
Expand Up @@ -2,11 +2,9 @@
package org.fcrepo;

import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Iterators.filter;
import static com.yammer.metrics.MetricRegistry.name;
import static org.fcrepo.services.RepositoryService.metrics;
import static org.fcrepo.services.ServiceHelpers.getNodePropertySize;
import static org.fcrepo.utils.FedoraTypesUtils.isFedoraDatastream;
import static org.fcrepo.services.ServiceHelpers.getObjectSize;
import static org.fcrepo.utils.FedoraTypesUtils.isOwned;
import static org.fcrepo.utils.FedoraTypesUtils.map;
import static org.fcrepo.utils.FedoraTypesUtils.nodetype2name;
Expand All @@ -15,18 +13,15 @@

import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.fcrepo.utils.FedoraJcrTypes;
import org.fcrepo.utils.FedoraNodeIterator;
import org.modeshape.jcr.api.JcrTools;

import com.google.common.base.Predicate;
import com.yammer.metrics.Timer;

/**
Expand Down Expand Up @@ -80,28 +75,6 @@ public Node getNode() {
return node;
}

public void setNode(Node node) {
this.node = node;
}

/**
* convenient setter for otherwise injected member
* for unit tests
* @param isowned
*/
public void setIsOwned(Predicate<Node> isowned) {
isOwned = isowned;
}

/**
* convenient setter for otherwise injected member
* for unit tests
* @param isfedoraDatastream
*/
public void setIsFedoraDatastream(Predicate<Node> isfedoraDatastream) {
isFedoraDatastream = isfedoraDatastream;
}

public String getOwnerId() throws RepositoryException {
if (isOwned.apply(node)) {
return node.getProperty(FEDORA_OWNERID).getString();
Expand All @@ -123,7 +96,7 @@ public String getLabel() throws RepositoryException {
if (node.hasProperty(DC_TITLE)) {
Property dcTitle = node.getProperty(DC_TITLE);
if (!dcTitle.isMultiple()) {
return node.getProperty(DC_TITLE).getString();
return dcTitle.getString();
} else {
return on('/').join(map(dcTitle.getValues(), value2string));
}
Expand All @@ -136,11 +109,11 @@ public void setLabel(String label) throws RepositoryException {
}

public String getCreated() throws RepositoryException {
return node.getProperty("jcr:created").getString();
return node.getProperty(JCR_CREATED).getString();
}

public String getLastModified() throws RepositoryException {
return node.getProperty("jcr:lastModified").getString();
return node.getProperty(JCR_LASTMODIFIED).getString();
}

public long getSize() throws RepositoryException {
Expand All @@ -151,25 +124,4 @@ public Collection<String> getModels() throws RepositoryException {
return map(node.getMixinNodeTypes(), nodetype2name);
}

/**
* @param obj
* @return object size in bytes
* @throws RepositoryException
*/
static Long getObjectSize(Node obj) throws RepositoryException {
return getNodePropertySize(obj) + getObjectDSSize(obj);
}

/**
* @param obj
* @return object's datastreams' total size in bytes
* @throws RepositoryException
*/
private static Long getObjectDSSize(Node obj) throws RepositoryException {
Long size = 0L;
for (final Iterator<Node> i = filter(new FedoraNodeIterator(obj.getNodes()),isFedoraDatastream); i.hasNext();) {
size += new Datastream(i.next()).getSize();
}
return size;
}
}
119 changes: 58 additions & 61 deletions fcrepo-kernel/src/test/java/org/fcrepo/FedoraObjectTest.java
Expand Up @@ -2,12 +2,16 @@

import static org.fcrepo.TestHelpers.getContentNodeMock;
import static org.fcrepo.services.PathService.getDatastreamJcrNodePath;
import static org.fcrepo.services.PathService.getObjectJcrNodePath;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;


import java.util.Collection;
import java.util.Date;
Expand All @@ -23,14 +27,22 @@
import javax.jcr.nodetype.NodeType;

import org.fcrepo.services.ObjectService;
import org.fcrepo.services.ServiceHelpers;
import org.fcrepo.utils.FedoraJcrTypes;
import org.fcrepo.utils.FedoraTypesUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.api.Repository;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.google.common.base.Predicate;

@RunWith(PowerMockRunner.class)
@PrepareForTest( { ServiceHelpers.class })
public class FedoraObjectTest implements FedoraJcrTypes {

String testPid = "testObj";
Expand All @@ -47,44 +59,44 @@ public class FedoraObjectTest implements FedoraJcrTypes {

Node mockRootNode;

Node mockDsNode;
Node mockObjNode;

FedoraObject testObj;
FedoraObject testFedoraObject;

NodeType[] mockNodetypes;

Predicate<Node> isOwned;


@Before
public void setUp() throws LoginException, RepositoryException {
String relPath = getDatastreamJcrNodePath(testPid, testDsId).substring(1);
this.isOwned = FedoraTypesUtils.isOwned;
String relPath = getObjectJcrNodePath(testPid).substring(1);

mockSession = mock(Session.class);
mockRootNode = mock(Node.class);
mockDsNode = mock(Node.class);
Predicate<Node> isOwned = mock(Predicate.class);
Predicate<Node> mockIsFedoraDatastream = mock(Predicate.class);
mockObjNode = mock(Node.class);
Predicate<Node> mockPredicate = mock(Predicate.class);

try{

when(mockDsNode.getName()).thenReturn(testDsId);
when(mockDsNode.getSession()).thenReturn(mockSession);
when(mockObjNode.getName()).thenReturn(testPid);
when(mockObjNode.getSession()).thenReturn(mockSession);
when(mockSession.getRootNode()).thenReturn(mockRootNode);
when(mockRootNode.getNode(relPath)).thenReturn(mockDsNode);
when(mockRootNode.getNode(relPath)).thenReturn(mockObjNode);
when(mockSession.getUserID()).thenReturn(mockUser);
testObj = new FedoraObject(mockSession, relPath);
testFedoraObject = new FedoraObject(mockSession, relPath);

verify(mockRootNode).getNode(relPath);

mockNodetypes = new NodeType[2];
mockNodetypes[0] = mock(NodeType.class);
mockNodetypes[1] = mock(NodeType.class);

when(mockDsNode.getMixinNodeTypes()).thenReturn(mockNodetypes);
testObj.setNode(mockDsNode);
testObj.setIsOwned(isOwned);
testObj.setIsFedoraDatastream(mockIsFedoraDatastream);
when(isOwned.apply(mockDsNode)).thenReturn(true);
when(mockIsFedoraDatastream.apply(mockDsNode)).thenReturn(true);
when(mockObjNode.getMixinNodeTypes()).thenReturn(mockNodetypes);

when(mockPredicate.apply(mockObjNode)).thenReturn(true);
FedoraTypesUtils.isOwned = mockPredicate;

} catch(RepositoryException e) {
e.printStackTrace();
Expand All @@ -97,95 +109,80 @@ public void setUp() throws LoginException, RepositoryException {
public void tearDown() {
mockSession = null;
mockRootNode = null;
mockDsNode = null;
mockObjNode = null;
if (this.isOwned != null){
FedoraTypesUtils.isOwned = this.isOwned;
}
}

@Test
public void testGetName() throws RepositoryException {
assertEquals(testObj.getName(), testDsId);
assertEquals(testFedoraObject.getName(), testPid);
}

@Test
public void testGetNode() {
assertEquals(testObj.getNode(), mockDsNode);
assertEquals(testFedoraObject.getNode(), mockObjNode);
}

@Test
public void testSetOwnerId() throws RepositoryException {
Property mockProp = mock(Property.class);
when(mockDsNode.getProperty(FEDORA_OWNERID)).thenReturn(mockProp);
when(mockProp.getString()).thenReturn(mockUser);
testObj.setOwnerId(mockUser);
String expected = mockDsNode.getProperty(FEDORA_OWNERID).getString();
assertEquals(mockUser, expected);
when(mockObjNode.getProperty(FEDORA_OWNERID)).thenReturn(mockProp);
String expected = "resuKcom";
testFedoraObject.setOwnerId(expected);
verify(mockObjNode).setProperty(FEDORA_OWNERID, expected);
}

@Test
public void testGetOwnerId() throws RepositoryException {
String expected = "asdf";
Property mockProp = mock(Property.class);
Node mockContent = getContentNodeMock(expected);
when(mockDsNode.getNode("jcr:content")).thenReturn(mockContent);
when(mockDsNode.getProperty(FEDORA_OWNERID)).thenReturn(mockProp);
when(mockProp.getString()).thenReturn("mockUser");
String actual = testObj.getOwnerId();
when(mockObjNode.getProperty(FEDORA_OWNERID)).thenReturn(mockProp);
when(mockProp.getString()).thenReturn(mockUser);
String actual = testFedoraObject.getOwnerId();
assertEquals(mockUser, actual);
verify(mockObjNode).getProperty(FEDORA_OWNERID);
}

@Test
public void testGetLabel() throws RepositoryException {
Property mockProp = mock(Property.class);
when(mockDsNode.hasProperty(DC_TITLE)).thenReturn(true);
when(mockDsNode.getProperty(DC_TITLE)).thenReturn(mockProp);
when(mockObjNode.hasProperty(DC_TITLE)).thenReturn(true);
when(mockObjNode.getProperty(DC_TITLE)).thenReturn(mockProp);
when(mockProp.getString()).thenReturn("mockTitle");
String actual = testObj.getLabel();
assertEquals("mockTitle", actual);
testFedoraObject.getLabel();
verify(mockObjNode).getProperty(DC_TITLE);
}

@Test
public void testGetCreated() throws RepositoryException {
Date expected = new Date();
String expectedString = Long.toString(expected.getTime());
Property mockProp = mock(Property.class);
when(mockProp.getString()).thenReturn(expectedString);
when(mockDsNode.getProperty(JCR_CREATED)).thenReturn(mockProp);
String actual = testObj.getCreated();
assertEquals(expectedString, actual);
when(mockProp.getString()).thenReturn("mockDate");
when(mockObjNode.getProperty(JCR_CREATED)).thenReturn(mockProp);
testFedoraObject.getCreated();
verify(mockObjNode).getProperty(JCR_CREATED);
}

@Test
public void testGetLastModified() throws RepositoryException {
Property mockProp = mock(Property.class);
when(mockDsNode.getProperty("jcr:lastModified")).thenReturn(mockProp);
when(mockObjNode.getProperty(JCR_LASTMODIFIED)).thenReturn(mockProp);
when(mockProp.getString()).thenReturn("mockDate");
String actual = testObj.getLastModified();
assertEquals("mockDate", actual);
testFedoraObject.getLastModified();
verify(mockObjNode).getProperty(JCR_LASTMODIFIED);
}

@Test
public void testGetSize() throws RepositoryException {
String expected = "asdf";
Binary mockBinary = mock(Binary.class);
PropertyIterator mockPI = mock(PropertyIterator.class);
NodeIterator mockNI = mock(NodeIterator.class);
Node mockContent = getContentNodeMock(expected);
Property mockProp = mock(Property.class);
when(mockDsNode.getNode("jcr:content")).thenReturn(mockContent);
when(mockDsNode.getProperties()).thenReturn(mockPI);
when(mockPI.hasNext()).thenReturn(true,false);
when(mockPI.nextProperty()).thenReturn(mockProp);
when(mockProp.getBinary()).thenReturn(mockBinary);
when(mockBinary.getSize()).thenReturn(0L);
when(mockDsNode.getNodes()).thenReturn(mockNI);
when(mockNI.hasNext()).thenReturn(true,false);
when(mockNI.nextNode()).thenReturn(mockDsNode);
long actual = testObj.getSize();
assertEquals(4, actual);
PowerMockito.mockStatic(ServiceHelpers.class);
when(ServiceHelpers.getObjectSize(mockObjNode)).thenReturn(-8L); // obviously not a real value
long actual = testFedoraObject.getSize();
assertEquals(-8, actual);
}

@Test
public void testGetModels() throws RepositoryException {
Collection<String> actual = testObj.getModels();
Collection<String> actual = testFedoraObject.getModels();
assertNotNull(actual);
}

Expand Down

0 comments on commit 038e8e8

Please sign in to comment.