use of org.xwiki.notifications.preferences.NotificationPreference in project xwiki-platform by xwiki.
the class DefaultNotificationManagerTest method getEventsWhenNoPreferences.
@Test
public void getEventsWhenNoPreferences() throws Exception {
NotificationPreference pref1 = mock(NotificationPreference.class);
when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
when(pref1.isNotificationEnabled()).thenReturn(false);
when(notificationPreferenceManager.getAllPreferences(userReference)).thenReturn(Arrays.asList(pref1));
// Test
List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 2);
// Verify
assertEquals(0, results.size());
}
use of org.xwiki.notifications.preferences.NotificationPreference in project xwiki-platform by xwiki.
the class DefaultNotificationManagerTest method setUp.
@Before
public void setUp() throws Exception {
eventStream = mocker.getInstance(EventStream.class);
queryGenerator = mocker.getInstance(QueryGenerator.class);
documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
documentReferenceResolver = mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
notificationPreferenceManager = mocker.getInstance(NotificationPreferenceManager.class);
authorizationManager = mocker.getInstance(AuthorizationManager.class);
startDate = new Date(10);
when(documentReferenceResolver.resolve("xwiki:XWiki.UserA")).thenReturn(userReference);
query = mock(Query.class);
when(queryGenerator.generateQuery(any(DocumentReference.class), any(NotificationFormat.class), nullable(Date.class), nullable(Date.class), nullable(List.class))).thenReturn(query);
NotificationPreference pref1 = mock(NotificationPreference.class);
when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
when(pref1.isNotificationEnabled()).thenReturn(true);
when(notificationPreferenceManager.getAllPreferences(userReference)).thenReturn(Arrays.asList(pref1));
}
use of org.xwiki.notifications.preferences.NotificationPreference in project xwiki-platform by xwiki.
the class NotificationPreferenceScriptServiceTest method saveNotificationPreferences.
@Test
public void saveNotificationPreferences() throws Exception {
DocumentReference userRef = new DocumentReference("xwiki", "XWiki", "UserA");
NotificationPreferenceImpl existingPref1 = new NotificationPreferenceImpl(true, NotificationFormat.ALERT, "create");
NotificationPreferenceImpl existingPref2 = new NotificationPreferenceImpl(true, NotificationFormat.EMAIL, "update");
NotificationPreferenceImpl existingPref3 = new NotificationPreferenceImpl(false, NotificationFormat.EMAIL, "delete");
when(notificationPreferenceManager.getAllPreferences(eq(userRef))).thenReturn(Arrays.asList(existingPref1, existingPref2, existingPref3));
final MutableBoolean isOk = new MutableBoolean(false);
doAnswer(invocationOnMock -> {
List<NotificationPreference> prefsToSave = invocationOnMock.getArgument(0);
// 1 of the preferences contained in the JSON file should be saved because the inherited preference
// is the same
assertEquals(9, prefsToSave.size());
assertTrue(prefsToSave.contains(existingPref1));
assertTrue(prefsToSave.contains(existingPref2));
assertFalse(prefsToSave.contains(existingPref3));
isOk.setTrue();
return true;
}).when(notificationPreferenceManager).savePreferences(any(List.class));
mocker.getComponentUnderTest().saveNotificationPreferences(IOUtils.toString(getClass().getResourceAsStream("/preferences.json")), userRef);
assertTrue(isOk.booleanValue());
}
use of org.xwiki.notifications.preferences.NotificationPreference 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));
}
use of org.xwiki.notifications.preferences.NotificationPreference in project xwiki-platform by xwiki.
the class DefaultModelBridge method saveNotificationsPreferences.
@Override
public void saveNotificationsPreferences(DocumentReference targetDocument, List<NotificationPreference> notificationPreferences) throws NotificationException {
try {
XWikiContext context = contextProvider.get();
XWiki xwiki = context.getWiki();
XWikiDocument document = xwiki.getDocument(targetDocument, context);
for (NotificationPreference notificationPreference : notificationPreferences) {
// Ensure that the notification preference has an event type to save
if (!notificationPreference.getProperties().containsKey(NotificationPreferenceProperty.EVENT_TYPE)) {
continue;
}
// Try to find the corresponding XObject for the notification preference
BaseObject preferenceObject = this.findNotificationPreference(document, notificationPreference);
// If no preference exist, then create one
if (preferenceObject == null) {
preferenceObject = new BaseObject();
preferenceObject.setXClassReference(NOTIFICATION_PREFERENCE_CLASS);
document.addXObject(preferenceObject);
}
preferenceObject.set(EVENT_TYPE_FIELD, notificationPreference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE), context);
preferenceObject.set(FORMAT_FIELD, notificationPreference.getFormat().name().toLowerCase(), context);
preferenceObject.set(NOTIFICATION_ENABLED_FIELD, (notificationPreference.isNotificationEnabled() ? 1 : 0), context);
Date startDate = null;
if (notificationPreference.isNotificationEnabled()) {
startDate = notificationPreference.getStartDate();
if (startDate == null) {
// Fallback to the previous value if date is empty
startDate = preferenceObject.getDateValue(START_DATE_FIELD);
if (startDate == null) {
// Fallback to now
startDate = new Date();
}
}
}
preferenceObject.set(START_DATE_FIELD, startDate, context);
}
// Make this change a minor edit so it's not displayed, by default, in notifications
xwiki.saveDocument(document, "Update notification preferences.", true, context);
} catch (XWikiException e) {
throw new NotificationException(String.format("Failed to save the notification preference into [%s]", targetDocument));
}
}
Aggregations