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

File DefaultWatchedEntitiesManagerTest.java

 

Code metrics

0
135
9
1
317
197
9
0.07
15
9
1

Classes

Class Line # Actions
DefaultWatchedEntitiesManagerTest 45 135 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 8 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.notifications.filters.watch.internal;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.mockito.internal.util.collections.Sets;
26    import org.xwiki.model.reference.DocumentReference;
27    import org.xwiki.notifications.NotificationFormat;
28    import org.xwiki.notifications.filters.NotificationFilterPreferenceManager;
29    import org.xwiki.notifications.filters.NotificationFilterType;
30    import org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference;
31    import org.xwiki.notifications.filters.watch.WatchedEntityReference;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33   
34    import java.util.Set;
35   
36    import static org.junit.Assert.assertEquals;
37    import static org.mockito.ArgumentMatchers.anySet;
38    import static org.mockito.ArgumentMatchers.eq;
39    import static org.mockito.Mockito.*;
40   
41    /**
42    * @version $Id: d61d0c74845eb6464b1de07b8dc6bceed5ed28ad $
43    * @since 9.9RC1
44    */
 
45    public class DefaultWatchedEntitiesManagerTest
46    {
47    @Rule
48    public final MockitoComponentMockingRule<DefaultWatchedEntitiesManager> mocker =
49    new MockitoComponentMockingRule<>(DefaultWatchedEntitiesManager.class);
50   
51    private NotificationFilterPreferenceManager notificationFilterPreferenceManager;
52   
 
53  8 toggle @Before
54    public void setUp() throws Exception
55    {
56  8 notificationFilterPreferenceManager = mocker.getInstance(NotificationFilterPreferenceManager.class);
57    }
58   
 
59  1 toggle @Test
60    public void testWithSeveralFilterPreferences() throws Exception
61    {
62    // Mocks
63  1 WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
64  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
65   
66    // Filters
67  1 DefaultNotificationFilterPreference pref1 = new DefaultNotificationFilterPreference();
68  1 pref1.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
69   
70  1 DefaultNotificationFilterPreference pref2 = new DefaultNotificationFilterPreference();
71  1 pref2.setEventTypes(Sets.newSet("update"));
72  1 pref2.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
73  1 when(watchedEntityReference.matchExactly(pref2)).thenReturn(true);
74   
75  1 DefaultNotificationFilterPreference pref3 = new DefaultNotificationFilterPreference();
76  1 pref3.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT));
77  1 when(watchedEntityReference.matchExactly(pref3)).thenReturn(true);
78   
79  1 DefaultNotificationFilterPreference pref4 = new DefaultNotificationFilterPreference();
80  1 pref4.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
81  1 when(watchedEntityReference.matchExactly(pref4)).thenReturn(true);
82  1 pref4.setFilterType(NotificationFilterType.INCLUSIVE);
83  1 pref4.setEnabled(false);
84  1 pref4.setId("pref4");
85   
86  1 when(notificationFilterPreferenceManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1, pref2, pref3, pref4));
87   
88  1 when(watchedEntityReference.isWatched(user)).thenReturn(false, true);
89   
90    // Test
91  1 mocker.getComponentUnderTest().watchEntity(watchedEntityReference, user);
92   
93    // Checks
94  1 verify(watchedEntityReference, never()).matchExactly(pref2);
95  1 verify(watchedEntityReference, never()).matchExactly(pref3);
96  1 verify(notificationFilterPreferenceManager).setFilterPreferenceEnabled(user, "pref4", true);
97  1 verify(watchedEntityReference, never()).createInclusiveFilterPreference();
98    }
99   
 
100  1 toggle @Test
101    public void watchWhenExclusiveFilter() throws Exception
102    {
103    // Mocks
104  1 WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
105  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
106   
107    // Filters
108  1 DefaultNotificationFilterPreference pref1 = new DefaultNotificationFilterPreference();
109  1 pref1.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
110  1 when(watchedEntityReference.matchExactly(pref1)).thenReturn(true);
111  1 pref1.setFilterType(NotificationFilterType.EXCLUSIVE);
112  1 pref1.setEnabled(true);
113  1 pref1.setId("pref1");
114   
115  1 when(notificationFilterPreferenceManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1));
116   
117  1 when(watchedEntityReference.isWatched(user)).thenReturn(false, true);
118   
119    // Test
120  1 mocker.getComponentUnderTest().watchEntity(watchedEntityReference, user);
121   
122    // Checks
123  1 verify(notificationFilterPreferenceManager).deleteFilterPreference(user, "pref1");
124  1 verify(watchedEntityReference, never()).createInclusiveFilterPreference();
125    }
126   
 
