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

File DefaultParametrizedNotificationManagerTest.java

 

Code metrics

0
359
17
1
750
481
18
0.05
21.12
17
1.06

Classes

Class Line # Actions
DefaultParametrizedNotificationManagerTest 64 359 0% 18 0
1.0100%
 

Contributing tests

This file is covered by 14 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.sources.internal;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.Date;
25    import java.util.List;
26   
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.eventstream.Event;
31    import org.xwiki.eventstream.EventStream;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.notifications.CompositeEvent;
34    import org.xwiki.notifications.NotificationException;
35    import org.xwiki.notifications.NotificationFormat;
36    import org.xwiki.notifications.filters.NotificationFilter;
37    import org.xwiki.notifications.internal.SimilarityCalculator;
38    import org.xwiki.notifications.preferences.NotificationPreference;
39    import org.xwiki.notifications.preferences.NotificationPreferenceProperty;
40    import org.xwiki.notifications.sources.NotificationParameters;
41    import org.xwiki.query.Query;
42    import org.xwiki.query.QueryException;
43    import org.xwiki.security.authorization.AuthorizationManager;
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.assertTrue;
51    import static org.mockito.ArgumentMatchers.any;
52    import static org.mockito.ArgumentMatchers.anyCollection;
53    import static org.mockito.ArgumentMatchers.anyString;
54    import static org.mockito.ArgumentMatchers.eq;
55    import static org.mockito.ArgumentMatchers.isNull;
56    import static org.mockito.Mockito.mock;
57    import static org.mockito.Mockito.verifyZeroInteractions;
58    import static org.mockito.Mockito.when;
59   
60    /**
61    * @version $Id: 611b2ba3faa4ee03c5fe34bb9d8aa516505538e2 $
62    */
63    @ComponentList(SimilarityCalculator.class)
 
64    public class DefaultParametrizedNotificationManagerTest
65    {
66    @Rule
67    public final MockitoComponentMockingRule<DefaultParametrizedNotificationManager> mocker =
68    new MockitoComponentMockingRule<>(DefaultParametrizedNotificationManager.class);
69   
70    private EventStream eventStream;
71    private QueryGenerator queryGenerator;
72    private AuthorizationManager authorizationManager;
73    private RecordableEventDescriptorHelper recordableEventDescriptorHelper;
74   
75    private DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "UserA");
76    private Query query;
77    private Date startDate;
78   
 
79  14 toggle @Before
80    public void setUp() throws Exception
81    {
82  14 eventStream = mocker.getInstance(EventStream.class);
83  14 queryGenerator = mocker.getInstance(QueryGenerator.class);
84  14 authorizationManager = mocker.getInstance(AuthorizationManager.class);
85  14 startDate = new Date(10);
86  14 recordableEventDescriptorHelper = mocker.getInstance(RecordableEventDescriptorHelper.class);
87   
88  14 query = mock(Query.class);
89  14 when(queryGenerator.generateQuery(any(NotificationParameters.class))).thenReturn(query);
90   
91  14 NotificationPreference pref1 = mock(NotificationPreference.class);
92  14 when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
93  14 when(pref1.isNotificationEnabled()).thenReturn(true);
94   
95  14 when(recordableEventDescriptorHelper.hasDescriptor(anyString(), any(DocumentReference.class))).thenReturn(true);
96    }
97   
 
