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

File WatchListMessageDataExtractorTest.java

 

Code metrics

0
138
13
1
452
264
13
0.09
10.62
13
1

Classes

Class Line # Actions
WatchListMessageDataExtractorTest 59 138 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 10 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.watchlist.internal.notification;
21   
22    import java.util.ArrayList;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26   
27    import javax.mail.Address;
28    import javax.mail.internet.InternetAddress;
29   
30    import org.junit.Before;
31    import org.junit.Rule;
32    import org.junit.Test;
33    import org.xwiki.context.Execution;
34    import org.xwiki.context.ExecutionContext;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.model.reference.DocumentReferenceResolver;
37    import org.xwiki.model.reference.WikiReference;
38    import org.xwiki.test.mockito.MockitoComponentManagerRule;
39    import org.xwiki.watchlist.internal.WatchListEventMatcher;
40    import org.xwiki.watchlist.internal.api.WatchListEvent;
41    import org.xwiki.watchlist.internal.job.WatchListJob;
42   
43    import com.xpn.xwiki.XWiki;
44    import com.xpn.xwiki.XWikiContext;
45    import com.xpn.xwiki.doc.XWikiDocument;
46    import com.xpn.xwiki.objects.BaseObject;
47   
48    import static org.junit.Assert.assertEquals;
49    import static org.junit.Assert.assertNull;
50    import static org.mockito.Mockito.mock;
51    import static org.mockito.Mockito.when;
52   
53    /**
54    * Unit tests for {@link WatchListMessageDataExtractor}.
55    *
56    * @version $Id: 61d5e8112ed63c6e53ff9d43a7983216b6bb2194 $
57    * @since 7.1M1
58    */
 
59    public class WatchListMessageDataExtractorTest
60    {
61    @Rule
62    public final MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
63   
64    List<WatchListEvent> events;
65   
66    List<DocumentReference> subscribers;
67   
68    Map<String, Object> parameters;
69   
70    Map<String, Object> factoryParameters;
71   
72    WatchListEventMatcher mockEventMatcher;
73   
74    Execution mockExecution;
75   
76    DocumentReferenceResolver<String> mockExplicitDocumentReferenceResolver;
77   
78    WatchListMessageDataExtractor extractor;
79   
80    XWikiDocument mockDocument;
81   
82    BaseObject mockUserObject;
83   
84    XWiki mockWiki;
85   
86    XWikiContext mockContext;
87   
88    ExecutionContext mockExecutionContext;
89   
90    DocumentReference testSubscriberReference;
91   
92    String testSubscriberStringReference;
93   
94    List<WatchListEvent> testMatchingEvents;
95   
96    String testFirstName;
97   
98    String testLastName;
99   
100    String testEmail;
101   
102    DocumentReference testTemplateReference;
103   
 
104  10 toggle @Before
105    public void setup() throws Exception
106    {
107  10 events = new ArrayList<>();
108  10 subscribers = new ArrayList<>();
109  10 EventsAndSubscribersSource source = new EventsAndSubscribersSource(events, subscribers);
110   
111  10 parameters = new HashMap<>();
112  10 factoryParameters = new HashMap<>();
113  10 parameters.put(WatchListEventMimeMessageFactory.PARAMETERS_PARAMETER, factoryParameters);
114   
115  10 mockEventMatcher = mocker.registerMockComponent(WatchListEventMatcher.class);
116   
117  10 mockExecution = mocker.registerMockComponent(Execution.class);
118   
119  10 mockExplicitDocumentReferenceResolver =
120    mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "explicit");
121   
122  10 extractor =
123    new WatchListMessageDataExtractor(source, parameters, mockEventMatcher, mockExecution,
124    mockExplicitDocumentReferenceResolver);
125   
126    // Document and object
127  10 mockDocument = mock(XWikiDocument.class);
128  10 mockUserObject = mock(BaseObject.class);
129   
130    // Context and wiki
131  10 mockWiki = mock(XWiki.class);
132   
133  10 mockContext = mock(XWikiContext.class);
134  10 when(mockContext.getWiki()).thenReturn(mockWiki);
135   
136  10 mockExecutionContext = mocker.registerMockComponent(ExecutionContext.class);
137  10 when(mockExecution.getContext()).thenReturn(mockExecutionContext);
138  10 when(mockExecutionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).thenReturn(mockContext);
139   
140    // Subscriber reference value
141  10 testSubscriberReference = new DocumentReference("wiki", "XWiki", "User");
142   
143  10 testSubscriberStringReference = "wiki:XWiki.User";
144  10 when(mockDocument.getPrefixedFullName()).thenReturn(testSubscriberStringReference);
145   
146    // Matched events
147  10 testMatchingEvents = new ArrayList<>();
148  10 WatchListEvent matchingEvent = mock(WatchListEvent.class);
149  10 testMatchingEvents.add(matchingEvent);
150   
151  10 when(mockEventMatcher.getMatchingVisibleEvents(events, testSubscriberStringReference)).thenReturn(
152    testMatchingEvents);
153   
154    // User object field values
155  10 testFirstName = "U";
156  10 testLastName = "ser";
157  10 testEmail = "e@ma.il";
158   
159    }
160   
 
