diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/DocumentMovedListener.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/DocumentMovedListener.java index 0f3f20597297..32388300f4a4 100644 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/DocumentMovedListener.java +++ b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/DocumentMovedListener.java @@ -65,9 +65,6 @@ public class DocumentMovedListener extends AbstractEventListener @Inject private EntityReferenceSerializer serializer; - @Inject - private NotificationFilterPreferenceConfiguration filterPreferenceConfiguration; - @Inject private Logger logger; @@ -97,24 +94,16 @@ public void onEvent(Event event, Object source, Object data) DocumentReference targetLocation = renamedEvent.getTargetReference(); try { - if (filterPreferenceConfiguration.useMainStore()) { - namespaceContextExecutor.execute(new WikiNamespace(wikiDescriptorManager.getMainWikiId()), () -> { + // Filters are stored in the DB of the users, since each wiki could possibly contain a user + // we need to iterate over all DB to ensure we properly migrate the filters. + // We could have checked the configuration of the wiki to see if they are allowed to store user or not + // but this config might have changed over time... + for (String wikiId : wikiDescriptorManager.getAllIds()) { + namespaceContextExecutor.execute(new WikiNamespace(wikiId), () -> { updatePreferences(sourceLocation, targetLocation); return null; }); - } else if (filterPreferenceConfiguration.useLocalStore()) { - // Filters are stored in the DB of the users, since each wiki could possibly contain a user - // we need to iterate over all DB to ensure we properly migrate the filters. - // We could have checked the configuration of the wiki to see if they are allowed to store user or not - // but this config might have changed over time... - for (String wikiId : wikiDescriptorManager.getAllIds()) { - namespaceContextExecutor.execute(new WikiNamespace(wikiId), () -> { - updatePreferences(sourceLocation, targetLocation); - return null; - }); - } } - } catch (Exception e) { logger.error("Failed to update the notification filter preference when [{}] has been moved to [{}].", renamedEvent.getSourceReference(), renamedEvent.getTargetReference(), e); diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceConfiguration.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceConfiguration.java deleted file mode 100644 index b90310e7575f..000000000000 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceConfiguration.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * See the NOTICE file distributed with this work for additional - * information regarding copyright ownership. - * - * This 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 2.1 of - * the License, or (at your option) any later version. - * - * This software 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 this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ -package org.xwiki.notifications.filters.internal; - -import javax.inject.Inject; -import javax.inject.Singleton; - -import org.xwiki.component.annotation.Component; -import org.xwiki.configuration.ConfigurationSource; -import org.xwiki.observation.event.Event; - -/** - * The configuration of the default Notification Filter Preference implementation. Note: the configuration options - * {@link #useLocalStore()} and {@link #useMainStore()} are not used in a consistent way. - * {@link DocumentMovedListener#onEvent(Event, Object, Object)} consider that both method can return {@code true} at the - * same time, meaning that preferences can be stored duplicated in both the main wiki and a local wiki if both options - * are true. Whereas, {@link NotificationFilterPreferenceStore} only consider {@link #useMainStore()} to decides where - * to store the preferences. - * - * @version $Id$ - * @since 12.6 - */ -@Component(roles = NotificationFilterPreferenceConfiguration.class) -@Singleton -public class NotificationFilterPreferenceConfiguration -{ - private static final String PREFERENCE_PREFIX = "eventstream."; - - @Inject - private ConfigurationSource configurationSource; - - /** - * Indicates if the filter preference must be stored in the local wiki, by reading the - * {@code eventstream.usemainstore} and {@code eventstream.uselocalstore} properties. The default value of the - * properties is {@code true}. - * - * @return {@code false} if {@link #useMainStore()} returns {@code true}, and {@code eventstream.uselocalstore} is - * {@code false}, {@code true} is returned otherwise - */ - public boolean useLocalStore() - { - if (!useMainStore()) { - // If the main store is disabled, force local store. - return true; - } - - return getProperty("uselocalstore"); - } - - /** - * Indicates if the filter preference must be stored in the main wiki, by reading the - * {@code eventstream.usemainstore} property. The default value is {@code true}. - * - * @return {@code true} if the filter preferences should be stored in the main wiki, {@code false} otherwise - */ - public boolean useMainStore() - { - return getProperty("usemainstore"); - } - - private boolean getProperty(String name) - { - return this.configurationSource.getProperty(PREFERENCE_PREFIX + name, true); - } -} diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceStore.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceStore.java index 50b748544a0b..dfd4f380a70a 100644 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceStore.java +++ b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceStore.java @@ -65,9 +65,6 @@ public class NotificationFilterPreferenceStore { private static final String FILTER_PREFIX = "NFP_"; - @Inject - private NotificationFilterPreferenceConfiguration filterPreferenceConfiguration; - @Inject private EntityReferenceSerializer entityReferenceSerializer; @@ -462,11 +459,7 @@ private T configureContextWrapper(WikiReference wikiRef { XWikiContext context = this.contextProvider.get(); WikiReference currentWiki = context.getWikiReference(); - if (this.filterPreferenceConfiguration.useMainStore()) { - context.setWikiId(context.getMainXWiki()); - } else if (wikiReference != null) { - context.setWikiReference(wikiReference); - } + context.setWikiReference(wikiReference); try { return supplier.get(); } finally { diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R140401000XWIKI15460DataMigration.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R140401000XWIKI15460DataMigration.java index 3e4d535f2811..b8c9b3d23865 100644 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R140401000XWIKI15460DataMigration.java +++ b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R140401000XWIKI15460DataMigration.java @@ -35,13 +35,13 @@ import javax.inject.Singleton; import org.xwiki.component.annotation.Component; +import org.xwiki.configuration.ConfigurationSource; import org.xwiki.model.reference.DocumentReference; import org.xwiki.model.reference.DocumentReferenceResolver; import org.xwiki.model.reference.EntityReferenceSerializer; import org.xwiki.model.reference.WikiReference; import org.xwiki.notifications.NotificationException; import org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference; -import org.xwiki.notifications.filters.internal.NotificationFilterPreferenceConfiguration; import org.xwiki.notifications.filters.internal.NotificationFilterPreferenceStore; import org.xwiki.query.Query; import org.xwiki.query.QueryException; @@ -94,9 +94,6 @@ public class R140401000XWIKI15460DataMigration extends AbstractHibernateDataMigr @Named("local") private EntityReferenceSerializer entityReferenceSerializer; - @Inject - private NotificationFilterPreferenceConfiguration filterPreferenceConfiguration; - @Inject private QueryManager queryManager; @@ -107,6 +104,9 @@ public class R140401000XWIKI15460DataMigration extends AbstractHibernateDataMigr @Inject private UserManager userManager; + @Inject + private ConfigurationSource configurationSource; + @Override public XWikiDBVersion getVersion() { @@ -139,13 +139,18 @@ protected void hibernateMigrate() throws DataMigrationException // Stop the execution early if the configuration uses the main store and we are not upgrading the main wiki. // This check cannot be done in #shouldExecute because possibly missing columns are not yet added to the // database. - if (this.filterPreferenceConfiguration.useMainStore() && !isMainWiki) { + if (useMainStore() && !isMainWiki) { return; } internalHibernateMigrate(isMainWiki); } + private boolean useMainStore() + { + return this.configurationSource.getProperty("eventstream.usemainstore", true); + } + private void internalHibernateMigrate(boolean isMainWiki) throws DataMigrationException { try { diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R151002000XWIKI21448DataMigration.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R151002000XWIKI21448DataMigration.java index 9cc53cc473ed..ca7a5e562484 100644 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R151002000XWIKI21448DataMigration.java +++ b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R151002000XWIKI21448DataMigration.java @@ -35,10 +35,10 @@ import org.xwiki.cache.CacheManager; import org.xwiki.cache.config.LRUCacheConfiguration; import org.xwiki.component.annotation.Component; +import org.xwiki.configuration.ConfigurationSource; import org.xwiki.model.reference.DocumentReference; import org.xwiki.model.reference.DocumentReferenceResolver; import org.xwiki.model.reference.EntityReferenceSerializer; -import org.xwiki.notifications.filters.internal.NotificationFilterPreferenceConfiguration; import org.xwiki.query.Query; import org.xwiki.query.QueryException; import org.xwiki.query.QueryFilter; @@ -68,7 +68,7 @@ public class R151002000XWIKI21448DataMigration extends AbstractHibernateDataMigr private WikiDescriptorManager wikiDescriptorManager; @Inject - private NotificationFilterPreferenceConfiguration filterPreferenceConfiguration; + private ConfigurationSource configurationSource; @Inject private QueryManager queryManager; @@ -112,13 +112,18 @@ protected void hibernateMigrate() throws DataMigrationException, XWikiException // Stop the execution early if the configuration uses the main store, and we are not upgrading the main wiki. // This check cannot be done in #shouldExecute because possibly missing columns are not yet added to the // database. - if (this.filterPreferenceConfiguration.useMainStore() && !isMainWiki) { + if (useMainStore() && !isMainWiki) { return; } internalHibernateMigrate(); } + private boolean useMainStore() + { + return this.configurationSource.getProperty("eventstream.usemainstore", true); + } + private void internalHibernateMigrate() throws DataMigrationException { // Cache of boolean: true if the documents exists, false if it's missing. diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R160000001XWIKI21738DataMigration.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R160000001XWIKI21738DataMigration.java new file mode 100644 index 000000000000..434ec41458f3 --- /dev/null +++ b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/java/org/xwiki/notifications/filters/migration/R160000001XWIKI21738DataMigration.java @@ -0,0 +1,88 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This 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 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.notifications.filters.migration; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.slf4j.Logger; +import org.xwiki.component.annotation.Component; +import org.xwiki.model.reference.DocumentReferenceResolver; +import org.xwiki.query.QueryFilter; +import org.xwiki.query.QueryManager; + +import com.xpn.xwiki.XWikiException; +import com.xpn.xwiki.store.migration.DataMigrationException; +import com.xpn.xwiki.store.migration.XWikiDBVersion; +import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration; + +/** + * Migrate old WatchListClass xobjects to save them as proper notification filters. The migration doesn't directly + * remove the xobjects but asks {@link WatchListObjectsRemovalTaskConsumer} to do it. + * + * @version $Id$ + * @since 16.0.0RC1 + */ +@Component +@Named("R160000000XWIKI17243") +@Singleton +public class R160000001XWIKI21738DataMigration extends AbstractHibernateDataMigration +{ + private static final int BATCH_SIZE = 100; + + @Inject + private QueryManager queryManager; + + @Inject + @Named("unique") + private QueryFilter uniqueFilter; + + @Inject + private DocumentReferenceResolver documentReferenceResolver; + + @Inject + private Logger logger; + + @Override + public String getDescription() + { + return "Migrate filters next to users."; + } + + @Override + public XWikiDBVersion getVersion() + { + return new XWikiDBVersion(160000000); + } + + @Override + protected void hibernateMigrate() throws DataMigrationException, XWikiException + { + // This migration only needs to be performed on main wiki: if we have filters on local wikis it's because + // local filters was enabled. + // Migration steps: + // - Retrieve all filters from main wiki where owner belongs to a subwiki + // - Store that filter on the subwiki DB and remove it from main wiki + + String statement = "from doc.object(XWiki.WatchListClass) as user"; + } + +} diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/resources/META-INF/components.txt b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/resources/META-INF/components.txt index ef6153436b28..f3b1d8cfbe0c 100644 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/resources/META-INF/components.txt +++ b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/main/resources/META-INF/components.txt @@ -1,7 +1,6 @@ org.xwiki.notifications.filters.internal.CachedModelBridgeInvalidatorListener org.xwiki.notifications.filters.internal.DefaultFilterPreferencesModelBridge org.xwiki.notifications.filters.internal.DocumentMovedListener -org.xwiki.notifications.filters.internal.NotificationFilterPreferenceConfiguration org.xwiki.notifications.filters.internal.NotificationFilterPreferenceStore org.xwiki.notifications.filters.internal.ToggleableFilterPreferenceDocumentInitializer org.xwiki.notifications.filters.internal.WikiNotificationFilterDisplayer diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/DocumentMovedListenerTest.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/DocumentMovedListenerTest.java index 6624a53a832e..4fa169f72ae8 100644 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/DocumentMovedListenerTest.java +++ b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/DocumentMovedListenerTest.java @@ -56,7 +56,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; /** @@ -79,9 +78,6 @@ class DocumentMovedListenerTest @MockComponent private EntityReferenceSerializer serializer; - @MockComponent - private NotificationFilterPreferenceConfiguration filterPreferencesConfiguration; - @MockComponent private NamespaceContextExecutor namespaceContextExecutor; @@ -137,9 +133,7 @@ void onEventWhenNonTerminalDocumentOnMainWiki() throws Exception when(serializer.serialize(target)).thenReturn("xwiki:PageB.WebHome"); // Mock - when(filterPreferencesConfiguration.useLocalStore()).thenReturn(true); - when(filterPreferencesConfiguration.useMainStore()).thenReturn(true); - when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki"); + when(wikiDescriptorManager.getAllIds()).thenReturn(List.of("mainWiki")); session = mock(Session.class); when(hibernateStore.getSession(eq(xwikicontext))).thenReturn(session); @@ -180,8 +174,6 @@ void onEventWhenNonTerminalDocumentOnMainWikiLocalStoreOnly() throws Exception when(serializer.serialize(target)).thenReturn("xwiki:PageB.WebHome"); // Mock - when(filterPreferencesConfiguration.useLocalStore()).thenReturn(true); - when(filterPreferencesConfiguration.useMainStore()).thenReturn(false); when(wikiDescriptorManager.getAllIds()).thenReturn(List.of("foo", "bar", "mainWiki")); session = mock(Session.class); @@ -214,28 +206,6 @@ void onEventWhenNonTerminalDocumentOnMainWikiLocalStoreOnly() throws Exception verify(namespaceContextExecutor).execute(eq(new WikiNamespace("bar")), any()); } - @Test - void onEventWhenNonTerminalDocumentOnMainWikiNoStore() throws Exception - { - DocumentReference source = new DocumentReference("xwiki", "PageA", "WebHome"); - DocumentReference target = new DocumentReference("xwiki", "PageB", "WebHome"); - when(serializer.serialize(new SpaceReference("PageA", new WikiReference("xwiki")))).thenReturn("xwiki:PageA"); - when(serializer.serialize(new SpaceReference("PageB", new WikiReference("xwiki")))).thenReturn("xwiki:PageB"); - when(serializer.serialize(source)).thenReturn("xwiki:PageA.WebHome"); - when(serializer.serialize(target)).thenReturn("xwiki:PageB.WebHome"); - - // Mock - when(filterPreferencesConfiguration.useLocalStore()).thenReturn(false); - when(filterPreferencesConfiguration.useMainStore()).thenReturn(false); - - // Test - DocumentRenamedEvent event = new DocumentRenamedEvent(source, target); - this.listener.onEvent(event, null, null); - - // Verify - verify(cachedModelBridge).clearCache(); - verifyNoInteractions(hibernateStore); - } @Test void onEventWhenNonTerminalDocumentOnSubWiki() throws Exception @@ -248,9 +218,7 @@ void onEventWhenNonTerminalDocumentOnSubWiki() throws Exception when(serializer.serialize(target)).thenReturn("xwiki:PageB.WebHome"); // Mock - when(filterPreferencesConfiguration.useLocalStore()).thenReturn(true); - when(filterPreferencesConfiguration.useMainStore()).thenReturn(true); - when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki"); + when(wikiDescriptorManager.getAllIds()).thenReturn(List.of("mainWiki")); session = mock(Session.class); when(hibernateStore.getSession(eq(xwikicontext))).thenReturn(session); @@ -289,9 +257,7 @@ void onEventNonTerminalDocumentOnMainWiki() throws Exception when(serializer.serialize(target)).thenReturn("xwiki:PageB.Terminal"); // Mock - when(filterPreferencesConfiguration.useLocalStore()).thenReturn(true); - when(filterPreferencesConfiguration.useMainStore()).thenReturn(true); - when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki"); + when(wikiDescriptorManager.getAllIds()).thenReturn(List.of("mainWiki")); session = mock(Session.class); when(hibernateStore.getSession(eq(xwikicontext))).thenReturn(session); diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceConfigurationTest.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceConfigurationTest.java deleted file mode 100644 index 64e636029af7..000000000000 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceConfigurationTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * See the NOTICE file distributed with this work for additional - * information regarding copyright ownership. - * - * This 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 2.1 of - * the License, or (at your option) any later version. - * - * This software 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 this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ -package org.xwiki.notifications.filters.internal; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.xwiki.configuration.ConfigurationSource; -import org.xwiki.test.junit5.mockito.ComponentTest; -import org.xwiki.test.junit5.mockito.InjectMockComponents; -import org.xwiki.test.junit5.mockito.MockComponent; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.when; - -/** - * Validate {@link NotificationFilterPreferenceConfiguration}. - * - * @version $Id$ - */ -@ComponentTest -public class NotificationFilterPreferenceConfigurationTest -{ - @MockComponent - private ConfigurationSource configurationSource; - - @InjectMockComponents - private NotificationFilterPreferenceConfiguration configuration; - - @BeforeEach - void beforeEach() - { - when(this.configurationSource.getProperty("eventstream.uselocalstore", true)).thenReturn(true); - when(this.configurationSource.getProperty("eventstream.usemainstore", true)).thenReturn(true); - } - - @Test - void useLocalStore() - { - assertTrue(this.configuration.useLocalStore()); - - when(this.configurationSource.getProperty("eventstream.uselocalstore", true)).thenReturn(false); - - assertFalse(this.configuration.useLocalStore()); - - when(this.configurationSource.getProperty("eventstream.usemainstore", true)).thenReturn(false); - - assertTrue(this.configuration.useLocalStore()); - } - - @Test - void useMainStore() - { - assertTrue(this.configuration.useMainStore()); - - when(this.configurationSource.getProperty("eventstream.usemainstore", true)).thenReturn(false); - - assertFalse(this.configuration.useMainStore()); - } -} diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceStoreTest.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceStoreTest.java index 99446c6b6412..7e8ba9f34fee 100644 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceStoreTest.java +++ b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/internal/NotificationFilterPreferenceStoreTest.java @@ -26,8 +26,7 @@ import org.hibernate.Session; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -83,9 +82,6 @@ class NotificationFilterPreferenceStoreTest @MockComponent private EntityReferenceSerializer entityReferenceSerializer; - @MockComponent - private NotificationFilterPreferenceConfiguration filterPreferenceConfiguration; - @MockComponent private ObservationManager observationManager; @@ -128,21 +124,15 @@ public Void answer(InvocationOnMock invocation) throws Throwable when(this.query.setParameter(anyString(), any())).thenReturn(this.query); } - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void deleteWikiFilterPreferences(boolean useMainStore) throws Exception + @Test + void deleteWikiFilterPreferences() throws Exception { - when(this.filterPreferenceConfiguration.useMainStore()).thenReturn(useMainStore); when(this.context.getWikiReference()).thenReturn(CURRENT_WIKI_REFERENCE); WikiReference wikiReference = new WikiReference("wikiid"); this.notificationFilterPreferenceStore.deleteFilterPreference(wikiReference); - if (useMainStore) { - verify(this.context).setWikiId(MAIN_WIKI_REFERENCE.getName()); - } else { - verify(this.context).setWikiReference(wikiReference); - } + verify(this.context).setWikiReference(wikiReference); verify(this.session).createQuery("delete from DefaultNotificationFilterPreference " + "where page like :wikiPrefix " + "or pageOnly like :wikiPrefix " @@ -154,11 +144,9 @@ void deleteWikiFilterPreferences(boolean useMainStore) throws Exception verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); } - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void deleteWikiFilterPreferenceHibernateException(boolean useMainStore) throws Exception + @Test + void deleteWikiFilterPreferenceHibernateException() throws Exception { - when(this.filterPreferenceConfiguration.useMainStore()).thenReturn(useMainStore); when(this.context.getWikiReference()).thenReturn(CURRENT_WIKI_REFERENCE); when(this.hibernateStore.executeWrite(same(context), any())).thenThrow(XWikiException.class); @@ -172,22 +160,15 @@ void deleteWikiFilterPreferenceHibernateException(boolean useMainStore) throws E notificationException.getMessage()); assertEquals(XWikiException.class, notificationException.getCause().getClass()); - if (useMainStore) { - verify(this.context).setWikiId(MAIN_WIKI_REFERENCE.getName()); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } else { - verify(this.context).setWikiReference(wikiReference); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } + verify(this.context).setWikiReference(wikiReference); + verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); } - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void deleteAllUserFilterPreferences(boolean useMainStore) throws Exception + @Test + void deleteAllUserFilterPreferences() throws Exception { DocumentReference unknownUserDocumentReference = new DocumentReference("xwiki", "XWiki", "UnknownUser"); when(this.context.getWikiReference()).thenReturn(CURRENT_WIKI_REFERENCE); - when(this.filterPreferenceConfiguration.useMainStore()).thenReturn(useMainStore); when(this.entityReferenceSerializer.serialize(unknownUserDocumentReference)) .thenReturn("xwiki:XWiki.UnknownUser"); @@ -197,47 +178,34 @@ void deleteAllUserFilterPreferences(boolean useMainStore) throws Exception + "or user = :user"); verify(this.query).setParameter("user", "xwiki:XWiki.UnknownUser"); verify(this.query).executeUpdate(); - if (useMainStore) { - verify(this.context).setWikiId(MAIN_WIKI_REFERENCE.getName()); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } else { - verify(this.context).setWikiReference(new WikiReference("xwiki")); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } + + verify(this.context).setWikiReference(new WikiReference("xwiki")); + verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); } - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void deleteUserFilterPreferences(boolean useMainStore) throws Exception + @Test + void deleteUserFilterPreferences() throws Exception { DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "Foo"); Set internalIds = Set.of(12L, 14L, 23434L, 3243L, 223L); Set filterIds = Set.of("NFP_12", "NFP_14", "NFP_23434", "NFP_3243", "NFP_223"); when(this.context.getWikiReference()).thenReturn(CURRENT_WIKI_REFERENCE); - when(this.filterPreferenceConfiguration.useMainStore()).thenReturn(useMainStore); this.notificationFilterPreferenceStore.deleteFilterPreferences(userReference, filterIds); verify(this.session).createQuery( "delete from DefaultNotificationFilterPreference where internalId in (:id)"); verify(this.query).setParameter("id", internalIds); verify(this.query).executeUpdate(); - if (useMainStore) { - verify(this.context).setWikiId(MAIN_WIKI_REFERENCE.getName()); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } else { - verify(this.context).setWikiReference(new WikiReference("xwiki")); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } + verify(this.context).setWikiReference(new WikiReference("xwiki")); + verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); verify(this.observationManager).notify( any(NotificationFilterPreferenceDeletedEvent.class), eq(userReference), eq(filterIds)); } - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void deleteUserFilterPreferencesHibernateException(boolean useMainStore) throws Exception + @Test + void deleteUserFilterPreferencesHibernateException() throws Exception { DocumentReference unknownUserDocumentReference = new DocumentReference("xwiki", "XWiki", "UnknownUser"); when(this.context.getWikiReference()).thenReturn(CURRENT_WIKI_REFERENCE); - when(this.filterPreferenceConfiguration.useMainStore()).thenReturn(useMainStore); when(this.entityReferenceSerializer.serialize(unknownUserDocumentReference)) .thenReturn("xwiki:XWiki.UnknownUser"); when(this.hibernateStore.executeWrite(same(context), any())).thenThrow(XWikiException.class); @@ -249,73 +217,52 @@ void deleteUserFilterPreferencesHibernateException(boolean useMainStore) throws notificationException.getMessage()); assertEquals(XWikiException.class, notificationException.getCause().getClass()); - if (useMainStore) { - verify(this.context).setWikiId(MAIN_WIKI_REFERENCE.getName()); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } else { - verify(this.context).setWikiReference(new WikiReference("xwiki")); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } + verify(this.context).setWikiReference(new WikiReference("xwiki")); + verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); } - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void deleteUserFilterPreference(boolean useMainStore) throws NotificationException + @Test + void deleteUserFilterPreference() throws NotificationException { DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "Foo"); long internalId = 84523; String filterId = "NFP_" + internalId; when(this.context.getWikiReference()).thenReturn(CURRENT_WIKI_REFERENCE); - when(this.filterPreferenceConfiguration.useMainStore()).thenReturn(useMainStore); this.notificationFilterPreferenceStore.deleteFilterPreference(userReference, filterId); verify(this.session).createQuery( "delete from DefaultNotificationFilterPreference where internalId in (:id)"); verify(this.query).setParameter("id", Set.of(internalId)); verify(this.query).executeUpdate(); - if (useMainStore) { - verify(this.context).setWikiId(MAIN_WIKI_REFERENCE.getName()); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } else { - verify(this.context).setWikiReference(new WikiReference("xwiki")); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } + verify(this.context).setWikiReference(new WikiReference("xwiki")); + verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); verify(this.observationManager).notify( any(NotificationFilterPreferenceDeletedEvent.class), eq(userReference), eq(Set.of(filterId))); } - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void deleteWikiFilterPreference(boolean useMainStore) throws NotificationException + @Test + void deleteWikiFilterPreference() throws NotificationException { WikiReference wikiReference = new WikiReference("foo"); long internalId = 74; String filterId = "NFP_" + internalId; when(this.context.getWikiReference()).thenReturn(CURRENT_WIKI_REFERENCE); - when(this.filterPreferenceConfiguration.useMainStore()).thenReturn(useMainStore); this.notificationFilterPreferenceStore.deleteFilterPreference(wikiReference, filterId); verify(this.session).createQuery( "delete from DefaultNotificationFilterPreference where internalId in (:id)"); verify(this.query).setParameter("id", Set.of(internalId)); verify(this.query).executeUpdate(); - if (useMainStore) { - verify(this.context).setWikiId(MAIN_WIKI_REFERENCE.getName()); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } else { - verify(this.context).setWikiReference(wikiReference); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } + verify(this.context).setWikiReference(wikiReference); + verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); verify(this.observationManager).notify( any(NotificationFilterPreferenceDeletedEvent.class), eq(wikiReference), eq(filterId)); } - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void getPreferencesOfUser(boolean useMainStore) throws QueryException, NotificationException + @Test + void getPreferencesOfUser() throws QueryException, NotificationException { DocumentReference userRef = new DocumentReference("subwiki", "XWiki", "Foo"); String serializedRef = "subwiki:XWiki.Foo"; - when(this.filterPreferenceConfiguration.useMainStore()).thenReturn(useMainStore); when(this.entityReferenceSerializer.serialize(userRef)).thenReturn(serializedRef); when(this.context.getWikiReference()).thenReturn(CURRENT_WIKI_REFERENCE); @@ -330,13 +277,8 @@ void getPreferencesOfUser(boolean useMainStore) throws QueryException, Notificat assertEquals(List.of(pref1, pref2), this.notificationFilterPreferenceStore.getPreferencesOfUser(userRef)); - if (useMainStore) { - verify(this.context).setWikiId(MAIN_WIKI_REFERENCE.getName()); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } else { - verify(this.context).setWikiReference(new WikiReference("subwiki")); - verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); - } + verify(this.context).setWikiReference(new WikiReference("subwiki")); + verify(this.context).setWikiReference(CURRENT_WIKI_REFERENCE); verify(xwikiQuery).bindValue("owner", serializedRef); verify(pref1).setProviderHint(UserProfileNotificationFilterPreferenceProvider.HINT); verify(pref2).setProviderHint(UserProfileNotificationFilterPreferenceProvider.HINT); diff --git a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/migration/R140401000XWIKI15460DataMigrationTest.java b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/migration/R140401000XWIKI15460DataMigrationTest.java index 9ed5ba324adb..e26c6cb15482 100644 --- a/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/migration/R140401000XWIKI15460DataMigrationTest.java +++ b/xwiki-platform-core/xwiki-platform-notifications/xwiki-platform-notifications-filters/xwiki-platform-notifications-filters-default/src/test/java/org/xwiki/notifications/filters/migration/R140401000XWIKI15460DataMigrationTest.java @@ -30,6 +30,7 @@ import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import org.xwiki.configuration.ConfigurationSource; import org.xwiki.context.Execution; import org.xwiki.context.ExecutionContext; import org.xwiki.model.reference.DocumentReference; @@ -38,7 +39,6 @@ import org.xwiki.model.reference.WikiReference; import org.xwiki.notifications.NotificationException; import org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference; -import org.xwiki.notifications.filters.internal.NotificationFilterPreferenceConfiguration; import org.xwiki.notifications.filters.internal.NotificationFilterPreferenceStore; import org.xwiki.query.Query; import org.xwiki.query.QueryFilter; @@ -100,9 +100,6 @@ class R140401000XWIKI15460DataMigrationTest @MockComponent private UserManager userManager; - @MockComponent - private NotificationFilterPreferenceConfiguration filterPreferenceConfiguration; - @MockComponent private Execution execution; @@ -117,6 +114,9 @@ class R140401000XWIKI15460DataMigrationTest @MockComponent private QueryManager queryManager; + @MockComponent + private ConfigurationSource configurationSource; + private XWikiContext context; @RegisterExtension @@ -163,6 +163,7 @@ void shouldExecute() @CsvSource({ "mainwikiid,1", "anotherwiki,0" }) void hibernateMigrate(String currentWikiId, int expectedTimes) throws Exception { + when(this.configurationSource.getProperty("eventstream.usemainstore", true)).thenReturn(false); // nfp0 is from main wiki // nfp1 is from a removed wiki // nfp2 is from a not-removed wikiA @@ -287,7 +288,7 @@ void hibernateMigrate(String currentWikiId, int expectedTimes) throws Exception void hibernateMigrateSkipped() throws Exception { when(this.wikiDescriptorManager.getCurrentWikiId()).thenReturn("anotherwiki"); - when(this.filterPreferenceConfiguration.useMainStore()).thenReturn(true); + when(this.configurationSource.getProperty("eventstream.usemainstore", true)).thenReturn(true); this.dataMigration.hibernateMigrate(); // The store is used on all execution path after the check of the first condition. @@ -298,6 +299,8 @@ void hibernateMigrateSkipped() throws Exception @Test void hibernateMigrateNotificationException() throws Exception { + + when(this.configurationSource.getProperty("eventstream.usemainstore", true)).thenReturn(false); when(this.store.getPaginatedFilterPreferences(1000, 0)).thenThrow(NotificationException.class); DataMigrationException dataMigrationException = assertThrows(DataMigrationException.class, () -> this.dataMigration.hibernateMigrate()); @@ -308,6 +311,7 @@ void hibernateMigrateNotificationException() throws Exception @Test void hibernateMigrateWikiManagerException() throws Exception { + when(this.configurationSource.getProperty("eventstream.usemainstore", true)).thenReturn(false); when(this.wikiDescriptorManager.getAllIds()).thenThrow(WikiManagerException.class); DataMigrationException dataMigrationException = assertThrows(DataMigrationException.class, () -> this.dataMigration.hibernateMigrate());