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

File WikiManagerRestTest.java

 

Code metrics

4
116
9
1
292
206
11
0.09
12.89
9
1.22

Classes

Class Line # Actions
WikiManagerRestTest 63 116 0% 11 78
0.3953488539.5%
 

Contributing tests

This file is covered by 1 test. .

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.wiki.test.ui;
21   
22    import java.io.StringWriter;
23    import java.util.Random;
24   
25    import javax.ws.rs.core.MediaType;
26    import javax.ws.rs.core.UriBuilder;
27    import javax.xml.bind.JAXBContext;
28    import javax.xml.bind.Marshaller;
29    import javax.xml.bind.Unmarshaller;
30   
31    import org.apache.commons.httpclient.HttpClient;
32    import org.apache.commons.httpclient.HttpStatus;
33    import org.apache.commons.httpclient.UsernamePasswordCredentials;
34    import org.apache.commons.httpclient.auth.AuthScope;
35    import org.apache.commons.httpclient.methods.GetMethod;
36    import org.apache.commons.httpclient.methods.PostMethod;
37    import org.apache.commons.httpclient.methods.PutMethod;
38    import org.apache.commons.httpclient.methods.RequestEntity;
39    import org.apache.commons.httpclient.methods.StringRequestEntity;
40    import org.apache.commons.httpclient.util.URIUtil;
41    import org.junit.Assert;
42    import org.junit.Before;
43    import org.junit.Ignore;
44    import org.junit.Test;
45    import org.xwiki.rest.model.jaxb.ObjectFactory;
46    import org.xwiki.rest.model.jaxb.Page;
47    import org.xwiki.rest.model.jaxb.SearchResult;
48    import org.xwiki.rest.model.jaxb.SearchResults;
49    import org.xwiki.rest.model.jaxb.Wiki;
50    import org.xwiki.rest.model.jaxb.Wikis;
51    import org.xwiki.rest.resources.pages.PageResource;
52    import org.xwiki.rest.resources.wikis.WikisResource;
53    import org.xwiki.rest.resources.wikis.WikisSearchQueryResource;
54    import org.xwiki.test.integration.XWikiExecutor;
55    import org.xwiki.wiki.rest.WikiManagerREST;
56   
57    /**
58    * Tests for the Wiki manager REST API.
59    *
60    * @version $Id: f199348504f92b1e2b29e09e8317a58f10591fb7 $
61    * @since 5.4RC1
62    */
 
63    public class WikiManagerRestTest
64    {
65    protected Random random;
66   
67    protected Marshaller marshaller;
68   
69    protected Unmarshaller unmarshaller;
70   
71    protected ObjectFactory objectFactory;
72   
73    protected int port = Integer.valueOf(XWikiExecutor.DEFAULT_PORT);
74   
75    private static final String RELATIVE_REST_API_ENTRYPOINT = "/xwiki/rest";
76   
 
77  1 toggle @Before
78    public void setUp() throws Exception
79    {
80  1 random = new Random();
81   
82  1 JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
83  1 marshaller = context.createMarshaller();
84  1 unmarshaller = context.createUnmarshaller();
85  1 objectFactory = new ObjectFactory();
86   
87    // Access once the wiki to make sure the DW is run on main wiki before messing with it
88  1 HttpClient httpClient = new HttpClient();
89  1 GetMethod getMethod = new GetMethod("http://localhost:" + port + "/xwiki/");
90  1 getMethod.setFollowRedirects(true);
91  1 httpClient.executeMethod(getMethod);
92    }
93   
 
94  1 toggle @Test
95    public void testCreateWiki() throws Exception
96    {
97  1 String WIKI_ID = "foo";
98   
99  1 Wiki wiki = objectFactory.createWiki();
100  1 wiki.setId(WIKI_ID);
101   
102  1 PostMethod postMethod = executePost(getFullUri(WikiManagerREST.class), "superadmin", "pass", wiki);
103  1 Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode());
104   
105  1 wiki = (Wiki) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());
106  1 Assert.assertEquals(WIKI_ID, wiki.getId());
107   
108  1 GetMethod getMethod = executeGet(getFullUri(WikisResource.class));
109  1 Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
110   
111  1 Wikis wikis = (Wikis) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
112   
113  1 boolean found = false;
114  1 for (Wiki w : wikis.getWikis()) {
115  2 if (WIKI_ID.equals(w.getId())) {
116  1 found = true;
117  1 break;
118    }
119    }
120   
121  1 Assert.assertTrue(found);
122    }
123   
124    //FIXME: Test is disabled for the moment. It works if tested against MySQL but with HSQLDB it seems that the
125    // Lucene plugin is not triggered. Anyway this should be better to rewrite it, if possible, as a unit test.
 