98  1 toggle @Test
99    public void getEventsWith2Queries() throws Exception
100    {
101    // Mocks
102  1 Event event1 = createMockedEvent();
103  1 Event event2 = createMockedEvent();
104  1 Event event3 = createMockedEvent();
105  1 Event event4 = createMockedEvent();
106  1 Event event5 = createMockedEvent();
107  1 Event event6 = createMockedEvent();
108   
109  1 DocumentReference doc1 = new DocumentReference("xwiki", "Main", "WebHome");
110  1 when(event1.getDocument()).thenReturn(doc1);
111  1 DocumentReference doc2 = new DocumentReference("xwiki", "PrivateSpace", "WebHome");
112  1 when(event2.getDocument()).thenReturn(doc2);
113  1 when(event3.getDocument()).thenReturn(doc2);
114  1 when(event4.getDocument()).thenReturn(doc2);
115   
116  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc1)).thenReturn(true);
117  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc2)).thenReturn(false);
118   
119  1 when(event1.getType()).thenReturn("type1");
120  1 when(event2.getType()).thenReturn("type2");
121  1 when(event3.getType()).thenReturn("type3");
122  1 when(event4.getType()).thenReturn("type4");
123  1 when(event5.getType()).thenReturn("type5");
124  1 when(event6.getType()).thenReturn("type6");
125   
126  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2, event3, event4),
127    Arrays.asList(event5, event6));
128   
129    // Test
130  1 NotificationParameters parameters = new NotificationParameters();
131  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
132  1 parameters.expectedCount = 2;
133  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
134   
135    // Verify
136  1 assertEquals(2, results.size());
137  1 assertEquals(event1, results.get(0).getEvents().get(0));
138  1 assertEquals(event5, results.get(1).getEvents().get(0));
139    }
140   
 
141  39 toggle private Event createMockedEvent()
142    {
143  39 Event event = mock(Event.class);
144  39 when(event.getDate()).thenReturn(new Date(1L));
145  39 return event;
146    }
147   
 
148  1 toggle @Test
149    public void getEventsWhenNoPreferences() throws Exception
150    {
151  1 NotificationPreference pref1 = mock(NotificationPreference.class);
152  1 when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
153  1 when(pref1.isNotificationEnabled()).thenReturn(false);
154   
155    // Test
156  1 NotificationParameters parameters = new NotificationParameters();
157  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
158  1 parameters.expectedCount = 2;
159  1 parameters.preferences = Arrays.asList(pref1);
160  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
161   
162    // Verify
163  1 assertEquals(0, results.size());
164    }
165   
 
166  1 toggle @Test
167    public void getEventsWhenException() throws Exception
168    {
169    // Mocks
170  1 QueryException exception = new QueryException("Error", null, null);
171  1 when(queryGenerator.generateQuery(any(NotificationParameters.class))).thenThrow(exception);
172   
173    // Test
174  1 NotificationException caughtException = null;
175  1 try {
176  1 NotificationParameters parameters = new NotificationParameters();
177  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
178  1 parameters.expectedCount = 2;
179  1 mocker.getComponentUnderTest().getEvents(parameters);
180    } catch (NotificationException e) {
181  1 caughtException = e;
182    }
183   
184    // Verify
185  1 assertNotNull(caughtException);
186  1 assertEquals("Fail to get the list of notifications.", caughtException.getMessage());
187  1 assertEquals(exception, caughtException.getCause());
188    }
189   
 
190  1 toggle @Test
191    public void getEventsCount() throws Exception
192    {
193    // Mocks
194  1 Event event1 = createMockedEvent();
195  1 Event event2 = createMockedEvent();
196  1 Event event3 = createMockedEvent();
197   
198  1 when(eventStream.searchEvents(query)).thenReturn(
199    Arrays.asList(event1, event2, event1, event2, event2, event2, event1, event2, event2, event2),
200    Arrays.asList(event1, event2, event2, event1, event3));
201   
202  1 when(recordableEventDescriptorHelper.hasDescriptor(isNull(), any(DocumentReference.class))).thenReturn(true);
203   
204    // Test
205  1 NotificationParameters parameters = new NotificationParameters();
206  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
207  1 parameters.expectedCount = 5;
208  1 long result = mocker.getComponentUnderTest().getEvents(parameters).size();
209   
210    // Verify
211  1 assertEquals(5, result);
212  1 verifyZeroInteractions(event3);
213    }
214   
 
