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

File TemplateMimeMessageFactoryTest.java

 

Code metrics

0
31
3
1
126
80
3
0.1
10.33
3
1

Classes

Class Line # Actions
TemplateMimeMessageFactoryTest 56 31 0% 3 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.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    * Unit tests for {@link org.xwiki.mail.internal.factory.template.TemplateMimeMessageFactory}.
52    *
53    * @version $Id: 34852af0560316278ff505324daf174be15d3b65 $
54    * @since 6.1RC1
55    */
 
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   
 
66  2 toggle @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   
 
81  1 toggle @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    // Also verify that a body part has been added
94  1 assertEquals(this.mimeBodyPart, ((MimeMultipart) message.getContent()).getBodyPart(0));
95    }
96   
 
97  1 toggle @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    // Also verify that a body part has been added
124  1 assertEquals(this.mimeBodyPart, ((MimeMultipart) message.getContent()).getBodyPart(0));
125    }
126    }