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

File RestoreJobTest.java

 

Code metrics

0
61
9
1
195
126
10
0.16
6.78
9
1.11

Classes

Class Line # Actions
RestoreJobTest 48 61 0% 10 0
1.0100%
 

Contributing tests

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