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

File NotificationUserIteratorTest.java

 

Code metrics

0
45
2
1
132
89
2
0.04
22.5
2
1

Classes

Class Line # Actions
NotificationUserIteratorTest 51 45 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.notifications.notifiers.internal.email;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.mockito.ArgumentMatchers;
29    import org.xwiki.bridge.DocumentAccessBridge;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.DocumentReferenceResolver;
32    import org.xwiki.model.reference.WikiReference;
33    import org.xwiki.notifications.notifiers.email.NotificationEmailInterval;
34    import org.xwiki.query.Query;
35    import org.xwiki.query.QueryManager;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
38   
39    import static org.junit.Assert.assertEquals;
40    import static org.junit.Assert.assertFalse;
41    import static org.junit.Assert.assertTrue;
42    import static org.mockito.ArgumentMatchers.eq;
43    import static org.mockito.Mockito.atLeastOnce;
44    import static org.mockito.Mockito.mock;
45    import static org.mockito.Mockito.verify;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * @version $Id: 24e7d0e4c9969fba5d090b956acf28169d1277bd $
50    */
 
51    public class NotificationUserIteratorTest
52    {
53    @Rule
54    public final MockitoComponentMockingRule<NotificationUserIterator> mocker =
55    new MockitoComponentMockingRule<>(NotificationUserIterator.class);
56   
57    private QueryManager queryManager;
58    private DocumentReferenceResolver<String> resolver;
59    private WikiDescriptorManager wikiDescriptorManager;
60    private DocumentAccessBridge documentAccessBridge;
61   
 
62  1 toggle @Before
63    public void setUp() throws Exception
64    {
65  1 queryManager = mocker.getInstance(QueryManager.class);
66  1 resolver = mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
67  1 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
68  1 documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
69   
70  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wikiA");
71    }
72   
 
73  1 toggle @Test
74    public void test() throws Exception
75    {
76    // Mocks
77  1 Query query1 = mock(Query.class);
78  1 Query query2 = mock(Query.class);
79  1 Query query3 = mock(Query.class);
80  1 when(queryManager.createQuery(ArgumentMatchers.anyString(), eq(Query.XWQL))).thenReturn(query1, query2, query3,
81    query1, query2, query3);
82  1 when(query1.execute()).thenReturn(Arrays.asList("XWiki.UserA", "XWiki.UserB", "XWiki.UserC"));
83  1 when(query2.execute()).thenReturn(Arrays.asList("XWiki.UserD"));
84  1 when(query3.execute()).thenReturn(Collections.emptyList());
85   
86  1 DocumentReference userA = new DocumentReference("wikiA", "XWiki", "UserA");
87  1 DocumentReference userB = new DocumentReference("wikiA", "XWiki", "UserB");
88  1 DocumentReference userC = new DocumentReference("wikiA", "XWiki", "UserC");
89  1 DocumentReference userD = new DocumentReference("wikiA", "XWiki", "UserD");
90  1 when(resolver.resolve("XWiki.UserA", new WikiReference("wikiA"))).thenReturn(userA);
91  1 when(resolver.resolve("XWiki.UserB", new WikiReference("wikiA"))).thenReturn(userB);
92  1 when(resolver.resolve("XWiki.UserC", new WikiReference("wikiA"))).thenReturn(userC);
93  1 when(resolver.resolve("XWiki.UserD", new WikiReference("wikiA"))).thenReturn(userD);
94   
95  1 DocumentReference classReference = new DocumentReference("wikiA",
96    Arrays.asList("XWiki", "Notifications", "Code"), "NotificationEmailPreferenceClass");
97  1 when(documentAccessBridge.getProperty(userA, classReference, "interval")).thenReturn("weekly");
98  1 when(documentAccessBridge.getProperty(userB, classReference, "interval")).thenReturn("daily");
99  1 when(documentAccessBridge.getProperty(userC, classReference, "interval")).thenReturn(null);
100  1 when(documentAccessBridge.getProperty(userD, classReference, "interval")).thenReturn("daily");
101   
102    // Test with DAILY interval
103  1 NotificationUserIterator userIterator = mocker.getComponentUnderTest();
104  1 userIterator.initialize(NotificationEmailInterval.DAILY);
105   
106  1 assertTrue(userIterator.hasNext());
107  1 assertEquals(userB, userIterator.next());
108  1 assertTrue(userIterator.hasNext());
109  1 assertEquals(userC, userIterator.next());
110  1 assertTrue(userIterator.hasNext());
111  1 assertEquals(userD, userIterator.next());
112  1 assertFalse(userIterator.hasNext());
113   
114    // Test with WEEKLY interval
115  1 userIterator = mocker.getComponentUnderTest();
116  1 userIterator.initialize(NotificationEmailInterval.WEEKLY);
117  1 assertTrue(userIterator.hasNext());
118  1 assertEquals(userA, userIterator.next());
119  1 assertFalse(userIterator.hasNext());
120   
121    // Checks
122  1 verify(query1, atLeastOnce()).setLimit(50);
123  1 verify(query1, atLeastOnce()).setOffset(0);
124   
125  1 verify(query2, atLeastOnce()).setLimit(50);
126  1 verify(query2, atLeastOnce()).setOffset(50);
127   
128  1 verify(query3, atLeastOnce()).setLimit(50);
129  1 verify(query3, atLeastOnce()).setOffset(100);
130    }
131   
132    }