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

File WikiTemplateManagerScriptTest.java

 

Code metrics

0
126
21
1
386
274
21
0.17
6
21
1

Classes

Class Line # Actions
WikiTemplateManagerScriptTest 63 126 0% 21 0
1.0100%
 

Contributing tests

This file is covered by 17 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.template.script;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.List;
25   
26    import javax.inject.Provider;
27   
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.component.util.DefaultParameterizedType;
32    import org.xwiki.context.Execution;
33    import org.xwiki.context.ExecutionContext;
34    import org.xwiki.job.event.status.JobStatus;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.model.reference.EntityReferenceSerializer;
37    import org.xwiki.model.reference.WikiReference;
38    import org.xwiki.security.authorization.AccessDeniedException;
39    import org.xwiki.security.authorization.AuthorizationManager;
40    import org.xwiki.security.authorization.Right;
41    import org.xwiki.test.mockito.MockitoComponentMockingRule;
42    import org.xwiki.wiki.descriptor.WikiDescriptor;
43    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
44    import org.xwiki.wiki.manager.WikiManagerException;
45    import org.xwiki.wiki.provisioning.WikiProvisioningJob;
46    import org.xwiki.wiki.template.WikiTemplateManager;
47    import org.xwiki.wiki.template.WikiTemplateManagerException;
48   
49    import com.xpn.xwiki.XWikiContext;
50    import com.xpn.xwiki.doc.XWikiDocument;
51   
52    import static org.junit.Assert.assertEquals;
53    import static org.junit.Assert.assertFalse;
54    import static org.junit.Assert.assertNull;
55    import static org.junit.Assert.assertTrue;
56    import static org.mockito.ArgumentMatchers.anyList;
57    import static org.mockito.ArgumentMatchers.eq;
58    import static org.mockito.Mockito.doThrow;
59    import static org.mockito.Mockito.mock;
60    import static org.mockito.Mockito.verify;
61    import static org.mockito.Mockito.when;
62   
 
