From 81f5c589c5b09652dee7750962e59d1d1a0895f5 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Fri, 19 Jun 2015 09:52:13 +0000 Subject: [PATCH] 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 --- .../ClassificationServiceBootstrap.java | 9 +-- .../ClassificationLevelValidation.java | 68 ------------------ .../ClassificationSchemeEntityValidator.java | 70 +++++++++++++++++++ ...icationSchemeEntityValidatorUnitTest.java} | 38 +++++++--- 4 files changed, 102 insertions(+), 83 deletions(-) delete mode 100644 rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationLevelValidation.java create mode 100644 rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationSchemeEntityValidator.java rename rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/{ClassificationLevelValidationUnitTest.java => ClassificationSchemeEntityValidatorUnitTest.java} (56%) diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java index 2c7b3d207c..9a8fbaccaa 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceBootstrap.java @@ -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 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)) { diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationLevelValidation.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationLevelValidation.java deleted file mode 100644 index a3b4786c80..0000000000 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationLevelValidation.java +++ /dev/null @@ -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 . - */ -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 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 levels) - { - if (levels == null || levels.isEmpty()) - { - throw new MissingConfiguration("Classification level configuration is missing."); - } - - final List duplicateLevels = getDuplicateElements(levels); - if (!duplicateLevels.isEmpty()) - { - throw new IllegalConfiguration("Duplicate ID abbreviations are not allowed: " + duplicateLevels); - } - - for (ClassificationLevel level : levels) - { - classificationLevelFieldsValidator.validate(level); - } - } -} diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationSchemeEntityValidator.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationSchemeEntityValidator.java new file mode 100644 index 0000000000..2dff7fbf1b --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationSchemeEntityValidator.java @@ -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 . + */ +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 +{ + private EntityFieldsValidator entityFieldsValidator; + + public ClassificationSchemeEntityValidator(EntityFieldsValidator 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 objects, String entityName) + { + if (objects == null || objects.isEmpty()) + { + throw new MissingConfiguration(entityName + " configuration is missing."); + } + + final List 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); + } + } +} diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationLevelValidationUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationSchemeEntityValidatorUnitTest.java similarity index 56% rename from rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationLevelValidationUnitTest.java rename to rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationSchemeEntityValidatorUnitTest.java index ff1d03b403..40e9c866f0 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationLevelValidationUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationSchemeEntityValidatorUnitTest.java @@ -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 mockFieldsValidator; + /** The class under test. */ + private ClassificationSchemeEntityValidator 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 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(),