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

File PermanentlyDeleteJobTest.java

 

Code metrics

0
69
10
1
215
142
11
0.16
6.9
10
1.1

Classes

Class Line # Actions
PermanentlyDeleteJobTest 52 69 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 6 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.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.job.Job;
30    import org.xwiki.job.JobGroupPath;
31    import org.xwiki.model.ModelContext;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.WikiReference;
34    import org.xwiki.refactoring.job.PermanentlyDeleteRequest;
35    import org.xwiki.refactoring.job.RestoreRequest;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    import static org.junit.Assert.assertEquals;
39    import static org.mockito.ArgumentMatchers.any;
40    import static org.mockito.ArgumentMatchers.eq;
41    import static org.mockito.Mockito.atMost;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.never;
44    import static org.mockito.Mockito.verify;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Unit tests for {@link PermanentlyDeleteJob}.
49    *
50    * @version $Id: c105f2bcfa67ceb118aec8fa9c482262cf5a4b61 $
51    */
 
52    public class PermanentlyDeleteJobTest extends AbstractJobTest
53    {
54    @Rule
55    public MockitoComponentMockingRule<Job> mocker = new MockitoComponentMockingRule<>(PermanentlyDeleteJob.class);
56   
57    private ModelContext modelContext;
58   
59    private WikiReference wikiReference = new WikiReference("mywiki");
60   
61    private DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
62   
 
63  12 toggle @Override
64    protected MockitoComponentMockingRule<Job> getMocker()
65    {
66  12 return this.mocker;
67    }
68   
 
69  6 toggle @Override
70    public void configure() throws Exception
71    {
72  6 super.configure();
73   
74  6 Execution execution = mocker.getInstance(Execution.class);
75  6 ExecutionContext executionContext = mock(ExecutionContext.class);
76  6 when(execution.getContext()).thenReturn(executionContext);
77   
78  6 modelContext = mocker.getInstance(ModelContext.class);
79    }
80   
 
81  1 toggle @Test
82    public void permanentlyDeleteSingleDocument() throws Throwable
83    {
84  1 long deletedDocumentId = 13;
85   
86  1 PermanentlyDeleteRequest request = createRequest();
87  1 request.setDeletedDocumentIds(Arrays.asList(deletedDocumentId));
88  1 request.setCheckRights(true);
89  1 assertEquals(request.getAuthorReference(), userReference);
90  1 run(request);
91   
92  1 verifyContext();
93   
94    // Verify that the specified document is deleted.
95  1 verify(this.modelBridge).permanentlyDeleteDocument(deletedDocumentId, request);
96    }
97   
 
98  1 toggle @Test
99    public void permanentlyDeleteAll() throws Throwable
100    {
101  1 PermanentlyDeleteRequest request = createRequest();
102  1 request.setDeletedDocumentIds(Collections.emptyList());
103  1 request.setCheckRights(true);
104  1 run(request);
105   
106  1 verifyContext();
107   
108    // Verify that the specified document is deleted.
109  1 verify(this.modelBridge).permanentlyDeleteAllDocuments(any(), eq(request));
110    }
111   
 
112  1 toggle @Test
113    public void permanentlyDeleteBatch() throws Throwable
114    {
115  1 long deletedDocumentId1 = 13;
116  1 long deletedDocumentId2 = 42;
117  1 String batchId = "abc123";
118   
119  1 when(modelBridge.getDeletedDocumentIds(batchId))
120    .thenReturn(Arrays.asList(deletedDocumentId1, deletedDocumentId2));
121   
122  1 PermanentlyDeleteRequest request = createRequest();
123  1 request.setBatchId(batchId);
124  1 run(request);
125   
126  1 verifyContext();
127   
128    // Verify that the individual documents from the batch are deleted.
129  1 verify(this.modelBridge).permanentlyDeleteDocument(deletedDocumentId1, request);
130  1 verify(this.modelBridge).permanentlyDeleteDocument(deletedDocumentId2, request);
131    }
132   
 
133  1 toggle @Test
134    public void permanentlyDeleteBatchAndDocuments() throws Throwable
135    {
136    // Batch documents.
137  1 long deletedDocumentId1 = 13;
138  1 long deletedDocumentId2 = 42;
139  1 String batchId = "abc123";
140   
141    // Individual documents.
142    // Note: A has the same ID as a document in the batch, so it should be restored only once.
143  1 long deletedDocumentIdA = deletedDocumentId2;
144  1 long deletedDocumentIdB = 7;
145  1 long deletedDocumentIdC = 3;
146   
147  1 when(modelBridge.getDeletedDocumentIds(batchId))
148    .thenReturn(Arrays.asList(deletedDocumentId1, deletedDocumentId2));
149   
150  1 PermanentlyDeleteRequest request = createRequest();
151  1 request.setBatchId(batchId);
152  1 request.setDeletedDocumentIds(Arrays.asList(deletedDocumentIdA, deletedDocumentIdB, deletedDocumentIdC));
153  1 run(request);
154   
155  1 verifyContext();
156   
157    // Verify that each document is permanently deleted exactly 1 time.
158  1 verify(this.modelBridge, atMost(1)).permanentlyDeleteDocument(deletedDocumentId1, request);
159  1 verify(this.modelBridge, atMost(1)).permanentlyDeleteDocument(deletedDocumentId2, request);
160  1 verify(this.modelBridge, atMost(1)).permanentlyDeleteDocument(deletedDocumentIdB, request);
161  1 verify(this.modelBridge, atMost(1)).permanentlyDeleteDocument(deletedDocumentIdC, request);
162    }
163   
 
164  1 toggle @Test
165    public void jobGroupAtWikiLevel() throws Exception
166    {
167  1 PermanentlyDeleteRequest request = createRequest();
168  1 PermanentlyDeleteJob job = (PermanentlyDeleteJob) getMocker().getComponentUnderTest();
169  1 job.initialize(request);
170   
171  1 JobGroupPath expectedJobGroupPath = new JobGroupPath(wikiReference.getName(), PermanentlyDeleteJob.ROOT_GROUP);
172  1 assertEquals(expectedJobGroupPath, job.getGroupPath());
173    }
174   
 
175  1 toggle @Test
176    public void failToExecuteIfNoWikiSpecified() throws Throwable
177    {
178  1 long deletedDocumentId = 13;
179   
180  1 PermanentlyDeleteRequest request = createRequest();
181  1 request.setDeletedDocumentIds(Arrays.asList(deletedDocumentId));
182  1 request.setWikiReference(null);
183   
184  1 try {
185  1 run(request);
186    } catch (IllegalArgumentException actual) {
187    // Verify that the job threw an exception.
188  1 Throwable expected = new IllegalArgumentException("No wiki reference was specified in the job request");
189   
190  1 assertEquals(expected.getClass(), actual.getClass());
191  1 assertEquals(expected.getMessage(), actual.getMessage());
192    }
193   
194    // Verify that the document is not restored.
195  1 verify(this.modelBridge, never()).permanentlyDeleteDocument(deletedDocumentId, request);
196    }
197   
 
198  6 toggle private PermanentlyDeleteRequest createRequest()
199    {
200  6 PermanentlyDeleteRequest request = new PermanentlyDeleteRequest();
201   
202  6 request.setCheckRights(false);
203  6 request.setUserReference(userReference);
204  6 request.setAuthorReference(userReference);
205  6 request.setWikiReference(wikiReference);
206   
207  6 return request;
208    }
209   
 
210  4 toggle private void verifyContext()
211    {
212  4 verify(this.modelBridge).setContextUserReference(userReference);
213  4 verify(this.modelContext).setCurrentEntityReference(wikiReference);
214    }
215    }