diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationService.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationService.java index 743bb0d1d7..47dc44837a 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationService.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationService.java @@ -21,8 +21,10 @@ package org.alfresco.module.org_alfresco_module_rm.classification; import java.util.List; import java.util.Set; +import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.InvalidNode; import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.LevelIdNotFound; import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.ReasonIdNotFound; +import org.alfresco.service.cmr.repository.InvalidNodeRefException; import org.alfresco.service.cmr.repository.NodeRef; /** @@ -58,7 +60,10 @@ public interface ClassificationService * @param document The node to classify. * @throws LevelIdNotFound If the supplied level id is not found. * @throws ReasonIdNotFound If any of the supplied reason ids are not found. + * @throws InvalidNodeRefException If the node could not be found. + * @throws InvalidNode If the supplied node is not a document node. */ void addClassificationToDocument(String classificationLevelId, String classificationAuthority, - Set classificationReasonIds, NodeRef document) throws LevelIdNotFound, ReasonIdNotFound; + Set classificationReasonIds, NodeRef document) throws LevelIdNotFound, ReasonIdNotFound, + InvalidNodeRefException, InvalidNode; } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceException.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceException.java index 30f75f8981..5d48551e10 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceException.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceException.java @@ -19,6 +19,7 @@ package org.alfresco.module.org_alfresco_module_rm.classification; import org.alfresco.error.AlfrescoRuntimeException; +import org.alfresco.service.cmr.repository.NodeRef; /** * Generic class for any runtime exception thrown within the {@link ClassificationService}. @@ -87,4 +88,15 @@ public class ClassificationServiceException extends AlfrescoRuntimeException super("Could not find classification reason with id " + reasonId); } } + + public static class InvalidNode extends ClassificationServiceException + { + /** serial version uid */ + private static final long serialVersionUID = -4485335425932302477L; + + public InvalidNode(NodeRef nodeRef, String message) + { + super("Operation not permitted on node " + nodeRef + ", error message: " + message); + } + } } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImpl.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImpl.java index 215cb2832d..d625c6a776 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImpl.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImpl.java @@ -28,6 +28,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.alfresco.model.ContentModel; +import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.InvalidNode; import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.LevelIdNotFound; import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.MissingConfiguration; import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.ReasonIdNotFound; @@ -224,6 +226,11 @@ public class ClassificationServiceImpl extends ServiceBaseImpl mandatory("classificationReasonIds", classificationReasonIds); mandatory("document", document); + if (!nodeService.getType(document).equals(ContentModel.TYPE_CONTENT)) + { + throw new InvalidNode(document, "The supplied node is not a content node."); + } + Map properties = new HashMap(); checkClassificationLevelId(classificationLevelId); diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImplUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImplUnitTest.java index 0154c60c0e..ffdd3fc9eb 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImplUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImplUnitTest.java @@ -34,6 +34,9 @@ import java.util.List; import java.util.Map; import java.util.stream.Stream; +import com.google.common.collect.Sets; +import org.alfresco.model.ContentModel; +import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.InvalidNode; import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.MissingConfiguration; import org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils; import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper; @@ -253,20 +256,20 @@ public class ClassificationServiceImplUnitTest } /** Classify a document with a couple of reasons and check the NodeService is called correctly. */ - @Test public void addClassificationToDocument() + @Test public void addClassificationToDocument_success() { - // FIXME: Needs to be changed - /* // Create a level and two reasons. ClassificationLevel level = new ClassificationLevel("levelId1", "displayLabelKey"); ClassificationReason reason1 = new ClassificationReason("reasonId1", "displayLabelKey1"); ClassificationReason reason2 = new ClassificationReason("reasonId2", "displayLabelKey2"); Set reasons = Sets.newHashSet(reason1, reason2); - NodeRef document = new NodeRef("fake://document/"); // Set up the managers to return these objects when the ids are provided. doReturn(level).when(mockLevelManager).findLevelById("levelId1"); doReturn(reason1).when(mockReasonManager).findReasonById("reasonId1"); doReturn(reason2).when(mockReasonManager).findReasonById("reasonId2"); + // Create a document node. + NodeRef document = new NodeRef("fake://document/"); + doReturn(ContentModel.TYPE_CONTENT).when(mockNodeService).getType(document); // Call the method under test. classificationServiceImpl.addClassificationToDocument("levelId1", "classificationAuthority", @@ -285,6 +288,21 @@ public class ClassificationServiceImplUnitTest assertEquals("Unexpected current classification.", level, properties.get(ClassifiedContentModel.PROP_CURRENT_CLASSIFICATION)); assertEquals("Unexpected authority.", "classificationAuthority", properties.get(ClassifiedContentModel.PROP_CLASSIFICATION_AUTHORITY)); assertEquals("Unexpected set of reasons.", reasons, properties.get(ClassifiedContentModel.PROP_CLASSIFICATION_REASONS)); - */ + } + + /** + * Classify a folder using the addClassificationToDocument method and check that an exception is + * raised. + */ + @Test(expected = InvalidNode.class) + public void addClassificationToDocument_notDocument() + { + // Create a folder node. + NodeRef notADocument = new NodeRef("not://a/document/"); + doReturn(ContentModel.TYPE_FOLDER).when(mockNodeService).getType(notADocument); + + // Call the method under test. + classificationServiceImpl.addClassificationToDocument("levelId1", "classificationAuthority", + Sets.newHashSet("reasonId1", "reasonId2"), notADocument); } }