Search in sources :

Example 16 with CompositeEvent

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

the class DefaultNotificationManagerTest method getEventsUC5.

@Test
public void getEventsUC5() throws Exception {
    // Facts:
    // * Bob updates the page "Bike"
    // * Then Bob updates the page "Bike" again
    // Expected:
    // * Bob has updated the page "Bike"
    // Comment: we don't show 2 events, only one is interesting
    // Mocks
    Event event1 = createMockedEvent();
    Event event2 = createMockedEvent();
    DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
    when(event1.getDocument()).thenReturn(doc);
    when(event2.getDocument()).thenReturn(doc);
    when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
    when(event1.getType()).thenReturn("update");
    when(event2.getType()).thenReturn("update");
    when(event1.getGroupId()).thenReturn("g1");
    when(event2.getGroupId()).thenReturn("g2");
    when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2));
    // Test
    List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents("xwiki:XWiki.UserA", 5);
    // Verify
    assertEquals(1, results.size());
    assertEquals(event1, results.get(0).getEvents().get(0));
    assertEquals(event2, 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 17 with CompositeEvent

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

the class DefaultNotificationManager method getBestSimilarity.

private BestSimilarity getBestSimilarity(List<CompositeEvent> results, Event event) {
    BestSimilarity bestSimilarity = new BestSimilarity();
    // Looking for the most similar event inside the existing composite events
    for (CompositeEvent existingCompositeEvent : results) {
        for (Event existingEvent : existingCompositeEvent.getEvents()) {
            int similarity = similarityCalculator.computeSimilarity(event, existingEvent);
            if (similarity < existingCompositeEvent.getSimilarityBetweenEvents()) {
                // Penality
                similarity -= 5;
            }
            if (similarity > bestSimilarity.value) {
                bestSimilarity.value = similarity;
                bestSimilarity.event = existingEvent;
                bestSimilarity.compositeEvent = existingCompositeEvent;
            }
        }
    }
    return bestSimilarity;
}
Also used : CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) CompositeEvent(org.xwiki.notifications.CompositeEvent)

Example 18 with CompositeEvent

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

the class AbstractMimeMessageIterator method handleEvents.

private void handleEvents() throws NotificationException {
    String usedId = serializer.serialize(this.currentUser);
    // Render all the events both in HTML and Plain Text
    List<String> htmlEvents = new ArrayList<>();
    List<String> plainTextEvents = new ArrayList<>();
    EventsSorter eventsSorter = new EventsSorter();
    for (CompositeEvent event : currentEvents) {
        String html = defaultNotificationEmailRenderer.renderHTML(event, usedId);
        String plainText = defaultNotificationEmailRenderer.renderPlainText(event, usedId);
        htmlEvents.add(html);
        plainTextEvents.add(plainText);
        eventsSorter.add(event, html, plainText);
    }
    // Put in the velocity parameters all the events and their rendered version
    Map<String, Object> velocityVariables = getVelocityVariables();
    velocityVariables.put(EVENTS, currentEvents);
    velocityVariables.put(HTML_EVENTS, htmlEvents);
    velocityVariables.put(PLAIN_TEXT_EVENTS, plainTextEvents);
    velocityVariables.put(SORTED_EVENTS, eventsSorter.sort());
    handleAvatars();
}
Also used : ArrayList(java.util.ArrayList) CompositeEvent(org.xwiki.notifications.CompositeEvent)

Example 19 with CompositeEvent

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

the class AbstractMimeMessageIterator method handleAvatars.

private void handleAvatars() {
    Set<DocumentReference> userAvatars = new HashSet<>();
    for (CompositeEvent event : currentEvents) {
        userAvatars.addAll(event.getUsers());
    }
    Collection<Attachment> attachments = getAttachments();
    for (DocumentReference userAvatar : userAvatars) {
        try {
            attachments.add(userAvatarAttachmentExtractor.getUserAvatar(userAvatar, 32));
        } catch (Exception e) {
            logger.warn("Failed to add the avatar of [{}] in the email.", userAvatar, e);
        }
    }
}
Also used : Attachment(com.xpn.xwiki.api.Attachment) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) NotificationException(org.xwiki.notifications.NotificationException) AddressException(javax.mail.internet.AddressException) HashSet(java.util.HashSet)

Example 20 with CompositeEvent

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

the class DefaultPeriodicMimeMessageIteratorTest method test.

