RM-2319 Refactor ClassificationServiceDAO.

All the config loading is now done by a single method using a factory to
create the individual entities.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@106526 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-06-19 09:52:07 +00:00
parent cbe6ca551e
commit 88ee670c81
7 changed files with 199 additions and 102 deletions

View File

@@ -0,0 +1,84 @@
/*
* 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 static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
/**
* Unit tests for the {@link ClassificationSchemeEntityFactory}.
*
* @author tpage
* @since 3.0
*/
public class ClassificationSchemeEntityFactoryUnitTest
{
ClassificationSchemeEntityFactory classificationSchemeEntityFactory = new ClassificationSchemeEntityFactory();
@Mock JSONObject mockJsonObject;
@Before
public void setUp()
{
initMocks(this);
}
@Test
public void testCreateClassificationLevel() throws JSONException
{
when(mockJsonObject.getString("name")).thenReturn("name1");
when(mockJsonObject.getString("displayLabel")).thenReturn("displayLabelKey2");
ClassificationLevel classificationLevel = classificationSchemeEntityFactory.create(ClassificationLevel.class, mockJsonObject);
assertEquals("name1", classificationLevel.getId());
assertEquals("displayLabelKey2", classificationLevel.getDisplayLabelKey());
}
@Test
public void testCreateClassificationReason() throws JSONException
{
when(mockJsonObject.getString("id")).thenReturn("id1");
when(mockJsonObject.getString("displayLabel")).thenReturn("displayLabelKey2");
ClassificationReason classificationReason = classificationSchemeEntityFactory.create(ClassificationReason.class, mockJsonObject);
assertEquals("id1", classificationReason.getId());
assertEquals("displayLabelKey2", classificationReason.getDisplayLabelKey());
}
@Test
public void testCreateExemptionCategory() throws JSONException
{
when(mockJsonObject.getString("id")).thenReturn("id1");
when(mockJsonObject.getString("displayLabel")).thenReturn("displayLabelKey2");
ExemptionCategory exemptionCategory = classificationSchemeEntityFactory.create(ExemptionCategory.class, mockJsonObject);
assertEquals("id1", exemptionCategory.getId());
assertEquals("displayLabelKey2", exemptionCategory.getDisplayLabelKey());
}
}

View File

@@ -124,7 +124,7 @@ public class ClassificationServiceBootstrapUnitTest
@Test public void defaultLevelsConfigurationVanillaSystem()
{
when(mockClassificationServiceDAO.getConfiguredLevels()).thenReturn(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS);
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationLevel.class)).thenReturn(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS);
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
classificationServiceBootstrap.initConfiguredClassificationLevels();
@@ -135,7 +135,7 @@ public class ClassificationServiceBootstrapUnitTest
@Test public void alternativeLevelsConfigurationPreviouslyStartedSystem()
{
when(mockClassificationServiceDAO.getConfiguredLevels()).thenReturn(ALT_CLASSIFICATION_LEVELS);
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationLevel.class)).thenReturn(ALT_CLASSIFICATION_LEVELS);
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) DEFAULT_CLASSIFICATION_LEVELS);
@@ -159,7 +159,7 @@ public class ClassificationServiceBootstrapUnitTest
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
// We'll use a small set of placeholder classification reasons.
when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
classificationServiceBootstrap.initConfiguredClassificationReasons();
@@ -171,7 +171,7 @@ public class ClassificationServiceBootstrapUnitTest
{
// The classification reasons stored are the same values that are found on the classpath.
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
classificationServiceBootstrap.initConfiguredClassificationReasons();
@@ -192,7 +192,7 @@ public class ClassificationServiceBootstrapUnitTest
// The classification reasons stored are different from those found on the classpath.
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(
(Serializable) PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS);
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS);
// Put the mock Appender into the log4j logger and allow warning messages to be received.
org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(ClassificationServiceBootstrap.class);
@@ -224,7 +224,7 @@ public class ClassificationServiceBootstrapUnitTest
{
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) null);
when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(null);
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(null);
classificationServiceBootstrap.initConfiguredClassificationReasons();
}
@@ -234,7 +234,7 @@ public class ClassificationServiceBootstrapUnitTest
{
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) null);
when(mockClassificationServiceDAO.getConfiguredExemptionCategories()).thenReturn(EXEMPTION_CATEGORIES);
when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES);
classificationServiceBootstrap.initConfiguredExemptionCategories();
@@ -246,7 +246,7 @@ public class ClassificationServiceBootstrapUnitTest
{
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) EXEMPTION_CATEGORIES);
when(mockClassificationServiceDAO.getConfiguredExemptionCategories()).thenReturn(EXEMPTION_CATEGORIES);
when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES);
classificationServiceBootstrap.initConfiguredExemptionCategories();
@@ -266,7 +266,7 @@ public class ClassificationServiceBootstrapUnitTest
{
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) EXEMPTION_CATEGORIES);
when(mockClassificationServiceDAO.getConfiguredExemptionCategories()).thenReturn(CHANGED_EXEMPTION_CATEGORIES);
when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(CHANGED_EXEMPTION_CATEGORIES);
// Put the mock Appender into the log4j logger and allow warning messages to be received.
org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(ClassificationServiceBootstrap.class);
@@ -298,7 +298,7 @@ public class ClassificationServiceBootstrapUnitTest
{
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) null);
when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(null);
when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(null);
classificationServiceBootstrap.initConfiguredExemptionCategories();
}