127  1 toggle @Test
128    public void watchWhen2OppositeFilters() throws Exception
129    {
130    // Mocks
131  1 WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
132  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
133   
134    // Filters
135  1 DefaultNotificationFilterPreference pref1 = new DefaultNotificationFilterPreference();
136  1 pref1.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
137  1 when(watchedEntityReference.matchExactly(pref1)).thenReturn(true);
138  1 pref1.setFilterType(NotificationFilterType.INCLUSIVE);
139  1 pref1.setEnabled(true);
140  1 pref1.setId("pref1");
141  1 DefaultNotificationFilterPreference pref2 = new DefaultNotificationFilterPreference();
142  1 pref2.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
143  1 when(watchedEntityReference.matchExactly(pref2)).thenReturn(true);
144  1 pref2.setFilterType(NotificationFilterType.EXCLUSIVE);
145  1 pref2.setEnabled(true);
146  1 pref2.setId("pref2");
147   
148  1 when(notificationFilterPreferenceManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1, pref2));
149   
150  1 when(watchedEntityReference.isWatched(user)).thenReturn(false, true);
151   
152    // Test
153  1 mocker.getComponentUnderTest().watchEntity(watchedEntityReference, user);
154   
155    // Checks
156  1 verify(notificationFilterPreferenceManager).deleteFilterPreference(user, "pref2");
157  1 verify(watchedEntityReference, never()).createInclusiveFilterPreference();
158    }
159   
 
160  1 toggle @Test
161    public void watchWhenNoFilterMatch() throws Exception
162    {
163    // Mocks
164  1 WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
165  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
166   
167    // Filters
168  1 DefaultNotificationFilterPreference pref1 = new DefaultNotificationFilterPreference();
169  1 pref1.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
170   
171  1 when(notificationFilterPreferenceManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1));
172   
173  1 when(watchedEntityReference.isWatched(user)).thenReturn(false);
174   
175  1 DefaultNotificationFilterPreference createdPref = mock(DefaultNotificationFilterPreference.class);
176  1 when(watchedEntityReference.createInclusiveFilterPreference()).thenReturn(createdPref);
177   
178  1 when(watchedEntityReference.createExclusiveFilterPreference()).thenReturn(createdPref);
179  1 when(createdPref.getId()).thenReturn("aabbccdd");
180   
181  1 doAnswer(invocationOnMock -> {
182  1 Set set = (Set) invocationOnMock.getArgument(1);
183  1 DefaultNotificationFilterPreference prefToSave = (DefaultNotificationFilterPreference) set.toArray()[0];
184  1 assertEquals("aabbccdd", prefToSave.getId());
185  1 return null;
186    }).when(notificationFilterPreferenceManager).saveFilterPreferences(eq(user), anySet());
187   
188    // Test
189  1 mocker.getComponentUnderTest().watchEntity(watchedEntityReference, user);
190   
191    // Checks
192  1 verify(notificationFilterPreferenceManager).saveFilterPreferences(eq(user), anySet());
193    }
194   
 
195  1 toggle @Test
196    public void unwatchWhenInclusiveFilter() throws Exception
197    {
198    // Mocks
199  1 WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
200  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
201   
202    // Filters
203  1 DefaultNotificationFilterPreference pref1 = new DefaultNotificationFilterPreference();
204  1 pref1.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
205  1 when(watchedEntityReference.matchExactly(pref1)).thenReturn(true);
206  1 pref1.setFilterType(NotificationFilterType.INCLUSIVE);
207  1 pref1.setEnabled(true);
208  1 pref1.setId("pref1");
209   
210  1 when(notificationFilterPreferenceManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1));
211   
212  1 when(watchedEntityReference.isWatched(user)).thenReturn(true, false);
213   
214    // Test
215  1 mocker.getComponentUnderTest().unwatchEntity(watchedEntityReference, user);
216   
217    // Checks
218  1 verify(notificationFilterPreferenceManager).deleteFilterPreference(user, "pref1");
219  1 verify(watchedEntityReference, never()).createExclusiveFilterPreference();
220    }
221   
 
