RM-2045 Create Java API to classify a document.

Create dedicated objects to handle queries against the list of configured
classification levels and reasons.

+review RM-25

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@101986 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-04-16 10:07:07 +00:00
parent f67f5e66e7
commit 19596e3484
9 changed files with 400 additions and 27 deletions

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2005-2015 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.module.org_alfresco_module_rm.classification;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.LevelIdNotFound;
import org.junit.Before;
import org.junit.Test;
/**
* Unit tests for the {@link ClassificationLevelManager}.
*
* @author tpage
*/
public class ClassificationLevelManagerUnitTest
{
private static final ClassificationLevel LEVEL_1 = new ClassificationLevel("id1", "displayLabelKey1");
private static final ClassificationLevel LEVEL_2 = new ClassificationLevel("id2", "displayLabelKey2");
private static final ClassificationLevel LEVEL_3 = new ClassificationLevel("id3", "displayLabelKey3");
private static final List<ClassificationLevel> LEVELS = Arrays.asList(LEVEL_1, LEVEL_2, LEVEL_3);
private ClassificationLevelManager classificationLevelManager;
@Before public void setup()
{
classificationLevelManager = new ClassificationLevelManager(LEVELS);
}
@Test public void findClassificationById_found()
{
ClassificationLevel actual = classificationLevelManager.findLevelById("id2");
assertEquals(LEVEL_2, actual);
}
@Test(expected=LevelIdNotFound.class) public void findClassificationById_notFound()
{
classificationLevelManager.findLevelById("id_unknown");
}
@Test public void getMostSecureLevel()
{
ClassificationLevel actual = classificationLevelManager.getMostSecureLevel();
assertEquals(LEVEL_1, actual);
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2005-2015 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.module.org_alfresco_module_rm.classification;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.ReasonIdNotFound;
import org.junit.Before;
import org.junit.Test;
/**
* Unit tests for the {@link ClassificationReasonManager}.
*
* @author tpage
*/
public class ClassificationReasonManagerUnitTest
{
private static final ClassificationReason REASON_1 = new ClassificationReason("id1", "displayLabelKey1");
private static final ClassificationReason REASON_2 = new ClassificationReason("id2", "displayLabelKey2");
private static final ClassificationReason REASON_3 = new ClassificationReason("id3", "displayLabelKey3");
private static final List<ClassificationReason> REASONS = Arrays.asList(REASON_1, REASON_2, REASON_3);
private ClassificationReasonManager classificationReasonManager;
@Before public void setup()
{
classificationReasonManager = new ClassificationReasonManager(REASONS);
}
@Test public void findClassificationById_found()
{
ClassificationReason actual = classificationReasonManager.findReasonById("id2");
assertEquals(REASON_2, actual);
}
@Test(expected = ReasonIdNotFound.class) public void findClassificationById_notFound()
{
classificationReasonManager.findReasonById("id_unknown");
}
}

View File

@@ -24,26 +24,36 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import com.google.common.collect.Sets;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.MissingConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
import org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils;
import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
import org.alfresco.service.cmr.attributes.AttributeService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.apache.log4j.Appender;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -95,11 +105,16 @@ public class ClassificationServiceImplUnitTest
@InjectMocks private ClassificationServiceImpl classificationServiceImpl;
@Mock private AttributeService mockedAttributeService;
@Mock private AuthenticationUtil mockedAuthenticationUtil;
@Mock private ClassificationServiceDAO mockClassificationServiceDAO;
@Mock private AttributeService mockedAttributeService;
@Mock private AuthenticationUtil mockedAuthenticationUtil;
@Mock private ClassificationServiceDAO mockClassificationServiceDAO;
@Mock private NodeService mockNodeService;
/** Using a mock appender in the class logger so that we can verify some of the logging requirements. */
@Mock private Appender mockAppender;
@Mock private Appender mockAppender;
@Mock private ClassificationLevelManager mockLevelManager;
@Mock private ClassificationReasonManager mockReasonManager;
@Captor private ArgumentCaptor<LoggingEvent> loggingEventCaptor;
@Captor private ArgumentCaptor<Map<QName, Serializable>> propertiesCaptor;
@Before public void setUp()
{
@@ -196,9 +211,8 @@ public class ClassificationServiceImplUnitTest
anyString(), anyString(), anyString());
// Check that the warning message was logged.
ArgumentCaptor<LoggingEvent> argumentCaptor = ArgumentCaptor.forClass(LoggingEvent.class);
verify(mockAppender).doAppend(argumentCaptor.capture());
List<LoggingEvent> loggingEvents = argumentCaptor.getAllValues();
verify(mockAppender).doAppend(loggingEventCaptor.capture());
List<LoggingEvent> loggingEvents = loggingEventCaptor.getAllValues();
Stream<String> messages = loggingEvents.stream().map(event -> event.getRenderedMessage());
String expectedMessage = "Classification reasons configured in classpath do not match those stored in Alfresco. Alfresco will use the unchanged values stored in the database.";
assertTrue("Warning message not found in log.", messages.anyMatch(message -> message == expectedMessage));
@@ -243,4 +257,37 @@ public class ClassificationServiceImplUnitTest
assertEquals("Expected an empty list when the target level is not found.", 0, actual.size());
}
/** Classify a document with a couple of reasons and check the NodeService is called correctly. */
@Test public void addClassificationToDocument()
{
// 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<ClassificationReason> 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");
// Call the method under test.
classificationServiceImpl.addClassificationToDocument("levelId1", "classificationAuthority",
Sets.newHashSet("reasonId1", "reasonId2"), document);
verify(mockNodeService).addAspect(eq(document), eq(ClassifiedContentModel.ASPECT_CLASSIFIED),
propertiesCaptor.capture());
// Check the properties that were received.
Map<QName, Serializable> properties = propertiesCaptor.getValue();
HashSet<QName> expectedPropertyKeys = Sets.newHashSet(ClassifiedContentModel.PROP_INITIAL_CLASSIFICATION,
ClassifiedContentModel.PROP_CURRENT_CLASSIFICATION,
ClassifiedContentModel.PROP_CLASSIFICATION_AUTHORITY,
ClassifiedContentModel.PROP_CLASSIFICATION_REASONS);
assertEquals("Aspect created with unexpected set of keys.", expectedPropertyKeys, properties.keySet());
assertEquals("Unexpected initial classification.", level, properties.get(ClassifiedContentModel.PROP_INITIAL_CLASSIFICATION));
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));
}
}

View File

@@ -29,6 +29,8 @@ import org.junit.runners.Suite;
@Suite.SuiteClasses(
{
ClassificationLevelConstraintUnitTest.class,
ClassificationLevelManagerUnitTest.class,
ClassificationReasonManagerUnitTest.class,
ClassificationServiceDAOUnitTest.class,
ClassificationServiceImplUnitTest.class
})