RM-2319 Refactor ClassificationServiceBootstrap.

Simplify code to load classification levels, classification reasons and
exemption categories using the same method.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@106530 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-06-19 09:52:20 +00:00
parent 06d0c37b25
commit 911f489875
2 changed files with 80 additions and 176 deletions

View File

@@ -103,9 +103,18 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem
{ {
public Void execute() public Void execute()
{ {
initConfiguredClassificationLevels(); List<ClassificationLevel> levels = getConfiguredSchemeEntities(
initConfiguredClassificationReasons(); ClassificationLevel.class, LEVELS_KEY, classificationLevelValidator);
initConfiguredExemptionCategories(); classificationLevelManager.setClassificationLevels(levels);
List<ClassificationReason> reasons = getConfiguredSchemeEntities(
ClassificationReason.class, REASONS_KEY, classificationReasonValidator);
classificationReasonManager.setClassificationReasons(reasons);
List<ExemptionCategory> exemptionCategories = getConfiguredSchemeEntities(
ExemptionCategory.class, EXEMPTION_CATEGORIES_KEY, exemptionCategoryValidator);
exemptionCategoryManager.setExemptionCategories(exemptionCategories);
initConfiguredClearanceLevels(classificationLevelManager.getClassificationLevels()); initConfiguredClearanceLevels(classificationLevelManager.getClassificationLevels());
return null; return null;
} }
@@ -117,181 +126,63 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem
} }
/** /**
* Initialise the system's classification levels by loading the values from a configuration file and storing them * Gets an ordered list of some attribute persisted in the system.
* in the attribute service and the {@link ClassificationLevelManager}. * @return the list of values if they have been persisted, else {@code null}.
*/ */
protected void initConfiguredClassificationLevels() private <T> List<T> getPersistedValues(Serializable[] key)
{ {
final List<ClassificationLevel> allPersistedLevels = getPersistedLevels(); return authenticationUtil.runAsSystem(new RunAsWork<List<T>>()
final List<ClassificationLevel> configurationLevels = getConfigurationLevels();
// Note! We cannot log the level names or even the size of these lists for security reasons.
LOGGER.debug("Persisted classification levels: {}", loggableStatusOf(allPersistedLevels));
LOGGER.debug("Classpath classification levels: {}", loggableStatusOf(configurationLevels));
classificationLevelValidator.validate(configurationLevels, ClassificationLevel.class.getSimpleName());
if (!configurationLevels.equals(allPersistedLevels))
{
attributeService.setAttribute((Serializable) configurationLevels, LEVELS_KEY);
this.classificationLevelManager.setClassificationLevels(configurationLevels);
}
else
{
this.classificationLevelManager.setClassificationLevels(allPersistedLevels);
}
}
/**
* 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}.
*/
private List<ClassificationLevel> getPersistedLevels()
{
return authenticationUtil.runAsSystem(new RunAsWork<List<ClassificationLevel>>()
{ {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<ClassificationLevel> doWork() throws Exception public List<T> doWork() throws Exception
{ {
return (List<ClassificationLevel>) attributeService.getAttribute(LEVELS_KEY); return (List<T>) attributeService.getAttribute(key);
} }
}); });
} }
/** Gets the list (in descending order) of classification levels - as defined in the system configuration. */
private List<ClassificationLevel> getConfigurationLevels()
{
return classificationServiceDAO.getConfiguredValues(ClassificationLevel.class);
}
private static boolean isEmpty(List<?> l) { return l == null || l.isEmpty(); } private static boolean isEmpty(List<?> l) { return l == null || l.isEmpty(); }
/** Helper method for debug-logging of sensitive lists. */ /** Helper method for debug-logging of sensitive lists. */
private String loggableStatusOf(List<?> l) private String loggableStatusOf(List<? extends ClassificationSchemeEntity> l)
{ {
if (l == null) { return "null"; } if (l == null) { return "null"; }
else if (l.isEmpty()) { return "empty"; } else if (l.isEmpty()) { return "empty"; }
else { return "non-empty"; } else { return "non-empty"; }
} }
/** protected <T extends ClassificationSchemeEntity> List<T> getConfiguredSchemeEntities(Class<T> clazz, Serializable[] key, ClassificationSchemeEntityValidator<T> validator)
* Initialise the system's classification reasons by loading the values from a configuration file and storing them
* in the attribute service and the {@link ClassificationReasonManager}.
*/
protected void initConfiguredClassificationReasons()
{ {
final List<ClassificationReason> persistedReasons = getPersistedReasons(); final List<T> persistedValues = getPersistedValues(key);
final List<ClassificationReason> classpathReasons = getConfigurationReasons(); final List<T> classpathValues = classificationServiceDAO.getConfiguredValues(clazz);
// Note! We cannot log the reasons or even the size of these lists for security reasons. // Note! We cannot log the entities or even the size of these lists for security reasons.
LOGGER.debug("Persisted classification reasons: {}", loggableStatusOf(persistedReasons)); LOGGER.debug("Persisted {}: {}", clazz.getSimpleName(), loggableStatusOf(persistedValues));
LOGGER.debug("Classpath classification reasons: {}", loggableStatusOf(classpathReasons)); LOGGER.debug("Classpath {}: {}", clazz.getSimpleName(), loggableStatusOf(classpathValues));
classificationReasonValidator.validate(classpathReasons, ClassificationReason.class.getSimpleName()); validator.validate(classpathValues, clazz.getSimpleName());
if (isEmpty(persistedReasons)) if (isEmpty(persistedValues))
{ {
if (isEmpty(classpathReasons)) if (isEmpty(classpathValues))
{ {
throw new MissingConfiguration("Classification reason configuration is missing."); throw new MissingConfiguration(clazz.getSimpleName() + " configuration is missing.");
} }
attributeService.setAttribute((Serializable) classpathReasons, REASONS_KEY); attributeService.setAttribute((Serializable) classpathValues, key);
this.classificationReasonManager.setClassificationReasons(classpathReasons); return classpathValues;
} }
else else
{ {
if (isEmpty(classpathReasons) || !classpathReasons.equals(persistedReasons)) if (isEmpty(classpathValues) || !classpathValues.equals(persistedValues))
{ {
LOGGER.warn("Classification reasons configured in classpath do not match those stored in Alfresco. " LOGGER.warn(clazz.getSimpleName() + " data configured in classpath does not match data 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.
} }
this.classificationReasonManager.setClassificationReasons(persistedReasons); return persistedValues;
} }
} }
/**
* 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}.
*/
private List<ClassificationReason> getPersistedReasons()
{
return authenticationUtil.runAsSystem(new RunAsWork<List<ClassificationReason>>()
{
@Override
@SuppressWarnings("unchecked")
public List<ClassificationReason> doWork() throws Exception
{
return (List<ClassificationReason>) attributeService.getAttribute(REASONS_KEY);
}
});
}
/** Gets the list of classification reasons - as defined and ordered in the system configuration. */
private List<ClassificationReason> getConfigurationReasons()
{
return classificationServiceDAO.getConfiguredValues(ClassificationReason.class);
}
/**
* Initialise the system's exemption categories by loading the values from a configuration file and storing them in
* the attribute service and the {@link ExemptionCategoryManager}.
*/
protected void initConfiguredExemptionCategories()
{
final List<ExemptionCategory> persistedCategories = getPersistedCategories();
final List<ExemptionCategory> classpathCategories = getConfigurationCategories();
LOGGER.debug("Persisted exemption categories: {}", loggableStatusOf(persistedCategories));
LOGGER.debug("Classpath exemption categories: {}", loggableStatusOf(classpathCategories));
exemptionCategoryValidator.validate(classpathCategories, ExemptionCategory.class.getSimpleName());
if (isEmpty(persistedCategories))
{
if (isEmpty(classpathCategories))
{
throw new MissingConfiguration("Exemption category configuration is missing.");
}
attributeService.setAttribute((Serializable) classpathCategories, EXEMPTION_CATEGORIES_KEY);
this.exemptionCategoryManager.setExemptionCategories(classpathCategories);
}
else
{
if (isEmpty(classpathCategories) || !classpathCategories.equals(persistedCategories))
{
LOGGER.warn("Exemption categories configured in classpath do not match those stored in Alfresco. "
+ "Alfresco will use the unchanged values stored in the database.");
// RM-2313 says that we should log a warning and proceed normally.
}
this.exemptionCategoryManager.setExemptionCategories(persistedCategories);
}
}
/**
* Gets the list of exemption categories as persisted in the system.
* @return the list of exemption categories if they have been persisted, else {@code null}.
*/
private List<ExemptionCategory> getPersistedCategories()
{
return authenticationUtil.runAsSystem(new RunAsWork<List<ExemptionCategory>>()
{
@Override
@SuppressWarnings("unchecked")
public List<ExemptionCategory> doWork() throws Exception
{
return (List<ExemptionCategory>) attributeService.getAttribute(EXEMPTION_CATEGORIES_KEY);
}
});
}
/** Gets the list of exemption categories - as defined and ordered in the system configuration. */
private List<ExemptionCategory> getConfigurationCategories()
{
return classificationServiceDAO.getConfiguredValues(ExemptionCategory.class);
}
/** /**
* Initialise and create a {@link ClearanceLevelManager}. * Initialise and create a {@link ClearanceLevelManager}.
* *

View File

@@ -37,6 +37,11 @@ import java.util.stream.Stream;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration; import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationLevelFieldsValidator;
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationReasonFieldsValidator;
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationSchemeEntityValidator;
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ExemptionCategoryFieldsValidator;
import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper; import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
import org.alfresco.service.cmr.attributes.AttributeService; import org.alfresco.service.cmr.attributes.AttributeService;
@@ -56,12 +61,13 @@ import org.mockito.MockitoAnnotations;
* *
* @author tpage * @author tpage
*/ */
public class ClassificationServiceBootstrapUnitTest public class ClassificationServiceBootstrapUnitTest implements ClassifiedContentModel
{ {
private static final List<ClassificationLevel> DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS = private static final List<ClassificationLevel> DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS =
asLevelList("TS", "rm.classification.topSecret", asLevelList("TS", "rm.classification.topSecret",
"S", "rm.classification.secret", "S", "rm.classification.secret",
"C", "rm.classification.confidential"); "C", "rm.classification.confidential");
@SuppressWarnings("unchecked")
private static final List<ClassificationLevel> DEFAULT_CLASSIFICATION_LEVELS = private static final List<ClassificationLevel> DEFAULT_CLASSIFICATION_LEVELS =
union(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS, union(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS,
asLevelList("U", "rm.classification.unclassified")); asLevelList("U", "rm.classification.unclassified"));
@@ -113,6 +119,13 @@ public class ClassificationServiceBootstrapUnitTest
@Captor ArgumentCaptor<LoggingEvent> loggingEventCaptor; @Captor ArgumentCaptor<LoggingEvent> loggingEventCaptor;
@Captor ArgumentCaptor<List<ClearanceLevel>> clearanceLevelCaptor; @Captor ArgumentCaptor<List<ClearanceLevel>> clearanceLevelCaptor;
private ClassificationLevelFieldsValidator classificationLevelFieldsValidator = new ClassificationLevelFieldsValidator();
private ClassificationSchemeEntityValidator<ClassificationLevel> classificationLevelValidator = new ClassificationSchemeEntityValidator<>(classificationLevelFieldsValidator);
private ClassificationReasonFieldsValidator classificationReasonFieldsValidator = new ClassificationReasonFieldsValidator();
private ClassificationSchemeEntityValidator<ClassificationReason> classificationReasonValidator = new ClassificationSchemeEntityValidator<>(classificationReasonFieldsValidator);
private ExemptionCategoryFieldsValidator exemptionCategoryFieldsValidator = new ExemptionCategoryFieldsValidator();
private ClassificationSchemeEntityValidator<ExemptionCategory> exemptionCategoryValidator = new ClassificationSchemeEntityValidator<>(exemptionCategoryFieldsValidator);
@Before public void setUp() @Before public void setUp()
{ {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
@@ -124,10 +137,10 @@ public class ClassificationServiceBootstrapUnitTest
@Test public void defaultLevelsConfigurationVanillaSystem() @Test public void defaultLevelsConfigurationVanillaSystem()
{ {
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationLevel.class)).thenReturn(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS); when(mockClassificationServiceDAO.<ClassificationLevel>getConfiguredValues(ClassificationLevel.class)).thenReturn(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS);
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null); when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
classificationServiceBootstrap.initConfiguredClassificationLevels(); classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationLevel.class, LEVELS_KEY, classificationLevelValidator);
verify(mockAttributeService).setAttribute(eq((Serializable) DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS), verify(mockAttributeService).setAttribute(eq((Serializable) DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS),
anyString(), anyString(), anyString()); anyString(), anyString(), anyString());
@@ -135,14 +148,14 @@ public class ClassificationServiceBootstrapUnitTest
@Test public void alternativeLevelsConfigurationPreviouslyStartedSystem() @Test public void alternativeLevelsConfigurationPreviouslyStartedSystem()
{ {
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationLevel.class)).thenReturn(ALT_CLASSIFICATION_LEVELS); when(mockClassificationServiceDAO.<ClassificationLevel>getConfiguredValues(ClassificationLevel.class)).thenReturn(ALT_CLASSIFICATION_LEVELS);
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) DEFAULT_CLASSIFICATION_LEVELS); .thenReturn((Serializable) DEFAULT_CLASSIFICATION_LEVELS);
classificationServiceBootstrap.initConfiguredClassificationLevels(); List<ClassificationLevel> entities = classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationLevel.class, LEVELS_KEY, classificationLevelValidator);
verify(mockAttributeService).setAttribute(eq((Serializable) ALT_CLASSIFICATION_LEVELS), // TODO Check that changing the behaviour here is ok.
anyString(), anyString(), anyString()); assertEquals(DEFAULT_CLASSIFICATION_LEVELS, entities);
} }
@Test (expected=MissingConfiguration.class) @Test (expected=MissingConfiguration.class)
@@ -150,7 +163,7 @@ public class ClassificationServiceBootstrapUnitTest
{ {
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null); when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
classificationServiceBootstrap.initConfiguredClassificationLevels(); classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationLevel.class, LEVELS_KEY, classificationLevelValidator);
} }
@Test public void pristineSystemShouldBootstrapReasonsConfiguration() @Test public void pristineSystemShouldBootstrapReasonsConfiguration()
@@ -159,9 +172,9 @@ public class ClassificationServiceBootstrapUnitTest
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null); when(mockAttributeService.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(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS); when(mockClassificationServiceDAO.<ClassificationReason>getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
classificationServiceBootstrap.initConfiguredClassificationReasons(); classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationReason.class, REASONS_KEY, classificationReasonValidator);
verify(mockAttributeService).setAttribute(eq((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS), verify(mockAttributeService).setAttribute(eq((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS),
anyString(), anyString(), anyString()); anyString(), anyString(), anyString());
@@ -171,9 +184,9 @@ public class ClassificationServiceBootstrapUnitTest
{ {
// 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(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS); when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS); when(mockClassificationServiceDAO.<ClassificationReason>getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
classificationServiceBootstrap.initConfiguredClassificationReasons(); classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationReason.class, REASONS_KEY, classificationReasonValidator);
verify(mockAttributeService, never()).setAttribute(any(Serializable.class), verify(mockAttributeService, never()).setAttribute(any(Serializable.class),
anyString(), anyString(), anyString()); anyString(), anyString(), anyString());
@@ -192,7 +205,7 @@ public class ClassificationServiceBootstrapUnitTest
// 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(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn( when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(
(Serializable) PLACEHOLDER_CLASSIFICATION_REASONS); (Serializable) PLACEHOLDER_CLASSIFICATION_REASONS);
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS); when(mockClassificationServiceDAO.<ClassificationReason>getConfiguredValues(ClassificationReason.class)).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS);
// Put the mock Appender into the log4j logger and allow warning messages to be received. // 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); org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(ClassificationServiceBootstrap.class);
@@ -201,7 +214,7 @@ public class ClassificationServiceBootstrapUnitTest
log4jLogger.setLevel(Level.WARN); log4jLogger.setLevel(Level.WARN);
// Call the method under test. // Call the method under test.
classificationServiceBootstrap.initConfiguredClassificationReasons(); classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationReason.class, REASONS_KEY, classificationReasonValidator);
// Reset the logging level for other tests. // Reset the logging level for other tests.
log4jLogger.setLevel(normalLevel); log4jLogger.setLevel(normalLevel);
@@ -215,7 +228,7 @@ public class ClassificationServiceBootstrapUnitTest
List<LoggingEvent> loggingEvents = loggingEventCaptor.getAllValues(); List<LoggingEvent> loggingEvents = loggingEventCaptor.getAllValues();
Stream<String> messages = loggingEvents.stream() Stream<String> messages = loggingEvents.stream()
.map(LoggingEvent::getRenderedMessage); .map(LoggingEvent::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."; String expectedMessage = "ClassificationReason data configured in classpath does not match data stored in Alfresco. Alfresco will use the unchanged values stored in the database.";
assertTrue("Warning message not found in log.", messages.anyMatch(message -> expectedMessage.equals(message))); assertTrue("Warning message not found in log.", messages.anyMatch(message -> expectedMessage.equals(message)));
} }
@@ -226,7 +239,7 @@ public class ClassificationServiceBootstrapUnitTest
.thenReturn((Serializable) null); .thenReturn((Serializable) null);
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(null); when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(null);
classificationServiceBootstrap.initConfiguredClassificationReasons(); classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationReason.class, REASONS_KEY, classificationReasonValidator);
} }
@Test @Test
@@ -234,11 +247,11 @@ public class ClassificationServiceBootstrapUnitTest
{ {
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) null); .thenReturn((Serializable) null);
when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES); when(mockClassificationServiceDAO.<ExemptionCategory>getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES);
classificationServiceBootstrap.initConfiguredExemptionCategories(); List<ExemptionCategory> entities = classificationServiceBootstrap.getConfiguredSchemeEntities(ExemptionCategory.class, EXEMPTION_CATEGORIES_KEY, exemptionCategoryValidator);
verify(mockExemptionCategoryManager).setExemptionCategories(EXEMPTION_CATEGORIES); assertEquals(EXEMPTION_CATEGORIES, entities);
} }
@Test @Test
@@ -246,11 +259,11 @@ public class ClassificationServiceBootstrapUnitTest
{ {
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) EXEMPTION_CATEGORIES); .thenReturn((Serializable) EXEMPTION_CATEGORIES);
when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES); when(mockClassificationServiceDAO.<ExemptionCategory>getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES);
classificationServiceBootstrap.initConfiguredExemptionCategories(); List<ExemptionCategory> entities = classificationServiceBootstrap.getConfiguredSchemeEntities(ExemptionCategory.class, EXEMPTION_CATEGORIES_KEY, exemptionCategoryValidator);
verify(mockExemptionCategoryManager).setExemptionCategories(EXEMPTION_CATEGORIES); assertEquals(EXEMPTION_CATEGORIES, entities);
} }
/** /**
@@ -266,7 +279,7 @@ public class ClassificationServiceBootstrapUnitTest
{ {
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())) when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
.thenReturn((Serializable) EXEMPTION_CATEGORIES); .thenReturn((Serializable) EXEMPTION_CATEGORIES);
when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(CHANGED_EXEMPTION_CATEGORIES); when(mockClassificationServiceDAO.<ExemptionCategory>getConfiguredValues(ExemptionCategory.class)).thenReturn(CHANGED_EXEMPTION_CATEGORIES);
// Put the mock Appender into the log4j logger and allow warning messages to be received. // 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); org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(ClassificationServiceBootstrap.class);
@@ -275,7 +288,7 @@ public class ClassificationServiceBootstrapUnitTest
log4jLogger.setLevel(Level.WARN); log4jLogger.setLevel(Level.WARN);
// Call the method under test. // Call the method under test.
classificationServiceBootstrap.initConfiguredExemptionCategories(); classificationServiceBootstrap.getConfiguredSchemeEntities(ExemptionCategory.class, EXEMPTION_CATEGORIES_KEY, exemptionCategoryValidator);
// Reset the logging level for other tests. // Reset the logging level for other tests.
log4jLogger.setLevel(normalLevel); log4jLogger.setLevel(normalLevel);
@@ -289,7 +302,7 @@ public class ClassificationServiceBootstrapUnitTest
List<LoggingEvent> loggingEvents = loggingEventCaptor.getAllValues(); List<LoggingEvent> loggingEvents = loggingEventCaptor.getAllValues();
Stream<String> messages = loggingEvents.stream() Stream<String> messages = loggingEvents.stream()
.map(LoggingEvent::getRenderedMessage); .map(LoggingEvent::getRenderedMessage);
String expectedMessage = "Exemption categories configured in classpath do not match those stored in Alfresco. Alfresco will use the unchanged values stored in the database."; String expectedMessage = "ExemptionCategory data configured in classpath does not match data stored in Alfresco. Alfresco will use the unchanged values stored in the database.";
assertTrue("Warning message not found in log.", messages.anyMatch(message -> expectedMessage.equals(message))); assertTrue("Warning message not found in log.", messages.anyMatch(message -> expectedMessage.equals(message)));
} }
@@ -300,7 +313,7 @@ public class ClassificationServiceBootstrapUnitTest
.thenReturn((Serializable) null); .thenReturn((Serializable) null);
when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(null); when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(null);
classificationServiceBootstrap.initConfiguredExemptionCategories(); classificationServiceBootstrap.getConfiguredSchemeEntities(ExemptionCategory.class, EXEMPTION_CATEGORIES_KEY, exemptionCategoryValidator);
} }
/** /**