mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Added more unit tests for RenditionNodeManager which is used by the RenditionServiceImpl to decide where to create a rendition node and how to handle the old rendition node if one exists.
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@19445 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -18,7 +18,12 @@
|
||||
*/
|
||||
package org.alfresco.repo.rendition.executer;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyMap;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
@@ -26,25 +31,26 @@ import java.util.Map;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.action.ActionImpl;
|
||||
import org.alfresco.repo.action.executer.ActionExecuter;
|
||||
import org.alfresco.repo.rendition.RenditionDefinitionImpl;
|
||||
import org.alfresco.repo.rendition.executer.AbstractRenderingEngine.RenderingContext;
|
||||
import org.alfresco.service.cmr.rendition.RenditionDefinition;
|
||||
import org.alfresco.service.cmr.rendition.RenditionService;
|
||||
import org.alfresco.service.cmr.rendition.RenditionServiceException;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
/**
|
||||
* @author Nick Smith
|
||||
*/
|
||||
public class AbstractRenderingEngineTest extends TestCase
|
||||
{
|
||||
private final NodeRef source = new NodeRef("http://test/sourceId");
|
||||
private ContentService contentService;
|
||||
private NodeService nodeService;
|
||||
private TestRenderingEngine engine;
|
||||
@@ -55,43 +61,24 @@ public class AbstractRenderingEngineTest extends TestCase
|
||||
super.setUp();
|
||||
|
||||
this.contentService = mock(ContentService.class);
|
||||
NodeService nodeService1 = makeNodeService();
|
||||
this.nodeService = nodeService1;
|
||||
this.nodeService = mock(NodeService.class);
|
||||
engine = new TestRenderingEngine();
|
||||
engine.setContentService(contentService);
|
||||
engine.setNodeService(nodeService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a mock node service which fails with a helpful message by default if createNode is called.
|
||||
* @return
|
||||
*/
|
||||
private NodeService makeNodeService()
|
||||
{
|
||||
NodeService nodeService1 = mock(NodeService.class);
|
||||
return nodeService1;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testCreateRenditionNodeAssoc() throws Exception
|
||||
{
|
||||
NodeRef source = new NodeRef("http://test/sourceId");
|
||||
when(nodeService.exists(source)).thenReturn(true);
|
||||
|
||||
QName assocType = ContentModel.ASSOC_CONTAINS;
|
||||
QName assocName = QName.createQName("url", "renditionName");
|
||||
QName nodeType = ContentModel.TYPE_CONTENT;
|
||||
ChildAssociationRef renditionAssoc = mock(ChildAssociationRef.class);
|
||||
ChildAssociationRef renditionAssoc = makeRenditionAssoc();
|
||||
RenditionDefinition definition = makeRenditionDefinition(renditionAssoc);
|
||||
|
||||
// Set up the rendition definition.
|
||||
String id = "definitionId";
|
||||
RenditionDefinition definition = new RenditionDefinitionImpl(id, assocName, TestRenderingEngine.NAME);
|
||||
definition.setRenditionAssociationType(assocType);
|
||||
definition.setRenditionParent(source);
|
||||
|
||||
QName assocType = renditionAssoc.getTypeQName();
|
||||
// Stub the createNode() method to return renditionAssoc.
|
||||
when(nodeService
|
||||
.createNode(eq(source), eq(assocType), any(QName.class), any(QName.class), anyMap()))
|
||||
when(nodeService.createNode(eq(source), eq(assocType), any(QName.class), any(QName.class), anyMap()))
|
||||
.thenReturn(renditionAssoc);
|
||||
engine.execute(definition, source);
|
||||
|
||||
@@ -102,16 +89,17 @@ public class AbstractRenderingEngineTest extends TestCase
|
||||
Map<String, Serializable> props = captor.getValue();
|
||||
|
||||
// Check the node name is set to match teh rendition name local name.
|
||||
assertEquals(assocName.getLocalName(), props.get(ContentModel.PROP_NAME));
|
||||
|
||||
assertEquals(renditionAssoc.getQName().getLocalName(), props.get(ContentModel.PROP_NAME));
|
||||
|
||||
// Check content property name defaults to cm:content
|
||||
assertEquals(ContentModel.PROP_CONTENT, props.get(ContentModel.PROP_CONTENT_PROPERTY_NAME));
|
||||
|
||||
// Check the returned result is the association created by the call to nodeServcie.createNode().
|
||||
// Check the returned result is the association created by the call to
|
||||
// nodeServcie.createNode().
|
||||
Serializable result = definition.getParameterValue(ActionExecuter.PARAM_RESULT);
|
||||
assertEquals("The returned rendition association is not the one created by the node service!",
|
||||
renditionAssoc, result);
|
||||
|
||||
assertEquals("The returned rendition association is not the one created by the node service!", renditionAssoc,
|
||||
result);
|
||||
|
||||
// Check that setting the default content property and default node type
|
||||
// on the rendition engine works.
|
||||
nodeType = QName.createQName("url", "someNodeType");
|
||||
@@ -120,7 +108,7 @@ public class AbstractRenderingEngineTest extends TestCase
|
||||
engine.setDefaultRenditionNodeType(nodeType.toString());
|
||||
engine.execute(definition, source);
|
||||
verify(nodeService).createNode(eq(source), eq(assocType), any(QName.class), eq(nodeType), captor.capture());
|
||||
props=captor.getValue();
|
||||
props = captor.getValue();
|
||||
assertEquals(contentPropName, props.get(ContentModel.PROP_CONTENT_PROPERTY_NAME));
|
||||
|
||||
// Check that settign the rendition node type param works.
|
||||
@@ -130,15 +118,184 @@ public class AbstractRenderingEngineTest extends TestCase
|
||||
definition.setParameterValue(AbstractRenderingEngine.PARAM_TARGET_CONTENT_PROPERTY, contentPropName);
|
||||
engine.execute(definition, source);
|
||||
verify(nodeService).createNode(eq(source), eq(assocType), any(QName.class), eq(nodeType), captor.capture());
|
||||
props=captor.getValue();
|
||||
props = captor.getValue();
|
||||
assertEquals(contentPropName, props.get(ContentModel.PROP_CONTENT_PROPERTY_NAME));
|
||||
}
|
||||
|
||||
public void testCheckActionIsRenditionDefinition()
|
||||
{
|
||||
ActionImpl action = new ActionImpl(null, "actionId", TestRenderingEngine.NAME);
|
||||
try
|
||||
{
|
||||
engine.executeImpl(action, source);
|
||||
fail("Should have thrown an exception here!");
|
||||
} catch(RenditionServiceException e)
|
||||
{
|
||||
String expMsg = "02160000 Cannot execute action as it is not a RenditionDefinition: Action[ id=actionId, node=null ]";
|
||||
assertEquals(expMsg, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testCheckSourceNodeExists()
|
||||
{
|
||||
when(nodeService.exists(any(NodeRef.class))).thenReturn(false);
|
||||
RenditionDefinitionImpl definition = new RenditionDefinitionImpl("id", null, TestRenderingEngine.NAME);
|
||||
try
|
||||
{
|
||||
engine.executeImpl(definition, source);
|
||||
fail("Should have thrown an exception here!");
|
||||
} catch(RenditionServiceException e)
|
||||
{
|
||||
String expMsg = "02160001 Cannot execute action as node does not exist: http://test/sourceId";
|
||||
assertEquals(expMsg, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testRenderingContext()
|
||||
{
|
||||
when(nodeService.exists(source)).thenReturn(true);
|
||||
ChildAssociationRef renditionAssoc = makeRenditionAssoc();
|
||||
RenditionDefinition definition = makeRenditionDefinition(renditionAssoc);
|
||||
// Stub the createNode() method to return renditionAssoc.
|
||||
when(nodeService.createNode(eq(source), eq(renditionAssoc.getTypeQName()), any(QName.class), any(QName.class), anyMap()))
|
||||
.thenReturn(renditionAssoc);
|
||||
engine.execute(definition, source);
|
||||
|
||||
RenderingContext context = engine.getContext();
|
||||
assertEquals(definition, context.getDefinition());
|
||||
assertEquals(renditionAssoc.getChildRef(), context.getDestinationNode());
|
||||
assertEquals(source, context.getSourceNode());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testGetParameterWithDefault()
|
||||
{
|
||||
when(nodeService.exists(source)).thenReturn(true);
|
||||
ChildAssociationRef renditionAssoc = makeRenditionAssoc();
|
||||
RenditionDefinition definition = makeRenditionDefinition(renditionAssoc);
|
||||
// Stub the createNode() method to return renditionAssoc.
|
||||
when(nodeService.createNode(eq(source), eq(renditionAssoc.getTypeQName()), any(QName.class), any(QName.class), anyMap()))
|
||||
.thenReturn(renditionAssoc);
|
||||
engine.executeImpl(definition, source);
|
||||
RenderingContext context = engine.getContext();
|
||||
|
||||
// Check default value works.
|
||||
String paramName = "Some-param";
|
||||
String defaultValue = "default";
|
||||
Object result = context.getParamWithDefault(paramName, defaultValue);
|
||||
assertEquals(defaultValue, result);
|
||||
|
||||
// Check specific value overrides default.
|
||||
String value = "value";
|
||||
definition.setParameterValue(paramName, value);
|
||||
engine.executeImpl(definition, source);
|
||||
context = engine.getContext();
|
||||
result = context.getParamWithDefault(paramName, defaultValue);
|
||||
assertEquals(value, result);
|
||||
|
||||
// Check null default value throws exception.
|
||||
try
|
||||
{
|
||||
result = context.getParamWithDefault(paramName, null);
|
||||
fail("Should throw an Exception if default value is null!");
|
||||
} catch(RenditionServiceException e)
|
||||
{
|
||||
String msg = "02160002 The defaultValue cannot be null!";
|
||||
assertEquals(msg, e.getMessage());
|
||||
}
|
||||
|
||||
// Check wrong type of default value throws exception.
|
||||
try
|
||||
{
|
||||
result = context.getParamWithDefault(paramName, Boolean.TRUE);
|
||||
fail("Should throw an exception if default value is of incoorect type!");
|
||||
} catch (RenditionServiceException e)
|
||||
{
|
||||
String msg = "02160003 The parameter: Some-param must be of type: java.lang.Booleanbut was of type: java.lang.String";
|
||||
assertEquals(msg, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testGetCheckedParameter()
|
||||
{
|
||||
when(nodeService.exists(source)).thenReturn(true);
|
||||
ChildAssociationRef renditionAssoc = makeRenditionAssoc();
|
||||
RenditionDefinition definition = makeRenditionDefinition(renditionAssoc);
|
||||
// Stub the createNode() method to return renditionAssoc.
|
||||
when(nodeService.createNode(eq(source), eq(renditionAssoc.getTypeQName()), any(QName.class), any(QName.class), anyMap()))
|
||||
.thenReturn(renditionAssoc);
|
||||
engine.executeImpl(definition, source);
|
||||
RenderingContext context = engine.getContext();
|
||||
String paramName = "Some param";
|
||||
|
||||
// Check returns null by default.
|
||||
String result = context.getCheckedParam(paramName, String.class);
|
||||
assertNull(result);
|
||||
|
||||
// Check can set a value to return.
|
||||
String value = "value";
|
||||
definition.setParameterValue(paramName, value);
|
||||
engine.executeImpl(definition, source);
|
||||
context = engine.getContext();
|
||||
result = context.getCheckedParam(paramName, String.class);
|
||||
assertEquals(value, result);
|
||||
|
||||
// Check throws an exception if value is of wrong type.
|
||||
try
|
||||
{
|
||||
context.getCheckedParam(paramName, Boolean.class);
|
||||
fail("Should throw an exception if type is wrong!");
|
||||
} catch(RenditionServiceException e)
|
||||
{
|
||||
String msg = "02160004 The parameter: Some param must be of type: java.lang.Booleanbut was of type: java.lang.String";
|
||||
assertEquals(msg, e.getMessage());
|
||||
}
|
||||
|
||||
// Check throws an exception if value is of wrong type.
|
||||
try
|
||||
{
|
||||
context.getCheckedParam(paramName, null);
|
||||
fail("Should throw an exception if type is wrong!");
|
||||
} catch(RenditionServiceException e)
|
||||
{
|
||||
String msg = "02160005 The class must not be null!";
|
||||
assertEquals(msg, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the rendition definition.
|
||||
* @param renditionAssoc
|
||||
* @return
|
||||
*/
|
||||
private RenditionDefinition makeRenditionDefinition(ChildAssociationRef renditionAssoc)
|
||||
{
|
||||
String id = "definitionId";
|
||||
RenditionDefinition definition = new RenditionDefinitionImpl(id, renditionAssoc.getQName(), TestRenderingEngine.NAME);
|
||||
definition.setRenditionAssociationType(renditionAssoc.getTypeQName());
|
||||
definition.setRenditionParent(source);
|
||||
return definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the rendition association and destination node.
|
||||
*/
|
||||
private ChildAssociationRef makeRenditionAssoc()
|
||||
{
|
||||
|
||||
QName assocType = ContentModel.ASSOC_CONTAINS;
|
||||
QName assocName = QName.createQName("url", "renditionName");
|
||||
NodeRef destination = new NodeRef("http://test/destinationId");
|
||||
return new ChildAssociationRef(assocType, source, assocName, destination);
|
||||
}
|
||||
|
||||
private static class TestRenderingEngine extends AbstractRenderingEngine
|
||||
{
|
||||
private RenderingContext context;
|
||||
|
||||
public static String NAME = "Test";
|
||||
|
||||
private RenderingContext context;
|
||||
|
||||
@Override
|
||||
protected void render(RenderingContext context1)
|
||||
|
Reference in New Issue
Block a user