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

File WikiCreationJobScriptServicesTest.java

 

Code metrics

0
42
6
1
161
113
6
0.14
7
6
1

Classes

Class Line # Actions
WikiCreationJobScriptServicesTest 59 42 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.platform.wiki.creationjob.script;
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.xwiki.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.extension.ExtensionId;
30    import org.xwiki.extension.distribution.internal.DistributionManager;
31    import org.xwiki.job.Job;
32    import org.xwiki.job.event.status.JobStatus;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.WikiReference;
35    import org.xwiki.platform.wiki.creationjob.WikiCreationRequest;
36    import org.xwiki.platform.wiki.creationjob.WikiCreator;
37    import org.xwiki.security.authorization.AccessDeniedException;
38    import org.xwiki.security.authorization.AuthorizationManager;
39    import org.xwiki.security.authorization.Right;
40    import org.xwiki.test.mockito.MockitoComponentMockingRule;
41    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
42   
43    import com.xpn.xwiki.XWiki;
44    import com.xpn.xwiki.XWikiContext;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.assertNotNull;
48    import static org.junit.Assert.assertNull;
49    import static org.mockito.ArgumentMatchers.any;
50    import static org.mockito.ArgumentMatchers.eq;
51    import static org.mockito.Mockito.doThrow;
52    import static org.mockito.Mockito.mock;
53    import static org.mockito.Mockito.verify;
54    import static org.mockito.Mockito.when;
55   
56    /**
57    * @version $Id: 8e2c5cb97d9068686a03f09d356eabbb38539b52 $
58    */
 
59    public class WikiCreationJobScriptServicesTest
60    {
61    @Rule
62    public MockitoComponentMockingRule<WikiCreationJobScriptServices> mocker =
63    new MockitoComponentMockingRule<>(WikiCreationJobScriptServices.class);
64   
65    private WikiCreator wikiCreator;
66   
67    private Execution execution;
68   
69    private AuthorizationManager authorizationManager;
70   
71    private WikiDescriptorManager wikiDescriptorManager;
72   
73    private DistributionManager distributionManager;
74   
75    private Provider<XWikiContext> xcontextProvider;
76   
77    private XWikiContext xcontext;
78   
79    private XWiki xwiki;
80   
 
81  5 toggle @Before
82    public void setUp() throws Exception
83    {
84  5 wikiCreator = mocker.getInstance(WikiCreator.class);
85  5 execution = mocker.getInstance(Execution.class);
86  5 authorizationManager = mocker.getInstance(AuthorizationManager.class);
87  5 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
88  5 distributionManager = mocker.getInstance(DistributionManager.class);
89  5 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
90  5 xcontext = mock(XWikiContext.class);
91  5 when(xcontextProvider.get()).thenReturn(xcontext);
92  5 xwiki = mock(XWiki.class);
93  5 when(xcontext.getWiki()).thenReturn(xwiki);
94   
95  5 when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWikiId");
96   
97  5 ExecutionContext executionContext = new ExecutionContext();
98  5 when(execution.getContext()).thenReturn(executionContext);
99   
100  5 ExtensionId extensionId = new ExtensionId("authorized-extension", "1.0");
101  5 when(distributionManager.getWikiUIExtensionId()).thenReturn(extensionId);
102    }
103   
 
104  1 toggle @Test
105    public void createWiki() throws Exception
106    {
107  1 Job job = mock(Job.class);
108  1 when(wikiCreator.createWiki(any(WikiCreationRequest.class))).thenReturn(job);
109   
110  1 WikiCreationRequest wikiCreationRequest = new WikiCreationRequest();
111  1 wikiCreationRequest.setExtensionId("authorized-extension", "1.0");
112  1 assertEquals(job, mocker.getComponentUnderTest().createWiki(wikiCreationRequest));
113  1 assertNull(mocker.getComponentUnderTest().getLastError());
114    }
115   
 
116  1 toggle @Test
117    public void createWikiWhenExtensionIsNotAuthorized() throws Exception
118    {
119  1 WikiCreationRequest wikiCreationRequest = new WikiCreationRequest();
120  1 wikiCreationRequest.setExtensionId("badExtension", "version");
121   
122  1 assertNull(mocker.getComponentUnderTest().createWiki(wikiCreationRequest));
123  1 Exception lastError = mocker.getComponentUnderTest().getLastError();
124  1 assertNotNull(lastError);
125  1 assertEquals("The extension [badExtension/version] is not authorized.", lastError.getMessage());
126  1 verify(mocker.getMockedLogger()).warn("Failed to create a new wiki.", lastError);
127    }
128   
 
129  1 toggle @Test
130    public void createWikiWhenNoCreateWikiRight() throws Exception
131    {
132  1 DocumentReference currentUser = new DocumentReference("xwiki", "XWiki", "User");
133  1 when(xcontext.getUserReference()).thenReturn(currentUser);
134  1 AccessDeniedException exception =
135    new AccessDeniedException(Right.CREATE_WIKI, currentUser, new WikiReference("mainWikiId"));
136  1 doThrow(exception).when(authorizationManager).checkAccess(eq(Right.CREATE_WIKI), eq(currentUser),
137    eq(new WikiReference("mainWikiId")));
138   
139  1 WikiCreationRequest wikiCreationRequest = new WikiCreationRequest();
140  1 wikiCreationRequest.setExtensionId("authorized-extension", "1.0");
141   
142  1 assertNull(mocker.getComponentUnderTest().createWiki(wikiCreationRequest));
143  1 Exception lastError = mocker.getComponentUnderTest().getLastError();
144  1 assertNotNull(lastError);
145  1 assertEquals(exception, lastError);
146    }
147   
 
148  1 toggle @Test
149    public void getJobStatus() throws Exception
150    {
151  1 JobStatus jobStatus = mock(JobStatus.class);
152  1 when(wikiCreator.getJobStatus("wikiId")).thenReturn(jobStatus);
153  1 assertEquals(jobStatus, mocker.getComponentUnderTest().getJobStatus("wikiId"));
154    }
155   
 
156  1 toggle @Test
157    public void newWikiCreationRequest() throws Exception
158    {
159  1 assertNotNull(mocker.getComponentUnderTest().newWikiCreationRequest());
160    }
161    }