1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.index.tree.internal.nestedpages

File DocumentTreeNodeTest.java

 

Code metrics

0
113
7
1
357
257
7
0.06
16.14
7
1

Classes

Class Line # Actions
DocumentTreeNodeTest 68 113 0% 7 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.index.tree.internal.nestedpages;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.HashSet;
25    import java.util.Locale;
26   
27    import javax.inject.Named;
28    import javax.inject.Provider;
29   
30    import org.junit.jupiter.api.BeforeEach;
31    import org.junit.jupiter.api.Test;
32    import org.mockito.Mock;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.component.util.DefaultParameterizedType;
35    import org.xwiki.localization.LocalizationContext;
36    import org.xwiki.model.EntityType;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.EntityReference;
39    import org.xwiki.model.reference.EntityReferenceProvider;
40    import org.xwiki.model.reference.EntityReferenceResolver;
41    import org.xwiki.model.reference.EntityReferenceSerializer;
42    import org.xwiki.query.Query;
43    import org.xwiki.query.QueryFilter;
44    import org.xwiki.query.QueryManager;
45    import org.xwiki.security.authorization.ContextualAuthorizationManager;
46    import org.xwiki.security.authorization.Right;
47    import org.xwiki.test.annotation.BeforeComponent;
48    import org.xwiki.test.junit5.mockito.ComponentTest;
49    import org.xwiki.test.junit5.mockito.InjectMockComponents;
50    import org.xwiki.test.junit5.mockito.MockComponent;
51    import org.xwiki.test.mockito.MockitoComponentManager;
52    import org.xwiki.tree.TreeNode;
53   
54    import static org.junit.jupiter.api.Assertions.assertEquals;
55    import static org.mockito.ArgumentMatchers.any;
56    import static org.mockito.Mockito.mock;
57    import static org.mockito.Mockito.never;
58    import static org.mockito.Mockito.verify;
59    import static org.mockito.Mockito.when;
60   
61    /**
62    * Unit tests for {@link DocumentTreeNode}.
63    *
64    * @version $Id: 8debd46fa19c39608a6fb67757aa59f2dacb9ed5 $
65    * @since 9.11RC1
66    */
67    @ComponentTest
 
