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

File ExtensionManagerScriptServiceTest.java

 

Code metrics

12
52
13
1
243
158
19
0.37
4
13
1.46

Classes

Class Line # Actions
ExtensionManagerScriptServiceTest 58 52 0% 19 8
0.896103989.6%
 

Contributing tests

This file is covered by 10 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.extension.script;
21   
22    import java.util.HashMap;
23    import java.util.List;
24    import java.util.Map;
25   
26    import org.junit.Assert;
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.extension.InstallException;
31    import org.xwiki.extension.UninstallException;
32    import org.xwiki.extension.job.InstallRequest;
33    import org.xwiki.extension.repository.InstalledExtensionRepository;
34    import org.xwiki.extension.repository.internal.core.CoreExtensionScanner;
35    import org.xwiki.extension.test.MockitoRepositoryUtilsRule;
36    import org.xwiki.job.Job;
37    import org.xwiki.logging.LogLevel;
38    import org.xwiki.logging.event.LogEvent;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.script.service.ScriptService;
41    import org.xwiki.security.authorization.AccessDeniedException;
42    import org.xwiki.security.authorization.Right;
43    import org.xwiki.test.annotation.AfterComponent;
44    import org.xwiki.test.annotation.AllComponents;
45    import org.xwiki.test.mockito.MockitoComponentManagerRule;
46   
47    import com.xpn.xwiki.XWiki;
48    import com.xpn.xwiki.objects.classes.BaseClass;
49    import com.xpn.xwiki.test.MockitoOldcoreRule;
50    import com.xpn.xwiki.util.XWikiStubContextProvider;
51   
52    import static org.junit.Assert.assertEquals;
53    import static org.junit.Assert.assertNotNull;
54    import static org.mockito.Mockito.doThrow;
55    import static org.mockito.Mockito.mock;
56   
57    @AllComponents
 
