Search in sources :

Example 1 with NotificationPreferenceProperty

use of org.xwiki.notifications.preferences.NotificationPreferenceProperty in project xwiki-platform by xwiki.

the class NotificationPreferenceScriptServiceTest method isEventTypeEnabledForWiki.

@Test
public void isEventTypeEnabledForWiki() throws Exception {
    WikiReference wiki = new WikiReference("whatever");
    when(notificationPreferenceManager.getAllPreferences(wiki)).thenReturn(Collections.emptyList());
    assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT, wiki.getName()));
    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(wiki)).thenReturn(Arrays.asList(pref1, pref2));
    assertTrue(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT, wiki.getName()));
    assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.EMAIL, wiki.getName()));
}
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) WikiReference(org.xwiki.model.reference.WikiReference) Test(org.junit.Test)

Example 2 with NotificationPreferenceProperty

use of org.xwiki.notifications.preferences.NotificationPreferenceProperty in project xwiki-platform by xwiki.

the class DefaultModelBridge method getNotificationPreferences.

private List<NotificationPreference> getNotificationPreferences(DocumentReference document, String providerHint) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationPreferencesClass = NOTIFICATION_PREFERENCE_CLASS.setWikiReference(document.getWikiReference());
    List<NotificationPreference> preferences = new ArrayList<>();
    try {
        XWikiDocument doc = xwiki.getDocument(document, context);
        List<BaseObject> preferencesObj = doc.getXObjects(notificationPreferencesClass);
        if (preferencesObj != null) {
            for (BaseObject obj : preferencesObj) {
                if (obj != null) {
                    String objFormat = obj.getStringValue(FORMAT_FIELD);
                    Date objStartDate = obj.getDateValue(START_DATE_FIELD);
                    Map<NotificationPreferenceProperty, Object> properties = extractNotificationPreferenceProperties(obj);
                    notificationPreferenceBuilder.prepare();
                    notificationPreferenceBuilder.setProperties(properties);
                    notificationPreferenceBuilder.setStartDate((objStartDate != null) ? objStartDate : doc.getCreationDate());
                    notificationPreferenceBuilder.setFormat(StringUtils.isNotBlank(objFormat) ? NotificationFormat.valueOf(objFormat.toUpperCase()) : NotificationFormat.ALERT);
                    notificationPreferenceBuilder.setTarget(document);
                    notificationPreferenceBuilder.setProviderHint(providerHint);
                    notificationPreferenceBuilder.setEnabled(obj.getIntValue(NOTIFICATION_ENABLED_FIELD, 0) != 0);
                    notificationPreferenceBuilder.setCategory(NotificationPreferenceCategory.DEFAULT);
                    preferences.add(notificationPreferenceBuilder.build());
                }
            }
        }
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to get the notification preferences from the document [%s].", document), e);
    }
    return preferences;
}
Also used : ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 3 with NotificationPreferenceProperty

use of org.xwiki.notifications.preferences.NotificationPreferenceProperty in project xwiki-platform by xwiki.

the class XWikiEventTypesEnablerTest method whenOneXWikiEventTypesIsEnabled.

@Test
public void whenOneXWikiEventTypesIsEnabled() throws Exception {
    NotificationPreference pref = mock(NotificationPreference.class);
    when(pref.isNotificationEnabled()).thenReturn(true);
    Map<NotificationPreferenceProperty, Object> properties = new HashMap<>();
    properties.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
    when(pref.getProperties()).thenReturn(properties);
    when(notificationPreferenceManager.getAllPreferences(eq(currentUser))).thenReturn(Arrays.asList(pref));
    mocker.getComponentUnderTest().ensureXWikiNotificationsAreEnabled();
    verify(notificationPreferenceManager, never()).savePreferences(anyList());
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) HashMap(java.util.HashMap) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) Test(org.junit.Test)

Example 4 with NotificationPreferenceProperty

use of org.xwiki.notifications.preferences.NotificationPreferenceProperty in project xwiki-platform by xwiki.

the class XWikiEventTypesEnablerTest method whenOtherEventTypesIsEnabled.

