Search in sources :

Example 6 with NotificationPreferenceProperty

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));
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) HashMap(java.util.HashMap) SpaceReference(org.xwiki.model.reference.SpaceReference) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) Event(org.xwiki.eventstream.Event) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 7 with NotificationPreferenceProperty

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));
}
Also used : AbstractNotificationPreference(org.xwiki.notifications.preferences.internal.AbstractNotificationPreference) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) HashMap(java.util.HashMap) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)7 NotificationPreferenceProperty (org.xwiki.notifications.preferences.NotificationPreferenceProperty)7 HashMap (java.util.HashMap)6 Test (org.junit.Test)6 DocumentReference (org.xwiki.model.reference.DocumentReference)4 WikiReference (org.xwiki.model.reference.WikiReference)3 Event (org.xwiki.eventstream.Event)2 SpaceReference (org.xwiki.model.reference.SpaceReference)2 NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)2 AbstractNotificationPreference (org.xwiki.notifications.preferences.internal.AbstractNotificationPreference)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)1 NotificationException (org.xwiki.notifications.NotificationException)1