Search in sources :

Example 26 with NotificationFilterPreference

use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.

the class DefaultWatchedEntitiesManagerTest method testWithSeveralFilterPreferences.

@Test
public void testWithSeveralFilterPreferences() throws Exception {
    // Mocks
    WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
    DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
    // Filters
    NotificationFilterPreference pref1 = mock(NotificationFilterPreference.class);
    when(pref1.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
    when(pref1.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
    NotificationFilterPreference pref2 = mock(NotificationFilterPreference.class);
    when(pref2.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Arrays.asList("update"));
    when(pref2.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
    when(watchedEntityReference.matchExactly(pref2)).thenReturn(true);
    NotificationFilterPreference pref3 = mock(NotificationFilterPreference.class);
    when(pref3.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
    when(pref3.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT));
    when(watchedEntityReference.matchExactly(pref3)).thenReturn(true);
    NotificationFilterPreference pref4 = mock(NotificationFilterPreference.class);
    when(pref4.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
    when(pref4.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
    when(watchedEntityReference.matchExactly(pref4)).thenReturn(true);
    when(pref4.getFilterType()).thenReturn(NotificationFilterType.INCLUSIVE);
    when(pref4.isEnabled()).thenReturn(false);
    when(pref4.getFilterPreferenceName()).thenReturn("pref4");
    when(notificationFilterManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1, pref2, pref3, pref4));
    when(watchedEntityReference.isWatched(user)).thenReturn(false, true);
    // Test
    mocker.getComponentUnderTest().watchEntity(watchedEntityReference, user);
    // Checks
    verify(watchedEntityReference, never()).matchExactly(pref2);
    verify(watchedEntityReference, never()).matchExactly(pref3);
    verify(notificationFilterManager).setFilterPreferenceEnabled("pref4", true);
    verify(watchedEntityReference, never()).createInclusiveFilterPreference();
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) WatchedEntityReference(org.xwiki.notifications.filters.watch.WatchedEntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 27 with NotificationFilterPreference

use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.

the class DefaultWatchedEntitiesManagerTest method unwatchWhenNoFilterMatch.

@Test
public void unwatchWhenNoFilterMatch() throws Exception {
    // Mocks
    WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
    DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
    // Filters
    NotificationFilterPreference pref1 = mock(NotificationFilterPreference.class);
    when(pref1.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
    when(pref1.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
    when(notificationFilterManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1));
    when(watchedEntityReference.isWatched(user)).thenReturn(true);
    NotificationFilterPreference createdPref = mock(NotificationFilterPreference.class);
    when(watchedEntityReference.createExclusiveFilterPreference()).thenReturn(createdPref);
    // Test
    mocker.getComponentUnderTest().unwatchEntity(watchedEntityReference, user);
    // Checks
    verify(notificationFilterManager).saveFilterPreferences(eq(Sets.newSet(createdPref)));
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) WatchedEntityReference(org.xwiki.notifications.filters.watch.WatchedEntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 28 with NotificationFilterPreference

use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.

the class DefaultWatchedEntitiesManagerTest method unwatchWhenInclusiveFilter.

@Test
public void unwatchWhenInclusiveFilter() throws Exception {
    // Mocks
    WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
    DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
    // Filters
    NotificationFilterPreference pref1 = mock(NotificationFilterPreference.class);
    when(pref1.getProperties(NotificationFilterProperty.EVENT_TYPE)).thenReturn(Collections.emptyList());
    when(pref1.getFilterFormats()).thenReturn(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
    when(watchedEntityReference.matchExactly(pref1)).thenReturn(true);
    when(pref1.getFilterType()).thenReturn(NotificationFilterType.INCLUSIVE);
    when(pref1.isEnabled()).thenReturn(true);
    when(pref1.getFilterPreferenceName()).thenReturn("pref1");
    when(notificationFilterManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1));
    when(watchedEntityReference.isWatched(user)).thenReturn(true, false);
    // Test
    mocker.getComponentUnderTest().unwatchEntity(watchedEntityReference, user);
    // Checks
    verify(notificationFilterManager).deleteFilterPreference("pref1");
    verify(watchedEntityReference, never()).createExclusiveFilterPreference();
}
Also used : NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) WatchedEntityReference(org.xwiki.notifications.filters.watch.WatchedEntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 29 with NotificationFilterPreference

use of org.xwiki.notifications.filters.NotificationFilterPreference in project xwiki-platform by xwiki.

the class CachedModelBridge method getFilterPreferences.

@Override
public Set<NotificationFilterPreference> getFilterPreferences(DocumentReference user) throws NotificationException {
    // We need to store the user reference in the cache's key, otherwise all users of the same context will share
    // the same cache, which can happen when a notification email is triggered.
    final String contextEntry = String.format(CONTEXT_KEY_FORMAT, USER_FILTER_PREFERENCES, serializer.serialize(user));
    ExecutionContext context = execution.getContext();
    Object cachedPreferences = context.getProperty(contextEntry);
    if (cachedPreferences != null && cachedPreferences instanceof Set) {
        return (Set<NotificationFilterPreference>) cachedPreferences;
    }
    Set<NotificationFilterPreference> preferences = modelBridge.getFilterPreferences(user);
    context.setProperty(contextEntry, preferences);
    return preferences;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Set(java.util.Set) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference)

Aggregations

NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)29 Test (org.junit.Test)14 DocumentReference (org.xwiki.model.reference.DocumentReference)13 WatchedEntityReference (org.xwiki.notifications.filters.watch.WatchedEntityReference)8 BaseObject (com.xpn.xwiki.objects.BaseObject)4 HashSet (java.util.HashSet)4 XWiki (com.xpn.xwiki.XWiki)3 XWikiContext (com.xpn.xwiki.XWikiContext)3 XWikiException (com.xpn.xwiki.XWikiException)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 HashMap (java.util.HashMap)3 NotificationException (org.xwiki.notifications.NotificationException)3 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)3 Set (java.util.Set)2 Event (org.xwiki.eventstream.Event)2 SpaceReference (org.xwiki.model.reference.SpaceReference)2 WikiReference (org.xwiki.model.reference.WikiReference)2 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)2 NotificationFilterProperty (org.xwiki.notifications.filters.NotificationFilterProperty)2 NotificationPreferenceProperty (org.xwiki.notifications.preferences.NotificationPreferenceProperty)2