215  1 toggle @Test
216    public void getEventsUC1() throws Exception
217    {
218    // Facts:
219    // * Alice updates the page "Bike"
220    // * Bob updates the page "Bike"
221   
222    // Expected:
223    // * Alice and Bob have updated the page "Bike"
224   
225    // Comment:
226    // Note: the 2 events have been combined
227   
228    // Mocks
229  1 Event eventAlice = createMockedEvent();
230  1 Event eventBob = createMockedEvent();
231   
232  1 DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
233  1 when(eventAlice.getDocument()).thenReturn(doc);
234  1 when(eventBob.getDocument()).thenReturn(doc);
235   
236  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
237   
238   
239  1 when(eventAlice.getType()).thenReturn("update");
240  1 when(eventBob.getType()).thenReturn("update");
241   
242   
243  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(eventAlice, eventBob));
244   
245    // Test
246  1 NotificationParameters parameters = new NotificationParameters();
247  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
248  1 parameters.expectedCount = 2;
249  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
250   
251    // Verify
252  1 assertEquals(1, results.size());
253  1 assertEquals(eventAlice, results.get(0).getEvents().get(0));
254  1 assertEquals(eventBob, results.get(0).getEvents().get(1));
255    }
256   
 
257  1 toggle @Test
258    public void getEventsUC2() throws Exception
259    {
260    // Facts:
261    // * Bob comments the page "Bike" (which actually update the page too)
262   
263    // Expected:
264    // * Bob has commented the page "Bike"
265   
266    // Comment: we do not mention that Bob has updated the page "Bike", because it's actually a technical
267    // implementation of the "comment" feature.
268   
269    // Mocks
270  1 Event eventComment = createMockedEvent();
271  1 Event eventUpdate = createMockedEvent();
272   
273  1 DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
274  1 when(eventComment.getDocument()).thenReturn(doc);
275  1 when(eventUpdate.getDocument()).thenReturn(doc);
276   
277  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
278   
279  1 when(eventComment.getType()).thenReturn("addComment");
280  1 when(eventUpdate.getType()).thenReturn("update");
281   
282  1 when(eventComment.getGroupId()).thenReturn("g1");
283  1 when(eventUpdate.getGroupId()).thenReturn("g1");
284   
285  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(eventComment, eventUpdate));
286   
287    // Test
288  1 NotificationParameters parameters = new NotificationParameters();
289  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
290  1 parameters.expectedCount = 2;
291  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
292   
293    // Verify
294  1 assertEquals(1, results.size());
295  1 assertEquals(eventComment, results.get(0).getEvents().get(0));
296  1 assertEquals(eventUpdate, results.get(0).getEvents().get(1));
297    }
298   
 
299  1 toggle @Test
300    public void getEventsUC3() throws Exception
301    {
302    // Facts:
303    // * Alice updates the page "Bike"
304    // * Bob comments the page "Bike"
305   
306    // Expected:
307    // * Alice has updated the page "Bike"
308    // * Bob has commented the page "Bike"
309   
310    // Comment: same as UC2 but we make sure we don't lose the event concerning Alice
311   
312    // Note: the UC4 described in https://jira.xwiki.org/browse/XWIKI-14114 is actually similar to that one
313    // because we don't care of the event' user in our tests.
314   
315    // Mocks
316  1 Event event1 = createMockedEvent();
317  1 Event event2 = createMockedEvent();
318  1 Event event3 = createMockedEvent();
319   
320  1 DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
321  1 when(event1.getDocument()).thenReturn(doc);
322  1 when(event2.getDocument()).thenReturn(doc);
323  1 when(event3.getDocument()).thenReturn(doc);
324   
325  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
326   
327  1 when(event1.getType()).thenReturn("update");
328  1 when(event2.getType()).thenReturn("addComment");
329  1 when(event3.getType()).thenReturn("update");
330   
331  1 when(event1.getGroupId()).thenReturn("g1");
332  1 when(event2.getGroupId()).thenReturn("g2");
333  1 when(event3.getGroupId()).thenReturn("g2");
334   
335  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2, event3));
336   
337    // Test
338  1 NotificationParameters parameters = new NotificationParameters();
339  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
340  1 parameters.expectedCount = 5;
341  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
342   
343    // Verify
344  1 assertEquals(2, results.size());
345  1 assertEquals(event1, results.get(0).getEvents().get(0));
346  1 assertEquals(event2, results.get(1).getEvents().get(0));
347  1 assertEquals(event3, results.get(1).getEvents().get(1));
348    }
349   
 
