RM-2319 Create a general classification scheme entity validator.

Replace the existing classification level validator with the new class.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@106528 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-06-19 09:52:13 +00:00
parent 4ee3afa208
commit 81f5c589c5
4 changed files with 102 additions and 83 deletions

View File

@@ -22,11 +22,11 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationLevelValidation;
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.model.ClassifiedContentModel;
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationLevelFieldsValidator;
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationSchemeEntityValidator;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
@@ -60,7 +60,8 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem
/** The exemption categories currently configured in this server. */
private ExemptionCategoryManager exemptionCategoryManager = new ExemptionCategoryManager();
private ClassificationServiceDAO classificationServiceDAO;
private final ClassificationLevelValidation levelValidation = new ClassificationLevelValidation();
private ClassificationLevelFieldsValidator classificationLevelFieldsValidator = new ClassificationLevelFieldsValidator();
private ClassificationSchemeEntityValidator<ClassificationLevel> classificationLevelValidator = new ClassificationSchemeEntityValidator<>(classificationLevelFieldsValidator);
public ClassificationServiceBootstrap(AuthenticationUtil authUtil,
TransactionService txService,
@@ -122,7 +123,7 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem
LOGGER.debug("Persisted classification levels: {}", loggableStatusOf(allPersistedLevels));
LOGGER.debug("Classpath classification levels: {}", loggableStatusOf(configurationLevels));
levelValidation.validateLevels(configurationLevels);
classificationLevelValidator.validate(configurationLevels, ClassificationLevel.class.getSimpleName());
if (!configurationLevels.equals(allPersistedLevels))
{

View File

@@ -1,68 +0,0 @@
/*
* 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.validation;
import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.getDuplicateElements;
import java.util.List;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalAbbreviationChars;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
/**
* This class is responsible for validating {@link ClassificationLevel}s.
*
* @author Neil Mc Erlean
* @since 3.0
*/
public class ClassificationLevelValidation
{
/** A validator for the fields within the classification level. */
private EntityFieldsValidator<ClassificationLevel> classificationLevelFieldsValidator = new ClassificationLevelFieldsValidator();
/**
* Validates the provided {@link ClassificationLevel}s as a whole and individually.
* @param levels the levels to validate.
* @throws MissingConfiguration if the levels or any of their abbreviations are missing.
* @throws IllegalConfiguration if any of the level abbreviations violate the standard restrictions.
* @throws IllegalAbbreviationChars if the level abbreviation contains illegal characters.
*/
public void validateLevels(List<ClassificationLevel> levels)
{
if (levels == null || levels.isEmpty())
{
throw new MissingConfiguration("Classification level configuration is missing.");
}
final List<ClassificationLevel> duplicateLevels = getDuplicateElements(levels);
if (!duplicateLevels.isEmpty())
{
throw new IllegalConfiguration("Duplicate ID abbreviations are not allowed: " + duplicateLevels);
}
for (ClassificationLevel level : levels)
{
classificationLevelFieldsValidator.validate(level);
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* 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.validation;
import static org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.getDuplicateElements;
import java.util.List;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeEntity;
/**
* This class is responsible for validating configured data objects for use in the classification feature.
*
* @author Neil Mc Erlean
* @author tpage
* @since 3.0
*/
public class ClassificationSchemeEntityValidator<T extends ClassificationSchemeEntity>
{
private EntityFieldsValidator<T> entityFieldsValidator;
public ClassificationSchemeEntityValidator(EntityFieldsValidator<T> entityFieldsValidator)
{
this.entityFieldsValidator = entityFieldsValidator;
}
/**
* Validates the provided entities as a whole and individually.
* @param objects the entities to validate.
* @param entityName the simple name of the class to validate.
* @throws MissingConfiguration if the configuration is missing.
* @throws IllegalConfiguration if configuration is invalid.
*/
public void validate(List<T> objects, String entityName)
{
if (objects == null || objects.isEmpty())
{
throw new MissingConfiguration(entityName + " configuration is missing.");
}
final List<T> duplicateObjects= getDuplicateElements(objects);
if (!duplicateObjects.isEmpty())
{
throw new IllegalConfiguration("Illegal " + entityName + " configuration - duplicate values are not allowed: " + duplicateObjects);
}
for (T object : objects)
{
entityFieldsValidator.validate(object);
}
}
}

View File

@@ -23,39 +23,55 @@ import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtil
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.Collections;
import java.util.List;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
/**
* Unit tests for the {@link ClassificationLevelValidation}.
* Unit tests for the {@link ClassificationSchemeEntityValidator}.
*
* @author Neil Mc Erlean
* @author tpage
* @since 3.0
*/
public class ClassificationLevelValidationUnitTest
public class ClassificationSchemeEntityValidatorUnitTest
{
private final ClassificationLevelValidation validation = new ClassificationLevelValidation();
private static final String ENTITY_NAME = "ENTITY_NAME";
@Mock
private EntityFieldsValidator<ClassificationLevel> mockFieldsValidator;
/** The class under test. */
private ClassificationSchemeEntityValidator<ClassificationLevel> classificationEntitySchemeValidator = new ClassificationSchemeEntityValidator<>(mockFieldsValidator );
@Before
public void setUp()
{
initMocks(this);
}
@Test(expected=MissingConfiguration.class)
public void classificationLevelsAreRequired()
{
validation.validateLevels(Collections.emptyList());
classificationEntitySchemeValidator.validate(Collections.emptyList(), ENTITY_NAME);
}
@Test public void ensureUniquenessOfAbbreviationIds()
{
IllegalConfiguration e = expectedException(IllegalConfiguration.class, () ->
{
validation.validateLevels(asList(new ClassificationLevel("FOO", "value.does.not.matter"),
new ClassificationLevel("BAR", "value.does.not.matter"),
new ClassificationLevel("---", "value.does.not.matter"),
new ClassificationLevel("BAR", "value.does.not.matter"),
new ClassificationLevel("FOO", "value.does.not.matter")));
List<ClassificationLevel> objects = asList(new ClassificationLevel("FOO", "value.does.not.matter"),
new ClassificationLevel("BAR", "value.does.not.matter"),
new ClassificationLevel("---", "value.does.not.matter"),
new ClassificationLevel("BAR", "value.does.not.matter"),
new ClassificationLevel("FOO", "value.does.not.matter"));
classificationEntitySchemeValidator.validate(objects, ENTITY_NAME);
return null;
});
assertThat("Exception message did not identify the duplicate IDs", e.getMessage(),