1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal.factory.group

File GroupMimeMessageIteratorTest.java

 

Code metrics

0
47
2
1
147
96
2
0.04
23.5
2
1

Classes

Class Line # Actions
GroupMimeMessageIteratorTest 65 47 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.mail.internal.factory.group;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.Map;
26    import java.util.Properties;
27   
28    import javax.mail.Message;
29    import javax.mail.MessagingException;
30    import javax.mail.Session;
31    import javax.mail.internet.InternetAddress;
32    import javax.mail.internet.MimeMessage;
33   
34    import org.junit.Test;
35    import org.xwiki.bridge.DocumentAccessBridge;
36    import org.xwiki.component.manager.ComponentManager;
37    import org.xwiki.context.Execution;
38    import org.xwiki.context.ExecutionContext;
39    import org.xwiki.mail.ExtendedMimeMessage;
40    import org.xwiki.mail.MimeMessageFactory;
41    import org.xwiki.model.reference.DocumentReference;
42    import org.xwiki.model.reference.DocumentReferenceResolver;
43    import org.xwiki.model.reference.EntityReference;
44   
45    import com.xpn.xwiki.XWiki;
46    import com.xpn.xwiki.XWikiContext;
47    import com.xpn.xwiki.doc.XWikiDocument;
48    import com.xpn.xwiki.objects.BaseObject;
49   
50    import static org.junit.Assert.assertArrayEquals;
51    import static org.junit.Assert.assertFalse;
52    import static org.junit.Assert.assertTrue;
53    import static org.mockito.ArgumentMatchers.any;
54    import static org.mockito.ArgumentMatchers.eq;
55    import static org.mockito.Mockito.mock;
56    import static org.mockito.Mockito.when;
57   
58    /**
59    * Unit tests for {@link org.xwiki.mail.internal.factory.group.GroupMimeMessageIterator}.
60    *
61    * @version $Id: 0494b92316ea83b769a03da8d0cfe6b6b87822cb $
62    * @since 6.4M3
63    */
64    @Deprecated
 
65    public class GroupMimeMessageIteratorTest
66    {
 
67  1 toggle @Test
68    public void createMessage() throws Exception
69    {
70  1 DocumentReference groupReference = new DocumentReference("xwiki", "XWiki", "Marketing");
71   
72  1 DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
73  1 DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
74  1 DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
75   
76  1 Session session = Session.getInstance(new Properties());
77   
78  1 MimeMessageFactory<MimeMessage> factory = new MimeMessageFactory<MimeMessage>()
79    {
 
80  3 toggle @Override
81    public MimeMessage createMessage(Object source, Map parameters) throws MessagingException
82    {
83  3 return new ExtendedMimeMessage();
84    }
85    };
86   
87  1 Map<String, Object> parameters = new HashMap<>();
88  1 parameters.put("parameters", Collections.EMPTY_MAP);
89  1 parameters.put("session", session);
90   
91  1 DocumentAccessBridge accessBridge = mock(DocumentAccessBridge.class);
92  1 when(accessBridge.getProperty(eq(groupReference), any(), eq(0), eq("member"))).thenReturn("XWiki.JohnDoe");
93  1 when(accessBridge.getProperty(eq(groupReference), any(), eq(1), eq("member"))).thenReturn("XWiki.JaneDoe");
94  1 when(accessBridge.getProperty(eq(groupReference), any(), eq(2), eq("member"))).thenReturn("XWiki.JonnieDoe");
95   
96  1 when(accessBridge.getProperty(eq(userReference1), any(), eq("email"))).thenReturn("john@doe.com");
97  1 when(accessBridge.getProperty(eq(userReference2), any(), eq("email"))).thenReturn("jane@doe.com");
98  1 when(accessBridge.getProperty(eq(userReference3), any(), eq("email"))).thenReturn("jannie@doe.com");
99   
100  1 Execution execution = mock(Execution.class);
101  1 ExecutionContext executionContext = mock(ExecutionContext.class);
102  1 when(execution.getContext()).thenReturn(executionContext);
103   
104  1 XWikiContext xwikiContext = mock(XWikiContext.class);
105  1 when(executionContext.getProperty("xwikicontext")).thenReturn(xwikiContext);
106   
107  1 XWiki xwiki = mock(XWiki.class);
108  1 when(xwikiContext.getWiki()).thenReturn(xwiki);
109   
110  1 XWikiDocument document = mock(XWikiDocument.class);
111  1 when(xwiki.getDocument(eq(groupReference), eq(xwikiContext))).thenReturn(document);
112   
113  1 BaseObject object = mock(BaseObject.class);
114   
115  1 when(document.getXObjects(any(EntityReference.class))).thenReturn(Arrays.asList(object, object, object));
116   
117  1 DocumentReferenceResolver<String> resolver =
118    (DocumentReferenceResolver<String>) mock(DocumentReferenceResolver.class);
119  1 when(resolver.resolve("XWiki.JohnDoe")).thenReturn(userReference1);
120  1 when(resolver.resolve("XWiki.JaneDoe")).thenReturn(userReference2);
121  1 when(resolver.resolve("XWiki.JonnieDoe")).thenReturn(userReference3);
122   
123  1 ComponentManager componentManager = mock(ComponentManager.class);
124   
125  1 when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
126  1 when(componentManager.getInstance(eq(Execution.class))).thenReturn(execution);
127  1 when(componentManager.getInstance(eq(DocumentReferenceResolver.TYPE_STRING), eq("current"))).thenReturn(
128    resolver);
129   
130  1 GroupMimeMessageIterator iterator =
131    new GroupMimeMessageIterator(groupReference, factory, parameters, componentManager);
132   
133  1 assertTrue(iterator.hasNext());
134  1 MimeMessage message1 = iterator.next();
135  1 assertArrayEquals(message1.getRecipients(Message.RecipientType.TO), InternetAddress.parse("john@doe.com"));
136   
137  1 assertTrue(iterator.hasNext());
138  1 MimeMessage message2 = iterator.next();
139  1 assertArrayEquals(message2.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jane@doe.com"));
140   
141  1 assertTrue(iterator.hasNext());
142  1 MimeMessage message3 = iterator.next();
143  1 assertArrayEquals(message3.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jannie@doe.com"));
144   
145  1 assertFalse(iterator.hasNext());
146    }
147    }