222  1 toggle @Test
223    public void unwatchWhenExclusiveFilter() throws Exception
224    {
225    // Mocks
226  1 WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
227  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
228   
229    // Filters
230  1 DefaultNotificationFilterPreference pref1 = new DefaultNotificationFilterPreference();
231  1 pref1.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
232  1 when(watchedEntityReference.matchExactly(pref1)).thenReturn(true);
233  1 pref1.setFilterType(NotificationFilterType.EXCLUSIVE);
234  1 pref1.setEnabled(false);
235  1 pref1.setId("pref1");
236   
237  1 when(notificationFilterPreferenceManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1));
238   
239  1 when(watchedEntityReference.isWatched(user)).thenReturn(true, false);
240   
241    // Test
242  1 mocker.getComponentUnderTest().unwatchEntity(watchedEntityReference, user);
243   
244    // Checks
245  1 verify(notificationFilterPreferenceManager).setFilterPreferenceEnabled(user, "pref1", true);
246  1 verify(watchedEntityReference, never()).createExclusiveFilterPreference();
247    }
248   
 
249  1 toggle @Test
250    public void unwatchWhen2OppositeFilters() throws Exception
251    {
252    // Mocks
253  1 WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
254  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
255   
256    // Filters
257  1 DefaultNotificationFilterPreference pref1 = new DefaultNotificationFilterPreference();
258  1 pref1.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
259  1 when(watchedEntityReference.matchExactly(pref1)).thenReturn(true);
260  1 pref1.setFilterType(NotificationFilterType.INCLUSIVE);
261  1 pref1.setEnabled(true);
262  1 pref1.setId("pref1");
263  1 DefaultNotificationFilterPreference pref2 = new DefaultNotificationFilterPreference();
264   
265  1 pref2.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
266  1 when(watchedEntityReference.matchExactly(pref2)).thenReturn(true);
267  1 pref2.setFilterType(NotificationFilterType.EXCLUSIVE);
268  1 pref2.setEnabled(true);
269  1 pref2.setId("pref2");
270   
271  1 when(notificationFilterPreferenceManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1, pref2));
272   
273  1 when(watchedEntityReference.isWatched(user)).thenReturn(true, false);
274   
275    // Test
276  1 mocker.getComponentUnderTest().unwatchEntity(watchedEntityReference, user);
277   
278    // Checks
279  1 verify(notificationFilterPreferenceManager).deleteFilterPreference(user, "pref1");
280  1 verify(watchedEntityReference, never()).createExclusiveFilterPreference();
281    }
282   
 
283  1 toggle @Test
284    public void unwatchWhenNoFilterMatch() throws Exception
285    {
286    // Mocks
287  1 WatchedEntityReference watchedEntityReference = mock(WatchedEntityReference.class);
288  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
289   
290    // Filters
291  1 DefaultNotificationFilterPreference pref1 = new DefaultNotificationFilterPreference();
292  1 pref1.setNotificationFormats(Sets.newSet(NotificationFormat.ALERT, NotificationFormat.EMAIL));
293   
294  1 when(notificationFilterPreferenceManager.getFilterPreferences(user)).thenReturn(Sets.newSet(pref1));
295   
296  1 when(watchedEntityReference.isWatched(user)).thenReturn(true);
297   
298  1 DefaultNotificationFilterPreference createdPref = mock(DefaultNotificationFilterPreference.class);
299  1 when(watchedEntityReference.createExclusiveFilterPreference()).thenReturn(createdPref);
300  1 when(createdPref.getId()).thenReturn("aabbccdd");
301   
302  1 doAnswer(invocationOnMock -> {
303  1 Set set = (Set) invocationOnMock.getArgument(1);
304  1 DefaultNotificationFilterPreference prefToSave = (DefaultNotificationFilterPreference) set.toArray()[0];
305  1 assertEquals("aabbccdd", prefToSave.getId());
306  1 return null;
307    }).when(notificationFilterPreferenceManager).saveFilterPreferences(eq(user), anySet());
308   
309    // Test
310  1 mocker.getComponentUnderTest().unwatchEntity(watchedEntityReference, user);
311   
312    // Checks
313  1 verify(notificationFilterPreferenceManager).saveFilterPreferences(eq(user), anySet());
314    }
315   
316   
317    }