161  1 toggle @Test
162    public void noMatchingEvents() throws Exception
163    {
164    // Setup the matched events.
165  1 testMatchingEvents.clear();
166   
167    // Run the extraction.
168  1 WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
169   
170    // Check the extracted data.
171  1 assertNull(result);
172    }
173   
 
174  1 toggle @Test
175    public void noEmailSet() throws Exception
176    {
177    // Setup the user profile data to extract.
178  1 testEmail = null;
179  1 setUpUserObject();
180   
181    // Run the extraction.
182  1 WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
183   
184    // Check the extracted data.
185  1 assertNull(result);
186    }
187   
 
188  1 toggle @Test
189    public void invalidEmailSet() throws Exception
190    {
191    // Setup the user profile data to extract.
192    // TODO: Maybe we should pass the AddressUserDataExtractor as parameter and not end up testing its
193    // functionality but mock it instead.
194  1 testEmail = "@#$%^&*(";
195  1 setUpUserObject();
196   
197    // Run the extraction.
198  1 WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
199   
200    // Check the extracted data.
201  1 assertNull(result);
202    }
203   
 
204  1 toggle @Test
205    public void duplicateEmail() throws Exception
206    {
207    // Setup the user profile data to extract.
208  1 setUpUserObject();
209   
210    // Setup the template
211  1 String templateStringReference = "xwiki:Some.Template";
212  1 parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateStringReference);
213   
214  1 DocumentReference templateReference = new DocumentReference("xwiki", "Some", "Template");
215  1 when(mockExplicitDocumentReferenceResolver.resolve(templateStringReference)).thenReturn(templateReference);
216   
217  1 testTemplateReference = templateReference;
218   
219    // Run the extraction.
220  1 WatchListMessageData result1 = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
221  1 WatchListMessageData result2 = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
222   
223    // Check the extracted data.
224  1 assertResult(result1);
225  1 assertNull(result2);
226    }
227   
 
228  1 toggle @Test
229    public void absoluteTemplate() throws Exception
230    {
231    // Setup the user profile data to extract.
232  1 setUpUserObject();
233   
234    // Setup the template
235  1 String templateStringReference = "xwiki:Some.Template";
236  1 parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateStringReference);
237   
238  1 DocumentReference templateReference = new DocumentReference("xwiki", "Some", "Template");
239  1 when(mockExplicitDocumentReferenceResolver.resolve(templateStringReference)).thenReturn(templateReference);
240   
241  1 testTemplateReference = templateReference;
242   
243    // Run the extraction.
244  1 WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
245   
246    // Check the extracted data.
247  1 assertResult(result);
248    }
249   
 
250  1 toggle @Test
251    public void relativeTemplateUserWiki() throws Exception
252    {
253    // Setup the user profile data to extract.
254  1 setUpUserObject();
255   
256    // Setup the template
257  1 String templateStringReference = "Some.Template";
258  1 parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateStringReference);
259   
260  1 DocumentReference userWikiTemplateReference = new DocumentReference("wiki", "Some", "Template");
261  1 when(
262    mockExplicitDocumentReferenceResolver.resolve(templateStringReference,
263    testSubscriberReference.getWikiReference())).thenReturn(userWikiTemplateReference);
264   
265  1 when(mockWiki.exists(userWikiTemplateReference, mockContext)).thenReturn(true);
266   
267  1 testTemplateReference = userWikiTemplateReference;
268   
269    // Run the extraction.
270  1 WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
271   
272    // Check the extracted data.
273  1 assertResult(result);
274    }
275   
 