68    public class DocumentTreeNodeTest
69    {
70    @InjectMockComponents
71    private DocumentTreeNode documentTreeNode;
72   
73    @MockComponent
74    @Named("current")
75    private EntityReferenceResolver<String> currentEntityReferenceResolver;
76   
77    @MockComponent
78    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
79   
80    @MockComponent
81    @Named("local")
82    private EntityReferenceSerializer<String> localEntityReferenceSerializer;
83   
84    @MockComponent
85    private EntityReferenceProvider defaultEntityReferenceProvider;
86   
87    @MockComponent
88    private ContextualAuthorizationManager authorization;
89   
90    @MockComponent
91    private LocalizationContext localizationContext;
92   
93    @MockComponent
94    @Named("childPage/nestedPages")
95    private QueryFilter childPageFilter;
96   
97    @MockComponent
98    @Named("documentReferenceResolver/nestedPages")
99    private QueryFilter documentReferenceResolverFilter;
100   
101    @MockComponent
102    private QueryManager queryManager;
103   
104    @MockComponent
105    @Named("translations")
106    private TreeNode translationsTreeNode;
107   
108    @MockComponent
109    @Named("attachments")
110    private TreeNode attachmentsTreeNode;
111   
112    @MockComponent
113    @Named("classProperties")
114    private TreeNode classPropertiesTreeNode;
115   
116    @MockComponent
117    @Named("objects")
118    private TreeNode objectsTreeNode;
119   
120    @Mock
121    @Named("nestedPagesOrderedByName")
122    private Query nestedPagesOrderedByName;
123   
124    private DocumentReference documentReference =
125    new DocumentReference("wiki", Arrays.asList("Path", "To", "Page"), "WebHome");
126   
127    private DocumentReference terminalDocumentReference = new DocumentReference("wiki", "Some", "Page");
128   
 
129  5 toggle @BeforeComponent
130    public void configure(MockitoComponentManager componentManager) throws Exception
131    {
132  5 Provider<ComponentManager> contextComponentManagerProvider = componentManager.registerMockComponent(
133    new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
134  5 when(contextComponentManagerProvider.get()).thenReturn(componentManager);
135    }
136   
 
137  5 toggle @BeforeEach
138    public void before() throws Exception
139    {
140  5 when(this.defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT))
141    .thenReturn(new EntityReference("WebHome", EntityType.DOCUMENT));
142   
143  5 when(this.currentEntityReferenceResolver.resolve("wiki:Path.To.Page.WebHome", EntityType.DOCUMENT))
144    .thenReturn(documentReference);
145  5 when(this.defaultEntityReferenceSerializer.serialize(documentReference))
146    .thenReturn("wiki:Path.To.Page.WebHome");
147  5 when(this.localEntityReferenceSerializer.serialize(documentReference.getParent())).thenReturn("Path.To.Page");
148   
149  5 when(this.currentEntityReferenceResolver.resolve("wiki:Some.Page", EntityType.DOCUMENT))
150    .thenReturn(this.terminalDocumentReference);
151  5 when(this.defaultEntityReferenceSerializer.serialize(this.terminalDocumentReference))
152    .thenReturn("wiki:Some.Page");
153   
154  5 when(this.queryManager.getNamedQuery("nestedPagesOrderedByName")).thenReturn(this.nestedPagesOrderedByName);
155  5 when(this.nestedPagesOrderedByName.addFilter(any(QueryFilter.class))).thenReturn(this.nestedPagesOrderedByName);
156    }
157   
158    /**
159    * @see "XWIKI-14643: Missing page in breadcrumbs treeview when treeview is expanded"
160    */
 
161  1 toggle @Test
162    public void pagination() throws Exception
163    {
164  1 this.documentTreeNode.getProperties().put("hierarchyMode", "reference");
165  1 this.documentTreeNode.getProperties().put("showTranslations", true);
166  1 this.documentTreeNode.getProperties().put("showAttachments", true);
167  1 this.documentTreeNode.getProperties().put("showClassProperties", true);
168  1 this.documentTreeNode.getProperties().put("showObjects", true);
169  1 this.documentTreeNode.getProperties().put("showAddDocument", true);
170   
171  1 when(this.authorization.hasAccess(Right.EDIT, documentReference.getParent())).thenReturn(true);
172  1 when(this.translationsTreeNode.getChildCount("translations:wiki:Path.To.Page.WebHome")).thenReturn(1);
173  1 when(this.attachmentsTreeNode.getChildCount("attachments:wiki:Path.To.Page.WebHome")).thenReturn(1);
174  1 when(this.classPropertiesTreeNode.getChildCount("classProperties:wiki:Path.To.Page.WebHome")).thenReturn(1);
175  1 when(this.objectsTreeNode.getChildCount("objects:wiki:Path.To.Page.WebHome")).thenReturn(1);
176   
177  1 assertEquals(
178    Arrays.asList("translations:wiki:Path.To.Page.WebHome", "attachments:wiki:Path.To.Page.WebHome",
179    "classProperties:wiki:Path.To.Page.WebHome"),
180    this.documentTreeNode.getChildren("document:wiki:Path.To.Page.WebHome", 0, 3));
181   
182  1 verify(this.nestedPagesOrderedByName, never()).execute();
183   
184  1 DocumentReference alice = new DocumentReference("wiki", Arrays.asList("Path.To.Page"), "Alice");
185  1 when(this.nestedPagesOrderedByName.execute()).thenReturn(Collections.singletonList(alice));
186  1 when(this.defaultEntityReferenceSerializer.serialize(alice)).thenReturn("wiki:Path.To.Page.Alice");
187   
188  1 assertEquals(
189    Arrays.asList("objects:wiki:Path.To.Page.WebHome", "addDocument:wiki:Path.To.Page.WebHome",
190    "document:wiki:Path.To.Page.Alice"),
191    this.documentTreeNode.getChildren("document:wiki:Path.To.Page.WebHome", 3, 3));
192   
193  1 verify(this.nestedPagesOrderedByName).setOffset(0);
194  1 verify(this.nestedPagesOrderedByName).setLimit(1);
195   
196  1 DocumentReference bob = new DocumentReference("wiki", Arrays.asList("Path.To.Page"), "Bob");
197  1 DocumentReference carol = new DocumentReference("wiki", Arrays.asList("Path.To.Page"), "Carol");
198  1 when(this.nestedPagesOrderedByName.execute()).thenReturn(Arrays.asList(bob, carol));
199  1 when(this.defaultEntityReferenceSerializer.serialize(bob)).thenReturn("wiki:Path.To.Page.Bob");
200  1 when(this.defaultEntityReferenceSerializer.serialize(carol)).thenReturn("wiki:Path.To.Page.Carol");
201   
202  1 assertEquals(Arrays.asList("document:wiki:Path.To.Page.Bob", "document:wiki:Path.To.Page.Carol"),
203    this.documentTreeNode.getChildren("document:wiki:Path.To.Page.WebHome", 6, 3));
204   
205  1 verify(this.nestedPagesOrderedByName).setOffset(1);
206  1 verify(this.nestedPagesOrderedByName).setLimit(3);
207    }
208   
 
209  1 toggle @Test
210    public void getChildDocuments() throws Exception
211    {
212  1 assertEquals(Collections.emptyList(),
213    this.documentTreeNode.getChildDocuments(terminalDocumentReference, 0, 10));
214   
215  1 this.documentTreeNode.getProperties().put("showTerminalDocuments", false);
216  1 Query queryNonTerminalPagesByName = mock(Query.class, "nonTerminalPagesOrderedByTitle");
217  1 String statement = "select reference, 0 as terminal from XWikiSpace page order by lower(name), name";
218  1 when(this.queryManager.createQuery(statement, Query.HQL)).thenReturn(queryNonTerminalPagesByName);
219  1 when(queryNonTerminalPagesByName.addFilter(this.documentReferenceResolverFilter))
220    .thenReturn(queryNonTerminalPagesByName);
221  1 DocumentReference childReference = new DocumentReference("wiki", Arrays.asList("Path.To.Page"), "Alice");
222  1 when(queryNonTerminalPagesByName.execute()).thenReturn(Collections.singletonList(childReference));
223   
224  1 assertEquals(Collections.singletonList(childReference),
225    this.documentTreeNode.getChildDocuments(documentReference, 5, 3));
226   
227  1 verify(queryNonTerminalPagesByName).setWiki("wiki");
228  1 verify(queryNonTerminalPagesByName).setOffset(5);
229  1 verify(queryNonTerminalPagesByName).setLimit(3);
230  1 verify(queryNonTerminalPagesByName).addFilter(this.childPageFilter);
231  1 verify(queryNonTerminalPagesByName).bindValue("parent", "Path.To.Page");
232   
233  1 this.documentTreeNode.getProperties().put("orderBy", "title");
234  1 Query queryNonTerminalPagesByTitle = mock(Query.class, "nonTerminalPagesOrderedByTitle");
235  1 when(this.queryManager.getNamedQuery("nonTerminalPagesOrderedByTitle"))
236    .thenReturn(queryNonTerminalPagesByTitle);
237  1 childReference = new DocumentReference("wiki", Arrays.asList("Path.To.Page"), "Bob");
238  1 when(queryNonTerminalPagesByTitle.addFilter(this.documentReferenceResolverFilter))
239    .thenReturn(queryNonTerminalPagesByTitle);
240  1 when(queryNonTerminalPagesByTitle.execute()).thenReturn(Collections.singletonList(childReference));
241  1 when(this.localizationContext.getCurrentLocale()).thenReturn(Locale.GERMAN);
242   
243  1 assertEquals(Collections.singletonList(childReference),
244    this.documentTreeNode.getChildDocuments(documentReference, 0, 5));
245   
246  1 verify(queryNonTerminalPagesByTitle).bindValue("locale", "de");
247    }
248   
 
249  1 toggle @Test
250    public void getChildrenByNameWithExclusions() throws Exception
251    {
252  1 this.documentTreeNode.getProperties().put("exclusions",
253    new HashSet<>(Arrays.asList("document:wiki:Path.To.OtherPage", "document:wiki:Path.To.Page.Alice",
254    "document:wiki:Path.WebHome", "document:wiki:Path.To.Page.Bob.WebHome")));
255   
256  1 when(this.currentEntityReferenceResolver.resolve("wiki:Path.To.OtherPage", EntityType.DOCUMENT))
257    .thenReturn(new DocumentReference("wiki", Arrays.asList("Path", "To"), "OtherPage"));
258  1 when(this.currentEntityReferenceResolver.resolve("wiki:Path.WebHome", EntityType.DOCUMENT))
259    .thenReturn(new DocumentReference("wiki", "Path", "WebHome"));
260   
261  1 DocumentReference alice = new DocumentReference("Alice", this.documentReference.getLastSpaceReference());
262  1 when(this.currentEntityReferenceResolver.resolve("wiki:Path.To.Page.Alice", EntityType.DOCUMENT))
263    .thenReturn(alice);
264  1 when(this.localEntityReferenceSerializer.serialize(alice)).thenReturn("Path.To.Page.Alice");
265   
266  1 DocumentReference bob = new DocumentReference("wiki", Arrays.asList("Path", "To", "Page", "Bob"), "WebHome");
267  1 when(this.currentEntityReferenceResolver.resolve("wiki:Path.To.Page.Bob.WebHome", EntityType.DOCUMENT))
268    .thenReturn(bob);
269  1 when(this.localEntityReferenceSerializer.serialize(bob.getParent())).thenReturn("Path.To.Page.Bob");
270   
271  1 DocumentReference child = new DocumentReference("Child", this.documentReference.getLastSpaceReference());
272  1 when(this.nestedPagesOrderedByName.execute()).thenReturn(Collections.singletonList(child));
273  1 when(this.defaultEntityReferenceSerializer.serialize(child)).thenReturn("wiki:Path.To.Page.Child");
274   
275  1 assertEquals(Collections.singletonList("document:wiki:Path.To.Page.Child"),
276    this.documentTreeNode.getChildren("document:wiki:Path.To.Page.WebHome", 0, 5));
277   
278  1 verify(this.nestedPagesOrderedByName).bindValue("excludedDocuments",
279    Collections.singleton("Path.To.Page.Alice"));
280  1 verify(this.nestedPagesOrderedByName).bindValue("excludedSpaces", Collections.singleton("Path.To.Page.Bob"));
281    }
282   
 
283  1 toggle @Test
284    public void getChildCount() throws Exception
285    {
286  1 this.documentTreeNode.getProperties().put("exclusions",
287    new HashSet<>(Arrays.asList("document:wiki:Path.To.Page.Alice", "document:wiki:Path.To.Page.Bob.WebHome")));
288   
289  1 DocumentReference alice = new DocumentReference("Alice", this.documentReference.getLastSpaceReference());
290  1 when(this.currentEntityReferenceResolver.resolve("wiki:Path.To.Page.Alice", EntityType.DOCUMENT))
291    .thenReturn(alice);
292  1 when(this.localEntityReferenceSerializer.serialize(alice)).thenReturn("Path.To.Page.Alice");
293   
294  1 DocumentReference bob = new DocumentReference("wiki", Arrays.asList("Path", "To", "Page", "Bob"), "WebHome");
295  1 when(this.currentEntityReferenceResolver.resolve("wiki:Path.To.Page.Bob.WebHome", EntityType.DOCUMENT))
296    .thenReturn(bob);
297  1 when(this.localEntityReferenceSerializer.serialize(bob.getParent())).thenReturn("Path.To.Page.Bob");
298   
299  1 Query childSpacesQuery = mock(Query.class, "childSpaces");
300  1 when(this.queryManager.createQuery(
301    "select count(*) from XWikiSpace where parent = :parent " + "and reference not in (:excludedSpaces)",
302    Query.HQL)).thenReturn(childSpacesQuery);
303  1 when(childSpacesQuery.execute()).thenReturn(Collections.singletonList(2L));
304   
305  1 Query childTerminalPagesQuery = mock(Query.class, "childTerminalPages");
306  1 when(this.queryManager.createQuery("where doc.translation = 0 and doc.space = :space and "
307    + "doc.name <> :defaultDocName and doc.fullName not in (:excludedDocuments)", Query.HQL))
308    .thenReturn(childTerminalPagesQuery);
309  1 when(childTerminalPagesQuery.execute()).thenReturn(Collections.singletonList(3L));
310   
311  1 assertEquals(5L, this.documentTreeNode.getChildCount("document:wiki:Path.To.Page.WebHome"));
312   
313  1 verify(childSpacesQuery).setWiki("wiki");
314  1 verify(childSpacesQuery).bindValue("parent", "Path.To.Page");
315  1 verify(childSpacesQuery).bindValue("excludedSpaces", Collections.singleton("Path.To.Page.Bob"));
316   
317  1 verify(childTerminalPagesQuery).setWiki("wiki");
318  1 verify(childTerminalPagesQuery).bindValue("space", "Path.To.Page");
319  1 verify(childTerminalPagesQuery).bindValue("defaultDocName", "WebHome");
320  1 verify(childTerminalPagesQuery).bindValue("excludedDocuments", Collections.singleton("Path.To.Page.Alice"));
321   
322  1 this.documentTreeNode.getProperties().put("showTerminalDocuments", false);
323   
324  1 assertEquals(2L, this.documentTreeNode.getChildCount("document:wiki:Path.To.Page.WebHome"));
325    }
326   
 
327  1 toggle @Test
328    public void getPseudoChildCount()
329    {
330  1 when(this.translationsTreeNode.getChildCount("translations:wiki:Some.Page")).thenReturn(2);
331  1 when(this.attachmentsTreeNode.getChildCount("attachments:wiki:Some.Page")).thenReturn(1);
332  1 when(this.classPropertiesTreeNode.getChildCount("classProperties:wiki:Some.Page")).thenReturn(3);
333  1 when(this.objectsTreeNode.getChildCount("objects:wiki:Some.Page")).thenReturn(5);
334   
335  1 assertEquals(0L, this.documentTreeNode.getChildCount("document:wiki:Some.Page"));
336   
337  1 this.documentTreeNode.getProperties().put("showTranslations", true);
338   
339  1 assertEquals(1L, this.documentTreeNode.getChildCount("document:wiki:Some.Page"));
340   
341  1 this.documentTreeNode.getProperties().put("showAttachments", true);
342   
343  1 assertEquals(2L, this.documentTreeNode.getChildCount("document:wiki:Some.Page"));
344   
345  1 this.documentTreeNode.getProperties().put("showClassProperties", true);
346   
347  1 assertEquals(3L, this.documentTreeNode.getChildCount("document:wiki:Some.Page"));
348   
349  1 this.documentTreeNode.getProperties().put("showObjects", true);
350   
351  1 assertEquals(4L, this.documentTreeNode.getChildCount("document:wiki:Some.Page"));
352   
353  1 when(this.attachmentsTreeNode.getChildCount("attachments:wiki:Some.Page")).thenReturn(0);
354   
355  1 assertEquals(3L, this.documentTreeNode.getChildCount("document:wiki:Some.Page"));
356    }
357    }