@Test void test() throws Exception { Map<String, Object> factoryParameters = new HashMap<>(); 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(this.documentAccessBridge.getProperty(userA, userClass, 0, "email")).thenReturn("userA@xwiki.org"); when(this.documentAccessBridge.getProperty(userB, userClass, 0, "email")).thenReturn("bad email"); when(this.documentAccessBridge.getProperty(userC, userClass, 0, "email")).thenReturn("userC@xwiki.org"); when(this.serializer.serialize(userA)).thenReturn("xwiki:XWiki.UserA"); when(this.serializer.serialize(userB)).thenReturn("xwiki:XWiki.UserA"); when(this.serializer.serialize(userC)).thenReturn("xwiki:XWiki.UserC"); CompositeEvent event1 = mock(CompositeEvent.class); CompositeEvent event2 = mock(CompositeEvent.class); when(this.notificationManager.getEvents("xwiki:XWiki.UserA", NotificationFormat.EMAIL, Integer.MAX_VALUE / 4, null, new Date(0L), Collections.emptyList())).thenReturn(Arrays.asList(event1)); when(this.notificationManager.getEvents("xwiki:XWiki.UserC", NotificationFormat.EMAIL, Integer.MAX_VALUE / 4, null, new Date(0L), Collections.emptyList())).thenReturn(Arrays.asList(event2)); when(event1.getUsers()).thenReturn(Sets.newSet(userB)); when(event2.getUsers()).thenReturn(Sets.newSet(userB)); MimeMessage message = mock(MimeMessage.class); when(this.factory.createMessage(TEMPLATE_REFERENCE, factoryParameters)).thenReturn(message, message); when(this.defaultNotificationEmailRenderer.renderHTML(eq(event1), anyString())).thenReturn("eventHTML1"); when(this.defaultNotificationEmailRenderer.renderPlainText(eq(event1), anyString())).thenReturn("event1"); when(this.defaultNotificationEmailRenderer.renderHTML(eq(event2), anyString())).thenReturn("eventHTML2"); when(this.defaultNotificationEmailRenderer.renderPlainText(eq(event2), anyString())).thenReturn("event2"); Attachment userBAvatar = mock(Attachment.class); when(this.userAvatarAttachmentExtractor.getUserAvatar(eq(userB), anyInt())).thenReturn(userBAvatar); this.iterator.initialize(userIterator, factoryParameters, new Date(0L), TEMPLATE_REFERENCE); assertTrue(this.iterator.hasNext()); assertEquals(message, this.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")); assertEquals("xwiki:XWiki.UserA", velocityVariables.get("emailUser")); assertEquals(2, ((List) factoryParameters.get("attachments")).size()); assertTrue(((List) factoryParameters.get("attachments")).contains(userBAvatar)); assertTrue(this.iterator.hasNext()); assertEquals(message, this.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")); assertEquals("xwiki:XWiki.UserC", velocityVariables.get("emailUser")); assertEquals(2, ((List) factoryParameters.get("attachments")).size()); assertTrue(((List) factoryParameters.get("attachments")).contains(userBAvatar)); assertFalse(this.iterator.hasNext()); verify(this.serializer, never()).serialize(userB); assertEquals(this.iterator, this.iterator.iterator()); }
@Override public void initialize(NotificationUserIterator userIterator, Map<String, Object> factoryParameters, Date lastTrigger, DocumentReference templateReference) { this.lastTrigger = lastTrigger; super.initialize(userIterator, factoryParameters, templateReference); }
DefaultPeriodicMimeMessageIterator extends AbstractMimeMessageIterator implements PeriodicMimeMessageIterator { @Override public void initialize(NotificationUserIterator userIterator, Map<String, Object> factoryParameters, Date lastTrigger, DocumentReference templateReference) { this.lastTrigger = lastTrigger; super.initialize(userIterator, factoryParameters, templateReference); } }
DefaultPeriodicMimeMessageIterator extends AbstractMimeMessageIterator implements PeriodicMimeMessageIterator { @Override public void initialize(NotificationUserIterator userIterator, Map<String, Object> factoryParameters, Date lastTrigger, DocumentReference templateReference) { this.lastTrigger = lastTrigger; super.initialize(userIterator, factoryParameters, templateReference); }  }
DefaultPeriodicMimeMessageIterator extends AbstractMimeMessageIterator implements PeriodicMimeMessageIterator { @Override public void initialize(NotificationUserIterator userIterator, Map<String, Object> factoryParameters, Date lastTrigger, DocumentReference templateReference) { this.lastTrigger = lastTrigger; super.initialize(userIterator, factoryParameters, templateReference); }  @Override void initialize(NotificationUserIterator userIterator, Map<String, Object> factoryParameters,
        Date lastTrigger, DocumentReference templateReference); }
DefaultPeriodicMimeMessageIterator extends AbstractMimeMessageIterator implements PeriodicMimeMessageIterator { @Override public void initialize(NotificationUserIterator userIterator, Map<String, Object> factoryParameters, Date lastTrigger, DocumentReference templateReference) { this.lastTrigger = lastTrigger; super.initialize(userIterator, factoryParameters, templateReference); }  @Override void initialize(NotificationUserIterator userIterator, Map<String, Object> factoryParameters,
        Date lastTrigger, DocumentReference templateReference);  }