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

File DefaultWikiManagerTest.java

 

Code metrics

0
78
9
1
276
167
13
0.17
8.67
9
1.44

Classes

Class Line # Actions
DefaultWikiManagerTest 59 78 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 8 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.wiki.internal.manager;
21   
22    import javax.inject.Provider;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.slf4j.Logger;
28    import org.xwiki.bridge.event.WikiCopiedEvent;
29    import org.xwiki.bridge.event.WikiCreateFailedEvent;
30    import org.xwiki.bridge.event.WikiCreatedEvent;
31    import org.xwiki.bridge.event.WikiCreatingEvent;
32    import org.xwiki.bridge.event.WikiDeletedEvent;
33    import org.xwiki.observation.ObservationManager;
34    import org.xwiki.test.mockito.MockitoComponentMockingRule;
35    import org.xwiki.wiki.descriptor.WikiDescriptor;
36    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
37    import org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor;
38    import org.xwiki.wiki.manager.WikiManagerException;
39    import org.xwiki.wiki.provisioning.WikiCopier;
40   
41    import com.xpn.xwiki.XWikiContext;
42    import com.xpn.xwiki.store.XWikiStoreInterface;
43   
44    import static org.junit.Assert.assertFalse;
45    import static org.junit.Assert.assertNotNull;
46    import static org.junit.Assert.assertTrue;
47    import static org.mockito.ArgumentMatchers.any;
48    import static org.mockito.ArgumentMatchers.eq;
49    import static org.mockito.Mockito.mock;
50    import static org.mockito.Mockito.verify;
51    import static org.mockito.Mockito.when;
52   
53    /**
54    * Unit tests for {@link org.xwiki.wiki.internal.manager.DefaultWikiManager}.
55    *
56    * @version $Id: 875f4132232e8f727298b8e5ac09a36d51ea9a52 $
57    * @since 6.0M1
58    */
 
59    public class DefaultWikiManagerTest
60    {
61    @Rule
62    public MockitoComponentMockingRule<DefaultWikiManager> mocker =
63    new MockitoComponentMockingRule(DefaultWikiManager.class);
64   
65    private WikiDescriptorManager wikiDescriptorManager;
66   
67    private Provider<XWikiContext> xcontextProvider;
68   
69    private ObservationManager observationManager;
70   
71    private Logger logger;
72   
73    private XWikiContext xcontext;
74   
75    private com.xpn.xwiki.XWiki xwiki;
76   
77    private XWikiStoreInterface store;
78   
79    private WikiCreator wikiCreator;
80   
81    private WikiCopier wikiCopier;
82   
83    private WikiDeleter wikiDeleter;
84   
 
85  8 toggle @Before
86    public void setUp() throws Exception
87    {
88    // Injection
89  8 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
90  8 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
91  8 observationManager = mocker.getInstance(ObservationManager.class);
92  8 wikiCopier = mocker.getInstance(WikiCopier.class);
93  8 wikiDeleter = mocker.getInstance(WikiDeleter.class);
94  8 wikiCreator = mocker.getInstance(WikiCreator.class);
95   
96    // Frequent uses
97  8 xcontext = mock(XWikiContext.class);
98  8 when(xcontextProvider.get()).thenReturn(xcontext);
99  8 xwiki = mock(com.xpn.xwiki.XWiki.class);
100  8 when(xcontext.getWiki()).thenReturn(xwiki);
101  8 when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
102  8 store = mock(XWikiStoreInterface.class);
103  8 when(xwiki.getStore()).thenReturn(store);
104    }
105   
 
106  1 toggle @Test
107    public void idAvailable() throws Exception
108    {
109    // Forbidden list
110  1 when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden,wikiid3,toto");
111  1 when(store.isWikiNameAvailable(any(String.class), any(XWikiContext.class))).thenReturn(true);
112   
113    // When the wiki already exists
114  1 when(wikiDescriptorManager.exists("wikiid1")).thenReturn(true);
115  1 assertFalse(this.mocker.getComponentUnderTest().idAvailable("wikiid1"));
116   
117    // When the wiki does not already exists
118  1 when(wikiDescriptorManager.exists("wikiid2")).thenReturn(false);
119  1 assertTrue(this.mocker.getComponentUnderTest().idAvailable("wikiid2"));
120   
121    // When the wiki does not already exists but the id is forbidden
122  1 when(wikiDescriptorManager.exists("wikiid3")).thenReturn(false);
123  1 assertFalse(this.mocker.getComponentUnderTest().idAvailable("wikiid3"));
124    }
125   
 
126  1 toggle @Test
127    public void createWhenWikiExists() throws Exception
128    {
129    // When the wiki already exists
130  1 when(wikiDescriptorManager.exists("wikiid1")).thenReturn(true);
131   
132  1 boolean exceptionCaught = false;
133  1 try {
134  1 this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1", true);
135    } catch (WikiManagerException e) {
136  1 exceptionCaught = true;
137    }
138  1 assertTrue(exceptionCaught);
139    }
140   
 
141  1 toggle @Test
142    public void createWhenWikiIdIsForbidden() throws Exception
143    {
144    // The wiki does not already exist
145  1 when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false);
146   
147    // Forbidden list
148  1 when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden,wikiid1");
149   
150  1 boolean exceptionCaught = false;
151  1 try {
152  1 this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1", true);
153    } catch (WikiManagerException e) {
154  1 exceptionCaught = true;
155    }
156  1 assertTrue(exceptionCaught);
157    }
158   
 