126  0 toggle @Ignore("This test doesn't seem to work correctly with HSQLDB but it actually works if run against MySQL.")
127    @Test
128    public void testMultiwikiSearch() throws Exception
129    {
130  0 String WIKI1_ID = "w1";
131  0 String WIKI2_ID = "w2";
132  0 String PAGE_SPACE = "Main";
133  0 String PAGE_NAME = "Test";
134  0 String PAGE1_STRING = "foo";
135  0 String PAGE2_STRING = "bar";
136   
137  0 Wiki wiki = objectFactory.createWiki();
138  0 wiki.setId(WIKI1_ID);
139   
140  0 PostMethod postMethod = executePost(getFullUri(WikiManagerREST.class), "superadmin", "pass", wiki);
141  0 Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode());
142   
143  0 wiki = objectFactory.createWiki();
144  0 wiki.setId(WIKI2_ID);
145   
146  0 postMethod = executePost(getFullUri(WikiManagerREST.class), "superadmin", "pass", wiki);
147  0 Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode());
148   
149    /* Store the page */
150  0 Page page1 = objectFactory.createPage();
151  0 page1.setTitle(PAGE1_STRING);
152  0 page1.setContent(PAGE1_STRING);
153  0 PutMethod putMethod =
154    executePut(getUriBuilder(PageResource.class).build(WIKI1_ID, PAGE_SPACE, PAGE_NAME).toString(),
155    "superadmin",
156    "pass", page1);
157  0 Assert.assertEquals(HttpStatus.SC_CREATED, putMethod.getStatusCode());
158  0 page1 = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
159   
160    /* Retrieve the page to check that it exists */
161  0 GetMethod getMethod =
162    executeGet(getUriBuilder(PageResource.class).build(WIKI1_ID, PAGE_SPACE, PAGE_NAME).toString());
163  0 Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
164  0 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
165  0 Assert.assertEquals(WIKI1_ID, page.getWiki());
166  0 Assert.assertEquals(PAGE_SPACE, page.getSpace());
167  0 Assert.assertEquals(PAGE_NAME, page.getName());
168  0 Assert.assertEquals(PAGE1_STRING, page.getTitle());
169  0 Assert.assertEquals(PAGE1_STRING, page.getContent());
170  0 Assert.assertEquals(page1.getCreated(), page.getCreated());
171  0 Assert.assertEquals(page1.getModified(), page.getModified());
172   
173    /* Store the page */
174  0 Page page2 = objectFactory.createPage();
175  0 page2.setTitle(PAGE2_STRING);
176  0 page2.setContent(PAGE2_STRING);
177  0 putMethod =
178    executePut(getUriBuilder(PageResource.class).build(WIKI2_ID, PAGE_SPACE, PAGE_NAME).toString(),
179    "superadmin",
180    "pass", page2);
181  0 Assert.assertEquals(HttpStatus.SC_CREATED, putMethod.getStatusCode());
182  0 page2 = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
183   
184    /* Retrieve the page to check that it exists */
185  0 getMethod = executeGet(getUriBuilder(PageResource.class).build(WIKI2_ID, PAGE_SPACE, PAGE_NAME).toString());
186  0 Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
187  0 page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
188  0 Assert.assertEquals(WIKI2_ID, page.getWiki());
189  0 Assert.assertEquals(PAGE_SPACE, page.getSpace());
190  0 Assert.assertEquals(PAGE_NAME, page.getName());
191  0 Assert.assertEquals(PAGE2_STRING, page.getTitle());
192  0 Assert.assertEquals(PAGE2_STRING, page.getContent());
193  0 Assert.assertEquals(page2.getCreated(), page.getCreated());
194  0 Assert.assertEquals(page2.getModified(), page.getModified());
195   
196    /* Wait a bit that the Lucene Indexer indexes the pages. */
197  0 Thread.sleep(5000);
198   
199  0 getMethod = executeGet(URIUtil.encodeQuery(
200    String.format("%s?q=\"%s\"&wikis=w1,w2", getFullUri(WikisSearchQueryResource.class), PAGE_NAME)));
201  0 Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
202   
203  0 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
204   
205  0 Assert.assertEquals(2, searchResults.getSearchResults().size());
206   
207  0 for (SearchResult searchResult : searchResults.getSearchResults()) {
208  0 Page pageToBeCheckedAgainst = null;
209  0 if (searchResult.getWiki().equals(WIKI1_ID)) {
210  0 pageToBeCheckedAgainst = page1;
211    } else {
212  0 pageToBeCheckedAgainst = page2;
213    }
214   
215  0 Assert.assertEquals(pageToBeCheckedAgainst.getWiki(), searchResult.getWiki());
216  0 Assert.assertEquals(pageToBeCheckedAgainst.getTitle(), searchResult.getTitle());
217  0 Assert.assertEquals(pageToBeCheckedAgainst.getAuthor(), searchResult.getAuthor());
218  0 Assert.assertEquals(pageToBeCheckedAgainst.getModified(), searchResult.getModified());
219  0 Assert.assertEquals(pageToBeCheckedAgainst.getVersion(), searchResult.getVersion());
220    }
221    }
222   
 