350  1 toggle @Test
351    public void getEventsUC5() throws Exception
352    {
353    // Facts:
354    // * Bob updates the page "Bike"
355    // * Then Bob updates the page "Bike" again
356   
357    // Expected:
358    // * Bob has updated the page "Bike"
359   
360    // Comment: we don't show 2 events, only one is interesting
361   
362    // Mocks
363  1 Event event1 = createMockedEvent();
364  1 Event event2 = createMockedEvent();
365   
366  1 DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
367  1 when(event1.getDocument()).thenReturn(doc);
368  1 when(event2.getDocument()).thenReturn(doc);
369   
370  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
371   
372  1 when(event1.getType()).thenReturn("update");
373  1 when(event2.getType()).thenReturn("update");
374   
375  1 when(event1.getGroupId()).thenReturn("g1");
376  1 when(event2.getGroupId()).thenReturn("g2");
377   
378  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2));
379   
380    // Test
381  1 NotificationParameters parameters = new NotificationParameters();
382  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
383  1 parameters.expectedCount = 5;
384  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
385   
386    // Verify
387  1 assertEquals(1, results.size());
388  1 assertEquals(event1, results.get(0).getEvents().get(0));
389  1 assertEquals(event2, results.get(0).getEvents().get(1));
390    }
391   
 
