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

File DatabaseMailListenerTest.java

 

Code metrics

0
81
11
2
254
181
11
0.14
7.36
5.5
1

Classes

Class Line # Actions
DatabaseMailListenerTest 61 78 0% 9 0
1.0100%
DatabaseMailListenerTest.isSameMailStatus 232 3 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 8 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.Arrays;
23    import java.util.Collections;
24    import java.util.UUID;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.mockito.ArgumentCaptor;
30    import org.mockito.ArgumentMatcher;
31    import org.mockito.Mockito;
32    import org.slf4j.Logger;
33    import org.xwiki.context.Execution;
34    import org.xwiki.context.ExecutionContext;
35    import org.xwiki.mail.ExtendedMimeMessage;
36    import org.xwiki.mail.MailContentStore;
37    import org.xwiki.mail.MailListener;
38    import org.xwiki.mail.MailState;
39    import org.xwiki.mail.MailStatus;
40    import org.xwiki.mail.MailStatusStore;
41    import org.xwiki.mail.MailStoreException;
42    import org.xwiki.test.AllLogRule;
43    import org.xwiki.test.mockito.MockitoComponentMockingRule;
44   
45    import com.xpn.xwiki.XWikiContext;
46   
47    import static org.junit.Assert.assertEquals;
48    import static org.mockito.ArgumentMatchers.any;
49    import static org.mockito.ArgumentMatchers.anyMap;
50    import static org.mockito.ArgumentMatchers.argThat;
51    import static org.mockito.Mockito.doThrow;
52    import static org.mockito.Mockito.verify;
53    import static org.mockito.Mockito.when;
54   
55    /**
56    * Unit tests for {@link DatabaseMailListener}.
57    *
58    * @version $Id: 867860f519f71fa57b99098f9fa1d9d82c8ad369 $
59    * @since 6.4M3
60    */
 
61    public class DatabaseMailListenerTest
62    {
63    @Rule
64    public AllLogRule logRule = new AllLogRule();
65   
66    @Rule
67    public MockitoComponentMockingRule<DatabaseMailListener> mocker =
68    new MockitoComponentMockingRule<>(DatabaseMailListener.class, Arrays.asList(Logger.class));
69   
70    private ExtendedMimeMessage message;
71   
72    private String batchId = UUID.randomUUID().toString();
73   
74    private String mimeMessageId = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
75   
76    private String messageId;
77   
 
78  8 toggle @Before
79    public void setUp() throws Exception
80    {
81  8 this.message = new ExtendedMimeMessage();
82  8 this.message.setType("type");
83  8 this.message.saveChanges();
84  8 this.message.setHeader("Message-ID", mimeMessageId);
85  8 this.messageId = message.getUniqueMessageId();
86   
87  8 Execution execution = this.mocker.getInstance(Execution.class);
88  8 ExecutionContext executionContext = Mockito.mock(ExecutionContext.class);
89  8 XWikiContext xcontext = Mockito.mock(XWikiContext.class);
90  8 when(xcontext.getWikiId()).thenReturn("mywiki");
91  8 when(executionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).thenReturn(xcontext);
92  8 when(execution.getContext()).thenReturn(executionContext);
93    }
94   
 
95  1 toggle @Test
96    public void onPrepareSuccess() throws Exception
97    {
98  1 MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
99   
100  1 MailListener listener = this.mocker.getComponentUnderTest();
101  1 listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
102  1 listener.onPrepareMessageSuccess(this.message, Collections.<String, Object>emptyMap());
103   
104  1 verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.PREPARE_SUCCESS, "mywiki")), anyMap());
105    }
106   
 
107  1 toggle @Test
108    public void onPrepareError() throws Exception
109    {
110  1 MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
111   
112  1 MailListener listener = this.mocker.getComponentUnderTest();
113  1 listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
114  1 listener.onPrepareMessageError(this.message, new Exception("Error"), Collections.<String, Object>emptyMap());
115   
116  1 verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.PREPARE_ERROR, "mywiki")), anyMap());
117    }
118   
 
119  1 toggle @Test
120    public void onPrepareWhenSaveFails() throws Exception
121    {
122  1 MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
123  1 doThrow(new MailStoreException("error")).when(mailStatusStore).save(any(MailStatus.class), anyMap());
124   
125  1 MailListener listener = this.mocker.getComponentUnderTest();
126  1 listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
127  1 listener.onPrepareMessageSuccess(this.message, Collections.<String, Object>emptyMap());
128   
129  1 ArgumentCaptor<MailStatus> statusCapture = ArgumentCaptor.forClass(MailStatus.class);
130  1 verify(mailStatusStore).save(statusCapture.capture(), anyMap());
131   
132  1 assertEquals("Failed to save mail status [messageId = [" + this.messageId + "], batchId = ["
133    + this.batchId + "], state = [prepare_success], date = [" + statusCapture.getValue().getDate() + "], "
134    + "recipients = [<null>], type = [type], wiki = [mywiki]] to the database", this.logRule.getMessage(0));
135    }
136   
 
