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

File RequestFactoryTest.java

 

Code metrics

0
131
13
1
275
233
13
0.1
10.08
13
1

Classes

Class Line # Actions
RequestFactoryTest 51 131 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 12 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.apache.commons.lang3.StringUtils;
26    import org.junit.jupiter.api.BeforeEach;
27    import org.junit.jupiter.api.Test;
28    import org.mockito.MockitoAnnotations;
29    import org.xwiki.bridge.DocumentAccessBridge;
30    import org.xwiki.model.ModelContext;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.EntityReference;
33    import org.xwiki.model.reference.WikiReference;
34    import org.xwiki.refactoring.job.CopyRequest;
35    import org.xwiki.refactoring.job.CreateRequest;
36    import org.xwiki.refactoring.job.EntityRequest;
37    import org.xwiki.refactoring.job.MoveRequest;
38    import org.xwiki.refactoring.job.PermanentlyDeleteRequest;
39    import org.xwiki.refactoring.job.RefactoringJobs;
40    import org.xwiki.refactoring.job.RestoreRequest;
41    import org.xwiki.test.junit5.mockito.ComponentTest;
42    import org.xwiki.test.junit5.mockito.InjectMockComponents;
43    import org.xwiki.test.junit5.mockito.MockComponent;
44   
45    import static org.junit.Assert.assertEquals;
46    import static org.junit.Assert.assertFalse;
47    import static org.junit.Assert.assertTrue;
48    import static org.mockito.Mockito.when;
49   
50    @ComponentTest
 
