1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.messagestream

File MessageStreamTest.java

 

Code metrics

0
180
45
1
461
394
45
0.25
4
45
1

Classes

Class Line # Actions
MessageStreamTest 55 180 0% 45 0
1.0100%
 

Contributing tests

This file is covered by 28 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.messagestream;
21   
22    import java.util.List;
23    import java.util.UUID;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.jmock.Expectations;
27    import org.junit.Assert;
28    import org.junit.Before;
29    import org.junit.Test;
30    import org.xwiki.bridge.DocumentAccessBridge;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.eventstream.Event;
33    import org.xwiki.eventstream.Event.Importance;
34    import org.xwiki.eventstream.EventFactory;
35    import org.xwiki.eventstream.EventStream;
36    import org.xwiki.eventstream.internal.DefaultEvent;
37    import org.xwiki.messagestream.internal.DefaultMessageStream;
38    import org.xwiki.model.ModelContext;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.model.reference.EntityReferenceSerializer;
41    import org.xwiki.model.reference.ObjectReference;
42    import org.xwiki.query.Query;
43    import org.xwiki.query.QueryException;
44    import org.xwiki.query.QueryManager;
45    import org.xwiki.test.jmock.AbstractMockingComponentTestCase;
46    import org.xwiki.test.jmock.annotation.MockingRequirement;
47   
48    /**
49    * Tests for the {@link org.xwiki.userstatus.internal.DefaultEvent default event} and
50    * {@link org.xwiki.messagestream.internal.DefaultMessageStream default event factory}.
51    *
52    * @version $Id: 70dbbe5e3100711c28cd706c28cf4060147441a7 $
53    */
54    @MockingRequirement(DefaultMessageStream.class)
 
