1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.rest

File WikisResourceTest.java

 

Code metrics

0
161
13
1
427
296
13
0.08
12.38
13
1

Classes

Class Line # Actions
WikisResourceTest 63 161 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 12 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.test.rest;
21   
22    import java.io.InputStream;
23    import java.io.StringReader;
24    import java.util.Arrays;
25    import java.util.List;
26   
27    import org.apache.commons.httpclient.HttpStatus;
28    import org.apache.commons.httpclient.methods.GetMethod;
29    import org.apache.commons.httpclient.methods.PostMethod;
30    import org.apache.commons.httpclient.util.URIUtil;
31    import org.junit.Assert;
32    import org.junit.Before;
33    import org.junit.Test;
34    import org.restlet.engine.io.ReaderInputStream;
35    import org.xwiki.model.reference.AttachmentReference;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.rest.Relations;
38    import org.xwiki.rest.model.jaxb.Attachment;
39    import org.xwiki.rest.model.jaxb.Attachments;
40    import org.xwiki.rest.model.jaxb.Link;
41    import org.xwiki.rest.model.jaxb.Page;
42    import org.xwiki.rest.model.jaxb.PageSummary;
43    import org.xwiki.rest.model.jaxb.Pages;
44    import org.xwiki.rest.model.jaxb.SearchResult;
45    import org.xwiki.rest.model.jaxb.SearchResults;
46    import org.xwiki.rest.model.jaxb.Wiki;
47    import org.xwiki.rest.model.jaxb.Wikis;
48    import org.xwiki.rest.resources.pages.PageResource;
49    import org.xwiki.rest.resources.wikis.WikiAttachmentsResource;
50    import org.xwiki.rest.resources.wikis.WikiPagesResource;
51    import org.xwiki.rest.resources.wikis.WikiResource;
52    import org.xwiki.rest.resources.wikis.WikiSearchQueryResource;
53    import org.xwiki.rest.resources.wikis.WikiSearchResource;
54    import org.xwiki.rest.resources.wikis.WikisResource;
55    import org.xwiki.rest.resources.wikis.WikisSearchQueryResource;
56    import org.xwiki.test.rest.framework.AbstractHttpTest;
57    import org.xwiki.test.ui.TestUtils;
58   
59    import static org.junit.Assert.assertEquals;
60    import static org.junit.Assert.assertNotNull;
61    import static org.junit.Assert.assertNull;
62   
 
