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

File EntityJobTest.java

 

Code metrics

0
69
12
2
223
162
13
0.19
5.75
6
1.08

Classes

Class Line # Actions
EntityJobTest 56 63 0% 7 0
1.0100%
EntityJobTest.NoopEntityJob 58 6 0% 6 2
0.818181881.8%
 

Contributing tests

This file is covered by 4 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.ArrayList;
23    import java.util.Arrays;
24    import java.util.List;
25   
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.component.util.ReflectionUtils;
29    import org.xwiki.job.JobContext;
30    import org.xwiki.job.JobGroupPath;
31    import org.xwiki.job.event.status.JobProgressManager;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.EntityReference;
35    import org.xwiki.model.reference.EntityReferenceProvider;
36    import org.xwiki.model.reference.SpaceReference;
37    import org.xwiki.refactoring.internal.ModelBridge;
38    import org.xwiki.refactoring.internal.job.AbstractEntityJob.Visitor;
39    import org.xwiki.refactoring.job.EntityJobStatus;
40    import org.xwiki.refactoring.job.EntityRequest;
41    import org.xwiki.security.authorization.AuthorizationManager;
42    import org.xwiki.security.authorization.Right;
43    import org.xwiki.test.mockito.MockitoComponentManagerRule;
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.mock;
49    import static org.mockito.Mockito.when;
50   
51    /**
52    * Unit tests for {@link AbstractEntityJob}.
53    *
54    * @version $Id: b7d5a23829c18c6aa6df1267fca1c3a79615f266 $
55    */
 
56    public class EntityJobTest
57    {
 
58    private static class NoopEntityJob extends AbstractEntityJob<EntityRequest, EntityJobStatus<EntityRequest>>
59    {
 
60  7 toggle @Override
61    public String getType()
62    {
63  7 return null;
64    }
65   
 
66  1 toggle @Override
67    public void run()
68    {
69  1 try {
70  1 super.runInternal();
71    } catch (Exception e) {
72  0 throw new RuntimeException(e);
73    }
74    }
75   
 
76  0 toggle @Override
77    protected void process(EntityReference entityReference)
78    {
79    // No operation.
80    }
81   
 
82  1 toggle @Override
83    public void visitDocuments(SpaceReference spaceReference, Visitor<DocumentReference> visitor)
84    {
85  1 super.visitDocuments(spaceReference, visitor);
86    }
87   
 
88  3 toggle @Override
89    public boolean hasAccess(Right right, EntityReference reference)
90    {
91  3 return super.hasAccess(right, reference);
92    }
93    }
94   
95    @Rule
96    public MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
97   
98    private AuthorizationManager authorization = mock(AuthorizationManager.class);
99   
100    private ModelBridge modelBridge = mock(ModelBridge.class);
101   
 
102  7 toggle private void initialize(NoopEntityJob job, EntityRequest request)
103    {
104  7 ReflectionUtils.setFieldValue(job, "jobContext", mock(JobContext.class));
105  7 ReflectionUtils.setFieldValue(job, "progressManager", mock(JobProgressManager.class));
106  7 ReflectionUtils.setFieldValue(job, "authorization", this.authorization);
107  7 ReflectionUtils.setFieldValue(job, "modelBridge", this.modelBridge);
108   
109  7 EntityReferenceProvider defaultEntityReferenceProvider = mock(EntityReferenceProvider.class);
110  7 ReflectionUtils.setFieldValue(job, "defaultEntityReferenceProvider", defaultEntityReferenceProvider);
111  7 when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT))
112    .thenReturn(new EntityReference("WebHome", EntityType.DOCUMENT));
113   
114  7 job.initialize(request);
115    }
116   
 
117  1 toggle @Test
118    public void getGroupPath()
119    {
120  1 NoopEntityJob job = new NoopEntityJob();
121  1 EntityRequest request = new EntityRequest();
122   
123  1 DocumentReference aliceReference = new DocumentReference("chess", Arrays.asList("Path", "To"), "Alice");
124  1 request.setEntityReferences(aliceReference.getReversedReferenceChain());
125  1 initialize(job, request);
126  1 assertEquals(new JobGroupPath(Arrays.asList("refactoring", "chess")), job.getGroupPath());
127   
128  1 DocumentReference bobReference = new DocumentReference("dev", Arrays.asList("Path", "To"), "Bob");
129  1 request.setEntityReferences(Arrays.<EntityReference>asList(aliceReference, bobReference));
130  1 initialize(job, request);
131  1 assertEquals(new JobGroupPath(Arrays.asList("refactoring")), job.getGroupPath());
132   
133  1 DocumentReference carolReference = new DocumentReference("chess", Arrays.asList("Path", "To"), "Carol");
134  1 request.setEntityReferences(Arrays.<EntityReference>asList(aliceReference, carolReference));
135  1 initialize(job, request);
136  1 assertEquals(new JobGroupPath(Arrays.asList("refactoring", "chess", "Path", "To")), job.getGroupPath());
137   
138  1 DocumentReference daveReference = new DocumentReference("chess", Arrays.asList("Path", "To2"), "Dave");
139  1 request.setEntityReferences(Arrays.<EntityReference>asList(aliceReference, carolReference, daveReference));
140  1 initialize(job, request);
141  1 assertEquals(new JobGroupPath(Arrays.asList("refactoring", "chess", "Path")), job.getGroupPath());
142    }
143   
 
