Rename Configuration to ClassificationServiceDAO.

Also formatting changes as per code review comments.

+review RM-5

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@100687 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-03-30 09:13:32 +00:00
parent a734159cee
commit ad7ae82746
6 changed files with 108 additions and 105 deletions

View File

@@ -16,78 +16,77 @@
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.alfresco.module.org_alfresco_module_rm.classification; package org.alfresco.module.org_alfresco_module_rm.classification;
import org.springframework.extensions.surf.util.I18NUtil;
import java.io.Serializable; import java.io.Serializable;
import org.springframework.extensions.surf.util.I18NUtil;
/** /**
* This class is a POJO data type for a classification reason. * This class is a POJO data type for a classification reason.
* *
* @author Tom Page * @author Tom Page
* @since 3.0 * @since 3.0
*/ */
public final class ClassificationReason implements Serializable { public final class ClassificationReason implements Serializable
private static final long serialVersionUID = 4876939094239038838L; {
private final String id; private static final long serialVersionUID = 4876939094239038838L;
private final String displayLabelKey; private final String id;
private final String displayLabelKey;
/** /**
* Constructor to create a classification reason. * Constructor to create a classification reason.
* *
* @param id * @param id The unique identifier that represents this classification reason.
* The unique identifier that represents this classification * @param displayLabelKey The I18N key for the display label for the reason.
* reason. */
* @param displayLabelKey public ClassificationReason(final String id, final String displayLabelKey)
* The I18N key for the display label for the reason. {
*/ if (id == null || id.trim().equals("")) { throw new IllegalArgumentException("Illegal id: '" + id + "'"); }
public ClassificationReason(final String id, final String displayLabelKey) { this.id = id;
if (id == null || id.trim().equals("")) { this.displayLabelKey = displayLabelKey;
throw new IllegalArgumentException("Illegal id: '" + id + "'"); }
}
this.id = id;
this.displayLabelKey = displayLabelKey;
}
/** /**
* Returns the unique identifier that represents this classification reason. * Returns the unique identifier that represents this classification reason.
*/ */
public String getId() { public String getId()
return this.id; {
} return this.id;
}
/** /**
* Returns the localised (current locale) display label for this * Returns the localised (current locale) display label for this classification reason.
* classification reason. */
*/ public String getDisplayLabel()
public String getDisplayLabel() { {
return I18NUtil.getMessage(displayLabelKey); return I18NUtil.getMessage(displayLabelKey);
} }
@Override @Override
public String toString() { public String toString()
StringBuilder msg = new StringBuilder(); {
msg.append(ClassificationReason.class.getSimpleName()).append(":") StringBuilder msg = new StringBuilder();
.append(id); msg.append(ClassificationReason.class.getSimpleName()).append(":").append(id);
return msg.toString(); return msg.toString();
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o)
if (this == o) {
return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) if (o == null || getClass() != o.getClass()) return false;
return false;
ClassificationReason that = (ClassificationReason) o; ClassificationReason that = (ClassificationReason) o;
return this.id.equals(that.id); return this.id.equals(that.id);
} }
@Override @Override
public int hashCode() { public int hashCode()
return id.hashCode(); {
} return id.hashCode();
}
} }

View File

