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

File DefaultNotificationFilterManagerTest.java

 

Code metrics

0
38
6
1
161
98
6
0.16
6.33
6
1

Classes

Class Line # Actions
DefaultNotificationFilterManagerTest 50 38 0% 6 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.filters.internal;
21   
22    import java.util.Arrays;
23    import java.util.Collection;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.Map;
27    import java.util.stream.Collectors;
28   
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.notifications.filters.NotificationFilter;
35    import org.xwiki.notifications.preferences.NotificationPreference;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
38   
39    import static org.junit.Assert.assertEquals;
40    import static org.junit.Assert.assertTrue;
41    import static org.mockito.Mockito.mock;
42    import static org.mockito.Mockito.when;
43   
44    /**
45    * Unit tests for {@link DefaultNotificationFilterManager}
46    *
47    * @version $Id: 17c4bedf6e556698cb274c8c82b7a9037b101727 $
48    * @since 9.7RC1
49    */
 
50    public class DefaultNotificationFilterManagerTest
51    {
52    @Rule
53    public final MockitoComponentMockingRule<DefaultNotificationFilterManager> mocker =
54    new MockitoComponentMockingRule<>(DefaultNotificationFilterManager.class);
55   
56    private ComponentManager componentManager;
57   
58    private WikiDescriptorManager wikiDescriptorManager;
59   
60    private DocumentReference testUser;
61   
62    private ModelBridge modelBridge;
63   
 
64  5 toggle @Before
65    public void setUp() throws Exception
66    {
67  5 componentManager = mocker.registerMockComponent(ComponentManager.class);
68   
69  5 wikiDescriptorManager = mocker.registerMockComponent(WikiDescriptorManager.class);
70   
71  5 modelBridge = mocker.registerMockComponent(ModelBridge.class, "cached");
72   
73  5 testUser = new DocumentReference("wiki", "test", "user");
74   
75    // Set a default comportment for the wikiDescriptorManager
76  5 when(wikiDescriptorManager.getMainWikiId()).thenReturn("wiki");
77  5 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("currentWikiId");
78  5 when(wikiDescriptorManager.getAllIds()).thenReturn(Arrays.asList("wiki", "currentWikiId"));
79    }
80   
 
81  1 toggle @Test
82    public void getAllNotificationsFiltersWithSubWiki() throws Exception
83    {
84  1 NotificationFilter fakeFilter1 = mock(NotificationFilter.class);
85  1 NotificationFilter fakeFilter2 = mock(NotificationFilter.class);
86   
87  1 when(wikiDescriptorManager.getMainWikiId()).thenReturn("somethingElseThanwiki");
88   
89  1 when(componentManager.getInstanceList(NotificationFilter.class))
90    .thenReturn(Arrays.asList(fakeFilter1, fakeFilter2));
91   
92  1 Collection<NotificationFilter> filters = mocker.getComponentUnderTest().getAllFilters(testUser, false);
93   
94  1 assertEquals(2, filters.size());
95  1 assertTrue(filters.contains(fakeFilter1));
96  1 assertTrue(filters.contains(fakeFilter2));
97    }
98   
 
99  1 toggle @Test
100    public void getAllFiltersWithMainWiki() throws Exception
101    {
102  1 NotificationFilter fakeFilter1 = mock(NotificationFilter.class);
103   
104  1 when(componentManager.getInstanceMap(NotificationFilter.class))
105    .thenReturn(Collections.singletonMap("1", fakeFilter1));
106   
107  1 Collection<NotificationFilter> filters = mocker.getComponentUnderTest().getAllFilters(testUser, false);
108   
109  1 assertEquals(1, filters.size());
110  1 assertTrue(filters.contains(fakeFilter1));
111    }
112   
 
113  1 toggle @Test
114    public void getAllFiltersWithOneDisabledFilter() throws Exception
115    {
116  1 SystemUserNotificationFilter disabledFilter = mock(SystemUserNotificationFilter.class);
117   
118  1 when(componentManager.getInstanceMap(NotificationFilter.class))
119    .thenReturn(Collections.singletonMap("1", disabledFilter));
120   
121  1 when(disabledFilter.getName()).thenReturn(SystemUserNotificationFilter.FILTER_NAME);
122  1 Map<String, Boolean> filterActivations = new HashMap<>();
123  1 filterActivations.put(SystemUserNotificationFilter.FILTER_NAME, false);
124   
125  1 Collection<NotificationFilter> filters = mocker.getComponentUnderTest().getEnabledFilters(
126    mocker.getComponentUnderTest().getAllFilters(testUser, true), filterActivations).collect(Collectors.toList());
127   
128  1 assertEquals(0, filters.size());
129    }
130   
 
131  1 toggle @Test
132    public void getFiltersWithMatchingFilters() throws Exception
133    {
134  1 NotificationFilter fakeFilter1 = mock(NotificationFilter.class);
135   
136  1 NotificationPreference preference = mock(NotificationPreference.class);
137   
138  1 when(fakeFilter1.matchesPreference(preference)).thenReturn(true);
139   
140  1 Collection<NotificationFilter> filters = mocker.getComponentUnderTest().getFiltersRelatedToNotificationPreference(
141    Arrays.asList(fakeFilter1), preference).collect(Collectors.toList());
142   
143  1 assertEquals(1, filters.size());
144  1 assertTrue(filters.contains(fakeFilter1));
145    }
146   
 
147  1 toggle @Test
148    public void getFiltersWithOneBadFilter() throws Exception
149    {
150  1 NotificationFilter fakeFilter1 = mock(NotificationFilter.class);
151   
152  1 NotificationPreference preference = mock(NotificationPreference.class);
153   
154  1 when(fakeFilter1.matchesPreference(preference)).thenReturn(false);
155   
156  1 Collection<NotificationFilter> filters = mocker.getComponentUnderTest()
157    .getFiltersRelatedToNotificationPreference(Arrays.asList(fakeFilter1), preference).collect(Collectors.toList());
158   
159  1 assertEquals(0, filters.size());
160    }
161    }