63    public class WikiTemplateManagerScriptTest
64    {
65    @Rule
66    public MockitoComponentMockingRule<WikiTemplateManagerScript> mocker =
67    new MockitoComponentMockingRule(WikiTemplateManagerScript.class);
68   
69    private WikiTemplateManager wikiTemplateManager;
70   
71    private WikiDescriptorManager wikiDescriptorManager;
72   
73    private AuthorizationManager authorizationManager;
74   
75    private Provider<XWikiContext> xcontextProvider;
76   
77    private EntityReferenceSerializer<String> entityReferenceSerializer;
78   
79    private XWikiContext xcontext;
80   
81    private Execution execution;
82   
83    private DocumentReference currentUserRef;
84   
85    private XWikiDocument currentDoc;
86   
87    private ExecutionContext executionContext;
88   
 
89  17 toggle @Before
90    public void setUp() throws Exception
91    {
92  17 wikiTemplateManager = mocker.getInstance(WikiTemplateManager.class);
93  17 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
94  17 authorizationManager = mocker.getInstance(AuthorizationManager.class);
95  17 entityReferenceSerializer = mocker.getInstance(new DefaultParameterizedType(null,
96    EntityReferenceSerializer.class, String.class));
97  17 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
98  17 xcontext = mock(XWikiContext.class);
99  17 when(xcontextProvider.get()).thenReturn(xcontext);
100  17 execution = mocker.getInstance(Execution.class);
101  17 executionContext = new ExecutionContext();
102  17 when(execution.getContext()).thenReturn(executionContext);
103   
104  17 currentUserRef = new DocumentReference("mainWiki", "XWiki", "User");
105  17 when(xcontext.getUserReference()).thenReturn(currentUserRef);
106   
107  17 currentDoc = mock(XWikiDocument.class);
108  17 when(xcontext.getDoc()).thenReturn(currentDoc);
109   
110  17 when(xcontext.getMainXWiki()).thenReturn("mainWiki");
111   
112  17 when(entityReferenceSerializer.serialize(currentUserRef)).thenReturn("mainWiki:XWiki.User");
113    }
114   
115    /**
116    * @return the exception expected when the current script has the not the programing right
117    */
 
118  2 toggle private Exception currentScriptHasNotProgrammingRight() throws AccessDeniedException
119    {
120  2 DocumentReference authorDocRef = new DocumentReference("mainWiki", "XWiki", "Admin");
121  2 when(currentDoc.getAuthorReference()).thenReturn(authorDocRef);
122  2 DocumentReference currentDocRef = new DocumentReference("subwiki", "Test", "test");
123  2 when(currentDoc.getDocumentReference()).thenReturn(currentDocRef);
124   
125  2 Exception exception = new AccessDeniedException(Right.PROGRAM, authorDocRef, currentDocRef);
126  2 doThrow(exception).when(authorizationManager).checkAccess(Right.PROGRAM, authorDocRef, currentDocRef);
127   
128  2 return exception;
129    }
130   
131    /**
132    * @return the exception expected when the current user has the not the admin right
133    */
 
134  1 toggle private Exception currentUserHasNotAdminRight() throws AccessDeniedException
135    {
136  1 WikiReference wiki = new WikiReference("wikiId");
137  1 Exception exception = new AccessDeniedException(Right.ADMIN, currentUserRef, wiki);
138  1 doThrow(exception).when(authorizationManager).checkAccess(eq(Right.ADMIN), eq(currentUserRef), eq(wiki));
139   
140  1 return exception;
141    }
142   
143    /**
144    * @return the exception expected when the current user has the not the 'create wiki' right
145    */
 
146  1 toggle private Exception currentUserHasNotCreateWikiRight() throws AccessDeniedException
147    {
148  1 WikiReference wiki = new WikiReference("mainWiki");
149  1 Exception exception = new AccessDeniedException(Right.CREATE_WIKI, currentUserRef, wiki);
150  1 doThrow(exception).when(authorizationManager).checkAccess(eq(Right.CREATE_WIKI), eq(currentUserRef), eq(wiki));
151   
152  1 return exception;
153    }
154   
 
155  1 toggle @Test
156    public void getTemplates() throws Exception
157    {
158  1 Collection<WikiDescriptor> templates = new ArrayList<WikiDescriptor>();
159  1 WikiDescriptor descriptor = new WikiDescriptor("templateId", "templateAlias");
160  1 templates.add(descriptor);
161   
162  1 when(wikiTemplateManager.getTemplates()).thenReturn(templates);
163   
164  1 Collection<WikiDescriptor> results = mocker.getComponentUnderTest().getTemplates();
165  1 assertEquals(templates, results);
166    }
167   
 
168  1 toggle @Test
169    public void getTemplatesError() throws Exception
170    {
171  1 Exception exception = new WikiTemplateManagerException("Error in getTemplates");
172  1 when(wikiTemplateManager.getTemplates()).thenThrow(exception);
173   
174  1 Collection<WikiDescriptor> results = mocker.getComponentUnderTest().getTemplates();
175  1 assertTrue(results.isEmpty());
176  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
177  1 verify(mocker.getMockedLogger()).error("Error while getting all the wiki templates.", exception);
178    }
179   
 
180  1 toggle @Test
181    public void setTemplateWhenCurrentUserIsOwner() throws Exception
182    {
183  1 WikiDescriptor wikiDescriptor = new WikiDescriptor("wikiId", "wikiAlias");
184  1 wikiDescriptor.setOwnerId("mainWiki:XWiki.User");
185  1 when(wikiDescriptorManager.getById("wikiId")).thenReturn(wikiDescriptor);
186   
187    // Test 1
188  1 boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true);
189  1 assertTrue(result);
190  1 verify(wikiTemplateManager).setTemplate("wikiId", true);
191   
192    // Test 2
193  1 result = mocker.getComponentUnderTest().setTemplate("wikiId", false);
194  1 assertTrue(result);
195  1 verify(wikiTemplateManager).setTemplate("wikiId", false);
196    }
197   
 