392  1 toggle @Test
393    public void getEventsUC6() throws Exception
394    {
395    // Facts:
396    // * Bob updates the page "Bike" (E1)
397    // * Alice updates the page "Bike" (E2)
398    // * Bob comments the page "Bike" (E3 & E4)
399    // * Carol comments the page "Bike" (E5 & E6)
400    // * Dave comments the page "Guitar" (E7 & E8)
401    // * Bob adds an annotation on page "Bike" (E9 & E10)
402    // * Alice adds an annotation on page "Bike" (E11 & E12)
403    // * Alice adds an other annotation on page "Bike" (E12 & E13)
404   
405   
406    // Expected:
407    // * Bob and Alice have updated the page "Bike"
408    // * Bob and Carol have commented the page "Bike"
409    // * Dave has commented the page "Guitar"
410    // * Bob and Alice have annotated the page "Bike"
411   
412    // Comment: it's only a mix of other use cases to make sure we have the expected results.
413   
414    // Mocks
415  1 DocumentReference doc1 = new DocumentReference("xwiki", "Main", "Bike");
416  1 DocumentReference doc2 = new DocumentReference("xwiki", "Main", "Guitar");
417  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc1)).thenReturn(true);
418  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc2)).thenReturn(true);
419   
420    // * Bob updates the page "Bike" (E1)
421  1 Event event1 = createMockedEvent(); when(event1.toString()).thenReturn("event1");
422  1 when(event1.getDocument()).thenReturn(doc1);
423  1 when(event1.getType()).thenReturn("update");
424  1 when(event1.getGroupId()).thenReturn("g1");
425   
426    // * Alice updates the page "Bike" (E2)
427  1 Event event2 = createMockedEvent(); when(event2.toString()).thenReturn("event2");
428  1 when(event2.getDocument()).thenReturn(doc1);
429  1 when(event2.getType()).thenReturn("update");
430  1 when(event2.getGroupId()).thenReturn("g2");
431   
432    // * Bob comments the page "Bike" (E3 & E4)
433  1 Event event3 = createMockedEvent(); when(event3.toString()).thenReturn("event3");
434  1 when(event3.getDocument()).thenReturn(doc1);
435  1 when(event3.getType()).thenReturn("addComment");
436  1 when(event3.getGroupId()).thenReturn("g3");
437  1 Event event4 = createMockedEvent(); when(event4.toString()).thenReturn("event4");
438  1 when(event4.getDocument()).thenReturn(doc1);
439  1 when(event4.getType()).thenReturn("update");
440  1 when(event4.getGroupId()).thenReturn("g3");
441   
442    // * Carol comments the page "Bike" (E5 & E6)
443    // (note: we put the "update" event before the "addComment", because we can not guarantee the order so
444    // it's good to test both)
445  1 Event event5 = createMockedEvent(); when(event5.toString()).thenReturn("event5");
446  1 when(event5.getDocument()).thenReturn(doc1);
447  1 when(event5.getType()).thenReturn("update");
448  1 when(event5.getGroupId()).thenReturn("g5");
449  1 Event event6 = createMockedEvent(); when(event6.toString()).thenReturn("event6");
450  1 when(event6.getDocument()).thenReturn(doc1);
451  1 when(event6.getType()).thenReturn("addComment");
452  1 when(event6.getGroupId()).thenReturn("g5");
453   
454    // * Dave comments the page "Guitar" (E7 & E8)
455  1 Event event7 = createMockedEvent(); when(event7.toString()).thenReturn("event7");
456  1 when(event7.getDocument()).thenReturn(doc2);
457  1 when(event7.getType()).thenReturn("update");
458  1 when(event7.getGroupId()).thenReturn("g7");
459  1 Event event8 = createMockedEvent(); when(event8.toString()).thenReturn("event8");
460  1 when(event8.getDocument()).thenReturn(doc2);
461  1 when(event8.getType()).thenReturn("addComment");
462  1 when(event8.getGroupId()).thenReturn("g7");
463   
464    // * Bob adds an annotation on page "Bike" (E9 & E10)
465  1 Event event9 = createMockedEvent(); when(event8.toString()).thenReturn("event9");
466  1 when(event9.getDocument()).thenReturn(doc1);
467  1 when(event9.getType()).thenReturn("update");
468  1 when(event9.getGroupId()).thenReturn("g9");
469  1 Event event10 = createMockedEvent(); when(event8.toString()).thenReturn("event10");
470  1 when(event10.getDocument()).thenReturn(doc1);
471  1 when(event10.getType()).thenReturn("addAnnotation");
472  1 when(event10.getGroupId()).thenReturn("g9");
473   
474    // * Alice adds an annotation on page "Bike" (E11 & E12)
475  1 Event event11 = createMockedEvent(); when(event8.toString()).thenReturn("event11");
476  1 when(event11.getDocument()).thenReturn(doc1);
477  1 when(event11.getType()).thenReturn("update");
478  1 when(event11.getGroupId()).thenReturn("g11");
479  1 Event event12 = createMockedEvent(); when(event8.toString()).thenReturn("event12");
480  1 when(event12.getDocument()).thenReturn(doc1);
481  1 when(event12.getType()).thenReturn("addAnnotation");
482  1 when(event12.getGroupId()).thenReturn("g11");
483   
484    // * Alice adds an other annotation on page "Bike" (E12 & E13)
485  1 Event event13 = createMockedEvent(); when(event8.toString()).thenReturn("event11");
486  1 when(event13.getDocument()).thenReturn(doc1);
487  1 when(event13.getType()).thenReturn("addAnnotation");
488  1 when(event13.getGroupId()).thenReturn("g13");
489  1 Event event14 = createMockedEvent(); when(event8.toString()).thenReturn("event12");
490  1 when(event14.getDocument()).thenReturn(doc1);
491  1 when(event14.getType()).thenReturn("update");
492  1 when(event14.getGroupId()).thenReturn("g13");
493   
494  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2, event3, event4, event5, event6,
495    event7, event8, event9, event10, event11, event12, event13, event14));
496   
497    // Test
498  1 NotificationParameters parameters = new NotificationParameters();
499  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
500  1 parameters.expectedCount = 50;
501  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
502   
503    // Verify
504  1 assertEquals(4, results.size());
505   
506    // * Bob and Alice have updated the page "Bike"
507  1 assertTrue(results.get(0).getEvents().contains(event1));
508  1 assertTrue(results.get(0).getEvents().contains(event2));
509   
510    // * Bob and Carol have commented the page "Bike"
511  1 assertTrue(results.get(1).getEvents().contains(event3));
512  1 assertTrue(results.get(1).getEvents().contains(event4));
513  1 assertTrue(results.get(1).getEvents().contains(event5));
514  1 assertTrue(results.get(1).getEvents().contains(event6));
515   
516    // * Dave has commented the page "Guitar"
517  1 assertTrue(results.get(2).getEvents().contains(event7));
518  1 assertTrue(results.get(2).getEvents().contains(event8));
519   
520    // * Bob and Alice have annotated the page "Bike"
521  1 assertTrue(results.get(3).getEvents().contains(event9));
522  1 assertTrue(results.get(3).getEvents().contains(event10));
523  1 assertTrue(results.get(3).getEvents().contains(event11));
524  1 assertTrue(results.get(3).getEvents().contains(event12));
525  1 assertTrue(results.get(3).getEvents().contains(event13));
526  1 assertTrue(results.get(3).getEvents().contains(event14));
527    }
528   
 