63    public class WikisResourceTest extends AbstractHttpTest
64    {
65    private String wikiName;
66   
67    private List<String> spaces;
68   
69    private String pageName;
70   
71    private String fullName;
72   
73    private DocumentReference reference;
74   
 
75  12 toggle @Before
76    @Override
77    public void setUp() throws Exception
78    {
79  12 super.setUp();
80   
81  12 this.wikiName = getWiki();
82  12 this.spaces = Arrays.asList(getTestClassName());
83  12 this.pageName = getTestMethodName();
84  12 this.fullName = getTestClassName() + '.' + getTestMethodName();
85   
86  12 this.reference = new DocumentReference(this.wikiName, this.spaces, this.pageName);
87    }
88   
 
89  1 toggle @Override
90    @Test
91    public void testRepresentation() throws Exception
92    {
93  1 GetMethod getMethod = executeGet(getFullUri(WikisResource.class));
94  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
95   
96  1 Wikis wikis = (Wikis) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
97  1 Assert.assertTrue(getHttpMethodInfo(getMethod), wikis.getWikis().size() > 0);
98   
99  1 for (Wiki wiki : wikis.getWikis()) {
100  1 Link link = getFirstLinkByRelation(wiki, Relations.SPACES);
101  1 Assert.assertNotNull(link);
102   
103  1 link = getFirstLinkByRelation(wiki, Relations.CLASSES);
104  1 Assert.assertNotNull(link);
105   
106  1 link = getFirstLinkByRelation(wiki, Relations.MODIFICATIONS);
107  1 Assert.assertNotNull(link);
108   
109  1 link = getFirstLinkByRelation(wiki, Relations.SEARCH);
110  1 Assert.assertNotNull(link);
111   
112  1 link = getFirstLinkByRelation(wiki, Relations.QUERY);
113  1 Assert.assertNotNull(link);
114   
115  1 checkLinks(wiki);
116    }
117    }
118   
 
119  1 toggle @Test
120    public void testSearchWikis() throws Exception
121    {
122  1 this.testUtils.rest().delete(reference);
123  1 this.testUtils.rest().savePage(reference, "content" + this.pageName, "title" + this.pageName);
124   
125  1 GetMethod getMethod =
126    executeGet(String.format("%s?q=content" + this.pageName, buildURI(WikiSearchResource.class, getWiki())));
127  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
128   
129  1 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
130   
131  1 int resultSize = searchResults.getSearchResults().size();
132  1 assertEquals(1, resultSize);
133   
134  1 for (SearchResult searchResult : searchResults.getSearchResults()) {
135  1 checkLinks(searchResult);
136    }
137   
138  1 getMethod = executeGet(
139    String.format("%s?q=" + this.pageName + "&scope=name", buildURI(WikiSearchResource.class, getWiki())));
140  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
141   
142  1 searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
143   
144  1 resultSize = searchResults.getSearchResults().size();
145  1 assertEquals(1, resultSize);
146   
147  1 for (SearchResult searchResult : searchResults.getSearchResults()) {
148  1 checkLinks(searchResult);
149    }
150   
151    // Search in titles
152  1 getMethod = executeGet(String.format("%s?q=title" + this.pageName + "&scope=title",
153    buildURI(WikiSearchResource.class, getWiki())));
154  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
155   
156  1 searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
157   
158  1 resultSize = searchResults.getSearchResults().size();
159  1 assertEquals(1, resultSize);
160   
161  1 for (SearchResult searchResult : searchResults.getSearchResults()) {
162  1 checkLinks(searchResult);
163    }
164   
165    // Search for space names
166  1 getMethod = executeGet(String.format("%s?q=" + this.spaces.get(0) + "&scope=spaces",
167    buildURI(WikiSearchResource.class, getWiki())));
168  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
169   
170  1 searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
171   
172  1 resultSize = searchResults.getSearchResults().size();
173  1 assertEquals(1, resultSize);
174   
175  1 for (SearchResult searchResult : searchResults.getSearchResults()) {
176  1 checkLinks(searchResult);
177    }
178    }
179   
 
180  1 toggle @Test
181    public void testObjectSearchNotAuthenticated() throws Exception
182    {
183    /* Check search for an object containing XWiki.Admin (i.e., the admin profile) */
184  1 GetMethod getMethod =
185    executeGet(String.format("%s?q=XWiki.Admin&scope=objects", buildURI(WikiSearchResource.class, getWiki())));
186  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
187   
188  1 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
189   
190  1 int resultSize = searchResults.getSearchResults().size();
191  1 Assert.assertTrue(String.format("Found %s results", resultSize), resultSize == 0);
192    }
193   
 
194  1 toggle @Test
195    public void testObjectSearchAuthenticated() throws Exception
196    {
197    /* Check search for an object containing XWiki.Admin (i.e., the admin profile) */
198  1 GetMethod getMethod = executeGet(
199    String.format("%s?q=XWiki.XWikiGuest&scope=objects", buildURI(WikiSearchResource.class, getWiki())),
200    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
201  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
202   
203  1 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
204   
205    /*
206    * We get more results because previous tests have also created comments on behalf of XWiki.Admin. They will
207    * appear in the results.
208    */
209  1 int resultSize = searchResults.getSearchResults().size();
210  1 Assert.assertTrue(String.format("Found %s results", resultSize), resultSize >= 1);
211    }
212   
 
213  1 toggle @Test
214    public void testPages() throws Exception
215    {
216    // Create a clean test page.
217  1 this.testUtils.rest().delete(this.reference);
218  1 this.testUtils.rest().savePage(this.reference);
219   
220    // Get all pages
221  1 GetMethod getMethod = executeGet(String.format("%s", buildURI(WikiPagesResource.class, getWiki())));
222  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
223   
224  1 Pages pages = (Pages) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
225   
226  1 Assert.assertTrue(pages.getPageSummaries().size() > 0);
227   
228  1 for (PageSummary pageSummary : pages.getPageSummaries()) {
229  25 checkLinks(pageSummary);
230    }
231   
232    // Get all pages having a document name that contains "WebHome" (for all spaces)
233  1 getMethod = executeGet(String.format("%s?name=" + this.pageName, buildURI(WikiPagesResource.class, getWiki())));
234  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
235   
236  1 pages = (Pages) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
237   
238  1 List<PageSummary> pageSummaries = pages.getPageSummaries();
239  1 Assert.assertTrue(pageSummaries.size() == 1);
240  1 PageSummary pageSummary = pageSummaries.get(0);
241  1 assertEquals(this.fullName, pageSummary.getFullName());
242  1 checkLinks(pageSummary);
243   
244    // Get all pages having a document name that contains "WebHome" and a space with an "s" in its name.
245  1 getMethod = executeGet(String.format("%s?name=" + this.pageName + "&space=" + this.fullName.charAt(2),
246    buildURI(WikiPagesResource.class, getWiki())));
247  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
248   
249  1 pages = (Pages) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
250   
251  1 pageSummaries = pages.getPageSummaries();
252  1 Assert.assertTrue(pageSummaries.size() == 1);
253  1 pageSummary = pageSummaries.get(0);
254  1 assertEquals(this.fullName, pageSummary.getFullName());
255  1 checkLinks(pageSummary);
256    }
257   
 
258  1 toggle @Test
259    public void testAttachments() throws Exception
260    {
261  1 this.testUtils.rest().delete(reference);
262  1 this.testUtils.rest().attachFile(new AttachmentReference(getTestClassName() + ".txt", reference),
263    new ReaderInputStream(new StringReader("attachment content")), true);
264   
265    // Verify there are attachments in the whole wiki
266  1 GetMethod getMethod = executeGet(buildURI(WikiAttachmentsResource.class, getWiki()).toString());
267  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
268   
269  1 Attachments attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
270   
271  1 Assert.assertTrue(attachments.getAttachments().size() > 0);
272   
273  1 for (Attachment attachment : attachments.getAttachments()) {
274  17 checkLinks(attachment);
275    }
276   
277    // Verify we can search for a specific attachment name in the whole wiki
278  1 getMethod = executeGet(
279    String.format("%s?name=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
280  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
281   
282  1 attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
283   
284  1 Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
285   
286  1 for (Attachment attachment : attachments.getAttachments()) {
287  1 checkLinks(attachment);
288    }
289   
290    // Verify we can search for all attachments in a given space (sandbox)
291    // Also verify that a space can be looked up independtly of its case ("sandbox" will match the "Sandbox" space)
292  1 getMethod = executeGet(
293    String.format("%s?space=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
294  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
295   
296  1 attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
297   
298  1 Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
299   
300  1 for (Attachment attachment : attachments.getAttachments()) {
301  1 checkLinks(attachment);
302    }
303   
304    // Verify we can search for an attachment in a given space (sandbox)
305  1 getMethod = executeGet(String.format("%s?name=" + getTestClassName() + "&space=" + getTestClassName(),
306    buildURI(WikiAttachmentsResource.class, getWiki())));
307  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
308   
309  1 attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
310   
311  1 Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
312   
313  1 for (Attachment attachment : attachments.getAttachments()) {
314  1 checkLinks(attachment);
315    }
316    }
317   
 
318  1 toggle @Test
319    public void testHQLQuerySearch() throws Exception
320    {
321  1 this.testUtils.rest().delete(this.reference);
322  1 this.testUtils.rest().savePage(this.reference);
323   
324  1 GetMethod getMethod = executeGet(URIUtil
325    .encodeQuery(String.format("%s?q=where doc.name='" + this.pageName + "' order by doc.space desc&type=hql",
326    buildURI(WikiSearchQueryResource.class, getWiki()))));
327  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
328   
329  1 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
330   
331  1 int resultSize = searchResults.getSearchResults().size();
332  1 assertEquals(1, resultSize);
333  1 Assert.assertEquals(this.fullName, searchResults.getSearchResults().get(0).getPageFullName());
334    }
335   
 
336  1 toggle @Test
337    public void testHQLQuerySearchWithClassnameAuthenticated() throws Exception
338    {
339  1 GetMethod getMethod = executeGet(
340    URIUtil.encodeQuery(String.format(
341    "%s?q=where doc.space='XWiki' and doc.name='XWikiPreferences'&type=hql&className=XWiki.XWikiGlobalRights",
342    buildURI(WikiSearchQueryResource.class, getWiki()))),
343    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
344  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
345   
346  1 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
347   
348  1 int resultSize = searchResults.getSearchResults().size();
349  1 assertEquals(1, resultSize);
350  1 assertNotNull(searchResults.getSearchResults().get(0).getObject());
351    }
352   
 
353  1 toggle @Test
354    public void testHQLQuerySearchWithClassnameNotAuthenticated() throws Exception
355    {
356  1 GetMethod getMethod = executeGet(URIUtil.encodeQuery(String.format(
357    "%s?q=where doc.space='XWiki' and doc.name='XWikiPreferences'&type=hql&className=XWiki.XWikiGlobalRights",
358    buildURI(WikiSearchQueryResource.class, getWiki()))));
359  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
360   
361  1 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
362   
363  1 int resultSize = searchResults.getSearchResults().size();
364  1 assertEquals(1, resultSize);
365  1 assertNull(searchResults.getSearchResults().get(0).getObject());
366    }
367   
 
368  1 toggle @Test
369    public void testSolrSearch() throws Exception
370    {
371  1 this.testUtils.rest().delete(this.reference);
372  1 this.testUtils.rest().savePage(this.reference);
373   
374  1 this.solrUtils.waitEmpyQueue();
375   
376  1 GetMethod getMethod = executeGet(URIUtil.encodeQuery(String.format("%s?q=\"" + this.pageName + "\"&type=solr",
377    buildURI(WikiSearchQueryResource.class, getWiki()))));
378  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
379   
380  1 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
381   
382  1 int resultSize = searchResults.getSearchResults().size();
383  1 assertEquals(1, resultSize);
384  1 assertEquals(this.fullName, searchResults.getSearchResults().get(0).getPageFullName());
385    }
386   
 
387  1 toggle @Test
388    public void testGlobalSearch() throws Exception
389    {
390  1 this.testUtils.rest().delete(this.reference);
391  1 this.testUtils.rest().savePage(this.reference);
392   
393  1 this.solrUtils.waitEmpyQueue();
394   
395  1 GetMethod getMethod = executeGet(URIUtil.encodeQuery(
396    String.format("%s?q=\"" + this.pageName + "\"", buildURI(WikisSearchQueryResource.class, getWiki()))));
397  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
398   
399  1 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
400   
401  1 int resultSize = searchResults.getSearchResults().size();
402  1 assertEquals(1, resultSize);
403  1 assertEquals(this.fullName, searchResults.getSearchResults().get(0).getPageFullName());
404    }
405   
 
406  1 toggle @Test
407    public void testImportXAR() throws Exception
408    {
409  1 InputStream is = this.getClass().getResourceAsStream("/Main.Foo.xar");
410  1 String wiki = getWiki();
411   
412  1 PostMethod postMethod = executePost(buildURI(WikiResource.class, wiki).toString(), is,
413    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
414  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_OK, postMethod.getStatusCode());
415   
416  1 GetMethod getMethod = executeGet(buildURI(PageResource.class, wiki, Arrays.asList("Main"), "Foo").toString(),
417    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
418  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
419   
420  1 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
421   
422  1 Assert.assertEquals(wiki, page.getWiki());
423  1 Assert.assertEquals("Main", page.getSpace());
424  1 Assert.assertEquals("Foo", page.getName());
425  1 Assert.assertEquals("Foo", page.getContent());
426    }
427    }