Search in sources :

Example 1 with Event

use of org.xwiki.eventstream.Event in project xwiki-platform by xwiki.

the class DefaultUntypedRecordableEventConverter method convert.

@Override
public Event convert(RecordableEvent recordableEvent, String source, Object data) throws Exception {
    Event convertedEvent = super.convert(recordableEvent, source, data);
    convertedEvent.setType(((UntypedRecordableEvent) recordableEvent).getEventType());
    return convertedEvent;
}
Also used : UntypedRecordableEvent(org.xwiki.eventstream.UntypedRecordableEvent) Event(org.xwiki.eventstream.Event) RecordableEvent(org.xwiki.eventstream.RecordableEvent)

Example 2 with Event

use of org.xwiki.eventstream.Event 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 3 with Event

use of org.xwiki.eventstream.Event 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 4 with Event

use of org.xwiki.eventstream.Event 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 5 with Event

use of org.xwiki.eventstream.Event in project xwiki-platform by xwiki.

the class DefaultNotificationManager method getEvents.

private List<CompositeEvent> getEvents(List<CompositeEvent> results, Parameters parameters) throws NotificationException {
    // Because the user might not be able to see all notifications because of the rights, we take from the database
    // more events than expected and we will filter afterwards.
    final int batchSize = parameters.expectedCount * 2;
    try {
        // Create the query
        Query query = queryGenerator.generateQuery(parameters.userReference, parameters.format, parameters.endDate, parameters.fromDate, parameters.blackList);
        if (query == null) {
            return Collections.emptyList();
        }
        query.setLimit(batchSize);
        // Get a batch of events
        List<Event> batch = eventStream.searchEvents(query);
        // Add to the results the events the user has the right to see
        for (Event event : batch) {
            DocumentReference document = event.getDocument();
            // Don't record events concerning a doc the user cannot see
            if (document != null && !authorizationManager.hasAccess(Right.VIEW, parameters.userReference, document)) {
                continue;
            }
            if (filterEvent(event, parameters)) {
                continue;
            }
            // Record this event
            recordEvent(results, event);
            // If the expected count is reached, stop now
            if (results.size() >= parameters.expectedCount) {
                return results;
            }
        }
        // If we haven't get the expected number of events, perform a new batch
        if (results.size() < parameters.expectedCount && batch.size() == batchSize) {
            parameters.blackList.addAll(getEventsIds(batch));
            getEvents(results, parameters);
        }
        return results;
    } catch (Exception e) {
        throw new NotificationException("Fail to get the list of notifications.", e);
    }
}
Also used : Query(org.xwiki.query.Query) NotificationException(org.xwiki.notifications.NotificationException) CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) DocumentReference(org.xwiki.model.reference.DocumentReference) NotificationException(org.xwiki.notifications.NotificationException)

Aggregations

Event (org.xwiki.eventstream.Event)60 Test (org.junit.Test)31 DocumentReference (org.xwiki.model.reference.DocumentReference)21 DefaultEvent (org.xwiki.eventstream.internal.DefaultEvent)20 CompositeEvent (org.xwiki.notifications.CompositeEvent)17 ArrayList (java.util.ArrayList)7 Query (org.xwiki.query.Query)7 Date (java.util.Date)6 Expectations (org.jmock.Expectations)6 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)5 QueryException (org.xwiki.query.QueryException)5 ObjectReference (org.xwiki.model.reference.ObjectReference)4 HashMap (java.util.HashMap)3 WikiReference (org.xwiki.model.reference.WikiReference)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 ActivityEvent (com.xpn.xwiki.plugin.activitystream.api.ActivityEvent)2 EventStatus (org.xwiki.eventstream.EventStatus)2 EventStream (org.xwiki.eventstream.EventStream)2 EventStreamAddedEvent (org.xwiki.eventstream.events.EventStreamAddedEvent)2 SpaceReference (org.xwiki.model.reference.SpaceReference)2