198  1 toggle @Test
199    public void setTemplateWithoutPR() throws Exception
200    {
201  1 Exception exception = currentScriptHasNotProgrammingRight();
202   
203  1 boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true);
204  1 assertFalse(result);
205  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
206  1 verify(mocker.getMockedLogger()).error("Access denied for [mainWiki:XWiki.User] to change the template value" +
207    " of the wiki [wikiId]. The user has not the right to perform this operation or the script has not " +
208    "the programming right.", exception);
209    }
210   
 
211  1 toggle @Test
212    public void setTemplateWithoutAdminRight() throws Exception
213    {
214  1 Exception exception = currentUserHasNotAdminRight();
215   
216  1 WikiDescriptor wikiDescriptor = new WikiDescriptor("wikiId", "wikiAlias");
217  1 when(wikiDescriptorManager.getById("wikiId")).thenReturn(wikiDescriptor);
218   
219  1 boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true);
220  1 assertFalse(result);
221  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
222  1 verify(mocker.getMockedLogger()).error("Access denied for [mainWiki:XWiki.User] to change the template value" +
223    " of the wiki [wikiId]. The user has not the right to perform this operation or the script has not " +
224    "the programming right.", exception);
225    }
226   
 
227  1 toggle @Test
228    public void setTemplateErrorWithDescriptorManager() throws Exception
229    {
230  1 Exception exception = new WikiManagerException("error in getById");
231  1 when(wikiDescriptorManager.getById("wikiId")).thenThrow(exception);
232   
233  1 boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true);
234  1 assertFalse(result);
235  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
236  1 verify(mocker.getMockedLogger()).error("Failed to get the descriptor of the wiki [wikiId].", exception);
237    }
238   
 
239  1 toggle @Test
240    public void setTemplateErrorWithTemplateManager() throws Exception
241    {
242  1 WikiDescriptor wikiDescriptor = new WikiDescriptor("wikiId", "wikiAlias");
243  1 when(wikiDescriptorManager.getById("wikiId")).thenReturn(wikiDescriptor);
244   
245  1 Exception exception = new WikiTemplateManagerException("error in setTemplate");
246  1 doThrow(exception).when(wikiTemplateManager).setTemplate("wikiId", true);
247   
248  1 boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true);
249  1 assertFalse(result);
250  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
251  1 verify(mocker.getMockedLogger()).error("Failed to set the template value [true] for the wiki [wikiId].",
252    exception);
253    }
254   
 
255  1 toggle @Test
256    public void isTemplate() throws Exception
257    {
258  1 when(wikiTemplateManager.isTemplate("wikiTemplate")).thenReturn(true);
259  1 when(wikiTemplateManager.isTemplate("subwiki")).thenReturn(false);
260   
261  1 assertTrue(mocker.getComponentUnderTest().isTemplate("wikiTemplate"));
262  1 assertFalse(mocker.getComponentUnderTest().isTemplate("subwiki"));
263    }
264   
 
265  1 toggle @Test
266    public void isTemplateError() throws Exception
267    {
268  1 Exception exception = new WikiTemplateManagerException("error in isTemplate");
269   
270  1 when(wikiTemplateManager.isTemplate("wikiTemplate")).thenThrow(exception);
271   
272  1 assertNull(mocker.getComponentUnderTest().isTemplate("wikiTemplate"));
273  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
274  1 verify(mocker.getMockedLogger()).error("Failed to get if the wiki [wikiTemplate] is a template or not.",
275    exception);
276    }
277   
 
278  1 toggle @Test
279    public void createWikiFromTemplate() throws Exception
280    {
281    // Test
282  1 boolean result = mocker.getComponentUnderTest().createWikiFromTemplate("newWikiId", "newWikiAlias",
283    "templateId", "ownerId", true);
284   
285    // Verify
286  1 assertTrue(result);
287  1 verify(wikiTemplateManager).createWikiFromTemplate("newWikiId", "newWikiAlias", "templateId", "ownerId", true);
288    }
289   
 