View File

@@ -45,7 +45,7 @@ public class ClassificationServiceDAOUnitTest
{
ClassificationServiceDAO c = new ClassificationServiceDAO();
c.setLevelConfigLocation("/alfresco/module/org_alfresco_module_rm/classification/rm-classification-levels.json");
List<ClassificationLevel> config = c.getConfiguredLevels();
List<ClassificationLevel> config = c.getConfiguredValues(ClassificationLevel.class);
assertEquals(DEFAULT_CLASSIFICATION_LEVELS, config);
}
@@ -53,7 +53,7 @@ public class ClassificationServiceDAOUnitTest
{
ClassificationServiceDAO c = new ClassificationServiceDAO();
c.setLevelConfigLocation("/no/such/resource");
assertTrue(c.getConfiguredLevels().isEmpty());
assertTrue(c.getConfiguredValues(ClassificationLevel.class).isEmpty());
}
@Test (expected = MalformedConfiguration.class)
@@ -61,14 +61,14 @@ public class ClassificationServiceDAOUnitTest
{
ClassificationServiceDAO c = new ClassificationServiceDAO();
c.setLevelConfigLocation("/alfresco/classification/rm-classification-levels-malformed.json");
c.getConfiguredLevels();
c.getConfiguredValues(ClassificationLevel.class);
}
@Test public void getConfiguredReasons_readingDefaultConfigurationShouldWork()
{
ClassificationServiceDAO c = new ClassificationServiceDAO();
c.setReasonConfigLocation("/alfresco/module/org_alfresco_module_rm/classification/rm-classification-reasons.json");
List<ClassificationReason> config = c.getConfiguredReasons();
List<ClassificationReason> config = c.getConfiguredValues(ClassificationReason.class);
assertFalse(config.isEmpty());
}
@@ -76,7 +76,7 @@ public class ClassificationServiceDAOUnitTest
{
ClassificationServiceDAO c = new ClassificationServiceDAO();
c.setReasonConfigLocation("/no/such/resource");
assertTrue(c.getConfiguredReasons().isEmpty());
assertTrue(c.getConfiguredValues(ClassificationReason.class).isEmpty());
}
@Test (expected = MalformedConfiguration.class)
@@ -84,6 +84,6 @@ public class ClassificationServiceDAOUnitTest
{
ClassificationServiceDAO c = new ClassificationServiceDAO();
c.setReasonConfigLocation("/alfresco/classification/rm-classification-levels-malformed.json");
c.getConfiguredReasons();
c.getConfiguredValues(ClassificationReason.class);
}
}