1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
54 |
|
|
55 |
|
@version |
56 |
|
@since |
57 |
|
@since |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (49) |
Complexity: 5 |
Complexity Density: 0.11 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
74 |
2 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
89 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
1PASS
|
|
116 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
140 |
2 |
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 |
|
} |