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

File NotificationFilterPreferencesMigratorTest.java

 

Code metrics

10
62
4
1
216
170
9
0.15
15.5
4
2.25

Classes

Class Line # Actions
NotificationFilterPreferencesMigratorTest 61 62 0% 9 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.filters.internal.migrators;
21   
22    import java.util.Arrays;
23    import java.util.Date;
24    import java.util.List;
25    import java.util.Set;
26   
27    import javax.inject.Provider;
28   
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.bridge.event.ApplicationReadyEvent;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.DocumentReferenceResolver;
35    import org.xwiki.model.reference.WikiReference;
36    import org.xwiki.notifications.NotificationFormat;
37    import org.xwiki.notifications.filters.NotificationFilterType;
38    import org.xwiki.notifications.filters.internal.DefaultNotificationFilterPreference;
39    import org.xwiki.notifications.filters.internal.ModelBridge;
40    import org.xwiki.query.Query;
41    import org.xwiki.query.QueryManager;
42    import org.xwiki.test.mockito.MockitoComponentMockingRule;
43    import org.xwiki.text.StringUtils;
44    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
45   
46    import com.google.common.collect.Sets;
47    import com.xpn.xwiki.XWiki;
48    import com.xpn.xwiki.XWikiContext;
49    import com.xpn.xwiki.doc.XWikiDocument;
50    import com.xpn.xwiki.objects.BaseObject;
51   
52    import static org.mockito.ArgumentMatchers.anyString;
53    import static org.mockito.Mockito.mock;
54    import static org.mockito.Mockito.verify;
55    import static org.mockito.Mockito.when;
56   
57    /**
58    * @version $Id: c765df552747473daeb2848161c58c7530ef9632 $
59    * @since
60    */
 
