1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.mail.internal

File DatabaseMailResenderTest.java

 

Code metrics

0
57
4
1
166
109
5
0.09
14.25
4
1.25

Classes

Class Line # Actions
DatabaseMailResenderTest 53 57 0% 5 1
0.983606698.4%
 

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;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.List;
26    import java.util.Map;
27    import java.util.UUID;
28   
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.mail.ExtendedMimeMessage;
32    import org.xwiki.mail.MailContentStore;
33    import org.xwiki.mail.MailListener;
34    import org.xwiki.mail.MailSender;
35    import org.xwiki.mail.MailStatus;
36    import org.xwiki.mail.MailStatusStore;
37    import org.xwiki.mail.MailStoreException;
38    import org.xwiki.test.mockito.MockitoComponentMockingRule;
39   
40    import static org.junit.Assert.assertEquals;
41    import static org.junit.Assert.fail;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.ArgumentMatchers.same;
45    import static org.mockito.Mockito.verify;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * Unit tests for {@link DatabaseMailResender}.
50    *
51    * @version $Id: 77aa2738b4655ce6a1bbc3d064d17f225792c340 $
52    */
 
53    public class DatabaseMailResenderTest
54    {
55    @Rule
56    public MockitoComponentMockingRule<DatabaseMailResender> mocker =
57    new MockitoComponentMockingRule<>(DatabaseMailResender.class);
58   
 
59  1 toggle @Test
60    public void resendAsynchronouslySingleMesssage() throws Exception
61    {
62  1 MailListener listener = this.mocker.registerMockComponent(MailListener.class, "database");
63   
64  1 ExtendedMimeMessage message = new ExtendedMimeMessage();
65  1 String batchId = UUID.randomUUID().toString();
66   
67  1 MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
68  1 when(contentStore.load(any(), eq(batchId), eq("messageId"))).thenReturn(message);
69   
70  1 MailSender sender = this.mocker.getInstance(MailSender.class);
71  1 when(sender.sendAsynchronously(eq(Arrays.asList(message)), any(), any(MailListener.class)))
72    .thenReturn(new DefaultMailResult(batchId));
73   
74  1 this.mocker.getComponentUnderTest().resendAsynchronously(batchId, "messageId");
75   
76    // The test is here
77  1 verify(sender).sendAsynchronously(eq(Arrays.asList(message)), any(), same(listener));
78  1 verify(listener).getMailStatusResult();
79    }
80   
 
81  1 toggle @Test
82    public void resendAsynchronouslySingleMessageWhenMailContentStoreLoadingFails() throws Exception
83    {
84  1 MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
85  1 when(contentStore.load(any(), eq("batchId"), eq("messageId"))).thenThrow(
86    new MailStoreException("error"));
87   
88  1 try {
89  1 this.mocker.getComponentUnderTest().resendAsynchronously("batchId", "messageId");
90  0 fail("Should have thrown an exception here");
91    } catch (MailStoreException expected) {
92  1 assertEquals("error", expected.getMessage());
93    }
94    }
95   
 
96  1 toggle @Test
97    public void resendAsynchronouslySeveralMessages() throws Exception
98    {
99  1 Map filterMap = Collections.singletonMap("state", "prepare_%");
100   
101  1 MailStatus status1 = new MailStatus();
102  1 status1.setBatchId("batch1");
103  1 status1.setMessageId("message1");
104   
105  1 MailStatus status2 = new MailStatus();
106  1 status2.setBatchId("batch2");
107  1 status2.setMessageId("message2");
108   
109  1 List<MailStatus> statuses = new ArrayList<>();
110  1 statuses.add(status1);
111  1 statuses.add(status2);
112   
113  1 MailStatusStore statusStore = this.mocker.getInstance(MailStatusStore.class, "database");
114  1 when(statusStore.load(filterMap, 0, 0, null, true)).thenReturn(statuses);
115   
116  1 MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
117  1 ExtendedMimeMessage message1 = new ExtendedMimeMessage();
118  1 when(contentStore.load(any(), eq("batch1"), eq("message1"))).thenReturn(message1);
119  1 ExtendedMimeMessage message2 = new ExtendedMimeMessage();
120  1 when(contentStore.load(any(), eq("batch2"), eq("message2"))).thenReturn(message2);
121   
122  1 MailSender sender = this.mocker.getInstance(MailSender.class);
123   
124  1 this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 0, 0);
125   
126    // The test is here
127  1 verify(sender).sendAsynchronously(eq(Arrays.asList(message1)), any(), any(MailListener.class));
128  1 verify(sender).sendAsynchronously(eq(Arrays.asList(message2)), any(), any(MailListener.class));
129    }
130   
 
131  1 toggle @Test
132    public void resendAsynchronouslySeveralMessagesWhenMailContentStoreLoadingFailsForFirstMessage() throws Exception
133    {
134  1 Map filterMap = Collections.singletonMap("state", "prepare_%");
135   
136  1 MailStatus status1 = new MailStatus();
137  1 status1.setBatchId("batch1");
138  1 status1.setMessageId("message1");
139   
140  1 MailStatus status2 = new MailStatus();
141  1 status2.setBatchId("batch2");
142  1 status2.setMessageId("message2");
143   
144  1 List<MailStatus> statuses = new ArrayList<>();
145  1 statuses.add(status1);
146  1 statuses.add(status2);
147   
148  1 MailStatusStore statusStore = this.mocker.getInstance(MailStatusStore.class, "database");
149  1 when(statusStore.load(filterMap, 0, 0, null, true)).thenReturn(statuses);
150   
151  1 MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
152  1 when(contentStore.load(any(), eq("batch1"), eq("message1"))).thenThrow(
153    new MailStoreException("error1"));
154  1 ExtendedMimeMessage message2 = new ExtendedMimeMessage();
155  1 when(contentStore.load(any(), eq("batch2"), eq("message2"))).thenReturn(message2);
156   
157  1 MailSender sender = this.mocker.getInstance(MailSender.class);
158   
159  1 this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 0, 0);
160   
161    // The test is here
162  1 verify(this.mocker.getMockedLogger()).warn("Failed to load mail content for batchId [{}], messageId [{}]",
163    "batch1", "message1");
164  1 verify(sender).sendAsynchronously(eq(Arrays.asList(message2)), any(), any(MailListener.class));
165    }
166    }