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();
}
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)));
}
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();
}
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;
}
Aggregations