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

File MailStorageScriptServiceTest.java

 

Code metrics

0
33
5
1
151
101
5
0.15
6.6
5
1

Classes

Class Line # Actions
MailStorageScriptServiceTest 65 33 0% 5 0
1.0100%
 

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;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.List;
25    import java.util.Properties;
26    import java.util.UUID;
27   
28    import javax.inject.Provider;
29    import javax.mail.Session;
30   
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.DefaultMailResult;
39    import org.xwiki.mail.internal.MemoryMailListener;
40    import org.xwiki.mail.internal.UpdateableMailStatusResult;
41    import org.xwiki.mail.script.MailStorageScriptService;
42    import org.xwiki.mail.script.ScriptMailResult;
43    import org.xwiki.security.authorization.ContextualAuthorizationManager;
44    import org.xwiki.security.authorization.Right;
45    import org.xwiki.test.annotation.ComponentList;
46    import org.xwiki.test.mockito.MockitoComponentMockingRule;
47   
48    import static org.junit.Assert.assertEquals;
49    import static org.junit.Assert.assertNotNull;
50    import static org.junit.Assert.assertNull;
51    import static org.mockito.ArgumentMatchers.any;
52    import static org.mockito.ArgumentMatchers.eq;
53    import static org.mockito.ArgumentMatchers.same;
54    import static org.mockito.Mockito.when;
55   
56    /**
57    * Unit tests for {@link MailStorageScriptService}.
58    *
59    * @version $Id: 4e390512a09fa0fa986e008df33d8e63e1e58184 $
60    * @since 6.4
61    */
62    @ComponentList({
63    MemoryMailListener.class
64    })
 
65    public class MailStorageScriptServiceTest
66    {
67    @Rule
68    public MockitoComponentMockingRule<MailStorageScriptService> mocker =
69    new MockitoComponentMockingRule<>(MailStorageScriptService.class);
70   
 
71  4 toggle @Before
72    public void setUp() throws Exception
73    {
74  4 Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(
75    new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
76  4 when(componentManagerProvider.get()).thenReturn(this.mocker);
77   
78  4 Execution execution = this.mocker.getInstance(Execution.class);
79  4 ExecutionContext executionContext = new ExecutionContext();
80  4 when(execution.getContext()).thenReturn(executionContext);
81    }
82   
 
83  1 toggle @Test
84    public void resendWhenDatabaseListenerNotFound() throws Exception
85    {
86  1 ScriptMailResult result = this.mocker.getComponentUnderTest().resend("batchId", "messageId");
87   
88  1 assertNull(result);
89  1 assertEquals("Can't find descriptor for the component [role = [interface org.xwiki.mail.MailListener] "
90    + "hint = [database]]", this.mocker.getComponentUnderTest().getLastError().getMessage());
91    }
92   
 
93  1 toggle @Test
94    public void resendWhenMailContentStoreLoadingFails() throws Exception
95    {
96  1 this.mocker.registerComponent(MailListener.class, "database",
97    this.mocker.getInstance(MailListener.class, "memory"));
98   
99  1 MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
100  1 when(contentStore.load(any(), eq("batchId"), eq("messageId"))).thenThrow(
101    new MailStoreException("error"));
102   
103  1 ScriptMailResult result = this.mocker.getComponentUnderTest().resend("batchId", "messageId");
104   
105  1 assertNull(result);
106  1 assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
107    }
108   
 
109  1 toggle @Test
110    public void resend() throws Exception
111    {
112  1 MemoryMailListener memoryMailListener = this.mocker.getInstance(MailListener.class, "memory");
113  1 this.mocker.registerComponent(MailListener.class, "database", memoryMailListener);
114   
115  1 Session session = Session.getInstance(new Properties());
116  1 ExtendedMimeMessage message = new ExtendedMimeMessage();
117  1 String batchId = UUID.randomUUID().toString();
118   
119  1 MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
120  1 when(contentStore.load(any(), eq(batchId), eq("messageId"))).thenReturn(message);
121   
122  1 MailSender sender = this.mocker.getInstance(MailSender.class);
123  1 when(sender.sendAsynchronously(eq(Arrays.asList(message)), any(), same(memoryMailListener)))
124    .thenReturn(new DefaultMailResult(batchId));
125   
126    // Since resend() will wait indefinitely for the message count to be correct, we need to configure it here
127    // as we're mocking the MailSender.
128  1 ((UpdateableMailStatusResult) memoryMailListener.getMailStatusResult()).setTotalSize(1);
129  1 ((UpdateableMailStatusResult) memoryMailListener.getMailStatusResult()).incrementCurrentSize();
130   
131  1 ScriptMailResult result = this.mocker.getComponentUnderTest().resend(batchId, "messageId");
132   
133  1 assertNotNull(result);
134  1 assertEquals(batchId, result.getBatchId());
135    }
136   
 
137  1 toggle @Test
138    public void loadWhenNotAuthorized() throws Exception
139    {
140  1 ContextualAuthorizationManager authorizationManager =
141    this.mocker.getInstance(ContextualAuthorizationManager.class);
142  1 when(authorizationManager.hasAccess(Right.ADMIN)).thenReturn(false);
143   
144  1 List<MailStatus> result = this.mocker.getComponentUnderTest().load(
145    Collections.<String, Object>emptyMap(), 0, 0, null, false);
146   
147  1 assertNull(result);
148  1 assertEquals("You need Admin rights to load mail statuses",
149    this.mocker.getComponentUnderTest().getLastError().getMessage());
150    }
151    }