529  1 toggle @Test
530    public void getEvents1Update2Events() throws Exception
531    {
532    // Facts:
533    // * Bob comment and annotate the page "Bike" in the same time
534   
535    // Expected:
536    // * Bob has commented the page "Bike"
537    // * Bob has annotated the page "Bike"
538   
539    // Mocks
540  1 Event event1 = createMockedEvent(); when(event1.toString()).thenReturn("event1");
541  1 Event event2 = createMockedEvent(); when(event1.toString()).thenReturn("event2");
542  1 Event event3 = createMockedEvent(); when(event1.toString()).thenReturn("event3");
543   
544  1 DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
545  1 when(event1.getDocument()).thenReturn(doc);
546  1 when(event2.getDocument()).thenReturn(doc);
547  1 when(event3.getDocument()).thenReturn(doc);
548   
549  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
550   
551  1 when(event1.getType()).thenReturn("update");
552  1 when(event2.getType()).thenReturn("addComment");
553  1 when(event3.getType()).thenReturn("addAnnotation");
554   
555  1 when(event1.getGroupId()).thenReturn("g1");
556  1 when(event2.getGroupId()).thenReturn("g1");
557  1 when(event3.getGroupId()).thenReturn("g1");
558   
559  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2, event3));
560   
561    // Test
562  1 NotificationParameters parameters = new NotificationParameters();
563  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
564  1 parameters.expectedCount = 50;
565  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
566   
567    // Verify
568  1 assertEquals(1, results.size());
569  1 assertTrue(results.get(0).getEvents().contains(event1));
570  1 assertTrue(results.get(0).getEvents().contains(event2));
571  1 assertTrue(results.get(0).getEvents().contains(event3));
572    }
573   
 
574  1 toggle @Test
575    public void getEventsXWIKI14454() throws Exception
576    {
577    // Facts:
578    // * Then Bob updates the page "Bike"
579    // * Then Bob updates the page "Bike" again
580    // * Then bob add a comment to the "Bike" page
581   
582    // Expected:
583    // * Bob has commented the page "Bike"
584    // * Bob has updated the page "Bike"
585   
586    // Mocks
587  1 Event eventUpdate1 = createMockedEvent();
588  1 Event eventUpdate2 = createMockedEvent();
589  1 Event eventAddComment = createMockedEvent();
590  1 Event eventAddCommentUpdate = createMockedEvent();
591   
592  1 DocumentReference doc = new DocumentReference("xwiki", "Main", "Bike");
593  1 when(eventUpdate1.getDocument()).thenReturn(doc); when(eventUpdate1.toString()).thenReturn("update1");
594  1 when(eventUpdate2.getDocument()).thenReturn(doc); when(eventUpdate2.toString()).thenReturn("update2");
595  1 when(eventAddComment.getDocument()).thenReturn(doc); when(eventAddComment.toString()).thenReturn("addComment");
596  1 when(eventAddCommentUpdate.getDocument()).thenReturn(doc); when(eventAddCommentUpdate.toString()).thenReturn("updateComment");
597   
598  1 when(authorizationManager.hasAccess(Right.VIEW, userReference, doc)).thenReturn(true);
599   
600  1 when(eventUpdate1.getType()).thenReturn("update");
601  1 when(eventUpdate2.getType()).thenReturn("update");
602  1 when(eventAddComment.getType()).thenReturn("addComment");
603  1 when(eventAddCommentUpdate.getType()).thenReturn("update");
604   
605  1 when(eventUpdate1.getGroupId()).thenReturn("g1");
606  1 when(eventUpdate2.getGroupId()).thenReturn("g2");
607  1 when(eventAddComment.getGroupId()).thenReturn("g3");
608  1 when(eventAddCommentUpdate.getGroupId()).thenReturn("g3");
609   
610    // They comes with inverse chronological order because of the query
611  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(eventAddComment, eventAddCommentUpdate,
612    eventUpdate2, eventUpdate1));
613   
614    // Test
615  1 NotificationParameters parameters = new NotificationParameters();
616  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
617  1 parameters.expectedCount = 5;
618  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
619   
620    // Verify
621  1 assertEquals(2, results.size());
622    }
623   
 
