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

File DefaultNotificationPreferenceManagerTest.java

 

Code metrics

0
44
6
2
178
122
6
0.14
7.33
3
1

Classes

Class Line # Actions
DefaultNotificationPreferenceManagerTest 53 43 0% 5 0
1.0100%
DefaultNotificationPreferenceManagerTest.NotificationPreferenceImplementation 70 1 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 3 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.preferences.internal;
21   
22    import java.util.Arrays;
23    import java.util.Date;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.notifications.NotificationFormat;
33    import org.xwiki.notifications.preferences.NotificationPreference;
34    import org.xwiki.notifications.preferences.NotificationPreferenceCategory;
35    import org.xwiki.notifications.preferences.NotificationPreferenceProperty;
36    import org.xwiki.notifications.preferences.NotificationPreferenceProvider;
37    import org.xwiki.test.mockito.MockitoComponentMockingRule;
38   
39    import static org.junit.Assert.assertEquals;
40    import static org.junit.Assert.assertTrue;
41    import static org.mockito.ArgumentMatchers.eq;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.times;
44    import static org.mockito.Mockito.verify;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Unit tests for {@link DefaultNotificationPreferenceManager}.
49    *
50    * @version $Id: 68c8abc9f84fa02567d164cc2be7c8b7a9d830d9 $
51    * @since 9.7RC1
52    */
 
53    public class DefaultNotificationPreferenceManagerTest
54    {
55    @Rule
56    public final MockitoComponentMockingRule<DefaultNotificationPreferenceManager> mocker =
57    new MockitoComponentMockingRule<>(DefaultNotificationPreferenceManager.class);
58   
59    private ModelBridge modelBridge;
60   
61    private NotificationPreference mockPreference11;
62    private NotificationPreference mockPreference12;
63    private NotificationPreference mockPreference21;
64    private NotificationPreference mockPreference22;
65    private NotificationPreference mockPreference23;
66   
67    private NotificationPreferenceProvider mockPreferenceProvider1;
68    private NotificationPreferenceProvider mockPreferenceProvider2;
69   
 
70    private class NotificationPreferenceImplementation extends AbstractNotificationPreference
71    {
 
72  15 toggle public NotificationPreferenceImplementation(boolean isNotificationEnabled, NotificationFormat format,
73    NotificationPreferenceCategory category, Date startDate, String providerHint,
74    Map<NotificationPreferenceProperty, Object> properties)
75    {
76  15 super(isNotificationEnabled, format, category, startDate, providerHint, properties);
77    }
78    }
79   
 
80  3 toggle @Before
81    public void setUp() throws Exception
82    {
83  3 modelBridge = mocker.registerMockComponent(ModelBridge.class, "cached");
84   
85  3 Map<NotificationPreferenceProperty, Object> map1 = new HashMap<>();
86  3 map1.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
87  3 Map<NotificationPreferenceProperty, Object> map2 = new HashMap<>();
88  3 map1.put(NotificationPreferenceProperty.EVENT_TYPE, "addComment");
89  3 mockPreference11 = new NotificationPreferenceImplementation(true, NotificationFormat.ALERT,
90    NotificationPreferenceCategory.DEFAULT, null, "1", map1);
91  3 mockPreference12 = new NotificationPreferenceImplementation(true, NotificationFormat.EMAIL,
92    NotificationPreferenceCategory.DEFAULT, null, "1", map1);
93  3 mockPreference21 = new NotificationPreferenceImplementation(false, NotificationFormat.ALERT,
94    NotificationPreferenceCategory.DEFAULT, null, "2", map2);
95  3 mockPreference22 = new NotificationPreferenceImplementation(false, NotificationFormat.EMAIL,
96    NotificationPreferenceCategory.DEFAULT, null, "2", map2);
97  3 mockPreference23 = new NotificationPreferenceImplementation(false, NotificationFormat.EMAIL,
98    NotificationPreferenceCategory.DEFAULT, null, "2", map1);
99   
100  3 mockPreferenceProvider1 = mock(NotificationPreferenceProvider.class);
101  3 mockPreferenceProvider2 = mock(NotificationPreferenceProvider.class);
102    }
103   
 
104  3 toggle private void mockPreferenceProviders(DocumentReference user) throws Exception
105    {
106  3 when(mockPreferenceProvider1.getPreferencesForUser(user))
107    .thenReturn(Arrays.asList(mockPreference11, mockPreference12));
108  3 when(mockPreferenceProvider2.getPreferencesForUser(user))
109    .thenReturn(Arrays.asList(mockPreference21, mockPreference22, mockPreference23));
110   
111  3 mocker.registerComponent(NotificationPreferenceProvider.class, "1", mockPreferenceProvider1);
112  3 mocker.registerComponent(NotificationPreferenceProvider.class, "2", mockPreferenceProvider2);
113   
114  3 when(mockPreferenceProvider1.getProviderPriority()).thenReturn(100);
115  3 when(mockPreferenceProvider2.getProviderPriority()).thenReturn(200);
116    }
117   
 
118  1 toggle @Test
119    public void getNotificationPreferences() throws Exception
120    {
121  1 DocumentReference user = new DocumentReference("xwiki", "test", "user");
122   
123  1 mockPreferenceProviders(user);
124   
125  1 List<NotificationPreference> preferences = mocker.getComponentUnderTest().getAllPreferences(user);
126   
127  1 assertEquals(4, preferences.size());
128  1 assertTrue(preferences.contains(mockPreference11));
129  1 assertTrue(preferences.contains(mockPreference12));
130  1 assertTrue(preferences.contains(mockPreference21));
131  1 assertTrue(preferences.contains(mockPreference22));
132    }
133   
 
134  1 toggle @Test
135    public void getNotificationPreferencesWithAdditionalParameters() throws Exception
136    {
137  1 DocumentReference user = new DocumentReference("xwiki", "test", "user");
138   
139  1 mockPreferenceProviders(user);
140   
141  1 List<NotificationPreference> preferences = mocker.getComponentUnderTest().getPreferences(user,
142    true, NotificationFormat.ALERT);
143   
144  1 assertEquals(1, preferences.size());
145  1 assertTrue(preferences.contains(mockPreference11));
146   
147  1 preferences = mocker.getComponentUnderTest().getPreferences(user,
148    false, NotificationFormat.ALERT);
149   
150  1 assertEquals(1, preferences.size());
151  1 assertTrue(preferences.contains(mockPreference21));
152   
153  1 preferences = mocker.getComponentUnderTest().getPreferences(user,
154    true, NotificationFormat.EMAIL);
155   
156  1 assertEquals(0, preferences.size());
157   
158  1 preferences = mocker.getComponentUnderTest().getPreferences(user,
159    false, NotificationFormat.EMAIL);
160   
161  1 assertEquals(2, preferences.size());
162  1 assertTrue(preferences.contains(mockPreference22));
163  1 assertTrue(preferences.contains(mockPreference23));
164    }
165   
 
166  1 toggle @Test
167    public void saveNotificationsPreferences() throws Exception
168    {
169  1 mockPreferenceProviders(new DocumentReference("xwiki", "test", "user"));
170   
171  1 mocker.getComponentUnderTest()
172    .savePreferences(
173    Arrays.asList(mockPreference11, mockPreference12, mockPreference21, mockPreference22));
174   
175  1 verify(mockPreferenceProvider1, times(1))
176    .savePreferences(eq(Arrays.asList(mockPreference11, mockPreference12)));
177    }
178    }