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

File NotificationPreferenceScriptServiceTest.java

 

Code metrics

0
79
7
2
236
175
9
0.11
11.29
3.5
1.29

Classes

Class Line # Actions
NotificationPreferenceScriptServiceTest 70 77 0% 8 0
1.0100%
NotificationPreferenceScriptServiceTest.NotificationPreferenceImpl 94 2 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 5 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.script;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import javax.inject.Provider;
29   
30    import org.apache.commons.io.IOUtils;
31    import org.apache.commons.lang3.mutable.MutableBoolean;
32    import org.junit.Before;
33    import org.junit.Rule;
34    import org.junit.Test;
35    import org.xwiki.bridge.DocumentAccessBridge;
36    import org.xwiki.component.util.DefaultParameterizedType;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.WikiReference;
39    import org.xwiki.notifications.NotificationFormat;
40    import org.xwiki.notifications.preferences.NotificationPreference;
41    import org.xwiki.notifications.preferences.NotificationPreferenceManager;
42    import org.xwiki.notifications.preferences.NotificationPreferenceProperty;
43    import org.xwiki.notifications.preferences.TargetableNotificationPreferenceBuilder;
44    import org.xwiki.notifications.preferences.internal.AbstractNotificationPreference;
45    import org.xwiki.notifications.preferences.internal.DefaultTargetableNotificationPreferenceBuilder;
46    import org.xwiki.security.authorization.AccessDeniedException;
47    import org.xwiki.security.authorization.ContextualAuthorizationManager;
48    import org.xwiki.security.authorization.Right;
49    import org.xwiki.test.annotation.ComponentList;
50    import org.xwiki.test.mockito.MockitoComponentMockingRule;
51   
52    import static org.junit.Assert.assertEquals;
53    import static org.junit.Assert.assertFalse;
54    import static org.junit.Assert.assertNotNull;
55    import static org.junit.jupiter.api.Assertions.assertTrue;
56    import static org.mockito.ArgumentMatchers.any;
57    import static org.mockito.ArgumentMatchers.eq;
58    import static org.mockito.Mockito.doAnswer;
59    import static org.mockito.Mockito.doThrow;
60    import static org.mockito.Mockito.mock;
61    import static org.mockito.Mockito.when;
62   
63    /**
64    * Unit tests for {@link NotificationPreferenceScriptService}.
65    *
66    * @since 9.7RC1
67    * @version $Id: b31f4e82cd0898d51592283347adbf9a631c8376 $
68    */
69    @ComponentList(TargetableNotificationPreferenceBuilder.class)
 
70    public class NotificationPreferenceScriptServiceTest
71    {
72    @Rule
73    public final MockitoComponentMockingRule<NotificationPreferenceScriptService> mocker =
74    new MockitoComponentMockingRule<>(NotificationPreferenceScriptService.class);
75   
76    private NotificationPreferenceManager notificationPreferenceManager;
77    private DocumentAccessBridge documentAccessBridge;
78    private ContextualAuthorizationManager authorizationManager;
79    private Provider<TargetableNotificationPreferenceBuilder> targetableNotificationPreferenceBuilderProvider;
80   
 
81  5 toggle @Before
82    public void setUp() throws Exception
83    {
84  5 notificationPreferenceManager = mocker.getInstance(NotificationPreferenceManager.class);
85  5 documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
86  5 authorizationManager = mocker.getInstance(ContextualAuthorizationManager.class);
87  5 targetableNotificationPreferenceBuilderProvider = mock(Provider.class);
88  5 when(targetableNotificationPreferenceBuilderProvider.get())
89    .thenReturn(new DefaultTargetableNotificationPreferenceBuilder());
90  5 mocker.registerComponent(new DefaultParameterizedType(null, Provider.class,
91    TargetableNotificationPreferenceBuilder.class), targetableNotificationPreferenceBuilderProvider);
92    }
93   
 
94    private class NotificationPreferenceImpl extends AbstractNotificationPreference
95    {
 
96  3 toggle private NotificationPreferenceImpl(boolean isNotificationEnabled, NotificationFormat format,
97    String eventType)
98    {
99  3 super(isNotificationEnabled, format, null, null, null, new HashMap<>());
100  3 properties.put(NotificationPreferenceProperty.EVENT_TYPE, eventType);
101    }
102    }
103   
 
104  1 toggle @Test
105    public void saveNotificationPreferences() throws Exception
106    {
107  1 DocumentReference userRef = new DocumentReference("xwiki", "XWiki", "UserA");
108   
109  1 NotificationPreferenceImpl existingPref1 = new NotificationPreferenceImpl(true,
110    NotificationFormat.ALERT, "create");
111  1 NotificationPreferenceImpl existingPref2 = new NotificationPreferenceImpl(true,
112    NotificationFormat.EMAIL, "update");
113  1 NotificationPreferenceImpl existingPref3 = new NotificationPreferenceImpl(false,
114    NotificationFormat.EMAIL, "delete");
115   
116  1 when(notificationPreferenceManager.getAllPreferences(eq(userRef))).thenReturn(
117    Arrays.asList(existingPref1, existingPref2, existingPref3));
118   
119  1 final MutableBoolean isOk = new MutableBoolean(false);
120  1 doAnswer(invocationOnMock -> {
121  1 List<NotificationPreference> prefsToSave = invocationOnMock.getArgument(0);
122    // 1 of the preferences contained in the JSON file should be saved because the inherited preference
123    // is the same
124  1 assertEquals(9, prefsToSave.size());
125   
126  1 assertTrue(prefsToSave.contains(existingPref1));
127  1 assertTrue(prefsToSave.contains(existingPref2));
128  1 assertFalse(prefsToSave.contains(existingPref3));
129   
130  1 isOk.setTrue();
131  1 return true;
132    }).when(notificationPreferenceManager).savePreferences(any(List.class));
133   
134  1 mocker.getComponentUnderTest().saveNotificationPreferences(
135    IOUtils.toString(getClass().getResourceAsStream("/preferences.json")), userRef);
136   
137  1 assertTrue(isOk.booleanValue());
138    }
139   
 
140  1 toggle @Test
141    public void isEventTypeEnabled() throws Exception
142    {
143  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
144  1 when(documentAccessBridge.getCurrentUserReference()).thenReturn(user);
145   
146  1 when(notificationPreferenceManager.getAllPreferences(user)).thenReturn(Collections.emptyList());
147  1 assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT));
148   
149  1 NotificationPreference pref1 = mock(NotificationPreference.class);
150  1 NotificationPreference pref2 = mock(NotificationPreference.class);
151   
152  1 when(pref1.getFormat()).thenReturn(NotificationFormat.EMAIL);
153  1 Map<NotificationPreferenceProperty, Object> properties1 = new HashMap<>();
154  1 properties1.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
155  1 when(pref1.getProperties()).thenReturn(properties1);
156   
157  1 when(pref2.getFormat()).thenReturn(NotificationFormat.ALERT);
158  1 Map<NotificationPreferenceProperty, Object> properties2 = new HashMap<>();
159  1 properties2.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
160  1 when(pref2.getProperties()).thenReturn(properties2);
161  1 when(pref2.isNotificationEnabled()).thenReturn(true);
162   
163  1 when(notificationPreferenceManager.getAllPreferences(user)).thenReturn(Arrays.asList(pref1, pref2));
164  1 assertTrue(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT));
165  1 assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.EMAIL));
166    }
167   
 