58    public class ExtensionManagerScriptServiceTest
59    {
60    private MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
61   
62    private MockitoOldcoreRule xwikiBridge = new MockitoOldcoreRule(this.mocker);
63   
64    @Rule
65    public MockitoRepositoryUtilsRule repositoryUtil = new MockitoRepositoryUtilsRule(this.mocker, this.xwikiBridge);
66   
67    private XWiki mockXWiki;
68   
69    private Map<String, BaseClass> classes = new HashMap<String, BaseClass>();
70   
71    private DocumentReference contextUser;
72   
73    private ExtensionManagerScriptService scriptService;
74   
 
75  10 toggle @Before
76    public void before() throws Exception
77    {
78    // mock
79   
80  10 this.mockXWiki = mock(XWiki.class);
81   
82  10 this.xwikiBridge.getXWikiContext().setWiki(this.mockXWiki);
83  10 this.xwikiBridge.getXWikiContext().setWikiId("xwiki");
84  10 this.contextUser =
85    new DocumentReference(this.xwikiBridge.getXWikiContext().getWikiId(), "XWiki", "ExtensionUser");
86   
87    // classes
88   
89  10 BaseClass styleSheetClass = new BaseClass();
90  10 this.classes.put("StyleSheetExtension", styleSheetClass);
91   
92    // checking
93   
94  10 this.xwikiBridge.getXWikiContext().setUserReference(this.contextUser);
95   
96  10 ((XWikiStubContextProvider) this.mocker.getInstance(XWikiStubContextProvider.class))
97    .initialize(this.xwikiBridge.getXWikiContext());
98   
99    // lookup
100   
101  10 this.scriptService = this.mocker.getInstance(ScriptService.class, "extension");
102    }
103   
104    // tools
105   
 
106  4 toggle private Job install(String id, String version, String namespace) throws Throwable
107    {
108  4 Job job = this.scriptService.install(id, version, namespace);
109  4 if (job == null) {
110  0 throw this.scriptService.getLastError();
111    }
112   
113  4 job.join();
114   
115  4 List<LogEvent> errors = job.getStatus().getLog().getLogsFrom(LogLevel.WARN);
116  4 if (!errors.isEmpty()) {
117  2 throw errors.get(0).getThrowable();
118    }
119   
120  2 return job;
121    }
122   
 
123  4 toggle private Job uninstall(String id, String namespace) throws Throwable
124    {
125  4 Job job = this.scriptService.uninstall(id, namespace);
126  4 if (job == null) {
127  0 throw this.scriptService.getLastError();
128    }
129   
130  4 job.join();
131   
132  4 List<LogEvent> errors = job.getStatus().getLog().getLogsFrom(LogLevel.WARN);
133  4 if (!errors.isEmpty()) {
134  2 throw errors.get(0).getThrowable();
135    }
136   
137  2 return job;
138    }
139   
140    // Tests
141   
142    // install
143   
 
144  1 toggle @Test
145    public void testInstallOnRoot() throws Throwable
146    {
147  1 install("extension", "version", null);
148    }
149   
 
150  1 toggle @Test
151    public void testInstallOnNamespace() throws Throwable
152    {
153  1 install("extension", "version", "namespace");
154    }
155   
 
156  1 toggle @Test
157    public void testOverwriteAllowedNamespaces() throws Throwable
158    {
159  1 InstallRequest installRequest = this.scriptService.createInstallRequest("extension", "version", "namespace");
160   
161    // Indicate all extensions of type "test" should be installed on root
162  1 ((ScriptExtensionRewriter) installRequest.getRewriter()).installExtensionTypeOnRootNamespace("test");
163   
164    // Allow redirect on root
165  1 installRequest.setRootModificationsAllowed(true);
166   
167  1 Job job = this.scriptService.install(installRequest);
168  1 if (job == null) {
169  0 throw this.scriptService.getLastError();
170    }
171   
172  1 job.join();
173   
174  1 List<LogEvent> errors = job.getStatus().getLog().getLogsFrom(LogLevel.WARN);
175  1 if (!errors.isEmpty()) {
176  0 throw errors.get(0).getThrowable();
177    }
178   
179    // Validate
180   
181  1 InstalledExtensionRepository repository = mocker.getInstance(InstalledExtensionRepository.class);
182   
183  1 assertNotNull(repository.getInstalledExtension("extension", null));
184    }
185   
 
186  1 toggle @Test(expected = InstallException.class)
187    public void testInstallOnRootWithoutProgrammingRigths() throws Throwable
188    {
189  1 doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager())
190    .checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null);
191   
192  1 install("extension", "version", null);
193    }
194   
 
195  1 toggle @Test(expected = InstallException.class)
196    public void testInstallOnNamespaceWithoutProgrammingRigths() throws Throwable
197    {
198  1 doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager())
199    .checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null);
200   
201  1 install("extension", "version", "namespace");
202    }
203   
204    // uninstall
205   
 
206  1 toggle @Test
207    public void testUninstallFromRoot() throws Throwable
208    {
209  1 uninstall("installedonroot", null);
210    }
211   
 
212  1 toggle @Test
213    public void testUninstallOnNamespace() throws Throwable
214    {
215  1 uninstall("installedonnamespace", "namespace");
216    }
217   
 
218  1 toggle @Test(expected = UninstallException.class)
219    public void testUninstallOnRootWithoutProgrammingRigths() throws Throwable
220    {
221  1 doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager())
222    .checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null);
223   
224  1 uninstall("installedonroot", null);
225    }
226   
 
227  1 toggle @Test(expected = UninstallException.class)
228    public void testUninstallOnNamespaceWithoutProgrammingRigths() throws Throwable
229    {
230  1 doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager())
231    .checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null);
232   
233  1 uninstall("installedonnamespace", "namespace");
234    }
235   
 
236  1 toggle @Test
237    public void testGet()
238    {
239  1 Assert.assertNotNull(this.scriptService.get(CoreExtensionScriptService.ID));
240  1 Assert.assertNotNull(this.scriptService.get(LocalExtensionScriptService.ID));
241  1 Assert.assertNotNull(this.scriptService.get(InstalledExtensionScriptService.ID));
242    }
243    }