290  1 toggle @Test
291    public void createWikiFromTemplateWithoutPR() throws Exception
292    {
293  1 Exception exception = currentScriptHasNotProgrammingRight();
294   
295    // Test
296  1 boolean result = mocker.getComponentUnderTest().createWikiFromTemplate("newWikiId", "newWikiAlias",
297    "templateId", "ownerId", true);
298   
299    // Verify
300  1 assertFalse(result);
301  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
302  1 verify(mocker.getMockedLogger()).error(
303    "Error, you or this script does not have the right to create a wiki from a template.", exception);
304    }
305   
 
306  1 toggle @Test
307    public void createWikiFromTemplateWithoutCreateRight() throws Exception
308    {
309  1 Exception exception = currentUserHasNotCreateWikiRight();
310   
311    // Test
312  1 boolean result = mocker.getComponentUnderTest().createWikiFromTemplate("newWikiId", "newWikiAlias",
313    "templateId", "ownerId", true);
314   
315    // Verify
316  1 assertFalse(result);
317  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
318  1 verify(mocker.getMockedLogger()).error(
319    "Error, you or this script does not have the right to create a wiki from a template.", exception);
320    }
321   
 
322  1 toggle @Test
323    public void createWikiFromTemplateError() throws Exception
324    {
325  1 Exception exception = new WikiTemplateManagerException("error in createWikiFromTemplate.");
326   
327  1 when(wikiTemplateManager.createWikiFromTemplate("newWikiId", "newWikiAlias", "templateId",
328    "ownerId", true)).thenThrow(exception);
329   
330    // Test
331  1 boolean result = mocker.getComponentUnderTest().createWikiFromTemplate("newWikiId", "newWikiAlias",
332    "templateId", "ownerId", true);
333   
334    // Verify
335  1 assertFalse(result);
336  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
337  1 verify(mocker.getMockedLogger()).error("Failed to create the wiki from the template.", exception);
338    }
339   
 
340  1 toggle @Test
341    public void getLastException() throws Exception
342    {
343  1 Exception exception = new Exception("test");
344  1 executionContext.setProperty(WikiTemplateManagerScript.CONTEXT_LASTEXCEPTION, exception);
345  1 assertEquals(exception, mocker.getComponentUnderTest().getLastException());
346    }
347   
 
348  1 toggle @Test
349    public void getWikiProvisioningJobStatus() throws Exception
350    {
351  1 WikiProvisioningJob job = mock(WikiProvisioningJob.class);
352  1 when(wikiTemplateManager.getWikiProvisioningJob(anyList())).thenReturn(job);
353  1 JobStatus status = mock(JobStatus.class);
354  1 when(job.getStatus()).thenReturn(status);
355   
356  1 List<String> jobId = new ArrayList<String>();
357  1 JobStatus result = mocker.getComponentUnderTest().getWikiProvisioningJobStatus(jobId);
358   
359  1 assertEquals(status, result);
360    }
361   
 
362  1 toggle @Test
363    public void getWikiProvisioningJobStatusWithBadId() throws Exception
364    {
365  1 List<String> jobId = new ArrayList<String>();
366  1 JobStatus result = mocker.getComponentUnderTest().getWikiProvisioningJobStatus(jobId);
367   
368  1 assertEquals(null, result);
369    }
370   
 
371  1 toggle @Test
372    public void getWikiProvisioningJobStatusWithException() throws Exception
373    {
374  1 Exception exception = new WikiTemplateManagerException("test");
375  1 when(wikiTemplateManager.getWikiProvisioningJob(anyList())).thenThrow(exception);
376   
377  1 List<String> jobId = new ArrayList<String>();
378  1 JobStatus result = mocker.getComponentUnderTest().getWikiProvisioningJobStatus(jobId);
379   
380  1 assertEquals(null, result);
381  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
382  1 verify(mocker.getMockedLogger()).error("Failed to get tge wiki provisioning job.", exception);
383   
384    }
385   
386    }