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

File DeleteJobTest.java

 

Code metrics

0
42
8
1
141
94
8
0.19
5.25
8
1

Classes

Class Line # Actions
DeleteJobTest 44 42 0% 8 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   
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.xwiki.job.Job;
27    import org.xwiki.model.EntityType;
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.model.reference.WikiReference;
32    import org.xwiki.refactoring.job.EntityRequest;
33    import org.xwiki.security.authorization.Right;
34    import org.xwiki.test.mockito.MockitoComponentMockingRule;
35   
36    import static org.mockito.ArgumentMatchers.*;
37    import static org.mockito.Mockito.*;
38   
39    /**
40    * Unit tests for {@link DeleteJob}.
41    *
42    * @version $Id: 79c250c117399033bd42f3673e19eed17b774e24 $
43    */
 
44    public class DeleteJobTest extends AbstractEntityJobTest
45    {
46    @Rule
47    public MockitoComponentMockingRule<Job> mocker = new MockitoComponentMockingRule<Job>(DeleteJob.class);
48   
 
49  24 toggle @Override
50    protected MockitoComponentMockingRule<Job> getMocker()
51    {
52  24 return this.mocker;
53    }
54   
 
55  1 toggle @Test
56    public void deleteDocument() throws Exception
57    {
58  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
59  1 when(this.modelBridge.exists(documentReference)).thenReturn(true);
60   
61  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
62   
63  1 EntityRequest request = createRequest(documentReference);
64  1 request.setCheckRights(false);
65  1 request.setUserReference(userReference);
66  1 run(request);
67   
68  1 verify(this.modelBridge).setContextUserReference(userReference);
69  1 verify(this.modelBridge).delete(documentReference);
70    }
71   
 
72  1 toggle @Test
73    public void deleteMissingDocument() throws Exception
74    {
75  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
76  1 run(createRequest(documentReference));
77  1 verify(this.mocker.getMockedLogger()).warn("Skipping [{}] because it doesn't exist.", documentReference);
78  1 verify(this.modelBridge, never()).delete(any(DocumentReference.class));
79    }
80   
 
81  1 toggle @Test
82    public void deleteDocumentWithoutDeleteRight() throws Exception
83    {
84  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
85  1 when(this.modelBridge.exists(documentReference)).thenReturn(true);
86   
87  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
88  1 when(this.authorization.hasAccess(Right.DELETE, userReference, documentReference)).thenReturn(false);
89   
90  1 EntityRequest request = createRequest(documentReference);
91  1 request.setCheckRights(true);
92  1 request.setUserReference(userReference);
93  1 run(request);
94   
95  1 verify(this.mocker.getMockedLogger()).error("You are not allowed to delete [{}].", documentReference);
96  1 verify(this.modelBridge, never()).delete(any(DocumentReference.class));
97    }
98   
 
99  1 toggle @Test
100    public void deleteSpaceHomeDeep() throws Exception
101    {
102  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "WebHome");
103  1 EntityRequest request = createRequest(documentReference);
104  1 request.setDeep(true);
105  1 run(request);
106   
107    // We only verify if the job fetches the documents from the space. The rest of the test is in #deleteSpace()
108  1 verify(this.modelBridge).getDocumentReferences(documentReference.getLastSpaceReference());
109    }
110   
 
111  1 toggle @Test
112    public void deleteSpace() throws Exception
113    {
114  1 SpaceReference spaceReference = new SpaceReference("Space", new WikiReference("wiki"));
115  1 DocumentReference aliceReference = new DocumentReference("wiki", "Space", "Alice");
116  1 DocumentReference bobReference = new DocumentReference("wiki", "Space", "Bob");
117  1 when(this.modelBridge.getDocumentReferences(spaceReference)).thenReturn(
118    Arrays.asList(aliceReference, bobReference));
119   
120  1 run(createRequest(spaceReference));
121   
122    // We only verify that the code tries to delete the documents.
123  1 verify(this.mocker.getMockedLogger()).warn("Skipping [{}] because it doesn't exist.", aliceReference);
124  1 verify(this.mocker.getMockedLogger()).warn("Skipping [{}] because it doesn't exist.", bobReference);
125    }
126   
 
127  1 toggle @Test
128    public void deleteUnsupportedEntity() throws Exception
129    {
130  1 run(createRequest(new WikiReference("foo")));
131  1 verify(this.mocker.getMockedLogger()).error("Unsupported entity type [{}].", EntityType.WIKI);
132  1 verify(this.modelBridge, never()).delete(any(DocumentReference.class));
133    }
134   
 
135  6 toggle private EntityRequest createRequest(EntityReference... entityReference)
136    {
137  6 EntityRequest request = new EntityRequest();
138  6 request.setEntityReferences(Arrays.asList(entityReference));
139  6 return request;
140    }
141    }