RM-2319 Validate classification reasons and exemption categories.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@106529 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-06-19 09:52:18 +00:00
parent 8f5a8334f7
commit 06d0c37b25
5 changed files with 220 additions and 2 deletions

View File

@@ -26,7 +26,9 @@ 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.model.ClassifiedContentModel;
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationLevelFieldsValidator; 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.ClassificationSchemeEntityValidator;
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ExemptionCategoryFieldsValidator;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
@@ -62,6 +64,10 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem
private ClassificationServiceDAO classificationServiceDAO; private ClassificationServiceDAO classificationServiceDAO;
private ClassificationLevelFieldsValidator classificationLevelFieldsValidator = new ClassificationLevelFieldsValidator(); private ClassificationLevelFieldsValidator classificationLevelFieldsValidator = new ClassificationLevelFieldsValidator();
private ClassificationSchemeEntityValidator<ClassificationLevel> classificationLevelValidator = new ClassificationSchemeEntityValidator<>(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);
public ClassificationServiceBootstrap(AuthenticationUtil authUtil, public ClassificationServiceBootstrap(AuthenticationUtil authUtil,
TransactionService txService, TransactionService txService,
@@ -182,7 +188,7 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem
LOGGER.debug("Persisted classification reasons: {}", loggableStatusOf(persistedReasons)); LOGGER.debug("Persisted classification reasons: {}", loggableStatusOf(persistedReasons));
LOGGER.debug("Classpath classification reasons: {}", loggableStatusOf(classpathReasons)); LOGGER.debug("Classpath classification reasons: {}", loggableStatusOf(classpathReasons));
// TODO Add reason validation. classificationReasonValidator.validate(classpathReasons, ClassificationReason.class.getSimpleName());
if (isEmpty(persistedReasons)) if (isEmpty(persistedReasons))
{ {
@@ -240,7 +246,7 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem
LOGGER.debug("Persisted exemption categories: {}", loggableStatusOf(persistedCategories)); LOGGER.debug("Persisted exemption categories: {}", loggableStatusOf(persistedCategories));
LOGGER.debug("Classpath exemption categories: {}", loggableStatusOf(classpathCategories)); LOGGER.debug("Classpath exemption categories: {}", loggableStatusOf(classpathCategories));
// TODO Add exemption category validation. exemptionCategoryValidator.validate(classpathCategories, ExemptionCategory.class.getSimpleName());
if (isEmpty(persistedCategories)) if (isEmpty(persistedCategories))
{ {

View File

@@ -0,0 +1,56 @@
/*
* 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 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.ClassificationReason;
/**
* Validator for the fields in {@link ClassificationReason}.
*
* @author tpage
* @since 3.0
*/
public class ClassificationReasonFieldsValidator implements EntityFieldsValidator<ClassificationReason>
{
/** Validator for the length of the reason id and display key. */
private FieldValidator<String> lengthFieldValidator = new LengthFieldValidator(1);
/** Validator for the start characters in the reason id and display key. */
private FieldValidator<String> startCharacterFieldValidator = new StartCharacterFieldValidator();
/**
* Validates the given {@link ClassificationReason}.
*
* @param classificationReason the reason to validate.
* @throws MissingConfiguration if the reason is missing.
* @throws IllegalConfiguration if the reason configuration is invalid.
*/
@Override
public void validate(ClassificationReason classificationReason)
{
final String id = classificationReason.getId();
lengthFieldValidator.validate(id, "reason id");
startCharacterFieldValidator.validate(id, "reason id");
final String displayKey = classificationReason.getDisplayLabelKey();
lengthFieldValidator.validate(displayKey, "reason display key");
startCharacterFieldValidator.validate(displayKey, "reason display key");
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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 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.ExemptionCategory;
/**
* Validator for the fields in {@link ExemptionCategory}.
*
* @author tpage
* @since 3.0
*/
public class ExemptionCategoryFieldsValidator implements EntityFieldsValidator<ExemptionCategory>
{
/** Validator for the length of the category id and display key. */
private FieldValidator<String> lengthFieldValidator = new LengthFieldValidator(1);
/** Validator for the start characters in the category id and display key. */
private FieldValidator<String> startCharacterFieldValidator = new StartCharacterFieldValidator();
/**
* Validates the given {@link ExemptionCategory}.
*
* @param exemptionCategory the category to validate.
* @throws MissingConfiguration if the category is missing.
* @throws IllegalConfiguration if the category configuration is invalid.
*/
@Override
public void validate(ExemptionCategory exemptionCategory)
{
final String id = exemptionCategory.getId();
lengthFieldValidator.validate(id, "exemption category id");
startCharacterFieldValidator.validate(id, "exemption category id");
final String displayKey = exemptionCategory.getDisplayLabelKey();
lengthFieldValidator.validate(displayKey, "exemption category display key");
startCharacterFieldValidator.validate(displayKey, "exemption category display key");
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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 org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
import org.apache.commons.lang3.StringUtils;
/**
* Validator that fails if the first character of a field is non-alphanumeric.
*
* @author tpage
* @since 3.0
*/
public class StartCharacterFieldValidator implements FieldValidator<String>
{
@Override
public void validate(String field, String fieldName) throws ClassificationException
{
if (field == null || field.length() == 0)
{
// If the first character doesn't exist then don't fail.
return;
}
if (!StringUtils.isAlphanumeric(field.substring(0, 1)))
{
throw new IllegalConfiguration("Illegal " + fieldName + ": First character of '" + field + "' is not alphanumeric.");
}
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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 org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
import org.junit.Test;
/**
* Unit tests for the {@link StartCharacterFieldValidator}.
*
* @author tpage
* @since 3.0
*/
public class StartCharacterFieldValidatorUnitTest
{
private static final String FIELD_NAME = "FIELD_NAME";
/** The class under test. */
StartCharacterFieldValidator startCharacterFieldValidator = new StartCharacterFieldValidator();
@Test
public void testValidate_pass()
{
startCharacterFieldValidator.validate("acceptable!", FIELD_NAME);
}
@Test(expected = IllegalConfiguration.class)
public void testValidate_fail()
{
startCharacterFieldValidator.validate("!unacceptable", FIELD_NAME);
}
@Test
public void testValidate_passIfEmpty()
{
startCharacterFieldValidator.validate(null, FIELD_NAME);
startCharacterFieldValidator.validate("", FIELD_NAME);
}
}