223  2 toggle protected String getBaseURL()
224    {
225  2 return String.format("http://localhost:%s%s", port, RELATIVE_REST_API_ENTRYPOINT);
226    }
227   
 
228  0 toggle protected UriBuilder getUriBuilder(Class<?> resource)
229    {
230  0 return UriBuilder.fromUri(getBaseURL()).path(resource);
231    }
232   
 
233  2 toggle protected String getFullUri(Class<?> resourceClass)
234    {
235  2 return String.format("%s%s", getBaseURL(), UriBuilder.fromResource(resourceClass).build());
236    }
237   
 
238  1 toggle protected GetMethod executeGet(String uri) throws Exception
239    {
240  1 HttpClient httpClient = new HttpClient();
241   
242  1 GetMethod getMethod = new GetMethod(uri);
243  1 getMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
244  1 httpClient.executeMethod(getMethod);
245   
246  1 return getMethod;
247    }
248   
 
249  1 toggle protected PostMethod executePost(String uri, String userName, String password, Object object)
250    throws Exception
251    {
252  1 HttpClient httpClient = new HttpClient();
253  1 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
254  1 httpClient.getParams().setAuthenticationPreemptive(true);
255   
256  1 PostMethod postMethod = new PostMethod(uri);
257  1 postMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
258   
259  1 StringWriter writer = new StringWriter();
260  1 marshaller.marshal(object, writer);
261  1 RequestEntity entity =
262    new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
263   
264  1 postMethod.setRequestEntity(entity);
265   
266  1 httpClient.executeMethod(postMethod);
267   
268  1 return postMethod;
269    }
270   
 
271  0 toggle protected PutMethod executePut(String uri, String userName, String password, Object object)
272    throws Exception
273    {
274  0 HttpClient httpClient = new HttpClient();
275  0 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
276  0 httpClient.getParams().setAuthenticationPreemptive(true);
277   
278  0 PutMethod putMethod = new PutMethod(uri);
279  0 putMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
280   
281  0 StringWriter writer = new StringWriter();
282  0 marshaller.marshal(object, writer);
283  0 RequestEntity entity =
284    new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
285   
286  0 putMethod.setRequestEntity(entity);
287   
288  0 httpClient.executeMethod(putMethod);
289   
290  0 return putMethod;
291    }
292    }