Search in sources :

Example 1 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultNotificationManagerTest method getEventsUC3.

@Test
public void getEventsUC3() throws Exception {
    // Facts:
    // * Alice updates the page "Bike"
    // * Bob comments the page "Bike"
    // Expected:
    // * Alice has updated the page "Bike"
    // * Bob has commented the page "Bike"
    // Comment: same as UC2 but we make sure we don't lose the event concerning Alice
    // Note: the UC4 described in https://jira.xwiki.org/browse/XWIKI-14114 is actually similar to that one
    // because we don't care of the event' user in our tests.
    // Mocks
    Event event1 = createMockedEvent();
    Event event2 = createMockedEvent();
    Event event3 = createMockedEvent();
    DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
    when(event1.getDocument()).thenReturn(doc);
    when(event2.getDocument()).thenReturn(doc);
    when(event3.getDocument()).thenReturn(doc);
    when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
    when(event1.getType()).thenReturn("update");
    when(event2.getType()).thenReturn("addComment");
    when(event3.getType()).thenReturn("update");
    when(event1.getGroupId()).thenReturn("g1");
    when(event2.getGroupId()).thenReturn("g2");
    when(event3.getGroupId()).thenReturn("g2");
    when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2, event3));
    // Test
    List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 5);
    // Verify
    assertEquals(2, results.size());
    assertEquals(event1, results.get(0).getEvents().get(0));
    assertEquals(event2, results.get(1).getEvents().get(0));
    assertEquals(event3, results.get(1).getEvents().get(1));
}
Also used : CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 2 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultNotificationManagerTest method getEventsUC1.

@Test
public void getEventsUC1() throws Exception {
    // Facts:
    // * Alice updates the page "Bike"
    // * Bob updates the page "Bike"
    // Expected:
    // * Alice and Bob have updated the page "Bike"
    // Comment:
    // Note: the 2 events have been combined
    // Mocks
    Event eventAlice = createMockedEvent();
    Event eventBob = createMockedEvent();
    DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
    when(eventAlice.getDocument()).thenReturn(doc);
    when(eventBob.getDocument()).thenReturn(doc);
    when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
    when(eventAlice.getType()).thenReturn("update");
    when(eventBob.getType()).thenReturn("update");
    when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(eventAlice, eventBob));
    // Test
    List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 2);
    // Verify
    assertEquals(1, results.size());
    assertEquals(eventAlice, results.get(0).getEvents().get(0));
    assertEquals(eventBob, results.get(0).getEvents().get(1));
}
Also used : CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 3 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultNotificationManagerTest method getEventsXWIKI14454.

@Test
public void getEventsXWIKI14454() throws Exception {
    // Facts:
    // * Then Bob updates the page "Bike"
    // * Then Bob updates the page "Bike" again
    // * Then bob add a comment to the "Bike" page
    // Expected:
    // * Bob has commented the page "Bike"
    // * Bob has updated the page "Bike"
    // Mocks
    Event eventUpdate1 = createMockedEvent();
    Event eventUpdate2 = createMockedEvent();
    Event eventAddComment = createMockedEvent();
    Event eventAddCommentUpdate = createMockedEvent();
    DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
    when(eventUpdate1.getDocument()).thenReturn(doc);
    when(eventUpdate1.toString()).thenReturn("update1");
    when(eventUpdate2.getDocument()).thenReturn(doc);
    when(eventUpdate2.toString()).thenReturn("update2");
    when(eventAddComment.getDocument()).thenReturn(doc);
    when(eventAddComment.toString()).thenReturn("addComment");
    when(eventAddCommentUpdate.getDocument()).thenReturn(doc);
    when(eventAddCommentUpdate.toString()).thenReturn("updateComment");
    when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
    when(eventUpdate1.getType()).thenReturn("update");
    when(eventUpdate2.getType()).thenReturn("update");
    when(eventAddComment.getType()).thenReturn("addComment");
    when(eventAddCommentUpdate.getType()).thenReturn("update");
    when(eventUpdate1.getGroupId()).thenReturn("g1");
    when(eventUpdate2.getGroupId()).thenReturn("g2");
    when(eventAddComment.getGroupId()).thenReturn("g3");
    when(eventAddCommentUpdate.getGroupId()).thenReturn("g3");
    // They comes with inverse chronological order because of the query
    when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(eventAddComment, eventAddCommentUpdate, eventUpdate2, eventUpdate1));
    // Test
    List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 5);
    // Verify
    assertEquals(2, results.size());
}
Also used : CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultNotificationManager method recordEvent.