@@ -38,12 +38,12 @@ import org.json.JSONTokener;
* @author Neil Mc Erlean * @author Neil Mc Erlean
* @since 3.0 * @since 3.0
*/ */
class Configuration class ClassificationServiceDAO
{ {
public final String levelConfigLocation; public final String levelConfigLocation;
public final String reasonConfigLocation; public final String reasonConfigLocation;
public Configuration(String levelConfigLocation, String reasonConfigLocation) public ClassificationServiceDAO(String levelConfigLocation, String reasonConfigLocation)
{ {
this.levelConfigLocation = levelConfigLocation; this.levelConfigLocation = levelConfigLocation;
this.reasonConfigLocation = reasonConfigLocation; this.reasonConfigLocation = reasonConfigLocation;
@@ -88,7 +88,8 @@ class Configuration
* *
* @return the configured classification reasons in descending order, or an empty list if there are none. * @return the configured classification reasons in descending order, or an empty list if there are none.
*/ */
public List<ClassificationReason> getConfiguredReasons() { public List<ClassificationReason> getConfiguredReasons()
{
List<ClassificationReason> result; List<ClassificationReason> result;
try (final InputStream in = this.getClass().getResourceAsStream(reasonConfigLocation)) try (final InputStream in = this.getClass().getResourceAsStream(reasonConfigLocation))
{ {
@@ -114,5 +115,5 @@ class Configuration
throw new MalformedConfiguration("Could not read classification reason configuration", e); throw new MalformedConfiguration("Could not read classification reason configuration", e);
} }
return result; return result;
} }
} }

View File

@@ -55,22 +55,22 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
/** The classification reasons currently configured in this server. */ /** The classification reasons currently configured in this server. */
private List<ClassificationReason> configuredReasons; private List<ClassificationReason> configuredReasons;
private final Configuration config; private final ClassificationServiceDAO classificationServiceDao;
public ClassificationServiceImpl() public ClassificationServiceImpl()
{ {
this.config = new Configuration(DEFAULT_LEVELS_FILE, DEFAULT_REASONS_FILE); this.classificationServiceDao = new ClassificationServiceDAO(DEFAULT_LEVELS_FILE, DEFAULT_REASONS_FILE);
} }
/** /**
* Package protected constructor, primarily for unit testing purposes. * Package protected constructor, primarily for unit testing purposes.
* *
* @param config The object from which configuration options will be read. * @param classificationServiceDao The object from which configuration options will be read.
* @param logger The class logger (note - this will be set statically). * @param logger The class logger (note - this will be set statically).
*/ */
ClassificationServiceImpl(Configuration config, Logger logger) ClassificationServiceImpl(ClassificationServiceDAO classificationServiceDao, Logger logger)
{ {
this.config = config; this.classificationServiceDao = classificationServiceDao;
ClassificationServiceImpl.logger = logger; ClassificationServiceImpl.logger = logger;
} }
@@ -89,7 +89,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
{ {
throw new MissingConfiguration("Classification level configuration is missing."); throw new MissingConfiguration("Classification level configuration is missing.");
} }
else if ( !configurationLevels.equals(allPersistedLevels)) else if (!configurationLevels.equals(allPersistedLevels))
{ {
attributeService.setAttribute((Serializable) configurationLevels, LEVELS_KEY); attributeService.setAttribute((Serializable) configurationLevels, LEVELS_KEY);
this.configuredLevels = configurationLevels; this.configuredLevels = configurationLevels;
@@ -100,7 +100,8 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
} }
} }
void initConfiguredClassificationReasons() { void initConfiguredClassificationReasons()
{
final List<ClassificationReason> persistedReasons = getPersistedReasons(); final List<ClassificationReason> persistedReasons = getPersistedReasons();
final List<ClassificationReason> classpathReasons = getConfigurationReasons(); final List<ClassificationReason> classpathReasons = getConfigurationReasons();
@@ -121,8 +122,8 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
{ {
if (isEmpty(classpathReasons) || !classpathReasons.equals(persistedReasons)) if (isEmpty(classpathReasons) || !classpathReasons.equals(persistedReasons))
{ {
logger.warn("Classification reasons configured in classpath do not match those stored in Alfresco." + logger.warn("Classification reasons configured in classpath do not match those stored in Alfresco."
"Alfresco will use the unchanged values stored in the database."); + "Alfresco will use the unchanged values stored in the database.");
// RM-2073 says that we should log a warning and proceed normally. // RM-2073 says that we should log a warning and proceed normally.
} }
this.configuredReasons = persistedReasons; this.configuredReasons = persistedReasons;
@@ -143,7 +144,8 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
* Gets the list (in descending order) of classification levels - as persisted in the system. * Gets the list (in descending order) of classification levels - as persisted in the system.
* @return the list of classification levels if they have been persisted, else {@code null}. * @return the list of classification levels if they have been persisted, else {@code null}.
*/ */
List<ClassificationLevel> getPersistedLevels() { List<ClassificationLevel> getPersistedLevels()
{
return authenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<List<ClassificationLevel>>() return authenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<List<ClassificationLevel>>()
{ {
@Override @Override
@@ -158,14 +160,15 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
/** Gets the list (in descending order) of classification levels - as defined in the system configuration. */ /** Gets the list (in descending order) of classification levels - as defined in the system configuration. */
List<ClassificationLevel> getConfigurationLevels() List<ClassificationLevel> getConfigurationLevels()
{ {
return config.getConfiguredLevels(); return classificationServiceDao.getConfiguredLevels();
} }
/** /**
* Gets the list of classification reasons as persisted in the system. * Gets the list of classification reasons as persisted in the system.
* @return the list of classification reasons if they have been persisted, else {@code null}. * @return the list of classification reasons if they have been persisted, else {@code null}.
*/ */
List<ClassificationReason> getPersistedReasons() { List<ClassificationReason> getPersistedReasons()
{
return authenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<List<ClassificationReason>>() return authenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<List<ClassificationReason>>()
{ {
@Override @Override
@@ -180,7 +183,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
/** Gets the list of classification reasons - as defined and ordered in the system configuration. */ /** Gets the list of classification reasons - as defined and ordered in the system configuration. */
List<ClassificationReason> getConfigurationReasons() List<ClassificationReason> getConfigurationReasons()
{ {
return config.getConfiguredReasons(); return classificationServiceDao.getConfiguredReasons();
} }
@Override @Override

View File

@@ -29,12 +29,12 @@ import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationS
import org.junit.Test; import org.junit.Test;
/** /**
* Unit tests for {@link Configuration}. * Unit tests for {@link ClassificationServiceDAO}.
* *
* @author Neil Mc Erlean * @author Neil Mc Erlean
* @since 3.0 * @since 3.0
*/ */
public class ConfigurationUnitTest public class ClassificationServiceDAOUnitTest
{ {
private static final List<ClassificationLevel> DEFAULT_CLASSIFICATION_LEVELS = asLevelList("TopSecret", "TS", private static final List<ClassificationLevel> DEFAULT_CLASSIFICATION_LEVELS = asLevelList("TopSecret", "TS",
"Secret", "S", "Secret", "S",
@@ -43,29 +43,29 @@ public class ConfigurationUnitTest
@Test public void getConfiguredLevels_readingDefaultConfigurationShouldWork() @Test public void getConfiguredLevels_readingDefaultConfigurationShouldWork()
{ {
Configuration c = new Configuration(ClassificationServiceImpl.DEFAULT_LEVELS_FILE, ClassificationServiceImpl.DEFAULT_REASONS_FILE); ClassificationServiceDAO c = new ClassificationServiceDAO(ClassificationServiceImpl.DEFAULT_LEVELS_FILE, ClassificationServiceImpl.DEFAULT_REASONS_FILE);
List<ClassificationLevel> config = c.getConfiguredLevels(); List<ClassificationLevel> config = c.getConfiguredLevels();
assertEquals(DEFAULT_CLASSIFICATION_LEVELS, config); assertEquals(DEFAULT_CLASSIFICATION_LEVELS, config);
} }
@Test public void getConfiguredLevels_readingMissingConfigurationShouldProduceEmptyConfig() throws Exception @Test public void getConfiguredLevels_readingMissingConfigurationShouldProduceEmptyConfig() throws Exception
{ {
Configuration c = new Configuration("/no/such/resource", "/no/such/resource"); ClassificationServiceDAO c = new ClassificationServiceDAO("/no/such/resource", "/no/such/resource");
assertTrue(c.getConfiguredLevels().isEmpty()); assertTrue(c.getConfiguredLevels().isEmpty());
} }
@Test (expected = MalformedConfiguration.class) @Test (expected = MalformedConfiguration.class)
public void getConfiguredLevels_readingMalformedConfigurationShouldFail() public void getConfiguredLevels_readingMalformedConfigurationShouldFail()
{ {
Configuration c = new Configuration( ClassificationServiceDAO c = new ClassificationServiceDAO(
"/alfresco/classification/rm-classification-levels-malformed.json", "/alfresco/classification/rm-classification-levels-malformed.json",
"/alfresco/classification/rm-classification-levels-malformed.json"); "/alfresco/classification/rm-classification-levels-malformed.json");
c.getConfiguredLevels(); c.getConfiguredLevels();
} }
@Test public void getConfiguredReasons_readingDefaultConfigurationShouldWork() @Test public void getConfiguredReasons_readingDefaultConfigurationShouldWork()
{ {
Configuration c = new Configuration(ClassificationServiceImpl.DEFAULT_LEVELS_FILE, ClassificationServiceDAO c = new ClassificationServiceDAO(ClassificationServiceImpl.DEFAULT_LEVELS_FILE,
ClassificationServiceImpl.DEFAULT_REASONS_FILE); ClassificationServiceImpl.DEFAULT_REASONS_FILE);
List<ClassificationReason> config = c.getConfiguredReasons(); List<ClassificationReason> config = c.getConfiguredReasons();
assertFalse(config.isEmpty()); assertFalse(config.isEmpty());
@@ -73,16 +73,16 @@ public class ConfigurationUnitTest
@Test public void getConfiguredReasons_readingMissingConfigurationShouldProduceEmptyConfig() throws Exception @Test public void getConfiguredReasons_readingMissingConfigurationShouldProduceEmptyConfig() throws Exception
{ {
Configuration c = new Configuration("/no/such/resource", "/no/such/resource"); ClassificationServiceDAO c = new ClassificationServiceDAO("/no/such/resource", "/no/such/resource");
assertTrue(c.getConfiguredReasons().isEmpty()); assertTrue(c.getConfiguredReasons().isEmpty());
} }
@Test (expected = MalformedConfiguration.class) @Test (expected = MalformedConfiguration.class)
public void getConfiguredReasons_readingMalformedConfigurationShouldFail() public void getConfiguredReasons_readingMalformedConfigurationShouldFail()
{ {
Configuration c = new Configuration( ClassificationServiceDAO c = new ClassificationServiceDAO(
"/alfresco/classification/rm-classification-levels-malformed.json", "/alfresco/classification/rm-classification-levels-malformed.json",
"/alfresco/classification/rm-classification-levels-malformed.json"); "/alfresco/classification/rm-classification-levels-malformed.json");
c.getConfiguredReasons(); c.getConfiguredReasons();
} }
} }

View File

@@ -87,25 +87,25 @@ public class ClassificationServiceImplUnitTest
private ClassificationServiceImpl classificationService; private ClassificationServiceImpl classificationService;
private AttributeService mockedAttributeService = mock(AttributeService.class); private AttributeService mockedAttributeService = mock(AttributeService.class);
private AuthenticationUtil mockedAuthenticationUtil; private AuthenticationUtil mockedAuthenticationUtil;
private Configuration mockConfig = mock(Configuration.class); private ClassificationServiceDAO mockClassificationServiceDAO = mock(ClassificationServiceDAO.class);
/** Using a mock logger in the class so that we can verify some of the logging requirements. */ /** Using a mock logger in the class so that we can verify some of the logging requirements. */
private Logger mockLogger = mock(Logger.class); private Logger mockLogger = mock(Logger.class);
@Before public void setUp() @Before public void setUp()
{ {
reset(mockConfig, mockedAttributeService, mockLogger); reset(mockClassificationServiceDAO, mockedAttributeService, mockLogger);
mockedAuthenticationUtil = MockAuthenticationUtilHelper.create(); mockedAuthenticationUtil = MockAuthenticationUtilHelper.create();
classificationService = new ClassificationServiceImpl(mockConfig, mockLogger); classificationService = new ClassificationServiceImpl(mockClassificationServiceDAO, mockLogger);
classificationService.setAttributeService(mockedAttributeService); classificationService.setAttributeService(mockedAttributeService);
classificationService.setAuthenticationUtil(mockedAuthenticationUtil); classificationService.setAuthenticationUtil(mockedAuthenticationUtil);
} }
@Test public void defaultLevelsConfigurationVanillaSystem() @Test public void defaultLevelsConfigurationVanillaSystem()
{ {
when(mockConfig.getConfiguredLevels()).thenReturn(DEFAULT_CLASSIFICATION_LEVELS); when(mockClassificationServiceDAO.getConfiguredLevels()).thenReturn(DEFAULT_CLASSIFICATION_LEVELS);
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null); when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
classificationService.initConfiguredClassificationLevels(); classificationService.initConfiguredClassificationLevels();
@@ -116,7 +116,7 @@ public class ClassificationServiceImplUnitTest
@Test public void alternativeLevelsConfigurationPreviouslyStartedSystem() @Test public void alternativeLevelsConfigurationPreviouslyStartedSystem()
{ {
when(mockConfig.getConfiguredLevels()).thenReturn(ALT_CLASSIFICATION_LEVELS); when(mockClassificationServiceDAO.getConfiguredLevels()).thenReturn(ALT_CLASSIFICATION_LEVELS);
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())) when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) DEFAULT_CLASSIFICATION_LEVELS); .thenReturn((Serializable) DEFAULT_CLASSIFICATION_LEVELS);
@@ -140,7 +140,7 @@ public class ClassificationServiceImplUnitTest
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null); when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
// We'll use a small set of placeholder classification reasons. // We'll use a small set of placeholder classification reasons.
when(mockConfig.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS); when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
classificationService.initConfiguredClassificationReasons(); classificationService.initConfiguredClassificationReasons();
@@ -152,7 +152,7 @@ public class ClassificationServiceImplUnitTest
{ {
// The classification reasons stored are the same values that are found on the classpath. // The classification reasons stored are the same values that are found on the classpath.
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS); when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockConfig.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS); when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
classificationService.initConfiguredClassificationReasons(); classificationService.initConfiguredClassificationReasons();
@@ -169,23 +169,23 @@ public class ClassificationServiceImplUnitTest
// The classification reasons stored are different from those found on the classpath. // The classification reasons stored are different from those found on the classpath.
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn( when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(
(Serializable) PLACEHOLDER_CLASSIFICATION_REASONS); (Serializable) PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockConfig.getConfiguredReasons()).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS); when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS);
classificationService.initConfiguredClassificationReasons(); classificationService.initConfiguredClassificationReasons();
verify(mockLogger).warn("Classification reasons configured in classpath do not match those stored in Alfresco." verify(mockLogger).warn("Classification reasons configured in classpath do not match those stored in Alfresco."
+ "Alfresco will use the unchanged values stored in the database."); + "Alfresco will use the unchanged values stored in the database.");
verify(mockedAttributeService, never()).setAttribute(any(Serializable.class), anyString(), anyString(), verify(mockedAttributeService, never()).setAttribute(any(Serializable.class),
anyString()); anyString(), anyString(), anyString());
} }
@Test(expected=MissingConfiguration.class) @Test(expected = MissingConfiguration.class)
public void noReasonsFoundCausesException() public void noReasonsFoundCausesException()
{ {
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn( when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString()))
(Serializable) null); .thenReturn((Serializable) null);
when(mockConfig.getConfiguredReasons()).thenReturn(null); when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(null);
classificationService.initConfiguredClassificationReasons(); classificationService.initConfiguredClassificationReasons();
} }
} }

View File

@@ -29,7 +29,7 @@ import org.junit.runners.Suite;
@Suite.SuiteClasses( @Suite.SuiteClasses(
{ {
ClassificationServiceImplUnitTest.class, ClassificationServiceImplUnitTest.class,
ConfigurationUnitTest.class ClassificationServiceDAOUnitTest.class
}) })
public class ClassificationSuite public class ClassificationSuite
{ {