| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 58 |
|
|
| 59 |
|
@version |
| 60 |
|
@since |
| 61 |
|
|
| 62 |
|
@ComponentList({ |
| 63 |
|
MemoryMailListener.class |
| 64 |
|
}) |
| |
|
| 100% |
Uncovered Elements: 0 (38) |
Complexity: 5 |
Complexity Density: 0.15 |
|
| 65 |
|
public class MailStorageScriptServiceTest |
| 66 |
|
{ |
| 67 |
|
@Rule |
| 68 |
|
public MockitoComponentMockingRule<MailStorageScriptService> mocker = |
| 69 |
|
new MockitoComponentMockingRule<>(MailStorageScriptService.class); |
| 70 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 71 |
4 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 83 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 93 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
| 109 |
1 |
@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 |
|
|
| 127 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 137 |
1 |
@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 |
|
} |