276  1 toggle @Test
277    public void relativeTemplateContextWiki() throws Exception
278    {
279    // Setup the user profile data to extract.
280  1 setUpUserObject();
281   
282    // Setup the template
283  1 String templateStringReference = "Some.Template";
284  1 parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateStringReference);
285   
286  1 DocumentReference userWikiTemplateReference = new DocumentReference("wiki", "Some", "Template");
287  1 when(
288    mockExplicitDocumentReferenceResolver.resolve(templateStringReference,
289    testSubscriberReference.getWikiReference())).thenReturn(userWikiTemplateReference);
290   
291  1 when(mockWiki.exists(userWikiTemplateReference, mockContext)).thenReturn(false);
292   
293  1 String currentWikiId = "xwiki";
294  1 when(mockContext.getWikiId()).thenReturn(currentWikiId);
295  1 WikiReference currentWikiReference = new WikiReference(currentWikiId);
296   
297  1 DocumentReference currentWikiTemplateReference = new DocumentReference(currentWikiId, "Some", "Template");
298  1 when(mockExplicitDocumentReferenceResolver.resolve(templateStringReference, currentWikiReference)).thenReturn(
299    currentWikiTemplateReference);
300   
301  1 when(mockWiki.exists(currentWikiTemplateReference, mockContext)).thenReturn(true);
302   
303  1 testTemplateReference = currentWikiTemplateReference;
304   
305    // Run the extraction.
306  1 WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
307   
308    // Check the extracted data.
309  1 assertResult(result);
310    }
311   
 
312  1 toggle @Test
313    public void relativeTemplateDefaultTemplateContextWiki() throws Exception
314    {
315    // Setup the user profile data to extract.
316  1 setUpUserObject();
317   
318    // Setup the template
319  1 String templateStringReference = "Some.Template";
320  1 parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateStringReference);
321   
322  1 DocumentReference userWikiTemplateReference = new DocumentReference("wiki", "Some", "Template");
323  1 when(
324    mockExplicitDocumentReferenceResolver.resolve(templateStringReference,
325    testSubscriberReference.getWikiReference())).thenReturn(userWikiTemplateReference);
326   
327  1 when(mockWiki.exists(userWikiTemplateReference, mockContext)).thenReturn(false);
328   
329  1 String currentWikiId = "xwiki";
330  1 when(mockContext.getWikiId()).thenReturn(currentWikiId);
331  1 WikiReference currentWikiReference = new WikiReference(currentWikiId);
332   
333  1 DocumentReference currentWikiTemplateReference = new DocumentReference(currentWikiId, "Some", "Template");
334  1 when(mockExplicitDocumentReferenceResolver.resolve(templateStringReference, currentWikiReference)).thenReturn(
335    currentWikiTemplateReference);
336   
337  1 when(mockWiki.exists(currentWikiTemplateReference, mockContext)).thenReturn(false);
338   
339  1 DocumentReference defaultCurrentWikiTemplateReference =
340    new DocumentReference(currentWikiId, "Some", "Template");
341  1 when(mockExplicitDocumentReferenceResolver.resolve(WatchListJob.DEFAULT_EMAIL_TEMPLATE, currentWikiReference))
342    .thenReturn(defaultCurrentWikiTemplateReference);
343   
344  1 when(mockWiki.exists(defaultCurrentWikiTemplateReference, mockContext)).thenReturn(true);
345   
346  1 testTemplateReference = defaultCurrentWikiTemplateReference;
347   
348    // Run the extraction.
349  1 WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
350   
351    // Check the extracted data.
352  1 assertResult(result);
353    }
354   
 
