1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File ExportActionTest.java

 

Code metrics

0
93
2
1
254
160
2
0.02
46.5
2
1

Classes

Class Line # Actions
ExportActionTest 71 93 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 2 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 com.xpn.xwiki.web;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import javax.servlet.ServletOutputStream;
26   
27    import org.junit.jupiter.api.Test;
28    import org.mockito.ArgumentCaptor;
29    import org.xwiki.filter.input.InputFilterStream;
30    import org.xwiki.filter.input.InputFilterStreamFactory;
31    import org.xwiki.filter.instance.input.DocumentInstanceInputProperties;
32    import org.xwiki.filter.output.BeanOutputFilterStream;
33    import org.xwiki.filter.output.BeanOutputFilterStreamFactory;
34    import org.xwiki.filter.output.OutputFilterStreamFactory;
35    import org.xwiki.filter.type.FilterStreamType;
36    import org.xwiki.filter.xar.output.XAROutputProperties;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.DocumentReferenceResolver;
39    import org.xwiki.model.reference.WikiReference;
40    import org.xwiki.query.Query;
41    import org.xwiki.query.QueryManager;
42    import org.xwiki.security.authorization.AuthorizationManager;
43    import org.xwiki.security.authorization.Right;
44   
45    import com.xpn.xwiki.XWikiContext;
46    import com.xpn.xwiki.doc.XWikiDocument;
47    import com.xpn.xwiki.test.MockitoOldcore;
48    import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
49    import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
50   
51    import static org.junit.jupiter.api.Assertions.assertEquals;
52    import static org.junit.jupiter.api.Assertions.assertFalse;
53    import static org.junit.jupiter.api.Assertions.assertNull;
54    import static org.junit.jupiter.api.Assertions.assertTrue;
55    import static org.mockito.Mockito.any;
56    import static org.mockito.Mockito.anyList;
57    import static org.mockito.Mockito.anyMap;
58    import static org.mockito.Mockito.anyString;
59    import static org.mockito.Mockito.eq;
60    import static org.mockito.Mockito.mock;
61    import static org.mockito.Mockito.verify;
62    import static org.mockito.Mockito.when;
63   
64    /**
65    * Unit tests for {@link com.xpn.xwiki.web.ExportAction}.
66    *
67    * @version $Id: 2b77f175d68bed47cc0b80f2c7489f53fc6ef083 $
68    * @since 6.3M2
69    */
70    @OldcoreTest
 
