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

File DeleteJobTest.java

 

Code metrics

0
67
10
1
192
137
10
0.15
6.7
10
1

Classes

Class Line # Actions
DeleteJobTest 54 67 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 7 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.model.EntityType;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.model.reference.SpaceReference;
33    import org.xwiki.model.reference.WikiReference;
34    import org.xwiki.refactoring.internal.batch.DefaultBatchOperationExecutor;
35    import org.xwiki.refactoring.job.EntityRequest;
36    import org.xwiki.security.authorization.Right;
37    import org.xwiki.test.annotation.ComponentList;
38    import org.xwiki.test.mockito.MockitoComponentMockingRule;
39   
40    import static org.mockito.ArgumentMatchers.any;
41    import static org.mockito.Mockito.atLeastOnce;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.never;
44    import static org.mockito.Mockito.times;
45    import static org.mockito.Mockito.verify;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * Unit tests for {@link DeleteJob}.
50    *
51    * @version $Id: 099977b4f98c8ba48ae70c32c21e6f78b6b8052b $
52    */
53    @ComponentList(DefaultBatchOperationExecutor.class)
 
54    public class DeleteJobTest extends AbstractEntityJobTest
55    {
56    @Rule
57    public MockitoComponentMockingRule<Job> mocker = new MockitoComponentMockingRule<Job>(DeleteJob.class);
58   
 
59  35 toggle @Override
60    protected MockitoComponentMockingRule<Job> getMocker()
61    {
62  35 return this.mocker;
63    }
64   
 
65  7 toggle @Override
66    public void configure() throws Exception
67    {
68  7 super.configure();
69   
70  7 Execution execution = mocker.getInstance(Execution.class);
71  7 ExecutionContext executionContext = mock(ExecutionContext.class);
72  7 when(execution.getContext()).thenReturn(executionContext);
73    }
74   
 
75  1 toggle @Test
76    public void deleteDocument() throws Throwable
77    {
78  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
79  1 when(this.modelBridge.exists(documentReference)).thenReturn(true);
80   
81  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
82  1 DocumentReference authorReference = new DocumentReference("wiki", "Users", "Bob");
83   
84  1 EntityRequest request = createRequest(documentReference);
85  1 request.setCheckRights(false);
86  1 request.setCheckAuthorRights(false);
87  1 request.setUserReference(userReference);
88  1 request.setAuthorReference(authorReference);
89  1 run(request);
90   
91  1 verify(this.modelBridge).setContextUserReference(userReference);
92  1 verify(this.modelBridge).delete(documentReference);
93    }
94   
 
95  1 toggle @Test
96    public void deleteMissingDocument() throws Throwable
97    {
98  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
99  1 run(createRequest(documentReference));
100  1 verify(this.mocker.getMockedLogger()).warn("Skipping [{}] because it doesn't exist.", documentReference);
101  1 verify(this.modelBridge, never()).delete(any(DocumentReference.class));
102    }
103   
 
104  1 toggle @Test
105    public void deleteDocumentWithoutDeleteRightUser() throws Throwable
106    {
107  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
108  1 when(this.modelBridge.exists(documentReference)).thenReturn(true);
109   
110  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
111  1 when(this.authorization.hasAccess(Right.DELETE, userReference, documentReference)).thenReturn(false);
112   
113  1 DocumentReference authorReference = new DocumentReference("wiki", "Users", "Bob");
114  1 when(this.authorization.hasAccess(Right.DELETE, authorReference, documentReference)).thenReturn(true);
115   
116  1 EntityRequest request = createRequest(documentReference);
117  1 request.setCheckRights(true);
118  1 request.setCheckAuthorRights(true);
119  1 request.setUserReference(userReference);
120  1 request.setAuthorReference(authorReference);
121  1 run(request);
122   
123  1 verify(this.mocker.getMockedLogger()).error("You are not allowed to delete [{}].", documentReference);
124  1 verify(this.modelBridge, never()).delete(any(DocumentReference.class));
125    }
126   
 
127  1 toggle @Test
128    public void deleteDocumentWithoutDeleteRightAuthor() throws Throwable
129    {
130  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
131  1 when(this.modelBridge.exists(documentReference)).thenReturn(true);
132   
133  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
134  1 when(this.authorization.hasAccess(Right.DELETE, userReference, documentReference)).thenReturn(true);
135   
136  1 DocumentReference authorReference = new DocumentReference("wiki", "Users", "Bob");
137  1 when(this.authorization.hasAccess(Right.DELETE, authorReference, documentReference)).thenReturn(false);
138   
139  1 EntityRequest request = createRequest(documentReference);
140  1 request.setCheckRights(true);
141  1 request.setCheckAuthorRights(true);
142  1 request.setUserReference(userReference);
143  1 request.setAuthorReference(authorReference);
144  1 run(request);
145   
146  1 verify(this.mocker.getMockedLogger()).error("You are not allowed to delete [{}].", documentReference);
147  1 verify(this.modelBridge, never()).delete(any(DocumentReference.class));
148    }
149   
 
150  1 toggle @Test
151    public void deleteSpaceHomeDeep() throws Throwable
152    {
153  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "WebHome");
154  1 EntityRequest request = createRequest(documentReference);
155  1 request.setDeep(true);
156  1 run(request);
157   
158    // We only verify if the job fetches the documents from the space. The rest of the test is in #deleteSpace()
159  1 verify(this.modelBridge, atLeastOnce()).getDocumentReferences(documentReference.getLastSpaceReference());
160    }
161   
 
162  1 toggle @Test
163    public void deleteSpace() throws Throwable
164    {
165  1 SpaceReference spaceReference = new SpaceReference("Space", new WikiReference("wiki"));
166  1 DocumentReference aliceReference = new DocumentReference("wiki", "Space", "Alice");
167  1 DocumentReference bobReference = new DocumentReference("wiki", "Space", "Bob");
168  1 when(this.modelBridge.getDocumentReferences(spaceReference)).thenReturn(
169    Arrays.asList(aliceReference, bobReference));
170   
171  1 run(createRequest(spaceReference));
172   
173    // We only verify that the code tries to delete the documents.
174  1 verify(this.mocker.getMockedLogger()).warn("Skipping [{}] because it doesn't exist.", aliceReference);
175  1 verify(this.mocker.getMockedLogger()).warn("Skipping [{}] because it doesn't exist.", bobReference);
176    }
177   
 
178  1 toggle @Test
179    public void deleteUnsupportedEntity() throws Throwable
180    {
181  1 run(createRequest(new WikiReference("foo")));
182  1 verify(this.mocker.getMockedLogger(), times(2)).error("Unsupported entity type [{}].", EntityType.WIKI);
183  1 verify(this.modelBridge, never()).delete(any(DocumentReference.class));
184    }
185   
 
186  7 toggle private EntityRequest createRequest(EntityReference... entityReference)
187    {
188  7 EntityRequest request = new EntityRequest();
189  7 request.setEntityReferences(Arrays.asList(entityReference));
190  7 return request;
191    }
192    }