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

File UsersAndGroupsMimeMessageIteratorTest.java

 

Code metrics

0
45
4
1
152
103
5
0.11
11.25
4
1.25

Classes

Class Line # Actions
UsersAndGroupsMimeMessageIteratorTest 59 45 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

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.usersandgroups;
21   
22    import java.util.Collections;
23    import java.util.HashMap;
24    import java.util.Iterator;
25    import java.util.Map;
26   
27    import javax.mail.Message;
28    import javax.mail.MessagingException;
29    import javax.mail.internet.InternetAddress;
30    import javax.mail.internet.MimeMessage;
31   
32    import org.junit.Before;
33    import org.junit.Rule;
34    import org.junit.Test;
35    import org.xwiki.context.Execution;
36    import org.xwiki.context.ExecutionContext;
37    import org.xwiki.mail.MimeMessageFactory;
38    import org.xwiki.model.EntityType;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.model.reference.DocumentReferenceResolver;
41    import org.xwiki.model.reference.EntityReference;
42    import org.xwiki.test.mockito.MockitoComponentManagerRule;
43   
44    import com.xpn.xwiki.XWiki;
45    import com.xpn.xwiki.XWikiContext;
46    import com.xpn.xwiki.doc.XWikiDocument;
47    import com.xpn.xwiki.objects.BaseObject;
48   
49    import static org.junit.Assert.*;
50    import static org.mockito.Mockito.*;
51   
52    /**
53    * Unit tests for {@link UsersAndGroupsMimeMessageIterator}.
54    *
55    * @version $Id: 441591af29117a8355942a1d26f09ec965b6c658 $
56    * @since 6.4.2
57    * @since 7.0M2
58    */
 
59    public class UsersAndGroupsMimeMessageIteratorTest
60    {
61    @Rule
62    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
63   
64    private Execution execution;
65   
66    private XWikiContext xwikiContext;
67   
68    private MimeMessageFactory<MimeMessage> factory;
69   
70    private DocumentReferenceResolver<String> resolver;
71   
72    private XWiki xwiki;
73   
 
74  2 toggle @Before
75    public void setUpBaseMocks()
76    {
77  2 this.execution = mock(Execution.class);
78  2 this.xwikiContext = mock(XWikiContext.class);
79  2 ExecutionContext executionContext = new ExecutionContext();
80  2 executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.xwikiContext);
81  2 when(this.execution.getContext()).thenReturn(executionContext);
82  2 this.xwiki = mock(XWiki.class);
83  2 when(this.xwikiContext.getWiki()).thenReturn(this.xwiki);
84   
85  2 this.factory = mock(MimeMessageFactory.class);
86  2 this.resolver = mock(DocumentReferenceResolver.class);
87    }
88   
 
89  1 toggle @Test
90    public void getMimeMessageWithSingleUserReferenceAndEmail() throws Exception
91    {
92  1 DocumentReference userReference = new DocumentReference("userwiki", "userspace", "userpage");
93  1 setUpUserPageMocks(userReference, "john@doe.com");
94  1 Map<String, Object> source = new HashMap<>();
95  1 source.put("users", Collections.singletonList(userReference));
96  1 source.put("emails", Collections.singletonList("mary@doe.com"));
97  1 DocumentReference templateReference = new DocumentReference("templatewiki", "templatespace", "templatepage");
98  1 Map<String, Object> parameters = Collections.<String, Object>singletonMap("source", templateReference);
99   
100  1 MimeMessage message = mock(MimeMessage.class);
101  1 when(this.factory.createMessage(templateReference, null)).thenReturn(message);
102   
103  1 Iterator<MimeMessage> iterator = new UsersAndGroupsMimeMessageIterator(source, this.factory, parameters,
104    this.resolver, this.execution);
105   
106  1 assertTrue(iterator.hasNext());
107  1 assertNotNull(iterator.next());
108  1 assertTrue(iterator.hasNext());
109  1 assertNotNull(iterator.next());
110  1 assertFalse(iterator.hasNext());
111   
112  1 verify(message).addRecipient(Message.RecipientType.TO, new InternetAddress("john@doe.com"));
113  1 verify(message).addRecipient(Message.RecipientType.TO, new InternetAddress("mary@doe.com"));
114    }
115   
 
116  1 toggle @Test
117    public void getMimeMessageWhenErrorCreatingMessage() throws Exception
118    {
119  1 DocumentReference userReference = new DocumentReference("userwiki", "userspace", "userpage");
120  1 setUpUserPageMocks(userReference, "john@doe.com");
121  1 Map<String, Object> source = new HashMap<>();
122  1 source.put("users", Collections.singletonList(userReference));
123  1 DocumentReference templateReference = new DocumentReference("templatewiki", "templatespace", "templatepage");
124  1 Map<String, Object> parameters = Collections.<String, Object>singletonMap("source", templateReference);
125   
126  1 when(this.factory.createMessage(templateReference, null)).thenThrow(
127    new MessagingException("error"));
128   
129  1 Iterator<MimeMessage> iterator = new UsersAndGroupsMimeMessageIterator(source, this.factory, parameters,
130    this.resolver, this.execution);
131   
132  1 assertTrue(iterator.hasNext());
133  1 try {
134  1 iterator.next();
135    } catch (RuntimeException expected) {
136  1 assertEquals("Failed to create Mime Message for recipient john@doe.com", expected.getMessage());
137    }
138    }
139   
 
140  2 toggle private void setUpUserPageMocks(DocumentReference userReference, String email) throws Exception
141    {
142  2 XWikiDocument document = mock(XWikiDocument.class);
143  2 when(document.isNew()).thenReturn(false);
144  2 when(document.getDocumentReference()).thenReturn(userReference);
145  2 BaseObject baseObject = mock(BaseObject.class);
146  2 when(document.getXObject(
147    new EntityReference("XWikiUsers", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE))))
148    .thenReturn(baseObject);
149  2 when(this.xwiki.getDocument(userReference, this.xwikiContext)).thenReturn(document);
150  2 when(baseObject.getStringValue("email")).thenReturn(email);
151    }
152    }