Search in sources :

Example 16 with NotificationPreference

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());
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) CompositeEvent(org.xwiki.notifications.CompositeEvent) Test(org.junit.Test)

Example 17 with NotificationPreference

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));
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) NotificationPreferenceManager(org.xwiki.notifications.preferences.NotificationPreferenceManager) Query(org.xwiki.query.Query) EventStream(org.xwiki.eventstream.EventStream) NotificationFormat(org.xwiki.notifications.NotificationFormat) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ComponentList(org.xwiki.test.annotation.ComponentList) List(java.util.List) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) Date(java.util.Date) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 18 with NotificationPreference

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());
}
Also used : AbstractNotificationPreference(org.xwiki.notifications.preferences.internal.AbstractNotificationPreference) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) ComponentList(org.xwiki.test.annotation.ComponentList) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 19 with NotificationPreference

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

Example 20 with NotificationPreference

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));
    }
}
Also used : NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)32 Test (org.junit.Test)18 DocumentReference (org.xwiki.model.reference.DocumentReference)12 HashMap (java.util.HashMap)8 List (java.util.List)8 NotificationPreferenceProperty (org.xwiki.notifications.preferences.NotificationPreferenceProperty)8 Date (java.util.Date)7 ArrayList (java.util.ArrayList)6 WikiReference (org.xwiki.model.reference.WikiReference)5 NotificationException (org.xwiki.notifications.NotificationException)4 NotificationFormat (org.xwiki.notifications.NotificationFormat)4 NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)4 TargetableNotificationPreference (org.xwiki.notifications.preferences.TargetableNotificationPreference)4 Map (java.util.Map)3 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)3 NotificationPreferenceManager (org.xwiki.notifications.preferences.NotificationPreferenceManager)3 Query (org.xwiki.query.Query)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)2