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.Arrays; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import javax.mail.MessagingException; |
29 |
|
|
30 |
|
import org.junit.Before; |
31 |
|
import org.junit.Rule; |
32 |
|
import org.junit.Test; |
33 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
34 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
35 |
|
import org.xwiki.mail.MimeBodyPartFactory; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
38 |
|
|
39 |
|
import com.xpn.xwiki.api.Attachment; |
40 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
41 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
42 |
|
|
43 |
|
import static org.junit.Assert.assertEquals; |
44 |
|
import static org.junit.Assert.fail; |
45 |
|
import static org.mockito.Mockito.mock; |
46 |
|
import static org.mockito.Mockito.verify; |
47 |
|
import static org.mockito.Mockito.verifyNoMoreInteractions; |
48 |
|
import static org.mockito.Mockito.when; |
49 |
|
|
50 |
|
|
51 |
|
@link |
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
|
|
| 98.1% |
Uncovered Elements: 1 (54) |
Complexity: 6 |
Complexity Density: 0.12 |
|
56 |
|
public class TemplateMimeBodyPartFactoryTest |
57 |
|
{ |
58 |
|
private DocumentReference documentReference = new DocumentReference("wiki", "space", "page"); |
59 |
|
|
60 |
|
@Rule |
61 |
|
public MockitoComponentMockingRule<TemplateMimeBodyPartFactory> mocker = |
62 |
|
new MockitoComponentMockingRule<>(TemplateMimeBodyPartFactory.class); |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
64 |
4 |
@Before... |
65 |
|
public void setUp() throws Exception |
66 |
|
{ |
67 |
4 |
MailTemplateManager mailTemplateManager = this.mocker.getInstance(MailTemplateManager.class); |
68 |
4 |
when(mailTemplateManager.evaluate(this.documentReference, "text", new HashMap<String, Object>(), null)) |
69 |
|
.thenReturn( |
70 |
|
"Hello John Doe, john@doe.com"); |
71 |
4 |
when(mailTemplateManager.evaluate(this.documentReference, "html", new HashMap<String, Object>(), null)) |
72 |
|
.thenReturn( |
73 |
|
"Hello <b>John Doe</b> <br />john@doe.com"); |
74 |
|
} |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
76 |
1 |
@Test... |
77 |
|
public void createWithoutAttachment() throws Exception |
78 |
|
{ |
79 |
1 |
MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance( |
80 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html"); |
81 |
|
|
82 |
1 |
this.mocker.getComponentUnderTest().create(this.documentReference, |
83 |
|
Collections.<String, Object>singletonMap("velocityVariables", new HashMap<String, String>())); |
84 |
|
|
85 |
1 |
verify(htmlMimeBodyPartFactory).create("Hello <b>John Doe</b> <br />john@doe.com", |
86 |
|
Collections.<String, Object>singletonMap("alternate", "Hello John Doe, john@doe.com")); |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
89 |
1 |
@Test... |
90 |
|
public void createWithAttachment() throws Exception |
91 |
|
{ |
92 |
1 |
MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance( |
93 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html"); |
94 |
|
|
95 |
1 |
Attachment attachment = mock(Attachment.class); |
96 |
1 |
List<Attachment> attachments = Collections.singletonList(attachment); |
97 |
|
|
98 |
1 |
Map<String, Object> bodyPartParameters = new HashMap<>(); |
99 |
1 |
bodyPartParameters.put("velocityVariables", new HashMap<String, String>()); |
100 |
1 |
bodyPartParameters.put("attachments", attachments); |
101 |
|
|
102 |
1 |
this.mocker.getComponentUnderTest().create(this.documentReference, bodyPartParameters); |
103 |
|
|
104 |
1 |
Map<String, Object> htmlParameters = new HashMap<>(); |
105 |
1 |
htmlParameters.put("alternate", "Hello John Doe, john@doe.com"); |
106 |
1 |
htmlParameters.put("attachments", attachments); |
107 |
|
|
108 |
1 |
verify(htmlMimeBodyPartFactory).create("Hello <b>John Doe</b> <br />john@doe.com", htmlParameters); |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
111 |
1 |
@Test... |
112 |
|
public void createWithAttachmentAndTemplateAttachments() throws Exception |
113 |
|
{ |
114 |
1 |
MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance( |
115 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html"); |
116 |
|
|
117 |
1 |
Attachment attachment1 = mock(Attachment.class, "attachment1"); |
118 |
1 |
Map<String, Object> bodyPartParameters = new HashMap<>(); |
119 |
1 |
bodyPartParameters.put("velocityVariables", new HashMap<String, String>()); |
120 |
1 |
bodyPartParameters.put("attachments", Collections.singletonList(attachment1)); |
121 |
1 |
bodyPartParameters.put("includeTemplateAttachments", true); |
122 |
|
|
123 |
|
|
124 |
1 |
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class); |
125 |
1 |
XWikiDocument xwikiDocument = mock(XWikiDocument.class); |
126 |
1 |
when(dab.getDocument(this.documentReference)).thenReturn(xwikiDocument); |
127 |
1 |
XWikiAttachment xwikiAttachment = mock(XWikiAttachment.class); |
128 |
1 |
when(xwikiDocument.getAttachmentList()).thenReturn(Collections.singletonList(xwikiAttachment)); |
129 |
1 |
AttachmentConverter attachmentConverter = this.mocker.getInstance(AttachmentConverter.class); |
130 |
1 |
Attachment attachment2 = mock(Attachment.class, "attachment2"); |
131 |
1 |
when(attachmentConverter.convert(Collections.singletonList(xwikiAttachment))).thenReturn( |
132 |
|
Collections.singletonList(attachment2)); |
133 |
|
|
134 |
1 |
this.mocker.getComponentUnderTest().create(this.documentReference, bodyPartParameters); |
135 |
|
|
136 |
1 |
Map<String, Object> htmlParameters = new HashMap<>(); |
137 |
1 |
htmlParameters.put("alternate", "Hello John Doe, john@doe.com"); |
138 |
1 |
htmlParameters.put("attachments", Arrays.asList(attachment1, attachment2)); |
139 |
|
|
140 |
1 |
verify(htmlMimeBodyPartFactory).create("Hello <b>John Doe</b> <br />john@doe.com", htmlParameters); |
141 |
|
} |
142 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 2 |
Complexity Density: 0.15 |
1PASS
|
|
143 |
1 |
@Test... |
144 |
|
public void createWithAttachmentAndTemplateAttachmentsWhenError() throws Exception |
145 |
|
{ |
146 |
1 |
MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance( |
147 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html"); |
148 |
|
|
149 |
1 |
Attachment attachment1 = mock(Attachment.class, "attachment1"); |
150 |
1 |
Map<String, Object> bodyPartParameters = new HashMap<>(); |
151 |
1 |
bodyPartParameters.put("velocityVariables", new HashMap<String, String>()); |
152 |
1 |
bodyPartParameters.put("attachments", Collections.singletonList(attachment1)); |
153 |
1 |
bodyPartParameters.put("includeTemplateAttachments", true); |
154 |
|
|
155 |
|
|
156 |
1 |
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class); |
157 |
1 |
when(dab.getDocument(this.documentReference)).thenThrow(new Exception("error")); |
158 |
|
|
159 |
1 |
try { |
160 |
1 |
this.mocker.getComponentUnderTest().create(this.documentReference, bodyPartParameters); |
161 |
0 |
fail("Should have thrown an exception here"); |
162 |
|
} catch (MessagingException expected) { |
163 |
1 |
assertEquals("Failed to include attachments from the Mail Template [wiki:space.page]", |
164 |
|
expected.getMessage()); |
165 |
|
} |
166 |
|
|
167 |
1 |
verifyNoMoreInteractions(htmlMimeBodyPartFactory); |
168 |
|
} |
169 |
|
} |