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

View File

@@ -38,12 +38,12 @@ import org.json.JSONTokener;
* @author Neil Mc Erlean
* @since 3.0
*/
class Configuration
class ClassificationServiceDAO
{
public final String levelConfigLocation;
public final String reasonConfigLocation;
public Configuration(String levelConfigLocation, String reasonConfigLocation)
public ClassificationServiceDAO(String levelConfigLocation, String reasonConfigLocation)
{
this.levelConfigLocation = levelConfigLocation;
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.
*/
public List<ClassificationReason> getConfiguredReasons() {
public List<ClassificationReason> getConfiguredReasons()
{
List<ClassificationReason> result;
try (final InputStream in = this.getClass().getResourceAsStream(reasonConfigLocation))
{
@@ -114,5 +115,5 @@ class Configuration
throw new MalformedConfiguration("Could not read classification reason configuration", e);
}
return result;
}
}
}

View File

@@ -55,22 +55,22 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
/** The classification reasons currently configured in this server. */
private List<ClassificationReason> configuredReasons;
private final Configuration config;
private final ClassificationServiceDAO classificationServiceDao;
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.
*
* @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).
*/
ClassificationServiceImpl(Configuration config, Logger logger)
ClassificationServiceImpl(ClassificationServiceDAO classificationServiceDao, Logger logger)
{
this.config = config;
this.classificationServiceDao = classificationServiceDao;
ClassificationServiceImpl.logger = logger;
}
@@ -89,7 +89,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
{
throw new MissingConfiguration("Classification level configuration is missing.");
}
else if ( !configurationLevels.equals(allPersistedLevels))
else if (!configurationLevels.equals(allPersistedLevels))
{
attributeService.setAttribute((Serializable) configurationLevels, LEVELS_KEY);
this.configuredLevels = configurationLevels;
@@ -100,7 +100,8 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
}
}
void initConfiguredClassificationReasons() {
void initConfiguredClassificationReasons()
{
final List<ClassificationReason> persistedReasons = getPersistedReasons();
final List<ClassificationReason> classpathReasons = getConfigurationReasons();
@@ -121,8 +122,8 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
{
if (isEmpty(classpathReasons) || !classpathReasons.equals(persistedReasons))
{
logger.warn("Classification reasons configured in classpath do not match those stored in Alfresco." +
"Alfresco will use the unchanged values stored in the database.");
logger.warn("Classification reasons configured in classpath do not match those stored in Alfresco."
+ "Alfresco will use the unchanged values stored in the database.");
// RM-2073 says that we should log a warning and proceed normally.
}
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.
* @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>>()
{
@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. */
List<ClassificationLevel> getConfigurationLevels()
{
return config.getConfiguredLevels();
return classificationServiceDao.getConfiguredLevels();
}
/**
* 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}.
*/
List<ClassificationReason> getPersistedReasons() {
List<ClassificationReason> getPersistedReasons()
{
return authenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<List<ClassificationReason>>()
{
@Override
@@ -180,7 +183,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
/** Gets the list of classification reasons - as defined and ordered in the system configuration. */
List<ClassificationReason> getConfigurationReasons()
{
return config.getConfiguredReasons();
return classificationServiceDao.getConfiguredReasons();
}
@Override

View File

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

View File

@@ -87,25 +87,25 @@ public class ClassificationServiceImplUnitTest
private ClassificationServiceImpl classificationService;
private AttributeService mockedAttributeService = mock(AttributeService.class);
private AuthenticationUtil mockedAuthenticationUtil;
private Configuration mockConfig = mock(Configuration.class);
private AttributeService mockedAttributeService = mock(AttributeService.class);
private AuthenticationUtil mockedAuthenticationUtil;
private ClassificationServiceDAO mockClassificationServiceDAO = mock(ClassificationServiceDAO.class);
/** 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()
{
reset(mockConfig, mockedAttributeService, mockLogger);
reset(mockClassificationServiceDAO, mockedAttributeService, mockLogger);
mockedAuthenticationUtil = MockAuthenticationUtilHelper.create();
classificationService = new ClassificationServiceImpl(mockConfig, mockLogger);
classificationService = new ClassificationServiceImpl(mockClassificationServiceDAO, mockLogger);
classificationService.setAttributeService(mockedAttributeService);
classificationService.setAuthenticationUtil(mockedAuthenticationUtil);
}
@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);
classificationService.initConfiguredClassificationLevels();
@@ -116,7 +116,7 @@ public class ClassificationServiceImplUnitTest
@Test public void alternativeLevelsConfigurationPreviouslyStartedSystem()
{
when(mockConfig.getConfiguredLevels()).thenReturn(ALT_CLASSIFICATION_LEVELS);
when(mockClassificationServiceDAO.getConfiguredLevels()).thenReturn(ALT_CLASSIFICATION_LEVELS);
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) DEFAULT_CLASSIFICATION_LEVELS);
@@ -140,7 +140,7 @@ public class ClassificationServiceImplUnitTest
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
// 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();
@@ -152,7 +152,7 @@ public class ClassificationServiceImplUnitTest
{
// 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(mockConfig.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
classificationService.initConfiguredClassificationReasons();
@@ -169,22 +169,22 @@ public class ClassificationServiceImplUnitTest
// The classification reasons stored are different from those found on the classpath.
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(
(Serializable) PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockConfig.getConfiguredReasons()).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS);
when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS);
classificationService.initConfiguredClassificationReasons();
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.");
verify(mockedAttributeService, never()).setAttribute(any(Serializable.class), anyString(), anyString(),
anyString());
verify(mockedAttributeService, never()).setAttribute(any(Serializable.class),
anyString(), anyString(), anyString());
}
@Test(expected=MissingConfiguration.class)
@Test(expected = MissingConfiguration.class)
public void noReasonsFoundCausesException()
{
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(
(Serializable) null);
when(mockConfig.getConfiguredReasons()).thenReturn(null);
when(mockedAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) null);
when(mockClassificationServiceDAO.getConfiguredReasons()).thenReturn(null);
classificationService.initConfiguredClassificationReasons();
}

View File

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