51    public class RequestFactoryTest
52    {
53    @InjectMockComponents
54    private RequestFactory requestFactory;
55   
56    @MockComponent
57    private DocumentAccessBridge documentAccessBridge;
58   
59    @MockComponent
60    private ModelContext modelContext;
61   
62    private DocumentReference userReference = new DocumentReference("wiki", "Users", "Carol");
63    private WikiReference wikiReference = new WikiReference("wiki");
64   
 
65  12 toggle @BeforeEach
66    public void configure() throws Exception
67    {
68  12 MockitoAnnotations.initMocks(this);
69  12 when(documentAccessBridge.getCurrentUserReference()).thenReturn(this.userReference);
70  12 when(documentAccessBridge.getCurrentAuthorReference()).thenReturn(this.userReference);
71  12 when(modelContext.getCurrentEntityReference()).thenReturn(this.userReference);
72    }
73   
 
74  1 toggle @Test
75    public void createRestoreRequest() throws Exception
76    {
77  1 List<Long> documentIds = Arrays.asList(1L, 2L, 3L);
78  1 RestoreRequest restoreRequest =
79    requestFactory.createRestoreRequest(documentIds);
80  1 assertEquals(documentIds, restoreRequest.getDeletedDocumentIds());
81  1 assertTrue(StringUtils.join(restoreRequest.getId(), '/')
82    .startsWith(RefactoringJobs.RESTORE));
83  1 assertTrue(restoreRequest.isCheckRights());
84  1 assertEquals(wikiReference, restoreRequest.getWikiReference());
85    }
86   
 
87  1 toggle @Test
88    public void createRestoreRequestBatchId() throws Exception
89    {
90  1 String batchId = "batch-id";
91  1 RestoreRequest restoreRequest =
92    requestFactory.createRestoreRequest(batchId);
93  1 assertEquals(batchId, restoreRequest.getBatchId());
94  1 assertTrue(StringUtils.join(restoreRequest.getId(), '/')
95    .startsWith(RefactoringJobs.RESTORE));
96  1 assertTrue(restoreRequest.isCheckRights());
97  1 assertEquals(wikiReference, restoreRequest.getWikiReference());
98    }
99   
 
100  1 toggle @Test
101    public void createPermanentlyDeleteRequest() throws Exception
102    {
103  1 List<Long> documentIds = Arrays.asList(1L, 2L, 3L);
104  1 PermanentlyDeleteRequest permanentlyDeleteRequest =
105    requestFactory.createPermanentlyDeleteRequest(documentIds);
106  1 assertEquals(documentIds, permanentlyDeleteRequest.getDeletedDocumentIds());
107  1 assertTrue(StringUtils.join(permanentlyDeleteRequest.getId(), '/')
108    .startsWith(RefactoringJobs.PERMANENTLY_DELETE));
109  1 assertTrue(permanentlyDeleteRequest.isCheckRights());
110  1 assertEquals(wikiReference, permanentlyDeleteRequest.getWikiReference());
111    }
112   
 
113  1 toggle @Test
114    public void createPermanentlyDeleteRequestBatch() throws Exception
115    {
116  1 String batchId = "batch-id";
117  1 PermanentlyDeleteRequest permanentlyDeleteRequest =
118    requestFactory.createPermanentlyDeleteRequest(batchId);
119  1 assertEquals(batchId, permanentlyDeleteRequest.getBatchId());
120  1 assertTrue(StringUtils.join(permanentlyDeleteRequest.getId(), '/')
121    .startsWith(RefactoringJobs.PERMANENTLY_DELETE));
122  1 assertTrue(permanentlyDeleteRequest.isCheckRights());
123  1 assertEquals(wikiReference, permanentlyDeleteRequest.getWikiReference());
124    }
125   
 
126  1 toggle @Test
127    public void createMoveRequest() throws Exception
128    {
129  1 DocumentReference source = new DocumentReference("code", "Model", "Entity");
130  1 DocumentReference destination =
131    new DocumentReference("code", Arrays.asList("Model", "Entity"), "WebHome");
132  1 MoveRequest moveRequest = requestFactory.createMoveRequest(source, destination);
133  1 assertEquals(Arrays.asList(source), moveRequest.getEntityReferences());
134  1 assertEquals(destination, moveRequest.getDestination());
135  1 assertEquals(Arrays.asList(RefactoringJobs.GROUP, "move"), moveRequest.getId().subList(0, 2));
136  1 assertEquals(RefactoringJobs.MOVE, moveRequest.getJobType());
137  1 assertEquals(this.userReference, moveRequest.getUserReference());
138  1 assertFalse(moveRequest.isDeep());
139  1 assertTrue(moveRequest.isDeleteSource());
140  1 assertTrue(moveRequest.isUpdateLinks());
141  1 assertTrue(moveRequest.isUpdateParentField());
142  1 assertTrue(moveRequest.isAutoRedirect());
143  1 assertFalse(moveRequest.isInteractive());
144  1 assertTrue(moveRequest.isCheckRights());
145  1 moveRequest.setUpdateParentField(false);
146  1 assertFalse(moveRequest.isUpdateParentField());
147    }
148   
 
149  1 toggle @Test
150    public void createRenameRequest() throws Exception
151    {
152  1 DocumentReference source = new DocumentReference("code", "Model", "Entity");
153  1 DocumentReference destination =
154    new DocumentReference("code", Arrays.asList("Model", "Entity"), "WebHome");
155  1 MoveRequest renameRequest = requestFactory.createRenameRequest(source, destination);
156   
157  1 assertEquals(Arrays.asList(source), renameRequest.getEntityReferences());
158  1 assertEquals(destination, renameRequest.getDestination());
159  1 assertEquals(Arrays.asList(RefactoringJobs.GROUP, "rename"), renameRequest.getId().subList(0, 2));
160  1 assertEquals(RefactoringJobs.RENAME, renameRequest.getJobType());
161  1 assertEquals(this.userReference, renameRequest.getUserReference());
162  1 assertFalse(renameRequest.isDeep());
163  1 assertTrue(renameRequest.isDeleteSource());
164  1 assertTrue(renameRequest.isUpdateLinks());
165  1 assertTrue(renameRequest.isUpdateParentField());
166  1 assertTrue(renameRequest.isAutoRedirect());
167  1 assertFalse(renameRequest.isInteractive());
168  1 assertTrue(renameRequest.isCheckRights());
169    }
170   
 
171  1 toggle @Test
172    public void createRenameRequestString() throws Exception
173    {
174  1 DocumentReference source = new DocumentReference("code", "Model", "Entity");
175  1 String newName = "Bob";
176  1 MoveRequest renameRequest = requestFactory.createRenameRequest(source, newName);
177   
178  1 EntityReference destination = new EntityReference(newName, source.getType(), source.getParent());
179  1 assertEquals(Arrays.asList(source), renameRequest.getEntityReferences());
180  1 assertEquals(destination, renameRequest.getDestination());
181  1 assertEquals(Arrays.asList(RefactoringJobs.GROUP, "rename"), renameRequest.getId().subList(0, 2));
182  1 assertEquals(RefactoringJobs.RENAME, renameRequest.getJobType());
183  1 assertEquals(this.userReference, renameRequest.getUserReference());
184  1 assertFalse(renameRequest.isDeep());
185  1 assertTrue(renameRequest.isDeleteSource());
186  1 assertTrue(renameRequest.isUpdateLinks());
187  1 assertTrue(renameRequest.isUpdateParentField());
188  1 assertTrue(renameRequest.isAutoRedirect());
189  1 assertFalse(renameRequest.isInteractive());
190  1 assertTrue(renameRequest.isCheckRights());
191    }
192   
 
193  1 toggle @Test
194    public void createCopyRequest() throws Exception
195    {
196  1 DocumentReference source = new DocumentReference("code", "Model", "Entity");
197  1 DocumentReference destination =
198    new DocumentReference("code", Arrays.asList("Model", "Entity"), "WebHome");
199  1 CopyRequest copyRequest = requestFactory.createCopyRequest(source, destination);
200  1 assertEquals(Arrays.asList(source), copyRequest.getEntityReferences());
201  1 assertEquals(destination.getParent(), copyRequest.getDestination());
202  1 assertEquals(Arrays.asList(RefactoringJobs.GROUP, "copy"), copyRequest.getId().subList(0, 2));
203  1 assertEquals(RefactoringJobs.COPY, copyRequest.getJobType());
204  1 assertEquals(this.userReference, copyRequest.getUserReference());
205  1 assertFalse(copyRequest.isDeep());
206  1 assertTrue(copyRequest.isUpdateLinks());
207  1 assertFalse(copyRequest.isInteractive());
208  1 assertTrue(copyRequest.isCheckRights());
209    }
210   
 
211  1 toggle @Test
212    public void createCopyAsRequest() throws Exception
213    {
214  1 DocumentReference source = new DocumentReference("code", "Model", "Entity");
215  1 DocumentReference destination =
216    new DocumentReference("code", Arrays.asList("Model", "Entity"), "WebHome");
217  1 CopyRequest copyRequest = requestFactory.createCopyAsRequest(source, destination);
218  1 assertEquals(Arrays.asList(source), copyRequest.getEntityReferences());
219  1 assertEquals(destination, copyRequest.getDestination());
220  1 assertEquals(Arrays.asList(RefactoringJobs.GROUP, "copyAs"), copyRequest.getId().subList(0, 2));
221  1 assertEquals(RefactoringJobs.COPY_AS, copyRequest.getJobType());
222  1 assertEquals(this.userReference, copyRequest.getUserReference());
223  1 assertFalse(copyRequest.isDeep());
224  1 assertTrue(copyRequest.isUpdateLinks());
225  1 assertFalse(copyRequest.isInteractive());
226  1 assertTrue(copyRequest.isCheckRights());
227    }
228   
 
229  1 toggle @Test
230    public void createCopyAsRequestString() throws Exception
231    {
232  1 DocumentReference source = new DocumentReference("code", "Model", "Entity");
233  1 String newPlace = "Bob";
234   
235  1 CopyRequest copyRequest = requestFactory.createCopyAsRequest(source, newPlace);
236  1 EntityReference destination = new EntityReference(newPlace, source.getType(), source.getParent());
237  1 assertEquals(Arrays.asList(source), copyRequest.getEntityReferences());
238  1 assertEquals(destination, copyRequest.getDestination());
239  1 assertEquals(Arrays.asList(RefactoringJobs.GROUP, "copyAs"), copyRequest.getId().subList(0, 2));
240  1 assertEquals(RefactoringJobs.COPY_AS, copyRequest.getJobType());
241  1 assertEquals(this.userReference, copyRequest.getUserReference());
242  1 assertFalse(copyRequest.isDeep());
243  1 assertTrue(copyRequest.isUpdateLinks());
244  1 assertFalse(copyRequest.isInteractive());
245  1 assertTrue(copyRequest.isCheckRights());
246    }
247   
 
248  1 toggle @Test
249    public void createCreateRequest() throws Exception
250    {
251  1 DocumentReference source = new DocumentReference("code", "Model", "Entity");
252  1 CreateRequest createRequest = requestFactory.createCreateRequest(Arrays.asList(source));
253  1 assertEquals(Arrays.asList(source), createRequest.getEntityReferences());
254  1 assertEquals(Arrays.asList(RefactoringJobs.GROUP, "create"), createRequest.getId().subList(0, 2));
255  1 assertEquals(RefactoringJobs.CREATE, createRequest.getJobType());
256  1 assertEquals(this.userReference, createRequest.getUserReference());
257  1 assertTrue(createRequest.isDeep());
258  1 assertFalse(createRequest.isInteractive());
259  1 assertTrue(createRequest.isCheckRights());
260    }
261   
 
262  1 toggle @Test
263    public void createDeleteRequest() throws Exception
264    {
265  1 DocumentReference source = new DocumentReference("code", "Model", "Entity");
266  1 EntityRequest deleteRequest = requestFactory.createDeleteRequest(Arrays.asList(source));
267  1 assertEquals(Arrays.asList(source), deleteRequest.getEntityReferences());
268  1 assertEquals(Arrays.asList(RefactoringJobs.GROUP, "delete"), deleteRequest.getId().subList(0, 2));
269  1 assertEquals(RefactoringJobs.DELETE, deleteRequest.getJobType());
270  1 assertEquals(this.userReference, deleteRequest.getUserReference());
271  1 assertFalse(deleteRequest.isDeep());
272  1 assertFalse(deleteRequest.isInteractive());
273  1 assertTrue(deleteRequest.isCheckRights());
274    }
275    }