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:
N Smith
2010-03-22 11:05:04 +00:00
parent 9a6f2ccdae
commit f83eb0d509
4 changed files with 419 additions and 40 deletions

View File

@@ -65,7 +65,7 @@ public class RenditionNodeManager
public ChildAssociationRef findOrCreateRenditionNode()
{
QName renditionName = renditionDefinition.getRenditionName();
// If no rendition already exists create anew rendition node and
// If no rendition already exists create a new rendition node and
// association.
if (oldRendition == null)
{
@@ -94,7 +94,9 @@ public class RenditionNodeManager
private ChildAssociationRef moveRendition(QName associationName)
{
return nodeService.moveNode(oldRendition, location.getParentRef(), ContentModel.ASSOC_CONTAINS, associationName);
NodeRef parent = location.getParentRef();
QName assocType = sourceNode.equals(parent) ? RenditionModel.ASSOC_RENDITION : ContentModel.ASSOC_CONTAINS;
return nodeService.moveNode(oldRendition, parent, assocType, associationName);
}
private void orphanRendition(QNamePattern renditionName)

View File

@@ -0,0 +1,214 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.rendition;
import static org.mockito.Mockito.*;
import org.alfresco.model.ContentModel;
import org.alfresco.model.RenditionModel;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.rendition.RenditionDefinition;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import com.sun.tools.xjc.reader.gbind.SourceNode;
import junit.framework.TestCase;
/**
* @author Nick Smith
*/
public class RenditionNodeManagerTest extends TestCase
{
private final NodeService nodeService = mock(NodeService.class);
private final NodeRef source = new NodeRef("http://test/sourceId");
private final NodeRef destination = new NodeRef("http://test/destinationId");
private final NodeRef oldRendition = new NodeRef("http://test/oldRenditionId");
private final QName renditionName = QName.createQName(NamespaceService.RENDITION_MODEL_1_0_URI, "renditionName");
private final RenditionDefinition definition = new RenditionDefinitionImpl("definitionId", renditionName, "engineName" );
// Check findOrCreateRenditionNode() works when there is
// no old rendition and a destination node is specified and
public void testNoOldRenditionAndDestinationSpecified()
{
ChildAssociationRef parentAssoc = makeAssoc(source, destination, true);
when(nodeService.getPrimaryParent(any(NodeRef.class))).thenReturn(parentAssoc);
RenditionLocation location = new RenditionLocationImpl(source, destination, "destinationName");
RenditionNodeManager manager = new RenditionNodeManager(source, null, location, definition, nodeService);
ChildAssociationRef result = manager.findOrCreateRenditionNode();
assertEquals(parentAssoc, result);
}
// Check findOrCreateRenditionNode() works when there is
// no old rendition and a destination node is not specified.
// the parent node is the source node.
public void testNoOldRenditionAndNoDestinationSpecifiedAndParentIsSource()
{
ChildAssociationRef parentAssoc = makeAssoc(source, destination, true);
when(nodeService.createNode(source, RenditionModel.ASSOC_RENDITION, renditionName, ContentModel.TYPE_CONTENT))
.thenReturn(parentAssoc);
RenditionLocation location = new RenditionLocationImpl(source, null, renditionName.getLocalName());
RenditionNodeManager manager = new RenditionNodeManager(source, null, location, definition, nodeService);
ChildAssociationRef result = manager.findOrCreateRenditionNode();
assertEquals(parentAssoc, result);
}
// Check findOrCreateRenditionNode() works when there is
// no old rendition and a destination node is not specified.
// the parent node is not the source node.
public void testNoOldRenditionAndNoDestinationSpecifiedAndParentIsNotSource()
{
NodeRef parent = new NodeRef("http://test/parentId");
ChildAssociationRef parentAssoc = makeAssoc(parent, destination, false);
when(nodeService.createNode(parent, ContentModel.ASSOC_CONTAINS, renditionName, ContentModel.TYPE_CONTENT))
.thenReturn(parentAssoc);
RenditionLocation location = new RenditionLocationImpl(parent, null, renditionName.getLocalName());
RenditionNodeManager manager = new RenditionNodeManager(source, null, location, definition, nodeService);
ChildAssociationRef result = manager.findOrCreateRenditionNode();
assertEquals(parentAssoc, result);
// Check the rendition association is created.
verify(nodeService).addChild(source, destination, RenditionModel.ASSOC_RENDITION, renditionName);
}
// Check findOrCreateRenditionNode() works when there is
// an old rendition which is specified as the destination
// node in the location.
public void testHasOldRenditionMatchesSpecifiedDestinationNode()
{
RenditionLocation location = new RenditionLocationImpl(source, oldRendition, renditionName.getLocalName());
RenditionNodeManager manager = new RenditionNodeManager(source, oldRendition, location, definition, nodeService);
manager.findOrCreateRenditionNode();
verify(nodeService).getPrimaryParent(oldRendition);
}
// Check findOrCreateRenditionNode() works when there is
// an old rendition which has the specified parent folder.
// If no name is specified and the parent folder is correct then the location should match.
public void testHasOldRenditionCorrectParentNoNameSpecified()
{
NodeRef parent = new NodeRef("http://test/parentId");
ChildAssociationRef parentAssoc = makeAssoc(parent, oldRendition, false);
when(nodeService.getPrimaryParent(oldRendition)).thenReturn(parentAssoc);
RenditionLocation location = new RenditionLocationImpl(parent, null, null);
RenditionNodeManager manager = new RenditionNodeManager(source, oldRendition, location, definition, nodeService);
ChildAssociationRef result = manager.findOrCreateRenditionNode();
assertEquals(parentAssoc, result);
verify(nodeService, times(2)).getPrimaryParent(oldRendition);
}
// Check findOrCreateRenditionNode() works when there is
// an old rendition which has the specified parent folder.
// If the correct name is specified and the parent folder is correct then the location should match.
public void testHasOldRenditionCorrectParentCorrectNameSpecified()
{
String rendName = "Rendition Name";
NodeRef parent = new NodeRef("http://test/parentId");
ChildAssociationRef parentAssoc = makeAssoc(parent, oldRendition, false);
when(nodeService.getPrimaryParent(oldRendition)).thenReturn(parentAssoc);
when(nodeService.getProperty(oldRendition,ContentModel.PROP_NAME ))
.thenReturn(rendName);
RenditionLocationImpl location = new RenditionLocationImpl(parent, null, rendName);
RenditionNodeManager manager = new RenditionNodeManager(source, oldRendition, location, definition, nodeService);
ChildAssociationRef result = manager.findOrCreateRenditionNode();
assertEquals(parentAssoc, result);
verify(nodeService, times(2)).getPrimaryParent(oldRendition);
}
// Check findOrCreateRenditionNode() works when there is
// an old rendition which has the wrong parent folder.
public void testHasOldRenditionWrongParentSpecified()
{
NodeRef parent = new NodeRef("http://test/parentId");
ChildAssociationRef parentAssoc = makeAssoc(parent, oldRendition, false);
ChildAssociationRef sourceAssoc = makeAssoc(source, oldRendition, true);
// The old rendition is under the source node but should be under parent node.
when(nodeService.getPrimaryParent(oldRendition)).thenReturn(sourceAssoc);
when(nodeService.moveNode(oldRendition, parent, ContentModel.ASSOC_CONTAINS, renditionName))
.thenReturn(parentAssoc);
RenditionLocationImpl location = new RenditionLocationImpl(parent, null, null);
RenditionNodeManager manager = new RenditionNodeManager(source, oldRendition, location, definition, nodeService);
ChildAssociationRef result = manager.findOrCreateRenditionNode();
assertEquals(parentAssoc, result);
verify(nodeService).moveNode(oldRendition, parent, ContentModel.ASSOC_CONTAINS, renditionName);
// The old rendition is under the parent node but should be under the source node.
when(nodeService.getPrimaryParent(oldRendition)).thenReturn(parentAssoc);
when(nodeService.moveNode(oldRendition, source, RenditionModel.ASSOC_RENDITION, renditionName))
.thenReturn(sourceAssoc);
location = new RenditionLocationImpl(source, null, null);
manager = new RenditionNodeManager(source, oldRendition, location, definition, nodeService);
result = manager.findOrCreateRenditionNode();
assertEquals(sourceAssoc, result);
verify(nodeService).moveNode(oldRendition, source, RenditionModel.ASSOC_RENDITION, renditionName);
// The old rendition is under the parent node but should be under the new parent node.
NodeRef newParent = new NodeRef("http://test/newParentId");
ChildAssociationRef newParentAssoc = makeAssoc(newParent, oldRendition, false);
AuthenticationService s=null;
when(nodeService.getPrimaryParent(oldRendition)).thenReturn(parentAssoc);
when(nodeService.moveNode(oldRendition, newParent, ContentModel.ASSOC_CONTAINS, renditionName))
.thenReturn(newParentAssoc);
location = new RenditionLocationImpl(newParent, null, null);
manager = new RenditionNodeManager(source, oldRendition, location, definition, nodeService);
result = manager.findOrCreateRenditionNode();
assertEquals(newParentAssoc, result);
verify(nodeService).moveNode(oldRendition, newParent, ContentModel.ASSOC_CONTAINS, renditionName);
}
// Check findOrCreateRenditionNode() works when there is
// an old rendition which has the correct parent folder
// but the wrong name
public void testHasOldRenditionCorrectParentWrongNameSpecified()
{
NodeRef parent = new NodeRef("http://test/parentId");
ChildAssociationRef parentAssoc = makeAssoc(parent, oldRendition, false);
when(nodeService.getPrimaryParent(oldRendition)).thenReturn(parentAssoc);
when(nodeService.moveNode(oldRendition, parent, ContentModel.ASSOC_CONTAINS, renditionName))
.thenReturn(parentAssoc);
when(nodeService.getProperty(oldRendition, ContentModel.PROP_NAME))
.thenReturn("oldName");
String newName = "newName";
RenditionLocationImpl location = new RenditionLocationImpl(parent, null, newName);
RenditionNodeManager manager = new RenditionNodeManager(source, oldRendition, location, definition, nodeService);
ChildAssociationRef result = manager.findOrCreateRenditionNode();
assertEquals(parentAssoc, result);
verify(nodeService).moveNode(oldRendition, parent, ContentModel.ASSOC_CONTAINS, renditionName);
}
private ChildAssociationRef makeAssoc(NodeRef parent, NodeRef child, boolean isRenditionAssoc)
{
QName assocType= isRenditionAssoc? RenditionModel.ASSOC_RENDITION : ContentModel.ASSOC_CONTAINS;
return new ChildAssociationRef(assocType, parent, renditionName, child);
}
}

View File

@@ -51,6 +51,8 @@ import org.alfresco.util.GUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.star.lang.NullPointerException;
/**
* This class adds some new behaviour to the standard ActionExecuterAbstractBase
* in order to support the RenditionService.
@@ -450,8 +452,10 @@ public abstract class AbstractRenderingEngine extends ActionExecuterAbstractBase
return null;
else
{
if(clazz == null)
throw new RenditionServiceException("The class must not be null!", new NullPointerException());
Class<? extends Serializable> valueClass = value.getClass();
if (!valueClass.isAssignableFrom(clazz))
if ( !valueClass.isAssignableFrom(clazz))
{
throw new RenditionServiceException("The parameter: " + paramName + " must be of type: "
+ clazz.getName() + "but was of type: " + valueClass.getName());
@@ -476,6 +480,8 @@ public abstract class AbstractRenderingEngine extends ActionExecuterAbstractBase
@SuppressWarnings("unchecked")
public static <T> T getParamWithDefault(String paramName, T defaultValue, RenditionDefinition definition)
{
if(defaultValue == null)
throw new RenditionServiceException("The defaultValue cannot be null!", new NullPointerException());
Class<? extends T> clazz = (Class<? extends T>) defaultValue.getClass();
T result = getCheckedParam(paramName, clazz, definition);
if (result == null)

View File

@@ -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)