55    public class MessageStreamTest extends AbstractMockingComponentTestCase<MessageStream>
56    {
57    private MessageStream stream;
58   
59    private final DocumentReference currentUser = new DocumentReference("wiki", "XWiki", "JohnDoe");
60   
61    private final DocumentReference targetUser = new DocumentReference("wiki", "XWiki", "JaneBuck");
62   
63    private final DocumentReference targetGroup = new DocumentReference("wiki", "XWiki", "MyFriends");
64   
 
65  28 toggle @Before
66    public void configure() throws Exception
67    {
68  28 this.stream = getComponentManager().getInstance(MessageStream.class);
69    }
70   
 
71  1 toggle @Test
72    public void testPostPublicMessage() throws Exception
73    {
74  1 Event postedMessage = setupForPublicMessage();
75  1 this.stream.postPublicMessage("Hello World!");
76  1 Assert.assertEquals("Hello World!", postedMessage.getBody());
77  1 Assert.assertEquals(Importance.MINOR, postedMessage.getImportance());
78  1 Assert.assertEquals("publicMessage", postedMessage.getType());
79  1 Assert.assertEquals(this.currentUser, postedMessage.getRelatedEntity());
80    }
81   
 
82  1 toggle @Test
83    public void testPostPublicMessageWithNullMessage() throws Exception
84    {
85  1 Event postedMessage = setupForPublicMessage();
86  1 this.stream.postPublicMessage(null);
87  1 Assert.assertEquals(null, postedMessage.getBody());
88    }
89   
 
90  1 toggle @Test
91    public void testPostPublicMessageWithEmptyMessage() throws Exception
92    {
93  1 Event postedMessage = setupForPublicMessage();
94  1 this.stream.postPublicMessage("");
95  1 Assert.assertEquals("", postedMessage.getBody());
96    }
97   
 
98  1 toggle @Test
99    public void testPostPublicMessageWithLongMessage() throws Exception
100    {
101  1 Event postedMessage = setupForPublicMessage();
102  1 this.stream.postPublicMessage(StringUtils.repeat('a', 10000));
103  1 Assert.assertEquals(StringUtils.repeat('a', 2000), postedMessage.getBody());
104    }
105   
 
106  1 toggle @Test
107    public void testPostPersonalMessage() throws Exception
108    {
109  1 Event postedMessage = setupForPersonalMessage();
110  1 this.stream.postPersonalMessage("Hello World!");
111  1 Assert.assertEquals("Hello World!", postedMessage.getBody());
112  1 Assert.assertEquals(Importance.MEDIUM, postedMessage.getImportance());
113  1 Assert.assertEquals("personalMessage", postedMessage.getType());
114  1 Assert.assertEquals(this.currentUser, postedMessage.getRelatedEntity());
115    }
116   
 
117  1 toggle @Test
118    public void testPostPersonalMessageWithNullMessage() throws Exception
119    {
120  1 Event postedMessage = setupForPersonalMessage();
121  1 this.stream.postPersonalMessage(null);
122  1 Assert.assertEquals(null, postedMessage.getBody());
123    }
124   
 
125  1 toggle @Test
126    public void testPostPersonalMessageWithEmptyMessage() throws Exception
127    {
128  1 Event postedMessage = setupForPersonalMessage();
129  1 this.stream.postPersonalMessage("");
130  1 Assert.assertEquals("", postedMessage.getBody());
131    }
132   
 
133  1 toggle @Test
134    public void testPostPersonalMessageWithLongMessage() throws Exception
135    {
136  1 Event postedMessage = setupForPersonalMessage();
137  1 this.stream.postPersonalMessage(StringUtils.repeat('a', 10000));
138  1 Assert.assertEquals(StringUtils.repeat('a', 2000), postedMessage.getBody());
139    }
140   
 
141  1 toggle @Test
142    public void testPostDirectMessage() throws Exception
143    {
144  1 Event postedMessage = setupForDirectMessage();
145  1 this.stream.postDirectMessageToUser("Hello World!", this.targetUser);
146  1 Assert.assertEquals("Hello World!", postedMessage.getBody());
147  1 Assert.assertEquals(Importance.CRITICAL, postedMessage.getImportance());
148  1 Assert.assertEquals("directMessage", postedMessage.getType());
149  1 Assert.assertEquals("wiki:XWiki.JaneBuck", postedMessage.getStream());
150  1 Assert.assertEquals(new ObjectReference("XWiki.XWikiUsers", this.targetUser), postedMessage.getRelatedEntity());
151    }
152   
 
153  1 toggle @Test
154    public void testPostDirectMessageWithNullMessage() throws Exception
155    {
156  1 Event postedMessage = setupForDirectMessage();
157  1 this.stream.postDirectMessageToUser(null, this.targetUser);
158  1 Assert.assertEquals(null, postedMessage.getBody());
159    }
160   
 
161  1 toggle @Test
162    public void testPostDirectMessageWithEmptyMessage() throws Exception
163    {
164  1 Event postedMessage = setupForDirectMessage();
165  1 this.stream.postDirectMessageToUser("", this.targetUser);
166  1 Assert.assertEquals("", postedMessage.getBody());
167    }
168   
 
169  1 toggle @Test
170    public void testPostDirectMessageWithLongMessage() throws Exception
171    {
172  1 Event postedMessage = setupForDirectMessage();
173  1 this.stream.postDirectMessageToUser(StringUtils.repeat('a', 10000), this.targetUser);
174  1 Assert.assertEquals(StringUtils.repeat('a', 2000), postedMessage.getBody());
175    }
176   
 
177  1 toggle @Test(expected = IllegalArgumentException.class)
178    public void testPostDirectMessageWithNonExistingTarget() throws Exception
179    {
180  1 final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
181  1 final DocumentReference targetUser = new DocumentReference("xwiki", "XWiki", "Nobody");
182  1 getMockery().checking(new Expectations()
183    {
 
184  1 toggle {
185  1 exactly(1).of(mockBridge).exists(targetUser);
186  1 will(returnValue(false));
187    }
188    });
189  1 this.stream.postDirectMessageToUser("Hello Nobody!", targetUser);
190    }
191   
 
192  1 toggle @Test
193    public void testPostGroupMessage() throws Exception
194    {
195  1 Event postedMessage = setupForGroupMessage();
196  1 this.stream.postMessageToGroup("Hello Friends!", this.targetGroup);
197  1 Assert.assertEquals("Hello Friends!", postedMessage.getBody());
198  1 Assert.assertEquals(Importance.MAJOR, postedMessage.getImportance());
199  1 Assert.assertEquals("groupMessage", postedMessage.getType());
200  1 Assert.assertEquals("wiki:XWiki.MyFriends", postedMessage.getStream());
201  1 Assert.assertEquals(new ObjectReference("XWiki.XWikiGroups", this.targetGroup), postedMessage
202    .getRelatedEntity());
203    }
204   
 
205  1 toggle @Test(expected = IllegalArgumentException.class)
206    public void testPostGroupMessageWithNonExistingTarget() throws Exception
207    {
208  1 final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
209  1 final DocumentReference targetGroup = new DocumentReference("xwiki", "XWiki", "Nobodies");
210  1 getMockery().checking(new Expectations()
211    {
 
212  1 toggle {
213  1 exactly(1).of(mockBridge).exists(targetGroup);
214  1 will(returnValue(false));
215    }
216    });
217  1 this.stream.postMessageToGroup("Hello Nobodies!", targetGroup);
218    }
219   
 
220  1 toggle @Test
221    public void testGetRecentPersonalMessages() throws Exception
222    {
223  1 setupForLimitQueries(30, 0);
224  1 this.stream.getRecentPersonalMessages();
225    }
226   
 
227  1 toggle @Test
228    public void testGetRecentPersonalMessagesForAuthor() throws Exception
229    {
230  1 setupForLimitQueries(30, 0);
231  1 this.stream.getRecentPersonalMessages(this.currentUser);
232    }
233   
 
234  1 toggle @Test
235    public void testGetRecentPersonalMessagesWithNegativeLimit() throws Exception
236    {
237  1 setupForLimitQueries(30, 0);
238  1 this.stream.getRecentPersonalMessages(-4, 0);
239    }
240   
 
241  1 toggle @Test
242    public void testGetRecentPersonalMessagesWithZeroLimit() throws Exception
243    {
244  1 setupForLimitQueries(30, 0);
245  1 this.stream.getRecentPersonalMessages(0, 0);
246    }
247   
 
248  1 toggle @Test
249    public void testGetRecentPersonalMessagesWithLimit1() throws Exception
250    {
251  1 setupForLimitQueries(1, 0);
252  1 this.stream.getRecentPersonalMessages(1, 0);
253    }
254   
 
255  1 toggle @Test
256    public void testGetRecentPersonalMessagesWithLimit30() throws Exception
257    {
258  1 setupForLimitQueries(30, 0);
259  1 this.stream.getRecentPersonalMessages(30, 0);
260    }
261   
 
262  1 toggle @Test
263    public void testGetRecentPersonalMessagesWithLimit100() throws Exception
264    {
265  1 setupForLimitQueries(100, 0);
266  1 this.stream.getRecentPersonalMessages(100, 0);
267    }
268   
 
269  1 toggle @Test
270    public void testGetRecentPersonalMessagesWithNegativeOffset() throws Exception
271    {
272  1 setupForLimitQueries(30, 0);
273  1 this.stream.getRecentPersonalMessages(30, -4);
274    }
275   
 
276  1 toggle @Test
277    public void testGetRecentPersonalMessagesWithNegativeLimitAndOffset() throws Exception
278    {
279  1 setupForLimitQueries(30, 0);
280  1 this.stream.getRecentPersonalMessages(-1, -1);
281    }
282   
 
283  1 toggle @Test
284    public void testGetRecentPersonalMessagesWithZeroOffset() throws Exception
285    {
286  1 setupForLimitQueries(100, 0);
287  1 this.stream.getRecentPersonalMessages(100, 0);
288    }
289   
 
290  1 toggle @Test
291    public void testGetRecentPersonalMessagesWithOffset1() throws Exception
292    {
293  1 setupForLimitQueries(20, 1);
294  1 this.stream.getRecentPersonalMessages(20, 1);
295    }
296   
 
297  1 toggle @Test
298    public void testGetRecentPersonalMessagesWithOffset100() throws Exception
299    {
300  1 setupForLimitQueries(50, 100);
301  1 this.stream.getRecentPersonalMessages(50, 100);
302    }
303   
 
304  1 toggle @Test
305    public void testGetRecentPersonalMessagesWhenQueryFails() throws Exception
306    {
307  1 final Query mockQuery = getMockQuery();
308  1 final QueryManager mockQueryManager = getComponentManager().getInstance(QueryManager.class);
309  1 final EventStream mockEventStream = getComponentManager().getInstance(EventStream.class);
310  1 final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
311  1 final EntityReferenceSerializer<String> mockSerializer =
312    getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
313  1 getMockery().checking(new Expectations()
314    {
 
315  1 toggle {
316  1 allowing(mockBridge).getCurrentUserReference();
317  1 will(returnValue(MessageStreamTest.this.currentUser));
318  1 allowing(mockSerializer).serialize(MessageStreamTest.this.currentUser);
319  1 will(returnValue("wiki:XWiki.JohnDoe"));
320  1 exactly(1).of(mockQuery).setLimit(30);
321  1 will(returnValue(mockQuery));
322  1 exactly(1).of(mockQuery).setOffset(0);
323  1 will(returnValue(mockQuery));
324  1 allowing(mockQuery).bindValue(with(any(String.class)), with("wiki:XWiki.JohnDoe"));
325  1 allowing(mockQueryManager).createQuery(with(aNonNull(String.class)), with(aNonNull(String.class)));
326  1 will(returnValue(mockQuery));
327  1 exactly(1).of(mockEventStream).searchEvents(with(mockQuery));
328  1 will(throwException(new QueryException("", null, null)));
329    }
330    });
331  1 List<Event> result = this.stream.getRecentPersonalMessages();
332  1 Assert.assertNotNull(result);
333  1 Assert.assertTrue(result.isEmpty());
334    }
335   
 
336  13 toggle private Event setupForNewMessage() throws ComponentLookupException, Exception
337    {
338  13 final EventFactory mockEventFactory = getComponentManager().getInstance(EventFactory.class);
339  13 final Event e = new DefaultEvent();
340  13 e.setId(UUID.randomUUID().toString());
341  13 final ModelContext mockContext = getComponentManager().getInstance(ModelContext.class);
342  13 final EventStream mockEventStream = getComponentManager().getInstance(EventStream.class);
343  13 getMockery().checking(new Expectations()
344    {
 
345  13 toggle {
346  13 exactly(1).of(mockEventFactory).createEvent();
347  13 will(returnValue(e));
348  13 exactly(1).of(mockContext).getCurrentEntityReference();
349  13 will(returnValue(new DocumentReference("wiki", "Space", "Page")));
350  13 exactly(1).of(mockEventStream).addEvent(e);
351    }
352    });
353  13 return e;
354    }
355   
 
356  4 toggle private Event setupForPublicMessage() throws Exception
357    {
358  4 final Event e = setupForNewMessage();
359  4 final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
360  4 final EntityReferenceSerializer<String> mockSerializer =
361    getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
362  4 getMockery().checking(new Expectations()
363    {
 
364  4 toggle {
365  4 exactly(1).of(mockBridge).getCurrentUserReference();
366  4 will(returnValue(MessageStreamTest.this.currentUser));
367  4 exactly(1).of(mockSerializer).serialize(MessageStreamTest.this.currentUser);
368  4 will(returnValue("wiki:XWiki.JohnDoe"));
369    }
370    });
371  4 return e;
372    }
373   
 
374  4 toggle private Event setupForPersonalMessage() throws Exception
375    {
376  4 final Event e = setupForNewMessage();
377  4 final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
378  4 final EntityReferenceSerializer<String> mockSerializer =
379    getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
380  4 getMockery().checking(new Expectations()
381    {
 
382  4 toggle {
383  4 exactly(1).of(mockBridge).getCurrentUserReference();
384  4 will(returnValue(MessageStreamTest.this.currentUser));
385  4 exactly(1).of(mockSerializer).serialize(MessageStreamTest.this.currentUser);
386  4 will(returnValue("wiki:XWiki.JohnDoe"));
387    }
388    });
389  4 return e;
390    }
391   
 
392  4 toggle private Event setupForDirectMessage() throws ComponentLookupException, Exception
393    {
394  4 final Event e = setupForNewMessage();
395  4 final EntityReferenceSerializer<String> mockSerializer =
396    getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
397  4 final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
398  4 getMockery().checking(new Expectations()
399    {
 
400  4 toggle {
401  4 exactly(1).of(mockBridge).exists(MessageStreamTest.this.targetUser);
402  4 will(returnValue(true));
403  4 exactly(1).of(mockSerializer).serialize(MessageStreamTest.this.targetUser);
404  4 will(returnValue("wiki:XWiki.JaneBuck"));
405    }
406    });
407  4 return e;
408    }
409   
 
410  1 toggle private Event setupForGroupMessage() throws ComponentLookupException, Exception
411    {
412  1 final Event e = setupForNewMessage();
413  1 final EntityReferenceSerializer<String> mockSerializer =
414    getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
415  1 final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
416  1 getMockery().checking(new Expectations()
417    {
 
418  1 toggle {
419  1 exactly(1).of(mockBridge).exists(MessageStreamTest.this.targetGroup);
420  1 will(returnValue(true));
421  1 exactly(1).of(mockSerializer).serialize(MessageStreamTest.this.targetGroup);
422  1 will(returnValue("wiki:XWiki.MyFriends"));
423    }
424    });
425  1 return e;
426    }
427   
 
428  12 toggle private void setupForLimitQueries(final int expectedLimit, final int expectedOffset)
429    throws ComponentLookupException, Exception
430    {
431  12 final Query mockQuery = getMockQuery();
432  12 final QueryManager mockQueryManager = getComponentManager().getInstance(QueryManager.class);
433  12 final EventStream mockEventStream = getComponentManager().getInstance(EventStream.class);
434  12 final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
435  12 final EntityReferenceSerializer<String> mockSerializer =
436    getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
437  12 getMockery().checking(new Expectations()
438    {
 
439  12 toggle {
440  12 allowing(mockBridge).getCurrentUserReference();
441  12 will(returnValue(MessageStreamTest.this.currentUser));
442  12 allowing(mockSerializer).serialize(MessageStreamTest.this.currentUser);
443  12 will(returnValue("wiki:XWiki.JohnDoe"));
444  12 exactly(1).of(mockQuery).setLimit(expectedLimit);
445  12 will(returnValue(mockQuery));
446  12 exactly(1).of(mockQuery).setOffset(expectedOffset);
447  12 will(returnValue(mockQuery));
448  12 allowing(mockQuery).bindValue(with(any(String.class)), with("wiki:XWiki.JohnDoe"));
449  12 allowing(mockQueryManager).createQuery(with(aNonNull(String.class)), with(aNonNull(String.class)));
450  12 will(returnValue(mockQuery));
451  12 exactly(1).of(mockEventStream).searchEvents(with(mockQuery));
452  12 will(returnValue(null));
453    }
454    });
455    }
456   
 
457  13 toggle private Query getMockQuery()
458    {
459  13 return getMockery().mock(Query.class);
460    }
461    }