71    public class ExportActionTest
72    {
73    @InjectMockitoOldcore
74    public MockitoOldcore oldcore;
75   
 
76  1 toggle @Test
77    public void exportFullSpaceUsingWildcardsAsXAR() throws Exception
78    {
79  1 ExportAction action = new ExportAction();
80   
81  1 XWikiContext context = oldcore.getXWikiContext();
82   
83    // Make it a XAR export
84  1 XWikiRequest request = mock(XWikiRequest.class);
85  1 when(request.get("format")).thenReturn("xar");
86  1 context.setRequest(request);
87   
88    // Set other request parameters
89  1 when(request.get("name")).thenReturn("myexport");
90    // Export all pages in the "Space" space
91  1 when(request.getParameterValues("pages")).thenReturn(new String[] {"Space.%25"});
92  1 when(request.getCharacterEncoding()).thenReturn("UTF-8");
93   
94    // Make the current user have programming rights
95  1 when(oldcore.getMockRightService().hasWikiAdminRights(context)).thenReturn(true);
96   
97    // Query Manager-related mocking
98  1 QueryManager queryManager = oldcore.getMocker().registerMockComponent(QueryManager.class);
99  1 Query query = mock(Query.class);
100  1 when(queryManager.createQuery(anyString(), eq(Query.HQL))).thenReturn(query);
101  1 when(query.setWiki("xwiki")).thenReturn(query);
102  1 when(query.bindValues(any(List.class))).thenReturn(query);
103  1 when(query.execute()).thenReturn(Arrays.asList("Space.Page1", "Space.Page2"));
104   
105    // Register some mock resolver to resolve passed page references
106  1 AuthorizationManager authorizationManager = oldcore.getMocker().registerMockComponent(AuthorizationManager.class);
107   
108  1 DocumentReference page1Ref = new DocumentReference("xwiki", "Space", "Page1");
109  1 DocumentReference page2Ref = new DocumentReference("xwiki", "Space", "Page2");
110  1 when(authorizationManager.hasAccess(Right.VIEW, context.getUserReference(), page1Ref)).thenReturn(true);
111  1 when(authorizationManager.hasAccess(Right.VIEW, context.getUserReference(), page2Ref)).thenReturn(true);
112   
113  1 DocumentReferenceResolver<String> resolver =
114    oldcore.getMocker().registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
115  1 when(resolver.resolve("xwiki:Space.Page1")).thenReturn(page1Ref);
116  1 when(resolver.resolve("xwiki:Space.Page2")).thenReturn(page2Ref);
117   
118    // Register some mock filters so that the export does nothing.
119  1 InputFilterStreamFactory inputFilterStreamFactory = oldcore.getMocker().registerMockComponent(
120    InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
121  1 when(inputFilterStreamFactory.createInputFilterStream(anyMap())).thenReturn(mock(InputFilterStream.class));
122  1 BeanOutputFilterStreamFactory beanOutputFilterStreamFactory = mock(BeanOutputFilterStreamFactory.class);
123  1 oldcore.getMocker().registerComponent(OutputFilterStreamFactory.class,
124    FilterStreamType.XWIKI_XAR_CURRENT.serialize(), beanOutputFilterStreamFactory);
125  1 when(beanOutputFilterStreamFactory.createOutputFilterStream(any(XAROutputProperties.class))).thenReturn(
126    mock(BeanOutputFilterStream.class));
127   
128    // Set response stream
129  1 XWikiResponse response = mock(XWikiResponse.class);
130  1 ServletOutputStream outputStream = mock(ServletOutputStream.class);
131  1 when(response.getOutputStream()).thenReturn(outputStream);
132  1 context.setResponse(response);
133   
134  1 String result = action.render(oldcore.getXWikiContext());
135   
136    // The tests are below this line!
137   
138    // Verify null is returned (this means the response has been returned)
139  1 assertNull(result);
140   
141    // Verify that the parameters passed to the input stream factory are defining the correct pages
142  1 ArgumentCaptor<DocumentInstanceInputProperties> properties =
143    ArgumentCaptor.forClass(DocumentInstanceInputProperties.class);
144  1 verify(inputFilterStreamFactory).createInputFilterStream(properties.capture());
145  1 assertFalse(properties.getValue().isVerbose());
146  1 assertFalse(properties.getValue().isWithJRCSRevisions());
147  1 assertFalse(properties.getValue().isWithRevisions());
148   
149  1 assertTrue(properties.getValue().getEntities().matches(page1Ref));
150  1 assertTrue(properties.getValue().getEntities().matches(page2Ref));
151    }
152   
 
153  1 toggle @Test
154    public void exportXARSpaceUsingUncheckedPagesAndOtherPagesArgument() throws Exception
155    {
156    // Scenario:
157    // We want to export all Space.% that contains 3 pages, but with exclusion of Page1 and 2
158    // to export all Foo.% and to export all Bar.Baz.% except Bar.Baz.WebHome
159    //
160    // This test will check the request is properly built and the pages are correctly resolved by checking
161    // result only on Space: we ignore the others for sake of simplicity
162  1 ExportAction action = new ExportAction();
163   
164  1 XWikiContext context = oldcore.getXWikiContext();
165  1 context.setDoc(new XWikiDocument(new DocumentReference("xwiki", "Space",
166    "Page1")));
167   
168    // Make it a XAR export
169  1 XWikiRequest request = mock(XWikiRequest.class);
170  1 when(request.get("format")).thenReturn("xar");
171  1 context.setRequest(request);
172   
173    // Set other request parameters
174  1 when(request.get("name")).thenReturn("myexport");
175  1 when(request.getCharacterEncoding()).thenReturn("UTF-8");
176    // Export all pages in the "Space" space
177  1 when(request.getParameterValues("pages")).thenReturn(new String[] {
178    "Space.%25", "Foo.%25", "Bar.Baz.%25"});
179  1 when(request.getParameterValues("excludes")).thenReturn(new String[] {
180    "Space.Page1&Space.Page2", "", "Bar.Baz.WebHome" });
181   
182    // Make the current user have programming rights
183  1 when(oldcore.getMockRightService().hasWikiAdminRights(context)).thenReturn(true);
184   
185    // Query Manager-related mocking
186  1 QueryManager queryManager = oldcore.getMocker().registerMockComponent(QueryManager.class);
187  1 Query query = mock(Query.class);
188  1 String where = "where ( doc.fullName like ? and doc.fullName not like ? and doc.fullName not like ? ) or "
189    + "( doc.fullName like ? ) or ( doc.fullName like ? and doc.fullName not like ? ) ";
190  1 when(queryManager.createQuery(anyString(), eq(Query.HQL))).thenReturn(query);
191  1 when(query.setWiki("xwiki")).thenReturn(query);
192   
193  1 when(query.bindValues(anyList())).thenReturn(query);
194  1 when(query.execute()).thenReturn(Arrays.asList("Space.Page3"));
195   
196    // Register some mock resolver to resolve passed page references
197  1 AuthorizationManager authorizationManager = oldcore.getMocker().
198    registerMockComponent(AuthorizationManager.class);
199   
200  1 DocumentReference page1Ref = new DocumentReference("xwiki", "Space", "Page1");
201  1 DocumentReference page2Ref = new DocumentReference("xwiki", "Space", "Page2");
202  1 DocumentReference page3Ref = new DocumentReference("xwiki", "Space", "Page3");
203  1 when(authorizationManager.hasAccess(Right.VIEW, context.getUserReference(), page1Ref)).thenReturn(true);
204  1 when(authorizationManager.hasAccess(Right.VIEW, context.getUserReference(), page2Ref)).thenReturn(true);
205  1 when(authorizationManager.hasAccess(Right.VIEW, context.getUserReference(), page3Ref)).thenReturn(true);
206   
207  1 DocumentReferenceResolver<String> resolver =
208    oldcore.getMocker().registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
209   
210  1 WikiReference wikiReference = new WikiReference("xwiki");
211  1 when(resolver.resolve("Space.Page1", wikiReference)).thenReturn(page1Ref);
212  1 when(resolver.resolve("Space.Page2", wikiReference)).thenReturn(page2Ref);
213  1 when(resolver.resolve("Space.Page3", wikiReference)).thenReturn(page3Ref);
214   
215    // Register some mock filters so that the export does nothing.
216  1 InputFilterStreamFactory inputFilterStreamFactory = oldcore.getMocker().registerMockComponent(
217    InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
218  1 when(inputFilterStreamFactory.createInputFilterStream(anyMap())).thenReturn(mock(InputFilterStream.class));
219  1 BeanOutputFilterStreamFactory beanOutputFilterStreamFactory = mock(BeanOutputFilterStreamFactory.class);
220  1 oldcore.getMocker().registerComponent(OutputFilterStreamFactory.class,
221    FilterStreamType.XWIKI_XAR_CURRENT.serialize(), beanOutputFilterStreamFactory);
222  1 when(beanOutputFilterStreamFactory.createOutputFilterStream(any(XAROutputProperties.class))).thenReturn(
223    mock(BeanOutputFilterStream.class));
224   
225    // Set response stream
226  1 XWikiResponse response = mock(XWikiResponse.class);
227  1 ServletOutputStream outputStream = mock(ServletOutputStream.class);
228  1 when(response.getOutputStream()).thenReturn(outputStream);
229  1 context.setResponse(response);
230   
231  1 String result = action.render(oldcore.getXWikiContext());
232   
233    // The tests are below this line!
234   
235    // Verify null is returned (this means the response has been returned)
236  1 assertNull(result);
237   
238    // Verify that the parameters passed to the input stream factory are defining the correct pages
239  1 ArgumentCaptor<DocumentInstanceInputProperties> properties =
240    ArgumentCaptor.forClass(DocumentInstanceInputProperties.class);
241  1 verify(inputFilterStreamFactory).createInputFilterStream(properties.capture());
242   
243  1 ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
244  1 verify(queryManager).createQuery(argument.capture(), eq(Query.HQL));
245   
246  1 assertEquals(where, argument.getValue());
247  1 assertFalse(properties.getValue().isVerbose());
248  1 assertFalse(properties.getValue().isWithJRCSRevisions());
249  1 assertFalse(properties.getValue().isWithRevisions());
250  1 assertTrue(properties.getValue().getEntities().matches(page3Ref));
251  1 assertFalse(properties.getValue().getEntities().matches(page1Ref));
252  1 assertFalse(properties.getValue().getEntities().matches(page2Ref));
253    }
254    }