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

File CreateJobTest.java

 

Code metrics

0
103
13
1
252
180
13
0.13
7.92
13
1

Classes

Class Line # Actions
CreateJobTest 42 103 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 10 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: f70561c9d5d4250beb4160978b94f5e36d6176a7 $
41    */
 
42    public class CreateJobTest extends AbstractEntityJobTest
43    {
44    @Rule
45    public MockitoComponentMockingRule<Job> mocker = new MockitoComponentMockingRule<Job>(CreateJob.class);
46   
 
47  40 toggle @Override
48    protected MockitoComponentMockingRule<Job> getMocker()
49    {
50  40 return this.mocker;
51    }
52   
 
53  1 toggle @Test
54    public void createDocumentFromTemplate() throws Exception
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 run(request);
68   
69  1 verify(this.modelBridge).setContextUserReference(userReference);
70  1 verify(this.modelBridge).copy(templateReference, documentReference);
71  1 verify(this.modelBridge).removeLock(documentReference);
72    }
73   
 
74  1 toggle @Test
75    public void createSpaceFromTemplate() throws Exception
76    {
77  1 SpaceReference newSpaceReference = new SpaceReference("wiki", "Space");
78  1 SpaceReference templateSpaceReference = new SpaceReference("wiki", "Code", "Template");
79   
80  1 DocumentReference templateDocumentReference = new DocumentReference("Index", templateSpaceReference);
81  1 when(this.modelBridge.getDocumentReferences(templateSpaceReference)).thenReturn(
82    Arrays.asList(templateDocumentReference));
83  1 when(this.modelBridge.exists(templateDocumentReference)).thenReturn(true);
84   
85  1 DocumentReference newDocumentReference = new DocumentReference("Index", newSpaceReference);
86  1 when(this.modelBridge.exists(newDocumentReference)).thenReturn(false);
87   
88  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
89   
90  1 CreateRequest request = createRequest(newSpaceReference, templateSpaceReference);
91  1 request.setUserReference(userReference);
92  1 request.setCheckRights(false);
93  1 run(request);
94   
95  1 verify(this.modelBridge).setContextUserReference(userReference);
96  1 verify(this.modelBridge).copy(templateDocumentReference, newDocumentReference);
97  1 verify(this.modelBridge, never()).removeLock(any(DocumentReference.class));
98    }
99   
 
100  1 toggle @Test
101    public void createDocumentWithoutTemplate() throws Exception
102    {
103  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
104  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
105   
106  1 CreateRequest request = createRequest(documentReference, null);
107  1 request.setCheckRights(false);
108  1 run(request);
109   
110  1 verify(this.modelBridge).create(documentReference);
111  1 verify(this.modelBridge).removeLock(documentReference);
112  1 verify(this.modelBridge, never()).copy(any(DocumentReference.class), any(DocumentReference.class));
113    }
114   
 
115  1 toggle @Test
116    public void createSpaceWithoutTemplate() throws Exception
117    {
118  1 SpaceReference spaceReference = new SpaceReference("wiki", "Space");
119  1 DocumentReference spaceHomeReference = new DocumentReference("WebHome", spaceReference);
120  1 when(this.modelBridge.exists(spaceHomeReference)).thenReturn(false);
121   
122  1 CreateRequest request = createRequest(spaceReference, null);
123  1 request.setCheckRights(false);
124  1 run(request);
125   
126  1 verify(this.modelBridge).create(spaceHomeReference);
127  1 verify(this.modelBridge, never()).copy(any(DocumentReference.class), any(DocumentReference.class));
128  1 verify(this.modelBridge, never()).removeLock(any(DocumentReference.class));
129    }
130   
 