@Test
public void test() throws Exception {
    DocumentReference templateReference = new DocumentReference("xwiki", "XWiki", "Template");
    Map<String, Object> factoryParameters = new HashedMap();
    // Mocks
    NotificationUserIterator userIterator = mock(NotificationUserIterator.class);
    DocumentReference userA = new DocumentReference("xwiki", "XWiki", "UserA");
    DocumentReference userB = new DocumentReference("xwiki", "XWiki", "UserB");
    DocumentReference userC = new DocumentReference("xwiki", "XWiki", "UserC");
    when(userIterator.hasNext()).thenReturn(true, true, true, false);
    when(userIterator.next()).thenReturn(userA, userB, userC);
    DocumentReference userClass = new DocumentReference("xwiki", "XWiki", "XWikiUsers");
    when(documentAccessBridge.getProperty(userA, userClass, 0, "email")).thenReturn("userA@xwiki.org");
    when(documentAccessBridge.getProperty(userB, userClass, 0, "email")).thenReturn("bad email");
    when(documentAccessBridge.getProperty(userC, userClass, 0, "email")).thenReturn("userC@xwiki.org");
    when(serializer.serialize(userA)).thenReturn("xwiki:XWiki.UserA");
    when(serializer.serialize(userB)).thenReturn("xwiki:XWiki.UserA");
    when(serializer.serialize(userC)).thenReturn("xwiki:XWiki.UserC");
    CompositeEvent event1 = mock(CompositeEvent.class);
    CompositeEvent event2 = mock(CompositeEvent.class);
    when(notificationManager.getEvents("xwiki:XWiki.UserA", NotificationFormat.EMAIL, Integer.MAX_VALUE / 4, null, new Date(0L), Collections.emptyList())).thenReturn(Arrays.asList(event1));
    when(notificationManager.getEvents("xwiki:XWiki.UserC", NotificationFormat.EMAIL, Integer.MAX_VALUE / 4, null, new Date(0L), Collections.emptyList())).thenReturn(Arrays.asList(event2));
    MimeMessage message = mock(MimeMessage.class);
    when(factory.createMessage(templateReference, factoryParameters)).thenReturn(message, message);
    when(defaultNotificationEmailRenderer.renderHTML(eq(event1), anyString())).thenReturn("eventHTML1");
    when(defaultNotificationEmailRenderer.renderPlainText(eq(event1), anyString())).thenReturn("event1");
    when(defaultNotificationEmailRenderer.renderHTML(eq(event2), anyString())).thenReturn("eventHTML2");
    when(defaultNotificationEmailRenderer.renderPlainText(eq(event2), anyString())).thenReturn("event2");
    // Test
    PeriodicMimeMessageIterator iterator = mocker.getComponentUnderTest();
    iterator.initialize(userIterator, factoryParameters, new Date(0L), templateReference);
    // First iteration
    assertTrue(iterator.hasNext());
    assertEquals(message, iterator.next());
    assertEquals(new InternetAddress("xwiki@xwiki.org"), factoryParameters.get("from"));
    assertEquals(new InternetAddress("userA@xwiki.org"), factoryParameters.get("to"));
    Map<String, Object> velocityVariables = (Map<String, Object>) factoryParameters.get("velocityVariables");
    assertNotNull(velocityVariables);
    assertEquals(Arrays.asList(event1), velocityVariables.get("events"));
    assertEquals(Arrays.asList("eventHTML1"), velocityVariables.get("htmlEvents"));
    assertEquals(Arrays.asList("event1"), velocityVariables.get("plainTextEvents"));
    // Second iteration
    assertTrue(iterator.hasNext());
    assertEquals(message, iterator.next());
    assertEquals(new InternetAddress("xwiki@xwiki.org"), factoryParameters.get("from"));
    assertEquals(new InternetAddress("userC@xwiki.org"), factoryParameters.get("to"));
    velocityVariables = (Map<String, Object>) factoryParameters.get("velocityVariables");
    assertNotNull(velocityVariables);
    assertEquals(Arrays.asList(event2), velocityVariables.get("events"));
    assertEquals(Arrays.asList("eventHTML2"), velocityVariables.get("htmlEvents"));
    assertEquals(Arrays.asList("event2"), velocityVariables.get("plainTextEvents"));
    // End
    assertFalse(iterator.hasNext());
    // Verify
    verify(serializer, never()).serialize(userB);
    assertEquals(iterator, iterator.iterator());
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashedMap(org.apache.commons.collections.map.HashedMap) CompositeEvent(org.xwiki.notifications.CompositeEvent) Map(java.util.Map) HashedMap(org.apache.commons.collections.map.HashedMap) DocumentReference(org.xwiki.model.reference.DocumentReference) Date(java.util.Date) Test(org.junit.Test)

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