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