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

File DatabaseMailStatusStoreTest.java

 

Code metrics

0
28
3
1
116
74
3
0.11
9.33
3
1

Classes

Class Line # Actions
DatabaseMailStatusStoreTest 53 28 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 3 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.LinkedHashMap;
24    import java.util.Map;
25   
26    import javax.inject.Provider;
27   
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.slf4j.Logger;
31    import org.xwiki.component.util.ReflectionUtils;
32    import org.xwiki.mail.MailState;
33    import org.xwiki.mail.MailStatus;
34    import org.xwiki.test.AllLogRule;
35    import org.xwiki.test.LogLevel;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.store.XWikiHibernateStore;
40   
41    import static org.junit.Assert.*;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.Mockito.mock;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Unit tests for {@link DatabaseMailStatusStore}.
49    *
50    * @version $Id: 002eb0413224871e0118de5a7c0eb3b53b8b03ae $
51    * @since 6.4RC1
52    */
 
53    public class DatabaseMailStatusStoreTest
54    {
55    @Rule
56    public AllLogRule logRule = new AllLogRule(LogLevel.DEBUG);
57   
58    @Rule
59    public MockitoComponentMockingRule<DatabaseMailStatusStore> mocker =
60    new MockitoComponentMockingRule<>(DatabaseMailStatusStore.class, Arrays.asList(Logger.class));
61   
 
62  1 toggle @Test
63    public void computeSelectQueryString() throws Exception
64    {
65  1 Map<String, Object> filterMap = new LinkedHashMap<>();
66  1 filterMap.put("status", "failed");
67  1 filterMap.put("wiki", "mywiki");
68   
69  1 assertEquals("from org.xwiki.mail.MailStatus where mail_status like :status and mail_wiki like :wiki order by date desc",
70    this.mocker.getComponentUnderTest().computeSelectQueryString(filterMap, "date", false));
71    }
72   
 
73  1 toggle @Test
74    public void computeCountQueryString() throws Exception
75    {
76  1 Map<String, Object> filterMap = new LinkedHashMap<>();
77  1 filterMap.put("status", "failed");
78  1 filterMap.put("wiki", "mywiki");
79   
80  1 assertEquals("select count(*) from org.xwiki.mail.MailStatus where mail_status like :status and "
81    + "mail_wiki like :wiki", this.mocker.getComponentUnderTest().computeCountQueryString(filterMap));
82    }
83   
 
84  1 toggle @Test
85    public void logsForloadMailStatus() throws Exception
86    {
87  1 Provider<XWikiContext> xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
88  1 XWikiContext xcontext = mock(XWikiContext.class);
89  1 when(xcontextProvider.get()).thenReturn(xcontext);
90  1 when(xcontext.getWikiId()).thenReturn("wiki");
91  1 when(xcontext.getMainXWiki()).thenReturn("mainwiki");
92   
93  1 XWikiHibernateStore hibernateStore = mock(XWikiHibernateStore.class);
94  1 ReflectionUtils.setFieldValue(this.mocker.getComponentUnderTest(), "hibernateStore", hibernateStore);
95  1 MailStatus status = new MailStatus();
96  1 status.setBatchId("batchid");
97  1 status.setMessageId("messageid");
98  1 status.setState(MailState.PREPARE_SUCCESS);
99  1 status.setRecipients("recipients");
100  1 when(hibernateStore.executeRead(eq(xcontext), any())).thenReturn(Arrays.asList(status));
101   
102  1 Map<String, Object> filterMap = new LinkedHashMap<>();
103  1 filterMap.put("status", "failed");
104  1 filterMap.put("wiki", "mywiki");
105   
106  1 this.mocker.getComponentUnderTest().load(filterMap, 0, 0, null, false);
107   
108    // The test is here, we verify that debug logs are correct
109  1 assertEquals(2, this.logRule.size());
110  1 assertEquals("Find mail statuses for query [from org.xwiki.mail.MailStatus where mail_status like :status "
111    + "and mail_wiki like :wiki] and parameters [[status] = [failed], [wiki] = [mywiki]]",
112    this.logRule.getMessage(0));
113  1 assertEquals("Loaded mail status [messageId = [messageid], batchId = [batchid], state = [prepare_success], "
114    + "date = [<null>], recipients = [recipients]]", this.logRule.getMessage(1));
115    }
116    }