private void recordEvent(List<CompositeEvent> results, Event event) throws NotificationException {
    BestSimilarity bestSimilarity = getBestSimilarity(results, event);
    if (bestSimilarity.compositeEvent != null) {
        if (bestSimilarity.value > bestSimilarity.compositeEvent.getSimilarityBetweenEvents() && bestSimilarity.compositeEvent.getEvents().size() > 1) {
            // We have found an event A inside a composite event C1 that have a greater similarity with the event E
            // than the similarity between events (A, B, C) of that composite event (C1).
            // 
            // It means we must remove the existing event A from that composite event C1 and create a new composite
            // event C2 made of A and E.
            bestSimilarity.compositeEvent.remove(bestSimilarity.event);
            // Instead of creating a new composite event with A and E, we first look if an other composite event can
            // match with A and E.
            BestSimilarity bestSecondChoice = getBestSimilarity(results, event);
            if (bestSecondChoice.compositeEvent != null && bestSecondChoice.isCompositeEventCompatibleWith(event)) {
                // We have found a composite event C2 made of events (X, Y) which have a greater similarity between
                // themselves than between X and the event E.
                // It means we cannot add E in C2.
                // But there is actually an exception:
                // - if X is a "create" or an "update" event
                // - if X and Y have the same groupId
                // - if Y has the same type than E
                // (or vice versa)
                // It means the "update" event X has been triggered for technical reason, but the interesting event
                // is Y, which we can group with the event E.
                bestSecondChoice.compositeEvent.add(bestSimilarity.event, bestSecondChoice.compositeEvent.getSimilarityBetweenEvents());
                bestSecondChoice.compositeEvent.add(event, bestSecondChoice.compositeEvent.getSimilarityBetweenEvents());
            } else {
                CompositeEvent newCompositeEvent = new CompositeEvent(event);
                newCompositeEvent.add(bestSimilarity.event, bestSimilarity.value);
                results.add(newCompositeEvent);
            }
            return;
        } else if (bestSimilarity.value >= bestSimilarity.compositeEvent.getSimilarityBetweenEvents()) {
            // We have found a composite event C1 made of events (A, B, C) which have the same similarity between
            // themselves than between A end E.
            // All we need to do it to add E to C1.
            bestSimilarity.compositeEvent.add(event, bestSimilarity.value);
            return;
        } else if (bestSimilarity.isCompositeEventCompatibleWith(event)) {
            // We have found a composite event C1 made of events (A, B) which have a greater similarity between
            // themselves than between A and the event E.
            // It means we cannot add E in C1.
            // But there is actually an exception:
            // - if A is a "create" or an "update" event
            // - if A and B have the same groupId
            // - if B has the same type than E
            // (or vice versa)
            // It means the "update" event A has been triggered for technical reason, but the interesting event is
            // B, which we can group with the event E.
            bestSimilarity.compositeEvent.add(event, bestSimilarity.compositeEvent.getSimilarityBetweenEvents());
            return;
        }
    }
    // We haven't found an event that is similar to the current one, so we create a new composite event
    results.add(new CompositeEvent(event));
}
Also used : CompositeEvent(org.xwiki.notifications.CompositeEvent)

Example 5 with CompositeEvent

use of org.xwiki.notifications.CompositeEvent in project xwiki-platform by xwiki.

the class DefaultNotificationManager method getEventsCount.

@Override
public long getEventsCount(String userId, int maxCount) throws NotificationException {
    DocumentReference user = documentReferenceResolver.resolve(userId);
    List<CompositeEvent> events = getEvents(new ArrayList<>(), new Parameters(user, NotificationFormat.ALERT, maxCount, null, null, new ArrayList<>()));
    return events.size();
}
Also used : ArrayList(java.util.ArrayList) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

CompositeEvent (org.xwiki.notifications.CompositeEvent)23 DocumentReference (org.xwiki.model.reference.DocumentReference)15 Event (org.xwiki.eventstream.Event)14 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)5 Date (java.util.Date)3 NotificationException (org.xwiki.notifications.NotificationException)3 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)1 SyndFeedImpl (com.rometools.rome.feed.synd.SyndFeedImpl)1 Attachment (com.xpn.xwiki.api.Attachment)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 ScriptContext (javax.script.ScriptContext)1 HashedMap (org.apache.commons.collections.map.HashedMap)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1