131  1 toggle @Test
132    public void createDocumentDeep() throws Exception
133    {
134  1 DocumentReference spaceHomeReference = new DocumentReference("wiki", "Space", "WebHome");
135  1 DocumentReference templateHomeReference =
136    new DocumentReference("wiki", Arrays.asList("Code", "Template"), "WebHome");
137   
138  1 DocumentReference templateDocumentReference =
139    new DocumentReference("Index", templateHomeReference.getLastSpaceReference());
140  1 when(this.modelBridge.getDocumentReferences(templateHomeReference.getLastSpaceReference())).thenReturn(
141    Arrays.asList(templateDocumentReference));
142  1 when(this.modelBridge.exists(templateDocumentReference)).thenReturn(true);
143   
144  1 DocumentReference newDocumentReference =
145    new DocumentReference("Index", spaceHomeReference.getLastSpaceReference());
146  1 when(this.modelBridge.exists(newDocumentReference)).thenReturn(false);
147   
148  1 CreateRequest request = createRequest(spaceHomeReference, templateHomeReference);
149  1 request.setCheckRights(false);
150  1 request.setDeep(true);
151  1 run(request);
152   
153  1 verify(this.modelBridge).copy(templateDocumentReference, newDocumentReference);
154    }
155   
 
156  1 toggle @Test
157    public void createDocumentFromMissingTemplate() throws Exception
158    {
159  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
160  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
161   
162  1 DocumentReference templateReference = new DocumentReference("wiki", "Code", "Template");
163  1 when(this.modelBridge.exists(templateReference)).thenReturn(false);
164   
165  1 CreateRequest request = createRequest(documentReference, templateReference);
166  1 request.setCheckRights(false);
167  1 run(request);
168   
169  1 verifyNoCreate();
170  1 verify(this.mocker.getMockedLogger()).error("Template document [{}] does not exist.", templateReference);
171    }
172   
 
173  1 toggle @Test
174    public void createDocumentFromRestrictedTemplate() throws Exception
175    {
176  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
177  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
178   
179  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
180  1 when(this.authorization.hasAccess(Right.EDIT, userReference, documentReference)).thenReturn(true);
181   
182  1 DocumentReference templateReference = new DocumentReference("wiki", "Code", "Template");
183  1 when(this.authorization.hasAccess(Right.VIEW, userReference, templateReference)).thenReturn(false);
184   
185  1 CreateRequest request = createRequest(documentReference, templateReference);
186  1 request.setUserReference(userReference);
187  1 run(request);
188   
189  1 verifyNoCreate();
190  1 verify(this.mocker.getMockedLogger()).error("You are not allowed to view the template document [{}].",
191    templateReference);
192    }
193   
 
194  1 toggle @Test
195    public void createRestrictedDocument() throws Exception
196    {
197  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
198  1 when(this.modelBridge.exists(documentReference)).thenReturn(false);
199   
200  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
201  1 when(this.authorization.hasAccess(Right.EDIT, userReference, documentReference)).thenReturn(false);
202   
203  1 CreateRequest request = createRequest(documentReference, null);
204  1 request.setUserReference(userReference);
205  1 run(request);
206   
207  1 verifyNoCreate();
208  1 verify(this.mocker.getMockedLogger()).error("You are not allowed to create the document [{}].",
209    documentReference);
210    }
211   
 
212  1 toggle @Test
213    public void createExistingDocument() throws Exception
214    {
215  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
216  1 when(this.modelBridge.exists(documentReference)).thenReturn(true);
217   
218  1 run(createRequest(documentReference, null));
219   
220  1 verifyNoCreate();
221  1 verify(this.mocker.getMockedLogger()).warn("Skipping creation of document [{}] because it already exists.",
222    documentReference);
223    }
224   
 
225  1 toggle @Test
226    public void skipDocumentCreation() throws Exception
227    {
228  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
229   
230  1 CreateRequest request = createRequest(documentReference, null);
231  1 request.setSkippedEntities(Arrays.<EntityReference>asList(documentReference));
232  1 run(request);
233   
234  1 verifyNoCreate();
235  1 verify(this.mocker.getMockedLogger()).debug("Skipping creation of document [{}], as specified in the request.",
236    documentReference);
237    }
238   
 
239  10 toggle private CreateRequest createRequest(EntityReference targetReference, EntityReference templateReference)
240    {
241  10 CreateRequest request = new CreateRequest();
242  10 request.setEntityReferences(Collections.singletonList(targetReference));
243  10 request.setTemplateReference(templateReference);
244  10 return request;
245    }
246   
 
247  5 toggle private void verifyNoCreate()
248    {
249  5 verify(this.modelBridge, never()).create(any(DocumentReference.class));
250  5 verify(this.modelBridge, never()).copy(any(DocumentReference.class), any(DocumentReference.class));
251    }
252    }