1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.notifications.notifiers.internal.email

File DefaultPeriodicMimeMessageIteratorTest.java

 

Code metrics

0
59
2
1
176
122
2
0.03
29.5
2
1

Classes

Class Line # Actions
DefaultPeriodicMimeMessageIteratorTest 63 59 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.notifications.notifiers.internal.email;
21   
22    import java.lang.reflect.Type;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.Date;
26    import java.util.List;
27    import java.util.Map;
28   
29    import javax.mail.internet.InternetAddress;
30    import javax.mail.internet.MimeMessage;
31   
32    import org.apache.commons.collections.map.HashedMap;
33    import org.junit.Before;
34    import org.junit.Rule;
35    import org.junit.Test;
36    import org.xwiki.bridge.DocumentAccessBridge;
37    import org.xwiki.component.util.DefaultParameterizedType;
38    import org.xwiki.mail.MailSenderConfiguration;
39    import org.xwiki.mail.MimeMessageFactory;
40    import org.xwiki.model.reference.DocumentReference;
41    import org.xwiki.model.reference.EntityReferenceSerializer;
42    import org.xwiki.notifications.CompositeEvent;
43    import org.xwiki.notifications.NotificationFormat;
44    import org.xwiki.notifications.notifiers.email.NotificationEmailRenderer;
45    import org.xwiki.notifications.sources.NotificationManager;
46    import org.xwiki.test.mockito.MockitoComponentMockingRule;
47    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
48   
49    import static org.jgroups.util.Util.assertTrue;
50    import static org.junit.Assert.assertEquals;
51    import static org.junit.Assert.assertFalse;
52    import static org.junit.Assert.assertNotNull;
53    import static org.mockito.ArgumentMatchers.anyString;
54    import static org.mockito.ArgumentMatchers.eq;
55    import static org.mockito.Mockito.mock;
56    import static org.mockito.Mockito.never;
57    import static org.mockito.Mockito.verify;
58    import static org.mockito.Mockito.when;
59   
60    /**
61    * @version $Id: 4ff6c5a4a32c64799befc61ec5c840aca8592a72 $
62    */
 