137  1 toggle @Test
138    public void onSendMessageSuccess() throws Exception
139    {
140  1 MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
141  1 MailStatus status = new MailStatus(this.batchId, this.message, MailState.PREPARE_SUCCESS);
142  1 status.setWiki("otherwiki");
143  1 when(mailStatusStore.load(this.messageId)).thenReturn(status);
144   
145  1 MailListener listener = this.mocker.getComponentUnderTest();
146  1 listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
147  1 listener.onSendMessageSuccess(this.message, Collections.<String, Object>emptyMap());
148   
149  1 verify(mailStatusStore).load(this.messageId);
150  1 verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.SEND_SUCCESS, "otherwiki")), anyMap());
151   
152  1 MailContentStore mailContentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
153  1 verify(mailContentStore).delete(this.batchId, this.messageId);
154    }
155   
 
156  1 toggle @Test
157    public void onSuccessWhenStatusLoadFails() throws Exception
158    {
159  1 MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
160  1 when(mailStatusStore.load(this.messageId)).thenThrow(new MailStoreException("error"));
161   
162  1 MailListener listener = this.mocker.getComponentUnderTest();
163  1 listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
164  1 listener.onSendMessageSuccess(this.message, Collections.<String, Object>emptyMap());
165   
166  1 assertEquals("Error when looking for a previous mail status for message [" + this.messageId + "] of batch ["
167    + batchId + "] and state [send_success].", this.logRule.getMessage(0));
168  1 assertEquals("Forcing a new mail status for message [" + this.messageId + "] of batch [" + batchId
169    + "] to send_success state.", this.logRule.getMessage(1));
170   
171    // Verify that save and delete happened
172  1 verify(mailStatusStore).save(any(MailStatus.class), anyMap());
173  1 MailContentStore mailContentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
174  1 verify(mailContentStore).delete(any(), any());
175    }
176   
 
177  1 toggle @Test
178    public void onSuccessWhenMailContentDeletionFails() throws Exception
179    {
180  1 MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
181  1 MailStatus status = new MailStatus(this.batchId, this.message, MailState.PREPARE_SUCCESS);
182  1 status.setWiki("otherwiki");
183  1 when(mailStatusStore.load(this.messageId)).thenReturn(status);
184   
185  1 MailContentStore mailContentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
186  1 doThrow(new MailStoreException("error")).when(mailContentStore).delete(this.batchId, this.messageId);
187   
188  1 MailListener listener = this.mocker.getComponentUnderTest();
189  1 listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
190  1 listener.onSendMessageSuccess(this.message, Collections.<String, Object>emptyMap());
191   
192  1 assertEquals("Failed to remove previously failing message [" + this.messageId + "] (batch id ["
193    + this.batchId + "]) from the file system. Reason [MailStoreException: error].",
194    this.logRule.getMessage(0));
195    }
196   
 
197  1 toggle @Test
198    public void onSendMessageError() throws Exception
199    {
200  1 MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
201  1 MailStatus status = new MailStatus(this.batchId, this.message, MailState.PREPARE_SUCCESS);
202  1 status.setWiki("otherwiki");
203  1 when(mailStatusStore.load(this.messageId)).thenReturn(status);
204   
205  1 MailListener listener = this.mocker.getComponentUnderTest();
206  1 listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
207  1 listener.onSendMessageError(this.message, new Exception("Error"), Collections.<String, Object>emptyMap());
208   
209  1 verify(mailStatusStore).load(this.messageId);
210  1 verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.SEND_ERROR, "otherwiki")), anyMap());
211    }
212   
 
213  1 toggle @Test
214    public void onSendMessageFatalError() throws Exception
215    {
216  1 MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
217  1 MailStatus status = new MailStatus(this.batchId, this.message, MailState.PREPARE_SUCCESS);
218  1 status.setWiki("otherwiki");
219  1 when(mailStatusStore.load(this.messageId)).thenReturn(status);
220   
221  1 MailListener listener = this.mocker.getComponentUnderTest();
222  1 listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
223  1 listener.onSendMessageFatalError(this.messageId, new Exception("Error"), Collections.<String, Object>emptyMap());
224   
225  1 verify(mailStatusStore).load(this.messageId);
226  1 verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.SEND_FATAL_ERROR, "otherwiki")), anyMap());
227    }
228   
229    /**
230    * Custom Mokito Argument Matchers.
231    */
 
232    class isSameMailStatus implements ArgumentMatcher<MailStatus>
233    {
234    private String state;
235   
236    private String wikiId;
237   
 
238  5 toggle public isSameMailStatus(MailState state, String wikiId)
239    {
240  5 this.state = state.toString();
241  5 this.wikiId = wikiId;
242    }
243   
 
244  10 toggle @Override
245    public boolean matches(MailStatus argument)
246    {
247  10 return argument.getBatchId().equals(batchId) &&
248    argument.getMessageId().equals(messageId) &&
249    argument.getType().equals("type") &&
250    argument.getState().equals(state) &&
251    argument.getWiki().equals(wikiId);
252    }
253    }
254    }