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

File RefactoringScriptServiceTest.java

 

Code metrics

0
119
20
1
333
256
20
0.17
5.95
20
1

Classes

Class Line # Actions
RefactoringScriptServiceTest 64 119 0% 20 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.refactoring.script;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import org.junit.jupiter.api.BeforeEach;
26    import org.junit.jupiter.api.Test;
27    import org.xwiki.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.job.JobException;
30    import org.xwiki.job.JobExecutor;
31    import org.xwiki.model.EntityType;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceProvider;
35    import org.xwiki.model.reference.SpaceReference;
36    import org.xwiki.model.reference.WikiReference;
37    import org.xwiki.refactoring.job.AbstractCopyOrMoveRequest;
38    import org.xwiki.refactoring.job.CopyRequest;
39    import org.xwiki.refactoring.job.CreateRequest;
40    import org.xwiki.refactoring.job.EntityRequest;
41    import org.xwiki.refactoring.job.MoveRequest;
42    import org.xwiki.refactoring.job.PermanentlyDeleteRequest;
43    import org.xwiki.refactoring.job.RefactoringJobs;
44    import org.xwiki.refactoring.job.RestoreRequest;
45    import org.xwiki.security.authorization.ContextualAuthorizationManager;
46    import org.xwiki.security.authorization.Right;
47    import org.xwiki.test.junit5.mockito.ComponentTest;
48    import org.xwiki.test.junit5.mockito.InjectMockComponents;
49    import org.xwiki.test.junit5.mockito.MockComponent;
50   
51    import static org.junit.Assert.assertEquals;
52    import static org.junit.Assert.assertFalse;
53    import static org.junit.Assert.assertNull;
54    import static org.junit.Assert.assertSame;
55    import static org.mockito.Mockito.verify;
56    import static org.mockito.Mockito.when;
57   
58    /**
59    * Unit tests for {@link RefactoringScriptService}.
60    *
61    * @version $Id: c4b31a3c69c43c1e0632c2853116ad6bffc9cb38 $
62    */
63    @ComponentTest
 
