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