@Test
public void whenOtherEventTypesIsEnabled() throws Exception {
    NotificationPreference pref = mock(NotificationPreference.class);
    when(pref.isNotificationEnabled()).thenReturn(true);
    Map<NotificationPreferenceProperty, Object> properties = new HashMap<>();
    properties.put(NotificationPreferenceProperty.EVENT_TYPE, "blog");
    when(pref.getProperties()).thenReturn(properties);
    when(notificationPreferenceManager.getAllPreferences(eq(currentUser))).thenReturn(Arrays.asList(pref));
    doAnswer(invocationOnMock -> {
        List<NotificationPreference> preferencesToSave = invocationOnMock.getArgument(0);
        // Ensure there is a preference to save for each event type and each format = 8 items
        assertEquals(8, preferencesToSave.size());
        return null;
    }).when(notificationPreferenceManager).savePreferences(anyList());
    mocker.getComponentUnderTest().ensureXWikiNotificationsAreEnabled();
    verify(notificationPreferenceManager).savePreferences(anyList());
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) HashMap(java.util.HashMap) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) Test(org.junit.Test)

Example 5 with NotificationPreferenceProperty

use of org.xwiki.notifications.preferences.NotificationPreferenceProperty in project xwiki-platform by xwiki.

the class ScopeNotificationFilterTest method complexCase1.

@Test
public void complexCase1() throws Exception {
    // Preferences:
    // 
    // α: "update" event type enabled for format ALERT
    // 
    // β: Exclusive filter on "wikiA".
    // γ: Inclusive filter on "wikiA:SpaceB"
    // δ: Exclusive filter on "wikiA:SpaceB.SpaceC"
    // ε: Exclusive filter on "wikiA:SpaceB.SpaceC.SpaceD"
    // 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");
    NotificationFilterPreference prefβ = mockNotificationFilterPreference("wikiA", wikiReference, NotificationFilterType.EXCLUSIVE, null);
    // Mock γ
    SpaceReference spaceReferenceB = new SpaceReference("SpaceB", wikiReference);
    NotificationFilterPreference prefγ = mockNotificationFilterPreference("wikiA:SpaceB", spaceReferenceB, NotificationFilterType.INCLUSIVE, "update");
    // Mock δ
    SpaceReference spaceReferenceC = new SpaceReference("SpaceC", spaceReferenceB);
    NotificationFilterPreference prefδ = mockNotificationFilterPreference("wikiA:SpaceB.SpaceC", spaceReferenceC, NotificationFilterType.EXCLUSIVE, null);
    // Mock ε
    SpaceReference spaceReferenceD = new SpaceReference("SpaceD", spaceReferenceC);
    NotificationFilterPreference prefε = mockNotificationFilterPreference("wikiA:SpaceB.SpaceC.SpaceD", spaceReferenceD, NotificationFilterType.INCLUSIVE, null);
    when(notificationFilterManager.getFilterPreferences(any(DocumentReference.class))).thenReturn(Sets.newSet(prefβ, prefγ, prefδ, prefε));
    DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
    // Test 1
    String result = mocker.getComponentUnderTest().filterExpression(user, preference).toString();
    assertEquals("(((NOT (WIKI = \"wikiA\") OR (WIKI = \"wikiA\" AND SPACE STARTS WITH \"wikiA:SpaceB\"))" + " OR (WIKI = \"wikiA\" AND SPACE STARTS WITH \"wikiA:SpaceB.SpaceC.SpaceD\")) AND (NOT ((WIKI = \"wikiA\" " + "AND SPACE STARTS WITH \"wikiA:SpaceB.SpaceC\")) OR (WIKI = \"wikiA\" " + "AND SPACE STARTS WITH \"wikiA:SpaceB.SpaceC.SpaceD\")))", 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.DocumentE (kept by γ)
    Event event2 = mock(Event.class);
    when(event2.getDocument()).thenReturn(new DocumentReference("DocumentE", spaceReferenceB));
    assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event2, user, NotificationFormat.ALERT));
    // Test with wikiA:SpaceB.SpaceC.DocumentF (filtered by δ)
    Event event3 = mock(Event.class);
    when(event3.getDocument()).thenReturn(new DocumentReference("DocumentF", spaceReferenceC));
    assertEquals(NotificationFilter.FilterPolicy.FILTER, mocker.getComponentUnderTest().filterEvent(event3, user, NotificationFormat.ALERT));
    // Test with wikiA:SpaceB.SpaceC.SpaceD.DocumentG (kept by ε)
    Event event4 = mock(Event.class);
    when(event4.getDocument()).thenReturn(new DocumentReference("DocumentG", spaceReferenceD));
    assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event4, user, NotificationFormat.ALERT));
    // Test with wikiB:SpaceH.DocumentI - kept because nothing match and there is no top level inclusive filter
    Event event5 = mock(Event.class);
    when(event5.getDocument()).thenReturn(new DocumentReference("wikiB", "SpaceH", "DocumentI"));
    assertEquals(NotificationFilter.FilterPolicy.NO_EFFECT, mocker.getComponentUnderTest().filterEvent(event5, 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)

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