use of org.xwiki.notifications.preferences.NotificationPreferenceProperty in project xwiki-platform by xwiki.
the class ScopeNotificationFilterTest method testWithTopLevelInclusiveFilters.
@Test
public void testWithTopLevelInclusiveFilters() throws Exception {
// Preferences:
//
// α: "update" event type enabled for format ALERT
//
// γ: Inclusive filter on "wikiA:SpaceB"
// ζ: Inclusive filter on "wikiA:SpaceM.DocumentN"
// Mock α
NotificationPreference preference = mock(NotificationPreference.class);
when(preference.getFormat()).thenReturn(NotificationFormat.ALERT);
Map<NotificationPreferenceProperty, Object> properties = new HashMap<>();
properties.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
when(preference.getProperties()).thenReturn(properties);
// Mock γ
WikiReference wikiReference = new WikiReference("wikiA");
SpaceReference spaceReferenceB = new SpaceReference("SpaceB", new WikiReference(wikiReference));
NotificationFilterPreference prefγ = mockNotificationFilterPreference("wikiA:SpaceB", spaceReferenceB, NotificationFilterType.INCLUSIVE, null);
// Mock ζ
DocumentReference documentReference = new DocumentReference("wikiA", "SpaceM", "DocumentN");
NotificationFilterPreference prefζ = mockNotificationFilterPreference("wikiA:SpaceM.DocumentN", documentReference, NotificationFilterType.INCLUSIVE, null);
when(notificationFilterManager.getFilterPreferences(any(DocumentReference.class))).thenReturn(Sets.newSet(prefγ, prefζ));
DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
// Test 1
String result = mocker.getComponentUnderTest().filterExpression(user, preference).toString();
assertEquals("((WIKI = \"wikiA\" AND SPACE STARTS WITH \"wikiA:SpaceB\") " + "OR (WIKI = \"wikiA\" AND PAGE = \"wikiA:SpaceM.DocumentN\"))", result);
// Test with wikiA:SpaceE (filtered by γ & ζ)
Event event1 = mock(Event.class);
when(event1.getSpace()).thenReturn(new SpaceReference("SpaceE", wikiReference));
assertEquals(NotificationFilter.FilterPolicy.FILTER, mocker.getComponentUnderTest().filterEvent(event1, user, NotificationFormat.ALERT));
// Test with wikiA:SpaceB.DocumentJ (kept by γ)
Event event2 = mock(Event.class);
when(event2.getDocument()).thenReturn(new DocumentReference("wikiA", "SpaceB", "DocumentJ"));
assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event2, user, NotificationFormat.ALERT));
// Test with wikiB:SpaceK.DocumentL (filtered by γ & ζ)
Event event3 = mock(Event.class);
when(event3.getDocument()).thenReturn(new DocumentReference("wikiB", "SpaceK", "DocumentL"));
assertEquals(NotificationFilter.FilterPolicy.FILTER, mocker.getComponentUnderTest().filterEvent(event3, user, NotificationFormat.ALERT));
// Test with wikiA:SpaceM.DocumentN (kept by ζ)
Event event4 = mock(Event.class);
when(event4.getDocument()).thenReturn(new DocumentReference("wikiA", "SpaceM", "DocumentN"));
assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event4, user, NotificationFormat.ALERT));
}
use of org.xwiki.notifications.preferences.NotificationPreferenceProperty in project xwiki-platform by xwiki.
the class NotificationPreferenceScriptServiceTest method isEventTypeEnabled.
@Test
public void isEventTypeEnabled() throws Exception {
DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
when(documentAccessBridge.getCurrentUserReference()).thenReturn(user);
when(notificationPreferenceManager.getAllPreferences(user)).thenReturn(Collections.emptyList());
assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT));
NotificationPreference pref1 = mock(NotificationPreference.class);
NotificationPreference pref2 = mock(NotificationPreference.class);
when(pref1.getFormat()).thenReturn(NotificationFormat.EMAIL);
Map<NotificationPreferenceProperty, Object> properties1 = new HashMap<>();
properties1.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
when(pref1.getProperties()).thenReturn(properties1);
when(pref2.getFormat()).thenReturn(NotificationFormat.ALERT);
Map<NotificationPreferenceProperty, Object> properties2 = new HashMap<>();
properties2.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
when(pref2.getProperties()).thenReturn(properties2);
when(pref2.isNotificationEnabled()).thenReturn(true);
when(notificationPreferenceManager.getAllPreferences(user)).thenReturn(Arrays.asList(pref1, pref2));
assertTrue(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT));
assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.EMAIL));
}
Aggregations