diff --git a/repository/src/main/java/org/alfresco/repo/module/DeprecatedModulesValidator.java b/repository/src/main/java/org/alfresco/repo/module/DeprecatedModulesValidator.java new file mode 100644 index 0000000000..1a850a716c --- /dev/null +++ b/repository/src/main/java/org/alfresco/repo/module/DeprecatedModulesValidator.java @@ -0,0 +1,76 @@ +/* + * #%L + * Alfresco Repository + * %% + * Copyright (C) 2005 - 2022 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * 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 . + * #L% + */ +package org.alfresco.repo.module; + +import static java.util.Optional.ofNullable; +import static java.util.function.Predicate.not; +import static java.util.stream.Collectors.joining; + +import java.util.List; + +import org.alfresco.service.cmr.module.ModuleDetails; +import org.alfresco.service.cmr.module.ModuleService; +import org.springframework.extensions.surf.util.I18NUtil; + +/** + * Halts the bootstrap process if a deprecated module is present. + * + * @author Domenico Sibilio + * @since 7.3.0 + */ +public class DeprecatedModulesValidator +{ + private static final String ERROR_MSG = "module.err.deprecated_modules"; + private final ModuleService moduleService; + private final List deprecatedModules; + + public DeprecatedModulesValidator(final ModuleService moduleService, final List deprecatedModules) + { + this.moduleService = moduleService; + this.deprecatedModules = deprecatedModules; + } + + public void onInit() + { + ofNullable(moduleService.getAllModules()) + .map(this::getDeprecatedModules) + .filter(not(String::isBlank)) + .ifPresent(DeprecatedModulesValidator::throwException); + } + + private String getDeprecatedModules(List modules) + { + return modules.stream() + .filter(module -> deprecatedModules.contains(module.getId())) + .map(module -> module.getTitle() + " " + module.getModuleVersionNumber()) + .collect(joining(", ")); + } + + private static void throwException(String foundDeprecatedModules) + { + throw new IllegalStateException(I18NUtil.getMessage(ERROR_MSG, foundDeprecatedModules)); + } +} diff --git a/repository/src/main/resources/alfresco/messages/module-messages.properties b/repository/src/main/resources/alfresco/messages/module-messages.properties index 9040f17188..59f7f7bc71 100644 --- a/repository/src/main/resources/alfresco/messages/module-messages.properties +++ b/repository/src/main/resources/alfresco/messages/module-messages.properties @@ -18,3 +18,4 @@ module.err.component_already_registered=A component named ''{0}'' has already be module.err.unable_to_open_module_properties=The module properties file ''{0}'' could not be read. module.err.component_in_missing_module=The component ''{0}'' belongs to a non-existent module ''{1}''. module.err.orphaned_components={0} module components were not considered for execution. +module.err.deprecated_modules=The following deprecated modules are installed: {0}. Please remove these AMPs in order to continue. \ No newline at end of file diff --git a/repository/src/main/resources/alfresco/module-context.xml b/repository/src/main/resources/alfresco/module-context.xml index 87999b274e..218c37f213 100644 --- a/repository/src/main/resources/alfresco/module-context.xml +++ b/repository/src/main/resources/alfresco/module-context.xml @@ -24,8 +24,17 @@ + + + + + alfresco-saml-repo + + + + - + diff --git a/repository/src/test/java/org/alfresco/AllUnitTestsSuite.java b/repository/src/test/java/org/alfresco/AllUnitTestsSuite.java index 7c26b9959f..482f086765 100644 --- a/repository/src/test/java/org/alfresco/AllUnitTestsSuite.java +++ b/repository/src/test/java/org/alfresco/AllUnitTestsSuite.java @@ -67,6 +67,7 @@ import org.junit.runners.Suite; org.alfresco.repo.management.subsystems.CryptodocSwitchableApplicationContextFactoryTest.class, org.alfresco.repo.module.ModuleDetailsImplTest.class, org.alfresco.repo.module.ModuleVersionNumberTest.class, + org.alfresco.repo.module.DeprecatedModulesValidatorTest.class, org.alfresco.repo.node.integrity.IntegrityEventTest.class, org.alfresco.repo.policy.MTPolicyComponentTest.class, org.alfresco.repo.policy.PolicyComponentTest.class, diff --git a/repository/src/test/java/org/alfresco/repo/module/DeprecatedModulesValidatorTest.java b/repository/src/test/java/org/alfresco/repo/module/DeprecatedModulesValidatorTest.java new file mode 100644 index 0000000000..83beaeb6dc --- /dev/null +++ b/repository/src/test/java/org/alfresco/repo/module/DeprecatedModulesValidatorTest.java @@ -0,0 +1,129 @@ +/* + * #%L + * Alfresco Repository + * %% + * Copyright (C) 2005 - 2022 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * 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 . + * #L% + */ +package org.alfresco.repo.module; + +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; + +import org.alfresco.service.cmr.module.ModuleDetails; +import org.alfresco.service.cmr.module.ModuleService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +/** + * Test suite for the {@link DeprecatedModulesValidator} class. + * + * @author Domenico Sibilio + */ +@RunWith(MockitoJUnitRunner.class) +public class DeprecatedModulesValidatorTest +{ + + private static final String DEPRECATED_MODULE_1 = "deprecated-module-1"; + private static final String DEPRECATED_MODULE_2 = "deprecated-module-2"; + private static final String VALID_MODULE = "valid-module"; + private static final List DEPRECATED_MODULES = List.of(DEPRECATED_MODULE_1, DEPRECATED_MODULE_2); + @Mock + private ModuleService moduleService; + @Mock + private ModuleDetails moduleDetails; + private DeprecatedModulesValidator deprecatedModulesValidator; + + @Before + public void setUp() + { + deprecatedModulesValidator = new DeprecatedModulesValidator(moduleService, DEPRECATED_MODULES); + } + + @Test + public void shouldDoNothingWhenNoModulesAreFound() + { + when(moduleService.getAllModules()).thenReturn(null); + + deprecatedModulesValidator.onInit(); + + verify(moduleService).getAllModules(); + } + + @Test + public void shouldDoNothingWhenNoDeprecatedModulesAreFound() + { + when(moduleService.getAllModules()).thenReturn(List.of(moduleDetails)); + when(moduleDetails.getId()).thenReturn(VALID_MODULE); + + deprecatedModulesValidator.onInit(); + + verify(moduleService).getAllModules(); + verify(moduleDetails).getId(); + } + + @Test(expected = IllegalStateException.class) + public void shouldThrowExceptionWhenADeprecatedModuleIsFound() + { + when(moduleService.getAllModules()).thenReturn(List.of(moduleDetails)); + when(moduleDetails.getId()).thenReturn(DEPRECATED_MODULE_1); + + deprecatedModulesValidator.onInit(); + } + + @Test + public void shouldThrowExceptionWhenMultipleDeprecatedModulesAreFound() + { + when(moduleService.getAllModules()).thenReturn(List.of(moduleDetails, moduleDetails)); + when(moduleDetails.getId()).thenReturn(DEPRECATED_MODULE_1) + .thenReturn(DEPRECATED_MODULE_2); + + assertThrows("IllegalStateException should be thrown.", + IllegalStateException.class, + () -> deprecatedModulesValidator.onInit()); + + verify(moduleService).getAllModules(); + verify(moduleDetails, times(2)).getId(); + } + + @Test + public void shouldThrowExceptionWhenBothValidAndDeprecatedModulesAreFound() + { + when(moduleService.getAllModules()).thenReturn(List.of(moduleDetails, moduleDetails)); + when(moduleDetails.getId()).thenReturn(VALID_MODULE) + .thenReturn(DEPRECATED_MODULE_2); + + assertThrows("IllegalStateException should be thrown.", + IllegalStateException.class, + () -> deprecatedModulesValidator.onInit()); + + verify(moduleService).getAllModules(); + verify(moduleDetails, times(2)).getId(); + } + +} \ No newline at end of file