Skip to content

Commit

Permalink
XWIKI-21848: Migrate NotificationFilterPreferenceLivetableResults to …
Browse files Browse the repository at this point in the history
…a Live Data source

  * Improve a bit the code of NotificationSystemFiltersLiveDataEntryStore
  * Start adding tests
  • Loading branch information
surli committed Mar 14, 2024
1 parent ed23e99 commit 175afe2
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.xwiki.notifications.filters.internal.livedata.system;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -117,48 +118,43 @@ private boolean isEnabled(ToggleableNotificationFilter notificationFilter,
public LiveData get(LiveDataQuery query) throws LiveDataException
{
TargetInformation targetInformation = getTargetInformation(query);
Map<String, ToggleableNotificationFilterActivation> filtersActivations = null;
List<ToggleableNotificationFilter> allFilters = null;
Map<String, ToggleableNotificationFilterActivation> filtersActivations;

Collection<NotificationFilter> notificationFilters;
try {
if (targetInformation.isWikiTarget) {
WikiReference wikiReference = new WikiReference(targetInformation.ownerReference);
allFilters =
this.notificationFilterManager.getAllFilters(wikiReference)
.stream()
.filter(filter -> filter instanceof ToggleableNotificationFilter)
.map(item -> (ToggleableNotificationFilter) item)
.toList();
notificationFilters = this.notificationFilterManager.getAllFilters(wikiReference);
filtersActivations =
this.filterPreferencesModelBridge.getToggleableFilterActivations(
new DocumentReference(NOTIFICATION_ADMINISTRATION_REF, wikiReference));
} else {
DocumentReference userDoc = new DocumentReference(targetInformation.ownerReference);
allFilters = this.notificationFilterManager.getAllFilters(userDoc, false)
.stream()
.filter(filter -> filter instanceof ToggleableNotificationFilter)
.map(item -> (ToggleableNotificationFilter) item)
.toList();
notificationFilters = this.notificationFilterManager.getAllFilters(userDoc, false);
filtersActivations = this.filterPreferencesModelBridge.getToggleableFilterActivations(userDoc);
}
} catch (NotificationException e) {
throw new LiveDataException("Error when getting list of filters", e);
}

List<ToggleableNotificationFilter> allFilters = notificationFilters.stream()
.filter(filter -> filter instanceof ToggleableNotificationFilter)
.map(item -> (ToggleableNotificationFilter) item)
.toList();

int totalFilters = allFilters.size();
List<ToggleableNotificationFilter> filters = allFilters
LiveData liveData = new LiveData();
liveData.setCount(totalFilters);
List<Map<String, Object>> entries = liveData.getEntries();

allFilters
.stream()
.sorted(Comparator.comparing(NotificationFilter::getName))
.skip(query.getOffset())
.limit(query.getLimit())
.toList();
.forEach(filter ->
entries.add(getPreferencesInformation(filter, filtersActivations.get(filter.getName()))));

LiveData liveData = new LiveData();
List<Map<String, Object>> entries = liveData.getEntries();
for (ToggleableNotificationFilter notificationFilter : filters) {
entries.add(getPreferencesInformation(notificationFilter,
filtersActivations.get(notificationFilter.getName())));
}
liveData.setCount(totalFilters);
return liveData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.livedata.system;

import java.util.Map;

import javax.inject.Inject;
import javax.inject.Provider;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xwiki.livedata.LiveDataException;
import org.xwiki.livedata.LiveDataQuery;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.notifications.filters.NotificationFilterManager;
import org.xwiki.notifications.filters.internal.FilterPreferencesModelBridge;
import org.xwiki.security.authorization.ContextualAuthorizationManager;
import org.xwiki.security.authorization.Right;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.junit5.mockito.MockComponent;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.test.reference.ReferenceComponentList;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Tests for {@link NotificationSystemFiltersLiveDataEntryStore}.
*
* @version $Id$
* @since 16.2.0RC1
*/
@ComponentTest
@ReferenceComponentList
class NotificationSystemFiltersLiveDataEntryStoreTest
{
@InjectMockComponents
private NotificationSystemFiltersLiveDataEntryStore entryStore;

@MockComponent
private NotificationFilterManager notificationFilterManager;

@MockComponent
private FilterPreferencesModelBridge filterPreferencesModelBridge;

@MockComponent
private ContextualAuthorizationManager contextualAuthorizationManager;

@MockComponent
private Provider<XWikiContext> contextProvider;

private XWikiContext context;

@BeforeEach
void beforeEach()
{
this.context = mock(XWikiContext.class);
when(this.contextProvider.get()).thenReturn(this.context);
}

@Test
void getMissingTarget()
{
LiveDataQuery query = mock(LiveDataQuery.class);
LiveDataQuery.Source source = mock(LiveDataQuery.Source.class);
when(query.getSource()).thenReturn(source);
when(source.getParameters()).thenReturn(Map.of());
LiveDataException liveDataException = assertThrows(LiveDataException.class, () -> this.entryStore.get(query));
assertEquals("The target source parameter is mandatory.", liveDataException.getMessage());
}

@Test
void getBadAuthorization()
{
LiveDataQuery query = mock(LiveDataQuery.class);
LiveDataQuery.Source source = mock(LiveDataQuery.Source.class);
when(query.getSource()).thenReturn(source);
String target = "wiki";
String wiki = "Wiki foo";
DocumentReference userDoc = new DocumentReference("xwiki", "XWiki", "Foo");
when(this.contextualAuthorizationManager.hasAccess(Right.ADMIN)).thenReturn(false);
when(context.getUserReference()).thenReturn(userDoc);
}
}

0 comments on commit 175afe2

Please sign in to comment.