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

File MailStorageScriptServiceTest.java

 

Code metrics

0
49
6
1
169
115
6
0.12
8.17
6
1

Classes

Class Line # Actions
MailStorageScriptServiceTest 61 49 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 5 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;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.List;
25    import java.util.Map;
26   
27    import javax.inject.Provider;
28   
29    import org.apache.commons.lang3.tuple.ImmutablePair;
30    import org.apache.commons.lang3.tuple.Pair;
31    import org.junit.Before;
32    import org.junit.Rule;
33    import org.junit.Test;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.component.util.DefaultParameterizedType;
36    import org.xwiki.context.Execution;
37    import org.xwiki.context.ExecutionContext;
38    import org.xwiki.mail.internal.MemoryMailListener;
39    import org.xwiki.mail.script.MailStorageScriptService;
40    import org.xwiki.mail.script.ScriptMailResult;
41    import org.xwiki.security.authorization.ContextualAuthorizationManager;
42    import org.xwiki.security.authorization.Right;
43    import org.xwiki.test.annotation.ComponentList;
44    import org.xwiki.test.mockito.MockitoComponentMockingRule;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.assertNotNull;
48    import static org.junit.Assert.assertNull;
49    import static org.mockito.Mockito.mock;
50    import static org.mockito.Mockito.when;
51   
52    /**
53    * Unit tests for {@link MailStorageScriptService}.
54    *
55    * @version $Id: 815e4e8b23bc232a7ee5928b5f41de5b1cf3be91 $
56    * @since 6.4
57    */
58    @ComponentList({
59    MemoryMailListener.class
60    })
 
61    public class MailStorageScriptServiceTest
62    {
63    @Rule
64    public MockitoComponentMockingRule<MailStorageScriptService> mocker =
65    new MockitoComponentMockingRule<>(MailStorageScriptService.class);
66   
 
67  5 toggle @Before
68    public void setUp() throws Exception
69    {
70  5 Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(
71    new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
72  5 when(componentManagerProvider.get()).thenReturn(this.mocker);
73   
74  5 Execution execution = this.mocker.getInstance(Execution.class);
75  5 ExecutionContext executionContext = new ExecutionContext();
76  5 when(execution.getContext()).thenReturn(executionContext);
77    }
78   
 
79  1 toggle @Test
80    public void resendWhenMailResendingFailed() throws Exception
81    {
82  1 MailResender resender = this.mocker.getInstance(MailResender.class, "database");
83  1 when(resender.resendAsynchronously("batchId", "messageId")).thenThrow(new MailStoreException("error"));
84   
85  1 ScriptMailResult result = this.mocker.getComponentUnderTest().resend("batchId", "messageId");
86   
87  1 assertNull(result);
88  1 assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
89    }
90   
 
91  1 toggle @Test
92    public void resend() throws Exception
93    {
94  1 MailResender resender = this.mocker.getInstance(MailResender.class, "database");
95  1 MailStatusResult statusResult = mock(MailStatusResult.class);
96  1 when(resender.resendAsynchronously("batchId", "messageId")).thenReturn(statusResult);
97   
98  1 ScriptMailResult result = this.mocker.getComponentUnderTest().resend("batchId", "messageId");
99   
100  1 assertEquals("batchId", result.getBatchId());
101  1 assertNotNull(result.getStatusResult());
102    }
103   
 
104  1 toggle @Test
105    public void resendAsynchronouslySeveralMessages() throws Exception
106    {
107  1 Map filterMap = Collections.singletonMap("state", "prepare_%");
108   
109  1 MailStatus status1 = new MailStatus();
110  1 status1.setBatchId("batch1");
111  1 status1.setMessageId("message1");
112   
113  1 MailStatus status2 = new MailStatus();
114  1 status2.setBatchId("batch2");
115  1 status2.setMessageId("message2");
116   
117  1 MailStatusResult statusResult1 = mock(MailStatusResult.class, "status1");
118  1 when(statusResult1.getTotalMailCount()).thenReturn(1L);
119   
120  1 MailStatusResult statusResult2 = mock(MailStatusResult.class, "status2");
121  1 when(statusResult2.getTotalMailCount()).thenReturn(2L);
122   
123  1 List<Pair<MailStatus, MailStatusResult>> results = new ArrayList<>();
124  1 results.add(new ImmutablePair<>(status1, statusResult1));
125  1 results.add(new ImmutablePair<>(status2, statusResult2));
126   
127  1 MailResender resender = this.mocker.getInstance(MailResender.class, "database");
128  1 when(resender.resendAsynchronously(filterMap, 5, 10)).thenReturn(results);
129   
130  1 List<ScriptMailResult> scriptResults =
131    this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 5, 10);
132   
133  1 assertEquals(2, scriptResults.size());
134  1 assertEquals("batch1", scriptResults.get(0).getBatchId());
135  1 assertEquals(1L, scriptResults.get(0).getStatusResult().getTotalMailCount());
136  1 assertEquals("batch2", scriptResults.get(1).getBatchId());
137  1 assertEquals(2L, scriptResults.get(1).getStatusResult().getTotalMailCount());
138    }
139   
 
140  1 toggle @Test
141    public void resendAsynchronouslySeveralMessagesWhenMailResendingFailed() throws Exception
142    {
143  1 Map filterMap = Collections.singletonMap("state", "prepare_%");
144   
145  1 MailResender resender = this.mocker.getInstance(MailResender.class, "database");
146  1 when(resender.resendAsynchronously(filterMap, 5, 10)).thenThrow(new MailStoreException("error"));
147   
148  1 List<ScriptMailResult> scriptResults =
149    this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 5, 10);
150   
151  1 assertNull(scriptResults);
152  1 assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
153    }
154   
 
155  1 toggle @Test
156    public void loadWhenNotAuthorized() throws Exception
157    {
158  1 ContextualAuthorizationManager authorizationManager =
159    this.mocker.getInstance(ContextualAuthorizationManager.class);
160  1 when(authorizationManager.hasAccess(Right.ADMIN)).thenReturn(false);
161   
162  1 List<MailStatus> result = this.mocker.getComponentUnderTest().load(
163    Collections.<String, Object>emptyMap(), 0, 0, null, false);
164   
165  1 assertNull(result);
166  1 assertEquals("You need Admin rights to load mail statuses",
167    this.mocker.getComponentUnderTest().getLastError().getMessage());
168    }
169    }