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

File ExtensionHistoryScriptServiceTest.java

 

Code metrics

0
82
5
1
224
158
5
0.06
16.4
5
1

Classes

Class Line # Actions
ExtensionHistoryScriptServiceTest 63 82 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 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.Arrays;
23    import java.util.List;
24   
25    import javax.inject.Provider;
26   
27    import org.apache.commons.collections4.Predicate;
28    import org.apache.commons.lang3.StringUtils;
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.mockito.ArgumentCaptor;
33    import org.xwiki.bridge.DocumentAccessBridge;
34    import org.xwiki.context.Execution;
35    import org.xwiki.context.ExecutionContext;
36    import org.xwiki.extension.job.InstallRequest;
37    import org.xwiki.extension.job.UninstallRequest;
38    import org.xwiki.extension.job.history.ExtensionJobHistory;
39    import org.xwiki.extension.job.history.ExtensionJobHistoryRecord;
40    import org.xwiki.extension.job.history.ReplayRequest;
41    import org.xwiki.extension.job.history.internal.ReplayJob;
42    import org.xwiki.job.Job;
43    import org.xwiki.job.JobExecutor;
44    import org.xwiki.model.reference.DocumentReference;
45    import org.xwiki.model.reference.WikiReference;
46    import org.xwiki.script.service.ScriptService;
47    import org.xwiki.security.authorization.ContextualAuthorizationManager;
48    import org.xwiki.security.authorization.Right;
49    import org.xwiki.test.mockito.MockitoComponentMockingRule;
50   
51    import com.xpn.xwiki.XWikiContext;
52   
53    import static org.junit.Assert.*;
54    import static org.mockito.ArgumentMatchers.*;
55    import static org.mockito.Mockito.*;
56   
57    /**
58    * Unit tests for {@link ExtensionHistoryScriptService}.
59    *
60    * @version $Id: 47214153e58319221db8eb073729d03a6cc1a5e7 $
61    * @since 7.1RC1
62    */
 