159  1 toggle @Test
160    public void createWhenWikiIdIsValid() throws Exception
161    {
162    // The wiki does not already exist
163  1 when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false);
164   
165    // The wiki id is valid
166  1 when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden");
167   
168    // The wiki name is available
169  1 when(store.isWikiNameAvailable(eq("wikiid1"), any(XWikiContext.class))).thenReturn(true);
170   
171  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1");
172  1 when(wikiCreator.create("wikiid1", "wikialias1")).thenReturn(descriptor);
173   
174    // Create
175  1 WikiDescriptor newWikiDescriptor = this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1", true);
176   
177    // Verify a descriptor has been returned
178  1 assertNotNull(newWikiDescriptor);
179   
180    // Verify that the wiki descriptor is an instance of DefaultWikiDescriptor
181  1 assertTrue(newWikiDescriptor instanceof DefaultWikiDescriptor);
182   
183    // Verify that the wiki has been created
184  1 verify(wikiCreator).create("wikiid1", "wikialias1");
185   
186    // Verify the events has been sent
187  1 verify(observationManager).notify(new WikiCreatingEvent("wikiid1"), "wikiid1", xcontext);
188  1 verify(observationManager).notify(new WikiCreatedEvent("wikiid1"), "wikiid1", xcontext);
189    }
190   
 
191  1 toggle @Test
192    public void createWhenWikiIdIsValidButFail() throws Exception
193    {
194    // The wiki does not already exist
195  1 when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false);
196   
197    // The wiki id is valid
198  1 when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden");
199   
200    // The wiki name is available
201  1 when(store.isWikiNameAvailable(eq("wikiid1"), any(XWikiContext.class))).thenReturn(true);
202   
203  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1");
204  1 when(wikiCreator.create("wikiid1", "wikialias1")).thenThrow(new WikiManagerException("..."));
205   
206    // Create
207  1 boolean exceptionCaught = false;
208  1 try {
209  1 this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1", true);
210    } catch (WikiManagerException e) {
211  1 exceptionCaught = true;
212    }
213   
214    // verify the exception
215  1 assertTrue(exceptionCaught);
216   
217    // Verify the events has been sent
218  1 verify(observationManager).notify(new WikiCreatingEvent("wikiid1"), "wikiid1", xcontext);
219  1 verify(observationManager).notify(new WikiCreateFailedEvent("wikiid1"), "wikiid1", xcontext);
220    }
221   
 
222  1 toggle @Test
223    public void deleteWiki() throws Exception
224    {
225  1 this.mocker.getComponentUnderTest().delete("wikiid");
226  1 verify(wikiDeleter).delete("wikiid");
227  1 verify(observationManager).notify(eq(new WikiDeletedEvent("wikiid")), eq("wikiid"));
228    }
229   
 
230  1 toggle @Test
231    public void copyWhenWikiAlreadyExists() throws Exception
232    {
233  1 when(store.isWikiNameAvailable(any(String.class), any(XWikiContext.class))).thenReturn(true);
234    // The wiki already exists
235  1 when(wikiDescriptorManager.exists("existingid")).thenReturn(true);
236  1 boolean exceptionCaught = false;
237  1 try {
238  1 this.mocker.getComponentUnderTest().copy("wikiid", "existingid", "newwikialias", true, true, true);
239    } catch (WikiManagerException e) {
240  1 exceptionCaught = true;
241    }
242  1 assertTrue(exceptionCaught);
243    }
244   
 
245  1 toggle @Test
246    public void copyWhenWikiAvailable() throws Exception
247    {
248    // The wiki does not already exist
249  1 when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false);
250   
251    // The new id is valid
252  1 when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden");
253   
254    // The wiki name is available
255  1 when(store.isWikiNameAvailable(eq("wikiid1"), any(XWikiContext.class))).thenReturn(true);
256   
257    // Other mocks
258  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1");
259  1 when(wikiCreator.create("wikiid1", "wikialias1")).thenReturn(descriptor);
260   
261    // Copy
262  1 WikiDescriptor newWikiDescriptor = this.mocker.getComponentUnderTest().copy("wikiid", "wikiid1",
263    "wikialias1", true, true, true);
264  1 assertNotNull(newWikiDescriptor);
265   
266    // Verify that the wiki has been created
267  1 verify(wikiCreator).create("wikiid1", "wikialias1");
268    // Verify that the wiki has been copied
269  1 verify(wikiCopier).copyDocuments(eq("wikiid"), eq("wikiid1"), eq(true));
270    // Verify that deleted documents has been copied too
271  1 verify(wikiCopier).copyDeletedDocuments(eq("wikiid"), eq("wikiid1"));
272    // Verify that events has been sent
273  1 verify(observationManager).notify(new WikiCopiedEvent("wikiid", "wikiid1"), "wikiid", xcontext);
274    }
275    }
276