168  1 toggle @Test
169    public void isEventTypeEnabledForWiki() throws Exception
170    {
171  1 WikiReference wiki = new WikiReference("whatever");
172   
173  1 when(notificationPreferenceManager.getAllPreferences(wiki)).thenReturn(Collections.emptyList());
174  1 assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT,
175    wiki.getName()));
176   
177  1 NotificationPreference pref1 = mock(NotificationPreference.class);
178  1 NotificationPreference pref2 = mock(NotificationPreference.class);
179   
180  1 when(pref1.getFormat()).thenReturn(NotificationFormat.EMAIL);
181  1 Map<NotificationPreferenceProperty, Object> properties1 = new HashMap<>();
182  1 properties1.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
183  1 when(pref1.getProperties()).thenReturn(properties1);
184   
185  1 when(pref2.getFormat()).thenReturn(NotificationFormat.ALERT);
186  1 Map<NotificationPreferenceProperty, Object> properties2 = new HashMap<>();
187  1 properties2.put(NotificationPreferenceProperty.EVENT_TYPE, "update");
188  1 when(pref2.getProperties()).thenReturn(properties2);
189  1 when(pref2.isNotificationEnabled()).thenReturn(true);
190   
191  1 when(notificationPreferenceManager.getAllPreferences(wiki)).thenReturn(Arrays.asList(pref1, pref2));
192  1 assertTrue(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.ALERT,
193    wiki.getName()));
194  1 assertFalse(mocker.getComponentUnderTest().isEventTypeEnabled("update", NotificationFormat.EMAIL,
195    wiki.getName()));
196    }
197   
 
198  1 toggle @Test
199    public void saveNotificationPreferencesForCurrentWikiWithoutRight() throws Exception
200    {
201  1 when(documentAccessBridge.getCurrentDocumentReference()).thenReturn(
202    new DocumentReference("wikiA", "SpaceA", "PageA"));
203  1 AccessDeniedException e = mock(AccessDeniedException.class);
204  1 doThrow(e).when(authorizationManager).checkAccess(Right.ADMIN, new WikiReference("wikiA"));
205   
206  1 String json = "";
207  1 Exception caughtException = null;
208  1 try {
209  1 mocker.getComponentUnderTest().saveNotificationPreferencesForCurrentWiki(json);
210    } catch (Exception ex) {
211  1 caughtException = ex;
212    }
213   
214  1 assertNotNull(caughtException);
215  1 assertEquals(e, caughtException);
216    }
217   
 
218  1 toggle @Test
219    public void saveNotificationPreferencesWithoutRight() throws Exception
220    {
221  1 DocumentReference userDoc = new DocumentReference("wikiA", "SpaceA", "UserA");
222  1 AccessDeniedException e = mock(AccessDeniedException.class);
223  1 doThrow(e).when(authorizationManager).checkAccess(Right.EDIT, userDoc);
224   
225  1 String json = "";
226  1 Exception caughtException = null;
227  1 try {
228  1 mocker.getComponentUnderTest().saveNotificationPreferences(json, userDoc);
229    } catch (Exception ex) {
230  1 caughtException = ex;
231    }
232   
233  1 assertNotNull(caughtException);
234  1 assertEquals(e, caughtException);
235    }
236    }