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.template; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.Map; |
25 |
|
|
26 |
|
import javax.mail.Address; |
27 |
|
import javax.mail.Message; |
28 |
|
import javax.mail.internet.InternetAddress; |
29 |
|
import javax.mail.internet.MimeBodyPart; |
30 |
|
import javax.mail.internet.MimeMessage; |
31 |
|
import javax.mail.internet.MimeMultipart; |
32 |
|
|
33 |
|
import org.junit.Before; |
34 |
|
import org.junit.Rule; |
35 |
|
import org.junit.Test; |
36 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
37 |
|
import org.xwiki.mail.MimeBodyPartFactory; |
38 |
|
import org.xwiki.model.reference.DocumentReference; |
39 |
|
import org.xwiki.properties.ConverterManager; |
40 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
41 |
|
|
42 |
|
import static org.junit.Assert.assertArrayEquals; |
43 |
|
import static org.junit.Assert.assertEquals; |
44 |
|
import static org.mockito.ArgumentMatchers.any; |
45 |
|
import static org.mockito.ArgumentMatchers.eq; |
46 |
|
import static org.mockito.ArgumentMatchers.same; |
47 |
|
import static org.mockito.Mockito.mock; |
48 |
|
import static org.mockito.Mockito.when; |
49 |
|
|
50 |
|
|
51 |
|
@link |
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 3 |
Complexity Density: 0.1 |
|
56 |
|
public class TemplateMimeMessageFactoryTest |
57 |
|
{ |
58 |
|
@Rule |
59 |
|
public MockitoComponentMockingRule<TemplateMimeMessageFactory> mocker = |
60 |
|
new MockitoComponentMockingRule<>(TemplateMimeMessageFactory.class); |
61 |
|
|
62 |
|
private DocumentReference templateReference; |
63 |
|
|
64 |
|
private MimeBodyPart mimeBodyPart; |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
66 |
2 |
@Before... |
67 |
|
public void setUp() throws Exception |
68 |
|
{ |
69 |
2 |
this.templateReference = new DocumentReference("templatewiki", "templatespace", "templatepage"); |
70 |
|
|
71 |
2 |
MailTemplateManager mailTemplateManager = this.mocker.getInstance(MailTemplateManager.class); |
72 |
2 |
when(mailTemplateManager.evaluate(same(this.templateReference), eq("subject"), any(), any())) |
73 |
|
.thenReturn("XWiki news"); |
74 |
|
|
75 |
2 |
MimeBodyPartFactory<DocumentReference> templateBodyPartFactory = this.mocker.getInstance( |
76 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, DocumentReference.class), "xwiki/template"); |
77 |
2 |
this.mimeBodyPart = mock(MimeBodyPart.class); |
78 |
2 |
when(templateBodyPartFactory.create(same(this.templateReference), any())).thenReturn(this.mimeBodyPart); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
81 |
1 |
@Test... |
82 |
|
public void createMessage() throws Exception |
83 |
|
{ |
84 |
1 |
Map<String, Object> parameters = new HashMap<>(); |
85 |
1 |
parameters.put("language", "fr"); |
86 |
1 |
parameters.put("velocityVariables", Collections.<String, Object>singletonMap("company", "XWiki")); |
87 |
|
|
88 |
1 |
MimeMessage message = |
89 |
|
this.mocker.getComponentUnderTest().createMessage(this.templateReference, parameters); |
90 |
|
|
91 |
1 |
assertEquals("XWiki news", message.getSubject()); |
92 |
|
|
93 |
|
|
94 |
1 |
assertEquals(this.mimeBodyPart, ((MimeMultipart) message.getContent()).getBodyPart(0)); |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
97 |
1 |
@Test... |
98 |
|
public void createMessageWithToFromCCAndBCCAddressesAsStrings() throws Exception |
99 |
|
{ |
100 |
1 |
Map<String, Object> parameters = new HashMap<>(); |
101 |
1 |
parameters.put("language", "fr"); |
102 |
1 |
parameters.put("velocityVariables", Collections.<String, Object>singletonMap("company", "XWiki")); |
103 |
1 |
parameters.put("to", "to@doe.com"); |
104 |
1 |
parameters.put("cc", "cc@doe.com"); |
105 |
1 |
parameters.put("bcc", "bcc@doe.com"); |
106 |
1 |
parameters.put("from", "from@doe.com"); |
107 |
|
|
108 |
1 |
ConverterManager converterManager = this.mocker.getInstance(ConverterManager.class); |
109 |
1 |
when(converterManager.convert(Address[].class, "to@doe.com")).thenReturn(InternetAddress.parse("to@doe.com")); |
110 |
1 |
when(converterManager.convert(Address[].class, "cc@doe.com")).thenReturn(InternetAddress.parse("cc@doe.com")); |
111 |
1 |
when(converterManager.convert(Address[].class, "bcc@doe.com")).thenReturn(InternetAddress.parse("bcc@doe.com")); |
112 |
1 |
when(converterManager.convert(Address.class, "from@doe.com")).thenReturn( |
113 |
|
InternetAddress.parse("from@doe.com")[0]); |
114 |
|
|
115 |
1 |
MimeMessage message = this.mocker.getComponentUnderTest().createMessage(this.templateReference, parameters); |
116 |
|
|
117 |
1 |
assertEquals("XWiki news", message.getSubject()); |
118 |
1 |
assertArrayEquals(InternetAddress.parse("from@doe.com"), message.getFrom()); |
119 |
1 |
assertArrayEquals(InternetAddress.parse("to@doe.com"), message.getRecipients(Message.RecipientType.TO)); |
120 |
1 |
assertArrayEquals(InternetAddress.parse("cc@doe.com"), message.getRecipients(Message.RecipientType.CC)); |
121 |
1 |
assertArrayEquals(InternetAddress.parse("bcc@doe.com"), message.getRecipients(Message.RecipientType.BCC)); |
122 |
|
|
123 |
|
|
124 |
1 |
assertEquals(this.mimeBodyPart, ((MimeMultipart) message.getContent()).getBodyPart(0)); |
125 |
|
} |
126 |
|
} |