144  1 toggle @Test
145    public void processEachEntity()
146    {
147  1 final List<EntityReference> references = new ArrayList<>();
148  1 NoopEntityJob job = new NoopEntityJob()
149    {
 
150  4 toggle @Override
151    protected void process(EntityReference entityReference)
152    {
153  4 references.add(entityReference);
154    }
155    };
156   
157  1 DocumentReference documentReference = new DocumentReference("chess", Arrays.asList("Path", "To"), "Success");
158  1 EntityRequest request = new EntityRequest();
159  1 request.setEntityReferences(documentReference.getReversedReferenceChain());
160  1 initialize(job, request);
161  1 job.run();
162  1 assertEquals(request.getEntityReferences(), references);
163    }
164   
 
165  1 toggle @Test
166    public void visitDocuments()
167    {
168  1 DocumentReference alice = new DocumentReference("foo", "Alice", "WebHome");
169  1 DocumentReference alicePrefs = new DocumentReference("WebPreferences", alice.getLastSpaceReference());
170  1 DocumentReference aliceBio = new DocumentReference("Bio", alice.getLastSpaceReference());
171   
172  1 DocumentReference bob = new DocumentReference("foo", Arrays.asList("Alice", "Bob"), "WebHome");
173  1 DocumentReference bobPrefs = new DocumentReference("WebPreferences", bob.getLastSpaceReference());
174  1 DocumentReference bobBio = new DocumentReference("Bio", bob.getLastSpaceReference());
175   
176  1 DocumentReference carolBio = new DocumentReference("bar", Arrays.asList("Users", "Carol"), "Bio");
177   
178  1 SpaceReference spaceReference = mock(SpaceReference.class);
179  1 when(this.modelBridge.getDocumentReferences(spaceReference))
180    .thenReturn(Arrays.asList(alice, alicePrefs, aliceBio, bob, bobPrefs, bobBio, carolBio));
181   
182  1 NoopEntityJob job = new NoopEntityJob();
183  1 initialize(job, new EntityRequest());
184   
185  1 final List<DocumentReference> documentReferences = new ArrayList<>();
186  1 job.visitDocuments(spaceReference, new Visitor<DocumentReference>()
187    {
 
188  7 toggle @Override
189    public void visit(DocumentReference documentReference)
190    {
191  7 documentReferences.add(documentReference);
192    }
193    });
194    // Space preferences documents are handled after their siblings.
195  1 assertEquals(Arrays.asList(carolBio, aliceBio, bobBio, bob, bobPrefs, alice, alicePrefs), documentReferences);
196    }
197   
 
198  1 toggle @Test
199    public void hasAccess()
200    {
201  1 EntityRequest request = mock(EntityRequest.class);
202  1 when(request.isCheckRights()).thenReturn(true, true, false);
203   
204  1 DocumentReference userReference = new DocumentReference("foo", "Users", "Alice");
205  1 when(request.getUserReference()).thenReturn(userReference);
206  1 when(request.getAuthorReference()).thenReturn(userReference);
207   
208  1 DocumentReference documentReference = new DocumentReference("math", "Space", "Page");
209  1 when(this.authorization.hasAccess(Right.EDIT, userReference, documentReference.getLastSpaceReference()))
210    .thenReturn(true);
211  1 when(this.authorization.hasAccess(Right.DELETE, userReference, documentReference)).thenReturn(false);
212   
213  1 NoopEntityJob job = new NoopEntityJob();
214  1 initialize(job, request);
215   
216    // checkRights = true
217  1 assertTrue(job.hasAccess(Right.EDIT, documentReference.getLastSpaceReference()));
218  1 assertFalse(job.hasAccess(Right.DELETE, documentReference));
219   
220    // checkRights = false
221  1 assertTrue(job.hasAccess(Right.DELETE, documentReference));
222    }
223    }