61    public class NotificationFilterPreferencesMigratorTest
62    {
63    @Rule
64    public final MockitoComponentMockingRule<NotificationFilterPreferencesMigrator> mocker =
65    new MockitoComponentMockingRule<>(NotificationFilterPreferencesMigrator.class);
66   
67    private ModelBridge modelBridge;
68    private QueryManager queryManager;
69    private DocumentReferenceResolver<String> referenceResolver;
70    private Provider<XWikiContext> contextProvider;
71    private WikiDescriptorManager wikiDescriptorManager;
72    private XWikiContext xwikicontext;
73    private XWiki xwiki;
74   
 
75  1 toggle @Before
76    public void setUp() throws Exception
77    {
78  1 modelBridge = mocker.getInstance(ModelBridge.class);
79  1 queryManager = mocker.getInstance(QueryManager.class);
80  1 referenceResolver = mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
81  1 contextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
82  1 xwikicontext = mock(XWikiContext.class);
83  1 when(contextProvider.get()).thenReturn(xwikicontext);
84  1 xwiki = mock(XWiki.class);
85  1 when(xwikicontext.getWiki()).thenReturn(xwiki);
86  1 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
87  1 when(wikiDescriptorManager.getAllIds()).thenReturn(Arrays.asList("xwiki"));
88    }
89   
 
90  1 toggle @Test
91    public void migrate() throws Exception
92    {
93  1 Query query = mock(Query.class);
94  1 when(queryManager.createQuery(anyString(), anyString())).thenReturn(query);
95  1 when(query.setWiki("xwiki")).thenReturn(query);
96  1 when(query.execute()).thenReturn(Arrays.asList("XWiki.UserA"));
97   
98  1 WikiReference wikiReference = new WikiReference("xwiki");
99  1 when(xwikicontext.getWikiReference()).thenReturn(wikiReference);
100   
101  1 DocumentReference userA = new DocumentReference("xwiki", "XWiki", "UserA");
102  1 when(referenceResolver.resolve("XWiki.UserA", wikiReference)).thenReturn(userA);
103   
104  1 XWikiDocument userAdoc = mock(XWikiDocument.class);
105  1 when(xwiki.getDocument(userA, xwikicontext)).thenReturn(userAdoc);
106  1 List<BaseObject> objects = Arrays.asList(
107    mockObject(Arrays.asList("alert"), "filter1", true, "inclusive",
108    new Date(100L), Arrays.asList("update", "create"), Arrays.asList("page1", "page2"),
109    Arrays.asList("space1", "space2"), Arrays.asList("wiki1", "wiki2"),
110    Arrays.asList("user1", "user2")),
111    null,
112    mockObject(Arrays.asList("alert", "email"), "filter2", false, "exclusive",
113    new Date(90L), Arrays.asList("comment"), Arrays.asList("page3"),
114    Arrays.asList(), Arrays.asList(), Arrays.asList())
115    );
116  1 DocumentReference classReference = new DocumentReference("xwiki", Arrays.asList("XWiki", "Notifications", "Code"),
117    "NotificationFilterPreferenceClass");
118  1 when(userAdoc.getXObjects(classReference)).thenReturn(objects);
119   
120  1 DocumentReference notificationFilterPreferenceClass = new DocumentReference("xwiki",
121    Arrays.asList("XWiki", "Notifications", "Code"), "NotificationFilterPreferenceClass");
122  1 XWikiDocument oldClass = mock(XWikiDocument.class);
123  1 when(xwiki.getDocument(notificationFilterPreferenceClass, xwikicontext)).thenReturn(oldClass);
124  1 when(oldClass.isNew()).thenReturn(false);
125  1 when(xwiki.exists(notificationFilterPreferenceClass, xwikicontext)).thenReturn(true);
126   
127    // Test
128  1 mocker.getComponentUnderTest().onEvent(new ApplicationReadyEvent(), null, null);
129   
130    // Verify
131  1 verify(xwiki).deleteDocument(oldClass, false, xwikicontext);
132  1 verify(userAdoc).removeXObjects(classReference);
133  1 verify(xwiki).saveDocument(userAdoc, "Migrate notification filter preferences to the new store.",
134    xwikicontext);
135   
136  1 verify(modelBridge).saveFilterPreferences(userA, Arrays.asList(
137    createExpectedPreference(Sets.newHashSet("update", "create"),
138    Sets.newHashSet(NotificationFormat.ALERT), NotificationFilterType.INCLUSIVE,
139    "filter1", true, new Date(100L), "", "", "page1", ""),
140    createExpectedPreference(Sets.newHashSet("update", "create"),
141    Sets.newHashSet(NotificationFormat.ALERT), NotificationFilterType.INCLUSIVE,
142    "filter1", true, new Date(100L), "", "", "page2", ""),
143    createExpectedPreference(Sets.newHashSet("update", "create"),
144    Sets.newHashSet(NotificationFormat.ALERT), NotificationFilterType.INCLUSIVE,
145    "filter1", true, new Date(100L), "", "space1", "", ""),
146    createExpectedPreference(Sets.newHashSet("update", "create"),
147    Sets.newHashSet(NotificationFormat.ALERT), NotificationFilterType.INCLUSIVE,
148    "filter1", true, new Date(100L), "", "space2", "", ""),
149    createExpectedPreference(Sets.newHashSet("update", "create"),
150    Sets.newHashSet(NotificationFormat.ALERT), NotificationFilterType.INCLUSIVE,
151    "filter1", true, new Date(100L), "wiki1", "", "", ""),
152    createExpectedPreference(Sets.newHashSet("update", "create"),
153    Sets.newHashSet(NotificationFormat.ALERT), NotificationFilterType.INCLUSIVE,
154    "filter1", true, new Date(100L), "wiki2", "", "", ""),
155    createExpectedPreference(Sets.newHashSet("update", "create"),
156    Sets.newHashSet(NotificationFormat.ALERT), NotificationFilterType.INCLUSIVE,
157    "filter1", true, new Date(100L), "", "", "", "user1"),
158    createExpectedPreference(Sets.newHashSet("update", "create"),
159    Sets.newHashSet(NotificationFormat.ALERT), NotificationFilterType.INCLUSIVE,
160    "filter1", true, new Date(100L), "", "", "", "user2"),
161    createExpectedPreference(Sets.newHashSet("comment"),
162    Sets.newHashSet(NotificationFormat.ALERT, NotificationFormat.EMAIL), NotificationFilterType.EXCLUSIVE,
163    "filter2", false, new Date(90L), "", "", "page3", "")
164    ));
165    }
166   
 
167  2 toggle private BaseObject mockObject(List<String> formats, String filterName, boolean isEnabled,
168    String filterType, Date date, List<String> eventTypes, List<String> pages, List<String> spaces,
169    List<String> wikis, List<String> users)
170    {
171  2 BaseObject obj = mock(BaseObject.class);
172   
173  2 when(obj.getListValue("filterFormats")).thenReturn(formats);
174  2 when(obj.getStringValue("filterName")).thenReturn(filterName);
175  2 when(obj.getIntValue("isEnabled", 1)).thenReturn(isEnabled ? 1 : 0);
176  2 when(obj.getStringValue("filterType")).thenReturn(filterType);
177  2 when(obj.getDateValue("startingDate")).thenReturn(date);
178   
179  2 when(obj.getListValue("eventTypes")).thenReturn(eventTypes);
180  2 when(obj.getListValue("pages")).thenReturn(pages);
181  2 when(obj.getListValue("spaces")).thenReturn(spaces);
182  2 when(obj.getListValue("wikis")).thenReturn(wikis);
183  2 when(obj.getListValue("users")).thenReturn(users);
184   
185  2 return obj;
186    }
187   
 
188  9 toggle private DefaultNotificationFilterPreference createExpectedPreference(
189    Set<String> eventType,
190    Set<NotificationFormat> formats,
191    NotificationFilterType filterType, String filterName, boolean isEnabled, Date date,
192    String wiki, String page, String pageOnly, String user)
193    {
194  9 DefaultNotificationFilterPreference preference = new DefaultNotificationFilterPreference();
195  9 preference.setEventTypes(eventType);
196  9 preference.setNotificationFormats(formats);
197  9 preference.setFilterType(filterType);
198  9 preference.setProviderHint("userProfile");
199  9 preference.setFilterName(filterName);
200  9 preference.setEnabled(isEnabled);
201  9 preference.setStartingDate(date);
202  9 if (StringUtils.isNotBlank(wiki)) {
203  2 preference.setWiki(wiki);
204    }
205  9 if (StringUtils.isNotBlank(page)) {
206  2 preference.setPage(page);
207    }
208  9 if (StringUtils.isNotBlank(pageOnly)) {
209  3 preference.setPageOnly(pageOnly);
210    }
211  9 if (StringUtils.isNotBlank(user)) {
212  2 preference.setUser(user);
213    }
214  9 return preference;
215    }
216    }