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 9a8fbaccaa..372266e524 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 @@ -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.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.util.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; @@ -62,6 +64,10 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem private ClassificationServiceDAO classificationServiceDAO; private ClassificationLevelFieldsValidator classificationLevelFieldsValidator = new ClassificationLevelFieldsValidator(); private ClassificationSchemeEntityValidator classificationLevelValidator = new ClassificationSchemeEntityValidator<>(classificationLevelFieldsValidator); + private ClassificationReasonFieldsValidator classificationReasonFieldsValidator = new ClassificationReasonFieldsValidator(); + private ClassificationSchemeEntityValidator classificationReasonValidator = new ClassificationSchemeEntityValidator<>(classificationReasonFieldsValidator); + private ExemptionCategoryFieldsValidator exemptionCategoryFieldsValidator = new ExemptionCategoryFieldsValidator(); + private ClassificationSchemeEntityValidator exemptionCategoryValidator = new ClassificationSchemeEntityValidator<>(exemptionCategoryFieldsValidator); public ClassificationServiceBootstrap(AuthenticationUtil authUtil, TransactionService txService, @@ -182,7 +188,7 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem LOGGER.debug("Persisted classification reasons: {}", loggableStatusOf(persistedReasons)); LOGGER.debug("Classpath classification reasons: {}", loggableStatusOf(classpathReasons)); - // TODO Add reason validation. + classificationReasonValidator.validate(classpathReasons, ClassificationReason.class.getSimpleName()); if (isEmpty(persistedReasons)) { @@ -240,7 +246,7 @@ public class ClassificationServiceBootstrap extends AbstractLifecycleBean implem LOGGER.debug("Persisted exemption categories: {}", loggableStatusOf(persistedCategories)); LOGGER.debug("Classpath exemption categories: {}", loggableStatusOf(classpathCategories)); - // TODO Add exemption category validation. + exemptionCategoryValidator.validate(classpathCategories, ExemptionCategory.class.getSimpleName()); if (isEmpty(persistedCategories)) { diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationReasonFieldsValidator.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationReasonFieldsValidator.java new file mode 100644 index 0000000000..78b45e9fa6 --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ClassificationReasonFieldsValidator.java @@ -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 . + */ +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 +{ + /** Validator for the length of the reason id and display key. */ + private FieldValidator lengthFieldValidator = new LengthFieldValidator(1); + /** Validator for the start characters in the reason id and display key. */ + private FieldValidator 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"); + } +} diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ExemptionCategoryFieldsValidator.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ExemptionCategoryFieldsValidator.java new file mode 100644 index 0000000000..c5ccb71b4e --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/ExemptionCategoryFieldsValidator.java @@ -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 . + */ +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 +{ + /** Validator for the length of the category id and display key. */ + private FieldValidator lengthFieldValidator = new LengthFieldValidator(1); + /** Validator for the start characters in the category id and display key. */ + private FieldValidator 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"); + } +} diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/StartCharacterFieldValidator.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/StartCharacterFieldValidator.java new file mode 100644 index 0000000000..7cc36af100 --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/StartCharacterFieldValidator.java @@ -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 . + */ +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 +{ + @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."); + } + } +} diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/StartCharacterFieldValidatorUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/StartCharacterFieldValidatorUnitTest.java new file mode 100644 index 0000000000..4751af7b86 --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/validation/StartCharacterFieldValidatorUnitTest.java @@ -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 . + */ +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); + } +}