64    public class RefactoringScriptServiceTest
65    {
66    @InjectMockComponents
67    private RefactoringScriptService refactoringScriptService;
68   
69    @MockComponent
70    private JobExecutor jobExecutor;
71   
72    @MockComponent
73    private RequestFactory requestFactory;
74   
75    @MockComponent
76    private EntityReferenceProvider defaultEntityReferenceProvider;
77   
78    @MockComponent
79    private Execution execution;
80   
81    @MockComponent
82    private ContextualAuthorizationManager authorization;
83   
84    private ExecutionContext executionContext = new ExecutionContext();
85   
 
86  17 toggle @BeforeEach
87    public void setup()
88    {
89  17 when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(
90    new EntityReference("WebHome", EntityType.DOCUMENT));
91  17 when(execution.getContext()).thenReturn(executionContext);
92    }
93   
 
94  6 toggle private void fillCopyOrMoveRequest(AbstractCopyOrMoveRequest copyOrMoveRequest)
95    {
96  6 this.fillEntityRequest(copyOrMoveRequest);
97  6 copyOrMoveRequest.setDestination(new SpaceReference("wiki", "newSpace"));
98    }
99   
 
100  8 toggle private void fillEntityRequest(EntityRequest entityRequest)
101    {
102  8 entityRequest.setEntityReferences(Arrays.asList(new SpaceReference("wiki", "Space")));
103    }
104   
 
105  1 toggle @Test
106    public void move() throws Exception
107    {
108  1 SpaceReference source = new SpaceReference("Space", new WikiReference("math"));
109  1 WikiReference destination = new WikiReference("code");
110  1 MoveRequest moveRequest = new MoveRequest();
111  1 this.fillCopyOrMoveRequest(moveRequest);
112  1 when(this.requestFactory.createMoveRequest(source, destination)).thenReturn(moveRequest);
113   
114  1 this.refactoringScriptService.move(source, destination);
115  1 verify(this.requestFactory).createMoveRequest(source, destination);
116  1 verify(this.jobExecutor).execute(RefactoringJobs.MOVE, moveRequest);
117    }
118   
 
119  1 toggle @Test
120    public void moveWithoutPR() throws Exception
121    {
122  1 MoveRequest request = new MoveRequest();
123  1 request.setCheckRights(false);
124  1 request.setUserReference(new DocumentReference("wiki", "Users", "Bob"));
125   
126  1 this.refactoringScriptService.move(request);
127   
128  1 verify(this.requestFactory).setRightsProperties(request);
129    }
130   
 
131  1 toggle @Test
132    public void moveWithPR() throws Exception
133    {
134  1 MoveRequest request = new MoveRequest();
135  1 request.setCheckRights(false);
136   
137  1 DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
138  1 request.setUserReference(bobReference);
139   
140  1 when(authorization.hasAccess(Right.PROGRAM)).thenReturn(true);
141   
142  1 this.refactoringScriptService.move(request);
143   
144  1 assertFalse(request.isCheckRights());
145  1 assertEquals(bobReference, request.getUserReference());
146    }
147   
 
148  1 toggle @Test
149    public void moveWithException() throws Exception
150    {
151  1 MoveRequest request = new MoveRequest();
152  1 JobException exception = new JobException("Some error message");
153  1 when(this.jobExecutor.execute(RefactoringJobs.MOVE, request)).thenThrow(exception);
154   
155  1 assertNull(this.refactoringScriptService.move(request));
156  1 assertSame(exception, this.refactoringScriptService.getLastError());
157    }
158   
 
159  1 toggle @Test
160    public void rename() throws Exception
161    {
162  1 SpaceReference spaceReference =
163    new SpaceReference("Alice", new SpaceReference("Users", new WikiReference("dev")));
164  1 String newName = "Bob";
165  1 MoveRequest moveRequest = new MoveRequest();
166  1 this.fillCopyOrMoveRequest(moveRequest);
167  1 when(this.requestFactory.createRenameRequest(spaceReference, newName)).thenReturn(moveRequest);
168   
169  1 this.refactoringScriptService.rename(spaceReference, newName);
170  1 verify(this.requestFactory).createRenameRequest(spaceReference, newName);
171  1 verify(this.jobExecutor).execute(RefactoringJobs.RENAME, moveRequest);
172    }
173   
 
174  1 toggle @Test
175    public void copy() throws Exception
176    {
177  1 SpaceReference source = new SpaceReference("Space", new WikiReference("math"));
178  1 WikiReference destination = new WikiReference("code");
179  1 CopyRequest copyRequest = new CopyRequest();
180  1 this.fillCopyOrMoveRequest(copyRequest);
181  1 when(this.requestFactory.createCopyRequest(source, destination)).thenReturn(copyRequest);
182   
183  1 this.refactoringScriptService.copy(source, destination);
184  1 verify(this.requestFactory).createCopyRequest(source, destination);
185  1 verify(this.jobExecutor).execute(RefactoringJobs.COPY, copyRequest);
186    }
187   
 
188  1 toggle @Test
189    public void copyAs() throws Exception
190    {
191  1 SpaceReference spaceReference =
192    new SpaceReference("Alice", new SpaceReference("Users", new WikiReference("dev")));
193  1 String newName = "Bob";
194  1 CopyRequest copyRequest = new CopyRequest();
195  1 this.fillCopyOrMoveRequest(copyRequest);
196   
197  1 when(this.requestFactory.createCopyAsRequest(spaceReference, newName)).thenReturn(copyRequest);
198  1 this.refactoringScriptService.copyAs(spaceReference, newName);
199  1 verify(this.requestFactory).createCopyAsRequest(spaceReference, newName);
200  1 verify(this.jobExecutor).execute(RefactoringJobs.COPY_AS, copyRequest);
201    }
202   
 
203  1 toggle @Test
204    public void delete() throws Exception
205    {
206  1 WikiReference source = new WikiReference("math");
207   
208  1 EntityRequest deleteRequest = new EntityRequest();
209  1 this.fillEntityRequest(deleteRequest);
210  1 when(this.requestFactory.createDeleteRequest(Arrays.asList(source))).thenReturn(deleteRequest);
211   
212  1 this.refactoringScriptService.delete(source);
213  1 verify(this.requestFactory).createDeleteRequest(Arrays.asList(source));
214  1 verify(this.jobExecutor).execute(RefactoringJobs.DELETE, deleteRequest);
215    }
216   
 
217  1 toggle @Test
218    public void create() throws Exception
219    {
220  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
221  1 CreateRequest createRequest = new CreateRequest();
222  1 this.fillEntityRequest(createRequest);
223  1 when(this.requestFactory.createCreateRequest(Arrays.asList(documentReference))).thenReturn(createRequest);
224   
225  1 this.refactoringScriptService.create(documentReference);
226  1 verify(this.requestFactory).createCreateRequest(Arrays.asList(documentReference));
227  1 verify(this.jobExecutor).execute(RefactoringJobs.CREATE, createRequest);
228    }
229   
 
230  1 toggle @Test
231    public void convertToNestedDocumentAlreadyNested() throws Exception
232    {
233  1 DocumentReference nestedDocumentReference =
234    new DocumentReference("code", Arrays.asList("Model", "Entity"), "WebHome");
235   
236  1 assertNull(this.refactoringScriptService.convertToNestedDocument(nestedDocumentReference));
237    }
238   
 
239  1 toggle @Test
240    public void convertToNestedDocument() throws Exception
241    {
242  1 DocumentReference terminalDocumentReference = new DocumentReference("code", "Model", "Entity");
243  1 DocumentReference nestedDocumentReference =
244    new DocumentReference("code", Arrays.asList("Model", "Entity"), "WebHome");
245  1 MoveRequest moveRequest = new MoveRequest();
246  1 this.fillCopyOrMoveRequest(moveRequest);
247  1 when(this.requestFactory.createRenameRequest(terminalDocumentReference, nestedDocumentReference))
248    .thenReturn(moveRequest);
249   
250  1 this.refactoringScriptService.convertToNestedDocument(terminalDocumentReference);
251  1 verify(this.requestFactory).createRenameRequest(terminalDocumentReference, nestedDocumentReference);
252  1 verify(this.jobExecutor).execute(RefactoringJobs.RENAME, moveRequest);
253    }
254   
 
255  1 toggle @Test
256    public void convertToTerminalDocumentAlreadyTerminal() throws Exception
257    {
258  1 DocumentReference terminalDocumentReference = new DocumentReference("code", "Model", "Entity");
259  1 assertNull(this.refactoringScriptService.convertToTerminalDocument(terminalDocumentReference));
260   
261  1 DocumentReference rootDocumentReference = new DocumentReference("wiki", "Space", "WebHome");
262  1 assertNull(this.refactoringScriptService.convertToTerminalDocument(rootDocumentReference));
263    }
264   
 
265  1 toggle @Test
266    public void convertToTerminalDocument() throws Exception
267    {
268  1 DocumentReference terminalDocumentReference = new DocumentReference("code", "Model", "Entity");
269  1 DocumentReference nestedDocumentReference =
270    new DocumentReference("code", Arrays.asList("Model", "Entity"), "WebHome");
271   
272  1 MoveRequest moveRequest = new MoveRequest();
273  1 this.fillCopyOrMoveRequest(moveRequest);
274  1 when(this.requestFactory.createRenameRequest(nestedDocumentReference, terminalDocumentReference))
275    .thenReturn(moveRequest);
276   
277  1 this.refactoringScriptService.convertToTerminalDocument(nestedDocumentReference);
278  1 verify(this.requestFactory).createRenameRequest(nestedDocumentReference, terminalDocumentReference);
279  1 verify(this.jobExecutor).execute(RefactoringJobs.RENAME, moveRequest);
280    }
281   
 
282  1 toggle @Test
283    public void restore() throws Exception
284    {
285  1 List<Long> documentIds = Arrays.asList(1L, 2L, 3L);
286  1 RestoreRequest restoreRequest = new RestoreRequest();
287  1 restoreRequest.setDeletedDocumentIds(documentIds);
288  1 when(this.requestFactory.createRestoreRequest(documentIds)).thenReturn(restoreRequest);
289   
290  1 this.refactoringScriptService.restore(documentIds);
291  1 verify(this.requestFactory).createRestoreRequest(documentIds);
292  1 verify(this.jobExecutor).execute(RefactoringJobs.RESTORE, restoreRequest);
293    }
294   
 
295  1 toggle @Test
296    public void restoreBatch() throws Exception
297    {
298  1 String batchid = "mybatch-id";
299  1 RestoreRequest restoreRequest = new RestoreRequest();
300  1 restoreRequest.setBatchId(batchid);
301  1 when(this.requestFactory.createRestoreRequest(batchid)).thenReturn(restoreRequest);
302   
303  1 this.refactoringScriptService.restore(batchid);
304  1 verify(this.requestFactory).createRestoreRequest(batchid);
305  1 verify(this.jobExecutor).execute(RefactoringJobs.RESTORE, restoreRequest);
306    }
307   
 
308  1 toggle @Test
309    public void permanentlyDelete() throws Exception
310    {
311  1 List<Long> documentIds = Arrays.asList(1L, 2L, 3L);
312  1 PermanentlyDeleteRequest permanentlyDeleteRequest = new PermanentlyDeleteRequest();
313  1 permanentlyDeleteRequest.setDeletedDocumentIds(documentIds);
314  1 when(this.requestFactory.createPermanentlyDeleteRequest(documentIds)).thenReturn(permanentlyDeleteRequest);
315   
316  1 this.refactoringScriptService.permanentlyDelete(documentIds);
317  1 verify(this.requestFactory).createPermanentlyDeleteRequest(documentIds);
318  1 verify(this.jobExecutor).execute(RefactoringJobs.PERMANENTLY_DELETE, permanentlyDeleteRequest);
319    }
320   
 
321  1 toggle @Test
322    public void permanentlyDeleteBatch() throws Exception
323    {
324  1 String batchid = "mybatch-id";
325  1 PermanentlyDeleteRequest permanentlyDeleteRequest = new PermanentlyDeleteRequest();
326  1 permanentlyDeleteRequest.setBatchId(batchid);
327  1 when(this.requestFactory.createPermanentlyDeleteRequest(batchid)).thenReturn(permanentlyDeleteRequest);
328   
329  1 this.refactoringScriptService.permanentlyDelete(batchid);
330  1 verify(this.requestFactory).createPermanentlyDeleteRequest(batchid);
331  1 verify(this.jobExecutor).execute(RefactoringJobs.PERMANENTLY_DELETE, permanentlyDeleteRequest);
332    }
333    }