624  14 toggle private Event createMockedEvent(String type, DocumentReference user, DocumentReference doc, Date date,
625    String groupId)
626    {
627  14 Event event = mock(Event.class);
628  14 when(event.getDate()).thenReturn(date);
629  14 when(event.getDocument()).thenReturn(doc);
630  14 when(event.getUser()).thenReturn(user);
631  14 when(event.getType()).thenReturn(type);
632  14 when(event.getGroupId()).thenReturn(groupId);
633   
634  14 when(event.toString()).thenReturn(String.format("[%s] Event [%s] on document [%s] by [%s] on [%s]",
635    groupId, type, doc, user, date.toString()));
636   
637  14 return event;
638    }
639   
 
640  1 toggle @Test
641    public void getEventsXWIKI14719() throws Exception
642    {
643  1 DocumentReference userA = new DocumentReference("xwiki", "XWiki", "UserA");
644   
645    // Example taken from a real case
646  1 Event event0 = createMockedEvent("update", userA, userA, new Date(1510567729000L),
647    "1997830249-1510567729000-Puhs4MSa");
648  1 Event event1 = createMockedEvent("update", userA, userA, new Date(1510567724000L),
649    "1997830249-1510567724000-aCjmsmSh");
650  1 Event event2 = createMockedEvent("update", userA, userA, new Date(1510567718000L),
651    "1997830249-1510567718000-hEErMBp9");
652  1 Event event3 = createMockedEvent("update", userA, userA, new Date(1510567717000L),
653    "1997830249-1510567718000-hEErMBp9");
654  1 Event event4 = createMockedEvent("update", userA, userA, new Date(1510567715000L),
655    "1997830249-1510567715000-B723WWBC");
656  1 Event event5 = createMockedEvent("update", userA, userA, new Date(1510567715000L),
657    "1997830249-1510567715000-B723WWBC");
658  1 Event event6 = createMockedEvent("update", userA, userA, new Date(1510567714000L),
659    "1997830249-1510567714000-SHPmruCG");
660  1 Event event7 = createMockedEvent("update", userA, userA, new Date(1510567712000L),
661    "1997830249-1510567712000-Fy19J0v1");
662  1 Event event8 = createMockedEvent("update", userA, userA, new Date(1510567711000L),
663    "1997830249-1510567711000-zDfFnZbD");
664  1 Event event9 = createMockedEvent("update", userA, userA, new Date(1510567711000L),
665    "1997830249-1510567711000-zDfFnZbD");
666   
667  1 when(authorizationManager.hasAccess(eq(Right.VIEW), eq(userReference), any(DocumentReference.class)))
668    .thenReturn(true);
669  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event0, event1, event2, event3,
670    event4, event5, event6, event7, event8, event9), Collections.emptyList());
671   
672    // Test
673  1 NotificationParameters parameters = new NotificationParameters();
674  1 parameters.user = new DocumentReference("xwiki", "XWiki", "UserA");
675  1 parameters.expectedCount = 5;
676  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
677   
678  1 assertEquals(1, results.size());
679   
680    }
681   
 
