1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.refactoring.script

File RefactoringScriptServiceTest.java

 

Code metrics

0
100
14
1
282
202
14
0.14
7.14
14
1

Classes

Class Line # Actions
RefactoringScriptServiceTest 61 100 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 11 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   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.mockito.ArgumentCaptor;
28    import org.xwiki.bridge.DocumentAccessBridge;
29    import org.xwiki.component.manager.ComponentLookupException;
30    import org.xwiki.context.Execution;
31    import org.xwiki.context.ExecutionContext;
32    import org.xwiki.job.Job;
33    import org.xwiki.job.JobException;
34    import org.xwiki.job.JobExecutor;
35    import org.xwiki.job.script.JobScriptService;
36    import org.xwiki.model.EntityType;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.EntityReference;
39    import org.xwiki.model.reference.EntityReferenceProvider;
40    import org.xwiki.model.reference.SpaceReference;
41    import org.xwiki.model.reference.WikiReference;
42    import org.xwiki.refactoring.job.CreateRequest;
43    import org.xwiki.refactoring.job.EntityRequest;
44    import org.xwiki.refactoring.job.MoveRequest;
45    import org.xwiki.refactoring.job.RefactoringJobs;
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.annotation.BeforeComponent;
50    import org.xwiki.test.mockito.MockitoComponentMockingRule;
51   
52    import static org.junit.Assert.*;
53    import static org.mockito.ArgumentMatchers.*;
54    import static org.mockito.Mockito.*;
55   
56    /**
57    * Unit tests for {@link RefactoringScriptService}.
58    *
59    * @version $Id: 5c6adb13382433079fe3d9c9bfaf5507bb4c70f4 $
60    */
 
