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

File TemplateMimeBodyPartFactoryTest.java

 

Code metrics

0
49
5
1
169
113
6
0.12
9.8
5
1.2

Classes

Class Line # Actions
TemplateMimeBodyPartFactoryTest 56 49 0% 6 1
0.981481598.1%
 

Contributing tests

This file is covered by 4 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.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    * Unit tests for {@link org.xwiki.mail.internal.factory.template.TemplateMimeBodyPartFactory}.
52    *
53    * @version $Id: 6a71eb3ecacba485f17395ac92f91cdaf946408c $
54    * @since 6.1RC1
55    */
 
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   
 
64  4 toggle @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   
 
76  1 toggle @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   
 
89  1 toggle @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   
 
111  1 toggle @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    // Mock the retrieval and conversion of attachments from the Template document
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   
 
143  1 toggle @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    // Mock the retrieval and conversion of attachments from the Template document
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    }