63    public class DefaultPeriodicMimeMessageIteratorTest
64    {
65    @Rule
66    public final MockitoComponentMockingRule<DefaultPeriodicMimeMessageIterator> mocker =
67    new MockitoComponentMockingRule<>(DefaultPeriodicMimeMessageIterator.class);
68   
69    private NotificationManager notificationManager;
70    private MimeMessageFactory<MimeMessage> factory;
71    private DocumentAccessBridge documentAccessBridge;
72    private NotificationEmailRenderer defaultNotificationEmailRenderer;
73    private WikiDescriptorManager wikiDescriptorManager;
74    private MailSenderConfiguration mailSenderConfiguration;
75    private EntityReferenceSerializer<String> serializer;
76   
 
77  1 toggle @Before
78    public void setUp() throws Exception
79    {
80  1 notificationManager = mocker.getInstance(NotificationManager.class);
81  1 factory = mocker.getInstance(
82    new DefaultParameterizedType(null, MimeMessageFactory.class, new Type[]{MimeMessage.class}),
83    "template"
84    );
85  1 documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
86  1 defaultNotificationEmailRenderer = mocker.getInstance(NotificationEmailRenderer.class);
87  1 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
88  1 mailSenderConfiguration = mocker.getInstance(MailSenderConfiguration.class);
89  1 serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
90   
91  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("xwiki");
92  1 when(mailSenderConfiguration.getFromAddress()).thenReturn("xwiki@xwiki.org");
93    }
94   
 
95  1 toggle @Test
96    public void test() throws Exception
97    {
98  1 DocumentReference templateReference = new DocumentReference("xwiki", "XWiki", "Template");
99  1 Map<String, Object> factoryParameters = new HashedMap();
100   
101    // Mocks
102  1 NotificationUserIterator userIterator = mock(NotificationUserIterator.class);
103  1 DocumentReference userA = new DocumentReference("xwiki", "XWiki", "UserA");
104  1 DocumentReference userB = new DocumentReference("xwiki", "XWiki", "UserB");
105  1 DocumentReference userC = new DocumentReference("xwiki", "XWiki", "UserC");
106  1 when(userIterator.hasNext()).thenReturn(true, true, true, false);
107  1 when(userIterator.next()).thenReturn(userA, userB, userC);
108  1 DocumentReference userClass = new DocumentReference("xwiki", "XWiki", "XWikiUsers");
109  1 when(documentAccessBridge.getProperty(userA, userClass, 0, "email")).thenReturn("userA@xwiki.org");
110  1 when(documentAccessBridge.getProperty(userB, userClass, 0, "email")).thenReturn("bad email");
111  1 when(documentAccessBridge.getProperty(userC, userClass, 0, "email")).thenReturn("userC@xwiki.org");
112  1 when(serializer.serialize(userA)).thenReturn("xwiki:XWiki.UserA");
113  1 when(serializer.serialize(userB)).thenReturn("xwiki:XWiki.UserA");
114  1 when(serializer.serialize(userC)).thenReturn("xwiki:XWiki.UserC");
115   
116  1 CompositeEvent event1 = mock(CompositeEvent.class);
117  1 CompositeEvent event2 = mock(CompositeEvent.class);
118   
119  1 when(notificationManager.getEvents("xwiki:XWiki.UserA", NotificationFormat.EMAIL,
120    Integer.MAX_VALUE / 4, null, new Date(0L), Collections.emptyList()))
121    .thenReturn(Arrays.asList(event1));
122  1 when(notificationManager.getEvents("xwiki:XWiki.UserC", NotificationFormat.EMAIL,
123    Integer.MAX_VALUE / 4, null, new Date(0L), Collections.emptyList()))
124    .thenReturn(Arrays.asList(event2));
125   
126  1 MimeMessage message = mock(MimeMessage.class);
127  1 when(factory.createMessage(templateReference, factoryParameters)).thenReturn(message, message);
128   
129  1 when(defaultNotificationEmailRenderer.renderHTML(eq(event1), anyString())).thenReturn("eventHTML1");
130  1 when(defaultNotificationEmailRenderer.renderPlainText(eq(event1), anyString())).thenReturn("event1");
131  1 when(defaultNotificationEmailRenderer.renderHTML(eq(event2), anyString())).thenReturn("eventHTML2");
132  1 when(defaultNotificationEmailRenderer.renderPlainText(eq(event2), anyString())).thenReturn("event2");
133   
134    // Test
135  1 PeriodicMimeMessageIterator iterator = mocker.getComponentUnderTest();
136   
137  1 iterator.initialize(userIterator, factoryParameters, new Date(0L), templateReference);
138   
139    // First iteration
140  1 assertTrue(iterator.hasNext());
141  1 assertEquals(message, iterator.next());
142  1 assertEquals(new InternetAddress("xwiki@xwiki.org"), factoryParameters.get("from"));
143  1 assertEquals(new InternetAddress("userA@xwiki.org"), factoryParameters.get("to"));
144  1 Map<String, Object> velocityVariables = (Map<String, Object>) factoryParameters.get("velocityVariables");
145  1 assertNotNull(velocityVariables);
146  1 assertEquals(Arrays.asList(event1), velocityVariables.get("events"));
147  1 assertEquals(Arrays.asList("eventHTML1"), velocityVariables.get("htmlEvents"));
148  1 assertEquals(Arrays.asList("event1"), velocityVariables.get("plainTextEvents"));
149   
150    // Count the number of attachments
151  1 assertEquals(1, ((List)factoryParameters.get("attachments")).size());
152   
153    // Second iteration
154  1 assertTrue(iterator.hasNext());
155  1 assertEquals(message, iterator.next());
156  1 assertEquals(new InternetAddress("xwiki@xwiki.org"), factoryParameters.get("from"));
157  1 assertEquals(new InternetAddress("userC@xwiki.org"), factoryParameters.get("to"));
158  1 velocityVariables = (Map<String, Object>) factoryParameters.get("velocityVariables");
159  1 assertNotNull(velocityVariables);
160  1 assertEquals(Arrays.asList(event2), velocityVariables.get("events"));
161  1 assertEquals(Arrays.asList("eventHTML2"), velocityVariables.get("htmlEvents"));
162  1 assertEquals(Arrays.asList("event2"), velocityVariables.get("plainTextEvents"));
163   
164    // Make sure there is no duplicated attachments
165  1 assertEquals(1, ((List)factoryParameters.get("attachments")).size());
166   
167    // End
168  1 assertFalse(iterator.hasNext());
169   
170    // Verify
171  1 verify(serializer, never()).serialize(userB);
172   
173  1 assertEquals(iterator, iterator.iterator());
174    }
175   
176    }