355  1 toggle @Test
356    public void relativeTemplateDefaultTemplateMainWiki() throws Exception
357    {
358    // Setup the user profile data to extract.
359  1 setUpUserObject();
360   
361    // Setup the template
362  1 String templateStringReference = "Some.Template";
363  1 parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateStringReference);
364   
365  1 DocumentReference userWikiTemplateReference =
366    new DocumentReference(testSubscriberReference.getWikiReference().getName(), "Some", "Template");
367  1 when(
368    mockExplicitDocumentReferenceResolver.resolve(templateStringReference,
369    testSubscriberReference.getWikiReference())).thenReturn(userWikiTemplateReference);
370   
371  1 when(mockWiki.exists(userWikiTemplateReference, mockContext)).thenReturn(false);
372   
373  1 String currentWikiId = "someWiki";
374  1 when(mockContext.getWikiId()).thenReturn(currentWikiId);
375  1 WikiReference currentWikiReference = new WikiReference(currentWikiId);
376   
377  1 DocumentReference currentWikiTemplateReference = new DocumentReference(currentWikiId, "Some", "Template");
378  1 when(mockExplicitDocumentReferenceResolver.resolve(templateStringReference, currentWikiReference)).thenReturn(
379    currentWikiTemplateReference);
380   
381  1 when(mockWiki.exists(currentWikiTemplateReference, mockContext)).thenReturn(false);
382   
383  1 DocumentReference defaultCurrentWikiTemplateReference =
384    new DocumentReference(currentWikiId, "XWiki", "WatchListMessage");
385  1 when(mockExplicitDocumentReferenceResolver.resolve(WatchListJob.DEFAULT_EMAIL_TEMPLATE, currentWikiReference))
386    .thenReturn(defaultCurrentWikiTemplateReference);
387   
388  1 when(mockWiki.exists(defaultCurrentWikiTemplateReference, mockContext)).thenReturn(false);
389   
390  1 String mainWikiId = "xwiki";
391  1 when(mockContext.getMainXWiki()).thenReturn(mainWikiId);
392  1 WikiReference mainWikiReference = new WikiReference(mainWikiId);
393   
394  1 DocumentReference defaultMainWikiTemplateReference =
395    new DocumentReference(mainWikiId, "XWiki", "WatchListMessage");
396  1 when(mockExplicitDocumentReferenceResolver.resolve(WatchListJob.DEFAULT_EMAIL_TEMPLATE, mainWikiReference))
397    .thenReturn(defaultMainWikiTemplateReference);
398   
399  1 testTemplateReference = defaultMainWikiTemplateReference;
400   
401    // Run the extraction.
402  1 WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
403   
404    // Check the extracted data.
405  1 assertResult(result);
406    }
407   
 
408  1 toggle @Test
409    public void skipContextUser() throws Exception
410    {
411    // Set the skipContextUser parameter and reinitialize the extractor.
412  1 parameters.put(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER, true);
413   
414    // Reinitialize the extractor to consider the new parameter.
415  1 EventsAndSubscribersSource source = new EventsAndSubscribersSource(events, subscribers);
416  1 extractor =
417    new WatchListMessageDataExtractor(source, parameters, mockEventMatcher, mockExecution,
418    mockExplicitDocumentReferenceResolver);
419   
420    // Setup the current context user.
421  1 when(mockContext.getUserReference()).thenReturn(testSubscriberReference);
422   
423    // Run the extraction.
424  1 WatchListMessageData result = extractor.extract(testSubscriberReference, mockDocument, mockUserObject);
425   
426    // Check the extracted data.
427  1 assertNull(result);
428    }
429   
430    /*
431    * Helper methods
432    */
433   
 
434  8 toggle private void setUpUserObject()
435    {
436  8 when(mockUserObject.getStringValue("first_name")).thenReturn(testFirstName);
437  8 when(mockUserObject.getStringValue("last_name")).thenReturn(testLastName);
438  8 when(mockUserObject.getStringValue("email")).thenReturn(testEmail);
439    }
440   
 
441  6 toggle private void assertResult(WatchListMessageData result) throws Exception
442    {
443  6 assertEquals(testSubscriberReference, result.getUserReference());
444    // We don`t care about validity exception since it should not get this far because the result will be null.
445  6 Address address = InternetAddress.parse(testEmail)[0];
446  6 assertEquals(address, result.getAddress());
447  6 assertEquals(testFirstName, result.getFirstName());
448  6 assertEquals(testLastName, result.getLastName());
449  6 assertEquals(testMatchingEvents, result.getEvents());
450  6 assertEquals(testTemplateReference, result.getTemplateReference());
451    }
452    }