63    public class ExtensionHistoryScriptServiceTest
64    {
65    @Rule
66    public MockitoComponentMockingRule<ExtensionHistoryScriptService> mocker =
67    new MockitoComponentMockingRule<ExtensionHistoryScriptService>(ExtensionHistoryScriptService.class,
68    ScriptService.class);
69   
70    private XWikiContext xcontext = mock(XWikiContext.class);
71   
72    private ExecutionContext executionContext = new ExecutionContext();
73   
74    private JobExecutor jobExecutor;
75   
76    private ContextualAuthorizationManager authorization;
77   
78    private DocumentAccessBridge documentAccessBridge;
79   
 
80  4 toggle @Before
81    public void configure() throws Exception
82    {
83  4 Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
84  4 when(xcontextProvider.get()).thenReturn(this.xcontext);
85   
86  4 Execution execution = this.mocker.getInstance(Execution.class);
87  4 when(execution.getContext()).thenReturn(this.executionContext);
88   
89  4 this.jobExecutor = this.mocker.getInstance(JobExecutor.class);
90  4 this.authorization = this.mocker.getInstance(ContextualAuthorizationManager.class);
91  4 this.documentAccessBridge = this.mocker.getInstance(DocumentAccessBridge.class);
92    }
93   
 
94  1 toggle @Test
95    public void getRecords() throws Exception
96    {
97  1 InstallRequest devInstallReq = new InstallRequest();
98  1 devInstallReq.addNamespace("wiki:dev");
99  1 ExtensionJobHistoryRecord devInstall =
100    new ExtensionJobHistoryRecord("install", devInstallReq, null, null, null);
101   
102  1 UninstallRequest devUninstallReq = new UninstallRequest();
103  1 devUninstallReq.addNamespace("wiki:dev");
104  1 ExtensionJobHistoryRecord devUninstall =
105    new ExtensionJobHistoryRecord("uninstall", devUninstallReq, null, null, null);
106   
107  1 ExtensionJobHistoryRecord globalInstall =
108    new ExtensionJobHistoryRecord("install", new InstallRequest(), null, null, null);
109   
110  1 ExtensionJobHistoryRecord globalUninstall =
111    new ExtensionJobHistoryRecord("uninstall", new UninstallRequest(), null, null, null);
112   
113  1 InstallRequest draftsInstallReq = new InstallRequest();
114  1 draftsInstallReq.addNamespace("wiki:drafts");
115  1 ExtensionJobHistoryRecord draftsInstall =
116    new ExtensionJobHistoryRecord("install", draftsInstallReq, null, null, null);
117   
118  1 List<ExtensionJobHistoryRecord> records = Arrays.asList(devInstall, globalInstall);
119   
120  1 ExtensionJobHistory history = this.mocker.getInstance(ExtensionJobHistory.class);
121  1 ArgumentCaptor<Predicate<ExtensionJobHistoryRecord>> predicateCaptor =
122    ArgumentCaptor.forClass((Class) Predicate.class);
123  1 when(history.getRecords(predicateCaptor.capture(), eq("offsetRecordId"), eq(5))).thenReturn(records);
124   
125  1 when(this.xcontext.getWikiId()).thenReturn("dev");
126   
127  1 assertEquals(
128    records,
129    this.mocker.getComponentUnderTest().getRecords().fromThisWiki().ofType(Arrays.asList("install"))
130    .list("offsetRecordId", 5));
131   
132  1 Predicate<ExtensionJobHistoryRecord> predicate = predicateCaptor.getValue();
133  1 assertTrue(predicate.evaluate(devInstall));
134  1 assertTrue(predicate.evaluate(globalInstall));
135  1 assertFalse(predicate.evaluate(devUninstall));
136  1 assertFalse(predicate.evaluate(globalUninstall));
137  1 assertFalse(predicate.evaluate(draftsInstall));
138    }
139   
 
140  1 toggle @Test
141    public void replayWithoutAdmin() throws Exception
142    {
143  1 InstallRequest installRequest = new InstallRequest();
144  1 installRequest.addNamespace("wiki:dev");
145  1 ExtensionJobHistoryRecord install = new ExtensionJobHistoryRecord("install", installRequest, null, null, null);
146  1 List<ExtensionJobHistoryRecord> records = Arrays.asList(install);
147   
148  1 when(this.xcontext.getWikiId()).thenReturn("dev");
149  1 when(this.authorization.hasAccess(Right.ADMIN, new WikiReference("dev"))).thenReturn(false);
150   
151  1 Job replayJob = mock(Job.class);
152  1 ArgumentCaptor<ReplayRequest> requestCaptor = ArgumentCaptor.forClass(ReplayRequest.class);
153  1 when(jobExecutor.execute(eq(ReplayJob.JOB_TYPE), requestCaptor.capture())).thenReturn(replayJob);
154   
155  1 assertSame(replayJob, this.mocker.getComponentUnderTest().replay(records));
156   
157  1 ReplayRequest request = requestCaptor.getValue();
158  1 assertTrue(request.getRecords().isEmpty());
159    }
160   
 
161  1 toggle @Test
162    public void replayWithAdminButNoPR() throws Exception
163    {
164  1 InstallRequest installRequest = new InstallRequest();
165  1 installRequest.addNamespace("wiki:drafts");
166  1 installRequest.setProperty("user.reference", new DocumentReference("drafts", "Users", "Alice"));
167  1 ExtensionJobHistoryRecord install = new ExtensionJobHistoryRecord("install", installRequest, null, null, null);
168  1 List<ExtensionJobHistoryRecord> records = Arrays.asList(install);
169   
170  1 when(this.xcontext.getWikiId()).thenReturn("dev");
171  1 when(this.documentAccessBridge.getCurrentUserReference()).thenReturn(
172    new DocumentReference("dev", "Users", "Bob"));
173  1 when(this.authorization.hasAccess(Right.ADMIN, new WikiReference("dev"))).thenReturn(true);
174  1 when(this.authorization.hasAccess(Right.PROGRAM)).thenReturn(false);
175   
176  1 Job replayJob = mock(Job.class);
177  1 ArgumentCaptor<ReplayRequest> requestCaptor = ArgumentCaptor.forClass(ReplayRequest.class);
178  1 when(jobExecutor.execute(eq(ReplayJob.JOB_TYPE), requestCaptor.capture())).thenReturn(replayJob);
179   
180  1 assertSame(replayJob, this.mocker.getComponentUnderTest().replay(records));
181   
182  1 ReplayRequest request = requestCaptor.getValue();
183  1 assertEquals(Arrays.asList(install), request.getRecords());
184   
185  1 assertEquals(Arrays.asList("wiki:dev"), install.getRequest().getNamespaces());
186  1 assertEquals(this.documentAccessBridge.getCurrentUserReference(),
187    install.getRequest().getProperty("user.reference"));
188    }
189   
 
190  1 toggle @Test
191    public void replayWithPR() throws Exception
192    {
193  1 InstallRequest installRequest = new InstallRequest();
194  1 installRequest.addNamespace("wiki:drafts");
195  1 installRequest.setProperty("user.reference", new DocumentReference("drafts", "Users", "Alice"));
196  1 ExtensionJobHistoryRecord install = new ExtensionJobHistoryRecord("install", installRequest, null, null, null);
197  1 List<ExtensionJobHistoryRecord> records = Arrays.asList(install);
198   
199  1 when(this.xcontext.getWikiId()).thenReturn("dev");
200  1 when(this.xcontext.getAction()).thenReturn("foo");
201  1 when(this.documentAccessBridge.getCurrentUserReference()).thenReturn(
202    new DocumentReference("dev", "Users", "Bob"));
203  1 when(this.authorization.hasAccess(Right.ADMIN, new WikiReference("dev"))).thenReturn(true);
204  1 when(this.authorization.hasAccess(Right.PROGRAM)).thenReturn(true);
205   
206  1 Job replayJob = mock(Job.class);
207  1 ArgumentCaptor<ReplayRequest> requestCaptor = ArgumentCaptor.forClass(ReplayRequest.class);
208  1 when(jobExecutor.execute(eq(ReplayJob.JOB_TYPE), requestCaptor.capture())).thenReturn(replayJob);
209   
210  1 assertSame(replayJob, this.mocker.getComponentUnderTest().replay(records));
211   
212  1 ReplayRequest request = requestCaptor.getValue();
213  1 assertTrue(StringUtils.join(request.getId(), '/').startsWith("extension/history/"));
214  1 assertTrue(request.isInteractive());
215  1 assertEquals(Arrays.asList(install), request.getRecords());
216  1 assertEquals(this.xcontext.getWikiId(), request.getProperty("context.wiki"));
217  1 assertEquals(this.xcontext.getAction(), request.getProperty("context.action"));
218  1 assertEquals(this.documentAccessBridge.getCurrentUserReference(), request.getProperty("user.reference"));
219   
220  1 assertEquals(Arrays.asList("wiki:drafts"), install.getRequest().getNamespaces());
221  1 assertEquals(new DocumentReference("drafts", "Users", "Alice"),
222    install.getRequest().getProperty("user.reference"));
223    }
224    }