1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
58 |
|
|
59 |
|
@version |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (114) |
Complexity: 14 |
Complexity Density: 0.14 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
11 |
@BeforeComponent... |
76 |
|
public void initializeComponents() throws Exception |
77 |
|
{ |
78 |
11 |
this.mocker.registerComponent(ScriptService.class, "job", jobScriptService); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
81 |
11 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
97 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
122 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
135 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
153 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
164 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
179 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
195 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
212 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
227 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
242 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
259 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
278 |
15 |
private RefactoringScriptService getService() throws ComponentLookupException... |
279 |
|
{ |
280 |
15 |
return (RefactoringScriptService) this.mocker.getComponentUnderTest(); |
281 |
|
} |
282 |
|
} |