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

File CreateJobTest.java

 

Code metrics

0
131
14
1
291
213
14
0.11
9.36
14
1

Classes

Class Line # Actions
CreateJobTest 42 131 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.internal.job;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24   
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.job.Job;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.EntityReference;
30    import org.xwiki.model.reference.SpaceReference;
31    import org.xwiki.refactoring.job.CreateRequest;
32    import org.xwiki.security.authorization.Right;
33    import org.xwiki.test.mockito.MockitoComponentMockingRule;
34   
35    import static org.mockito.Mockito.*;
36   
37    /**
38    * Unit tests for {@link CreateJob}.
39    *
40    * @version $Id: c1c48ff3c6b71a168866baf85178e3b077eba2ee $
41    */
 
42    public class CreateJobTest extends AbstractEntityJobTest
43    {
44    @Rule
45    public MockitoComponentMockingRule<Job> mocker = new MockitoComponentMockingRule<Job>(CreateJob.class);
46   
 
47  55 toggle @Override
48    protected MockitoComponentMockingRule<Job> getMocker()
49    {
50  55 return this.mocker;
51    }
52   
 
53  1 toggle @Test
54    public void createDocumentFromTemplate() throws Throwable
55    {
56  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
57  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
58   
59  1 DocumentReference templateReference = new DocumentReference("wiki", "Code", "Template");
60  1 when(this.modelBridge.exists(templateReference)).thenReturn(true);
61   
62  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
63   
64  1 CreateRequest request = createRequest(documentReference, templateReference);
65  1 request.setUserReference(userReference);
66  1 request.setCheckRights(false);
67  1 request.setAuthorReference(userReference);
68  1 request.setCheckAuthorRights(false);
69  1 run(request);
70   
71  1 verify(this.modelBridge).setContextUserReference(userReference);
72  1 verify(this.modelBridge).copy(templateReference, documentReference);
73  1 verify(this.modelBridge).removeLock(documentReference);
74    }
75   
 
76  1 toggle @Test
77    public void createSpaceFromTemplate() throws Throwable
78    {
79  1 SpaceReference newSpaceReference = new SpaceReference("wiki", "Space");
80  1 SpaceReference templateSpaceReference = new SpaceReference("wiki", "Code", "Template");
81   
82  1 DocumentReference templateDocumentReference = new DocumentReference("Index", templateSpaceReference);
83  1 when(this.modelBridge.getDocumentReferences(templateSpaceReference)).thenReturn(
84    Arrays.asList(templateDocumentReference));
85  1 when(this.modelBridge.exists(templateDocumentReference)).thenReturn(true);
86   
87  1 DocumentReference newDocumentReference = new DocumentReference("Index", newSpaceReference);
88  1 when(this.modelBridge.exists(newDocumentReference)).thenReturn(false);
89   
90  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
91   
92  1 CreateRequest request = createRequest(newSpaceReference, templateSpaceReference);
93  1 request.setUserReference(userReference);
94  1 request.setCheckRights(false);
95  1 request.setAuthorReference(userReference);
96  1 request.setCheckAuthorRights(false);
97  1 run(request);
98   
99  1 verify(this.modelBridge).setContextUserReference(userReference);
100  1 verify(this.modelBridge).copy(templateDocumentReference, newDocumentReference);
101  1 verify(this.modelBridge, never()).removeLock(any(DocumentReference.class));
102    }
103   
 
104  1 toggle @Test
105    public void createDocumentWithoutTemplate() throws Throwable
106    {
107  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
108  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
109   
110  1 CreateRequest request = createRequest(documentReference, null);
111  1 request.setCheckRights(false);
112  1 request.setCheckAuthorRights(false);
113  1 run(request);
114   
115  1 verify(this.modelBridge).create(documentReference);
116  1 verify(this.modelBridge).removeLock(documentReference);
117  1 verify(this.modelBridge, never()).copy(any(DocumentReference.class), any(DocumentReference.class));
118    }
119   
 
120  1 toggle @Test
121    public void createSpaceWithoutTemplate() throws Throwable
122    {
123  1 SpaceReference spaceReference = new SpaceReference("wiki", "Space");
124  1 DocumentReference spaceHomeReference = new DocumentReference("WebHome", spaceReference);
125  1 when(this.modelBridge.exists(spaceHomeReference)).thenReturn(false);
126   
127  1 CreateRequest request = createRequest(spaceReference, null);
128  1 request.setCheckRights(false);
129  1 request.setCheckAuthorRights(false);
130  1 run(request);
131   
132  1 verify(this.modelBridge).create(spaceHomeReference);
133  1 verify(this.modelBridge, never()).copy(any(DocumentReference.class), any(DocumentReference.class));
134  1 verify(this.modelBridge, never()).removeLock(any(DocumentReference.class));
135    }
136   
 
137  1 toggle @Test
138    public void createDocumentDeep() throws Throwable
139    {
140  1 DocumentReference spaceHomeReference = new DocumentReference("wiki", "Space", "WebHome");
141  1 DocumentReference templateHomeReference =
142    new DocumentReference("wiki", Arrays.asList("Code", "Template"), "WebHome");
143   
144  1 DocumentReference templateDocumentReference =
145    new DocumentReference("Index", templateHomeReference.getLastSpaceReference());
146  1 when(this.modelBridge.getDocumentReferences(templateHomeReference.getLastSpaceReference())).thenReturn(
147    Arrays.asList(templateDocumentReference));
148  1 when(this.modelBridge.exists(templateDocumentReference)).thenReturn(true);
149   
150  1 DocumentReference newDocumentReference =
151    new DocumentReference("Index", spaceHomeReference.getLastSpaceReference());
152  1 when(this.modelBridge.exists(newDocumentReference)).thenReturn(false);
153   
154  1 CreateRequest request = createRequest(spaceHomeReference, templateHomeReference);
155  1 request.setCheckRights(false);
156  1 request.setCheckAuthorRights(false);
157  1 request.setDeep(true);
158  1 run(request);
159   
160  1 verify(this.modelBridge).copy(templateDocumentReference, newDocumentReference);
161    }
162   
 
163  1 toggle @Test
164    public void createDocumentFromMissingTemplate() throws Throwable
165    {
166  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
167  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
168   
169  1 DocumentReference templateReference = new DocumentReference("wiki", "Code", "Template");
170  1 when(this.modelBridge.exists(templateReference)).thenReturn(false);
171   
172  1 CreateRequest request = createRequest(documentReference, templateReference);
173  1 request.setCheckRights(false);
174  1 request.setCheckAuthorRights(false);
175  1 run(request);
176   
177  1 verifyNoCreate();
178  1 verify(this.mocker.getMockedLogger()).error("Template document [{}] does not exist.", templateReference);
179    }
180   
 
181  1 toggle @Test
182    public void createDocumentFromRestrictedTemplate() throws Throwable
183    {
184  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
185  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
186   
187  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
188  1 when(this.authorization.hasAccess(Right.EDIT, userReference, documentReference)).thenReturn(true);
189   
190  1 DocumentReference templateReference = new DocumentReference("wiki", "Code", "Template");
191  1 when(this.authorization.hasAccess(Right.VIEW, userReference, templateReference)).thenReturn(false);
192   
193  1 CreateRequest request = createRequest(documentReference, templateReference);
194  1 request.setUserReference(userReference);
195  1 request.setAuthorReference(userReference);
196  1 run(request);
197   
198  1 verifyNoCreate();
199  1 verify(this.mocker.getMockedLogger()).error("You are not allowed to view the template document [{}].",
200    templateReference);
201    }
202   
 
203  1 toggle @Test
204    public void createRestrictedDocument() throws Throwable
205    {
206  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
207  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
208   
209  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
210  1 when(this.authorization.hasAccess(Right.EDIT, userReference, documentReference)).thenReturn(false);
211   
212  1 DocumentReference authorReference = new DocumentReference("wiki", "Users", "Bob");
213  1 when(this.authorization.hasAccess(Right.EDIT, authorReference, documentReference)).thenReturn(true);
214   
215  1 CreateRequest request = createRequest(documentReference, null);
216  1 request.setUserReference(userReference);
217  1 request.setAuthorReference(authorReference);
218  1 request.setCheckRights(true);
219  1 request.setCheckAuthorRights(true);
220  1 run(request);
221   
222  1 verifyNoCreate();
223  1 verify(this.mocker.getMockedLogger()).error("You are not allowed to create the document [{}].",
224    documentReference);
225    }
226   
 
227  1 toggle @Test
228    public void createRestrictedDocumentAuthor() throws Throwable
229    {
230  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
231  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
232   
233  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
234  1 when(this.authorization.hasAccess(Right.EDIT, userReference, documentReference)).thenReturn(true);
235   
236  1 DocumentReference authorReference = new DocumentReference("wiki", "Users", "Bob");
237  1 when(this.authorization.hasAccess(Right.EDIT, authorReference, documentReference)).thenReturn(false);
238   
239  1 CreateRequest request = createRequest(documentReference, null);
240  1 request.setUserReference(userReference);
241  1 request.setAuthorReference(authorReference);
242  1 request.setCheckRights(true);
243  1 request.setCheckAuthorRights(true);
244  1 run(request);
245   
246  1 verifyNoCreate();
247  1 verify(this.mocker.getMockedLogger()).error("You are not allowed to create the document [{}].",
248    documentReference);
249    }
250   
 
251  1 toggle @Test
252    public void createExistingDocument() throws Throwable
253    {
254  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
255  1 when(this.modelBridge.exists(documentReference)).thenReturn(true);
256   
257  1 run(createRequest(documentReference, null));
258   
259  1 verifyNoCreate();
260  1 verify(this.mocker.getMockedLogger()).warn("Skipping creation of document [{}] because it already exists.",
261    documentReference);
262    }
263   
 
264  1 toggle @Test
265    public void skipDocumentCreation() throws Throwable
266    {
267  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
268   
269  1 CreateRequest request = createRequest(documentReference, null);
270  1 request.setSkippedEntities(Arrays.<EntityReference>asList(documentReference));
271  1 run(request);
272   
273  1 verifyNoCreate();
274  1 verify(this.mocker.getMockedLogger()).debug("Skipping creation of document [{}], as specified in the request.",
275    documentReference);
276    }
277   
 
278  11 toggle private CreateRequest createRequest(EntityReference targetReference, EntityReference templateReference)
279    {
280  11 CreateRequest request = new CreateRequest();
281  11 request.setEntityReferences(Collections.singletonList(targetReference));
282  11 request.setTemplateReference(templateReference);
283  11 return request;
284    }
285   
 
286  6 toggle private void verifyNoCreate()
287    {
288  6 verify(this.modelBridge, never()).create(any(DocumentReference.class));
289  6 verify(this.modelBridge, never()).copy(any(DocumentReference.class), any(DocumentReference.class));
290    }
291    }