682  1 toggle @Test
683    public void getEventsXWIKI15151() throws Exception
684    {
685  1 DocumentReference userA = new DocumentReference("xwiki", "XWiki", "UserA");
686   
687    // Example taken from a real case
688  1 Event event1 = createMockedEvent("update", userA, userA, new Date(1510567729000L), "id1");
689  1 Event event2 = createMockedEvent("update", userA, userA, new Date(1510567729000L), "id2");
690   
691  1 when(authorizationManager.hasAccess(eq(Right.VIEW), eq(userReference), any(DocumentReference.class)))
692    .thenReturn(true);
693  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2), Collections.emptyList());
694   
695    // Test
696  1 NotificationParameters parameters = new NotificationParameters();
697  1 parameters.user = userA;
698  1 parameters.expectedCount = 5;
699  1 parameters.format = NotificationFormat.ALERT;
700   
701    // Filter 1
702  1 NotificationFilter filter1 = mock(NotificationFilter.class);
703  1 when(filter1.getPriority()).thenReturn(1);
704  1 when(filter1.filterEvent(eq(event1), any(DocumentReference.class), anyCollection(),
705    any(NotificationFormat.class))).thenReturn(NotificationFilter.FilterPolicy.FILTER);
706  1 when(filter1.filterEvent(eq(event2), any(DocumentReference.class), anyCollection(),
707    any(NotificationFormat.class))).thenReturn(NotificationFilter.FilterPolicy.KEEP);
708  1 NotificationFilter filter2 = mock(NotificationFilter.class);
709  1 when(filter2.getPriority()).thenReturn(2);
710  1 when(filter2.filterEvent(eq(event1), any(DocumentReference.class), anyCollection(),
711    any(NotificationFormat.class))).thenReturn(NotificationFilter.FilterPolicy.KEEP);
712  1 when(filter2.filterEvent(eq(event2), any(DocumentReference.class), anyCollection(),
713    any(NotificationFormat.class))).thenReturn(NotificationFilter.FilterPolicy.FILTER);
714  1 parameters.filters = Arrays.asList(filter1, filter2);
715  1 when(filter1.compareTo(filter2)).thenReturn(1);
716  1 when(filter2.compareTo(filter1)).thenReturn(-1);
717   
718  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
719   
720  1 assertEquals(1, results.size());
721  1 assertEquals(event1, results.get(0).getEvents().get(0));
722    }
723   
 
724  1 toggle @Test
725    public void getEventsThatHaveNoDescriptor() throws Exception
726    {
727  1 DocumentReference userA = new DocumentReference("xwiki", "XWiki", "UserA");
728   
729    // Example taken from a real case
730  1 Event event1 = createMockedEvent("customThing", userA, userA, new Date(1510567729000L), "id1");
731  1 Event event2 = createMockedEvent("update", userA, userA, new Date(1510567729000L), "id2");
732   
733  1 when(authorizationManager.hasAccess(eq(Right.VIEW), eq(userReference), any(DocumentReference.class)))
734    .thenReturn(true);
735  1 when(eventStream.searchEvents(query)).thenReturn(Arrays.asList(event1, event2), Collections.emptyList());
736   
737  1 when(recordableEventDescriptorHelper.hasDescriptor(eq("customThing"), eq(userA))).thenReturn(false);
738   
739    // Test
740  1 NotificationParameters parameters = new NotificationParameters();
741  1 parameters.user = userA;
742  1 parameters.expectedCount = 5;
743  1 parameters.format = NotificationFormat.ALERT;
744   
745  1 List<CompositeEvent> results = mocker.getComponentUnderTest().getEvents(parameters);
746   
747  1 assertEquals(1, results.size());
748  1 assertEquals(event2, results.get(0).getEvents().get(0));
749    }
750    }