61    public class RefactoringScriptServiceTest
62    {
63    @Rule
64    public MockitoComponentMockingRule<ScriptService> mocker = new MockitoComponentMockingRule<ScriptService>(
65    RefactoringScriptService.class);
66   
67    private JobExecutor jobExecutor;
68   
69    private DocumentReference userReference = new DocumentReference("wiki", "Users", "Carol");
70   
71    private ExecutionContext executionContext = new ExecutionContext();
72   
73    private JobScriptService jobScriptService = mock(JobScriptService.class);
74   
 
75  11 toggle @BeforeComponent
76    public void initializeComponents() throws Exception
77    {
78  11 this.mocker.registerComponent(ScriptService.class, "job", jobScriptService);
79    }
80   
 
81  11 toggle @Before
82    public void configure() throws Exception
83    {
84  11 this.jobExecutor = this.mocker.getInstance(JobExecutor.class);
85   
86  11 Execution execution = this.mocker.getInstance(Execution.class);
87  11 when(execution.getContext()).thenReturn(executionContext);
88   
89  11 DocumentAccessBridge documentAccessBridge = this.mocker.getInstance(DocumentAccessBridge.class);
90  11 when(documentAccessBridge.getCurrentUserReference()).thenReturn(this.userReference);
91   
92  11 EntityReferenceProvider defaultEntityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
93  11 when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(
94    new EntityReference("WebHome", EntityType.DOCUMENT, null));
95    }
96   
 
97  1 toggle @Test
98    public void move() throws Exception
99    {
100  1 SpaceReference source = new SpaceReference("Space", new WikiReference("math"));
101  1 WikiReference destination = new WikiReference("code");
102   
103  1 Job job = mock(Job.class);
104  1 ArgumentCaptor<MoveRequest> request = ArgumentCaptor.forClass(MoveRequest.class);
105  1 when(this.jobExecutor.execute(eq(RefactoringJobs.MOVE), request.capture())).thenReturn(job);
106   
107  1 assertSame(job, getService().move(source, destination));
108   
109  1 assertEquals(Arrays.asList(source), request.getValue().getEntityReferences());
110  1 assertEquals(destination, request.getValue().getDestination());
111  1 assertEquals(Arrays.asList(RefactoringJobs.GROUP, "move"), request.getValue().getId().subList(0, 2));
112  1 assertEquals(RefactoringJobs.MOVE, request.getValue().getJobType());
113  1 assertEquals(this.userReference, request.getValue().getUserReference());
114  1 assertEquals(false, request.getValue().isDeep());
115  1 assertEquals(true, request.getValue().isDeleteSource());
116  1 assertEquals(true, request.getValue().isUpdateLinks());
117  1 assertEquals(true, request.getValue().isAutoRedirect());
118  1 assertEquals(false, request.getValue().isInteractive());
119  1 assertEquals(true, request.getValue().isCheckRights());
120    }
121   
 
122  1 toggle @Test
123    public void moveWithoutPR() throws Exception
124    {
125  1 MoveRequest request = new MoveRequest();
126  1 request.setCheckRights(false);
127  1 request.setUserReference(new DocumentReference("wiki", "Users", "Bob"));
128   
129  1 getService().move(request);
130   
131  1 assertTrue(request.isCheckRights());
132  1 assertEquals(this.userReference, request.getUserReference());
133    }
134   
 
135  1 toggle @Test
136    public void moveWithPR() throws Exception
137    {
138  1 MoveRequest request = new MoveRequest();
139  1 request.setCheckRights(false);
140   
141  1 DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
142  1 request.setUserReference(bobReference);
143   
144  1 ContextualAuthorizationManager authorization = this.mocker.getInstance(ContextualAuthorizationManager.class);
145  1 when(authorization.hasAccess(Right.PROGRAM)).thenReturn(true);
146   
147  1 getService().move(request);
148   
149  1 assertFalse(request.isCheckRights());
150  1 assertEquals(bobReference, request.getUserReference());
151    }
152   
 
153  1 toggle @Test
154    public void moveWithException() throws Exception
155    {
156  1 MoveRequest request = new MoveRequest();
157  1 JobException exception = new JobException("Some error message");
158  1 when(this.jobExecutor.execute(RefactoringJobs.MOVE, request)).thenThrow(exception);
159   
160  1 assertNull(getService().move(request));
161  1 assertSame(exception, getService().getLastError());
162    }
163   
 
164  1 toggle @Test
165    public void rename() throws Exception
166    {
167  1 SpaceReference spaceReference =
168    new SpaceReference("Alice", new SpaceReference("Users", new WikiReference("dev")));
169  1 getService().rename(spaceReference, "Bob");
170   
171  1 ArgumentCaptor<MoveRequest> request = ArgumentCaptor.forClass(MoveRequest.class);
172  1 verify(this.jobExecutor).execute(eq(RefactoringJobs.RENAME), request.capture());
173   
174  1 assertEquals(RefactoringJobs.RENAME, request.getValue().getJobType());
175  1 assertEquals(Arrays.asList(spaceReference), request.getValue().getEntityReferences());
176  1 assertEquals(new SpaceReference("Bob", spaceReference.getParent()), request.getValue().getDestination());
177    }
178   
 
179  1 toggle @Test
180    public void copy() throws Exception
181    {
182  1 SpaceReference source = new SpaceReference("Space", new WikiReference("math"));
183  1 WikiReference destination = new WikiReference("code");
184   
185  1 getService().copy(source, destination);
186   
187  1 ArgumentCaptor<MoveRequest> request = ArgumentCaptor.forClass(MoveRequest.class);
188    // The MOVE job can perform a COPY too.
189  1 verify(this.jobExecutor).execute(eq(RefactoringJobs.MOVE), request.capture());
190   
191  1 assertEquals(RefactoringJobs.COPY, request.getValue().getJobType());
192  1 assertFalse(request.getValue().isDeleteSource());
193    }
194   
 
195  1 toggle @Test
196    public void copyAs() throws Exception
197    {
198  1 SpaceReference spaceReference =
199    new SpaceReference("Alice", new SpaceReference("Users", new WikiReference("dev")));
200  1 getService().copyAs(spaceReference, "Bob");
201   
202  1 ArgumentCaptor<MoveRequest> request = ArgumentCaptor.forClass(MoveRequest.class);
203    // The RENAME job can perform a COPY too.
204  1 verify(this.jobExecutor).execute(eq(RefactoringJobs.RENAME), request.capture());
205   
206  1 assertEquals(RefactoringJobs.COPY_AS, request.getValue().getJobType());
207  1 assertEquals(Arrays.asList(spaceReference), request.getValue().getEntityReferences());
208  1 assertEquals(new SpaceReference("Bob", spaceReference.getParent()), request.getValue().getDestination());
209  1 assertFalse(request.getValue().isDeleteSource());
210    }
211   
 
212  1 toggle @Test
213    public void delete() throws Exception
214    {
215  1 WikiReference source = new WikiReference("math");
216   
217  1 getService().delete(source);
218   
219  1 ArgumentCaptor<EntityRequest> request = ArgumentCaptor.forClass(EntityRequest.class);
220  1 verify(this.jobExecutor).execute(eq(RefactoringJobs.DELETE), request.capture());
221   
222  1 assertEquals(RefactoringJobs.DELETE, request.getValue().getJobType());
223  1 assertEquals(Arrays.asList(source), request.getValue().getEntityReferences());
224  1 assertFalse(request.getValue().isDeep());
225    }
226   
 
227  1 toggle @Test
228    public void create() throws Exception
229    {
230  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
231   
232  1 getService().create(documentReference);
233   
234  1 ArgumentCaptor<CreateRequest> request = ArgumentCaptor.forClass(CreateRequest.class);
235  1 verify(this.jobExecutor).execute(eq(RefactoringJobs.CREATE), request.capture());
236   
237  1 assertEquals(RefactoringJobs.CREATE, request.getValue().getJobType());
238  1 assertEquals(Arrays.asList(documentReference), request.getValue().getEntityReferences());
239  1 assertTrue(request.getValue().isDeep());
240    }
241   
 
242  1 toggle @Test
243    public void convertToNestedDocument() throws Exception
244    {
245  1 DocumentReference terminalDocumentReference = new DocumentReference("code", "Model", "Entity");
246  1 DocumentReference nestedDocumentReference =
247    new DocumentReference("code", Arrays.asList("Model", "Entity"), "WebHome");
248   
249  1 getService().convertToNestedDocument(terminalDocumentReference);
250   
251  1 ArgumentCaptor<MoveRequest> request = ArgumentCaptor.forClass(MoveRequest.class);
252  1 verify(this.jobExecutor).execute(eq(RefactoringJobs.RENAME), request.capture());
253  1 assertEquals(Arrays.asList(terminalDocumentReference), request.getValue().getEntityReferences());
254  1 assertEquals(nestedDocumentReference, request.getValue().getDestination());
255   
256  1 assertNull(getService().convertToNestedDocument(nestedDocumentReference));
257    }
258   
 
259  1 toggle @Test
260    public void convertToTerminalDocument() throws Exception
261    {
262  1 DocumentReference terminalDocumentReference = new DocumentReference("code", "Model", "Entity");
263  1 DocumentReference nestedDocumentReference =
264    new DocumentReference("code", Arrays.asList("Model", "Entity"), "WebHome");
265  1 DocumentReference rootDocumentReference = new DocumentReference("wiki", "Space", "WebHome");
266   
267  1 getService().convertToTerminalDocument(nestedDocumentReference);
268   
269  1 ArgumentCaptor<MoveRequest> request = ArgumentCaptor.forClass(MoveRequest.class);
270  1 verify(this.jobExecutor).execute(eq(RefactoringJobs.RENAME), request.capture());
271  1 assertEquals(Arrays.asList(nestedDocumentReference), request.getValue().getEntityReferences());
272  1 assertEquals(terminalDocumentReference, request.getValue().getDestination());
273   
274  1 assertNull(getService().convertToTerminalDocument(terminalDocumentReference));
275  1 assertNull(getService().convertToTerminalDocument(rootDocumentReference));
276    }
277   
 
278  15 toggle private RefactoringScriptService getService() throws ComponentLookupException
279    {
280  15 return (RefactoringScriptService) this.mocker.getComponentUnderTest();
281    }
282    }