1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.test.rest.framework

File AbstractHttpTest.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

14
194
34
1
536
390
42
0.22
5.71
34
1.24

Classes

Class Line # Actions
AbstractHttpTest 79 194 0% 42 39
0.83884383.9%
 

Contributing tests

This file is covered by 67 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.framework;
21   
22    import java.io.InputStream;
23    import java.io.StringWriter;
24    import java.net.URI;
25    import java.util.ArrayList;
26    import java.util.List;
27    import java.util.Random;
28   
29    import javax.xml.bind.JAXBContext;
30    import javax.xml.bind.Marshaller;
31    import javax.xml.bind.Unmarshaller;
32   
33    import org.apache.commons.httpclient.HttpClient;
34    import org.apache.commons.httpclient.HttpMethod;
35    import org.apache.commons.httpclient.HttpStatus;
36    import org.apache.commons.httpclient.NameValuePair;
37    import org.apache.commons.httpclient.UsernamePasswordCredentials;
38    import org.apache.commons.httpclient.auth.AuthScope;
39    import org.apache.commons.httpclient.methods.DeleteMethod;
40    import org.apache.commons.httpclient.methods.GetMethod;
41    import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
42    import org.apache.commons.httpclient.methods.PostMethod;
43    import org.apache.commons.httpclient.methods.PutMethod;
44    import org.apache.commons.httpclient.methods.RequestEntity;
45    import org.apache.commons.httpclient.methods.StringRequestEntity;
46    import org.junit.Assert;
47    import org.junit.Before;
48    import org.junit.Rule;
49    import org.junit.rules.TestName;
50    import org.restlet.data.MediaType;
51    import org.xwiki.component.annotation.ComponentAnnotationLoader;
52    import org.xwiki.component.annotation.ComponentDeclaration;
53    import org.xwiki.component.embed.EmbeddableComponentManager;
54    import org.xwiki.component.manager.ComponentManager;
55    import org.xwiki.model.internal.DefaultModelConfiguration;
56    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
57    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceResolver;
58    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer;
59    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
60    import org.xwiki.model.internal.reference.RelativeStringEntityReferenceResolver;
61    import org.xwiki.model.reference.DocumentReference;
62    import org.xwiki.repository.test.SolrTestUtils;
63    import org.xwiki.rest.internal.Utils;
64    import org.xwiki.rest.model.jaxb.Attachment;
65    import org.xwiki.rest.model.jaxb.Attachments;
66    import org.xwiki.rest.model.jaxb.Link;
67    import org.xwiki.rest.model.jaxb.LinkCollection;
68    import org.xwiki.rest.model.jaxb.ObjectFactory;
69    import org.xwiki.rest.model.jaxb.Objects;
70    import org.xwiki.rest.model.jaxb.Page;
71    import org.xwiki.rest.model.jaxb.PageSummary;
72    import org.xwiki.rest.model.jaxb.Pages;
73    import org.xwiki.rest.model.jaxb.Wikis;
74    import org.xwiki.rest.resources.pages.PageResource;
75    import org.xwiki.rest.resources.wikis.WikisResource;
76    import org.xwiki.test.ui.AbstractTest;
77    import org.xwiki.test.ui.TestUtils;
78   
 
79    public abstract class AbstractHttpTest
80    {
81    /**
82    * The object used to access the name of the current test.
83    */
84    @Rule
85    public final TestName testName = new TestName();
86   
87    protected Random random;
88   
89    protected Marshaller marshaller;
90   
91    protected Unmarshaller unmarshaller;
92   
93    protected ObjectFactory objectFactory;
94   
95    // TODO: Refactor TestUtils to move REST tools to xwiki-platform-test-integration
96    protected TestUtils testUtils = new TestUtils();
97   
98    protected SolrTestUtils solrUtils;
99   
 
100  1 toggle static {
101  1 try {
102  1 initializeSystem();
103    } catch (Exception e) {
104   
105    }
106    }
107   
 
108  70 toggle @Before
109    public void setUp() throws Exception
110    {
111  70 random = new Random();
112   
113  70 JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
114  70 marshaller = context.createMarshaller();
115  70 unmarshaller = context.createUnmarshaller();
116  70 objectFactory = new ObjectFactory();
117   
118    // Make sure guest does not have edit right
119  70 Page page = this.testUtils.rest().page(new DocumentReference("xwiki", "XWiki", "XWikiPreferences"));
120  70 org.xwiki.rest.model.jaxb.Object rightObject = this.testUtils.rest().object("XWiki.XWikiGlobalRights");
121  70 rightObject.withProperties(this.testUtils.rest().property("users", "XWiki.XWikiGuest"),
122    this.testUtils.rest().property("levels", "edit"), this.testUtils.rest().property("allow", "0"));
123  70 Objects objects = new Objects();
124  70 objects.withObjectSummaries(rightObject);
125  70 page.setObjects(objects);
126  70 this.testUtils.rest().save(page);
127   
128    // Init solr utils
129  70 this.solrUtils = new SolrTestUtils(this.testUtils);
130    }
131   
 
132  1 toggle public static void initializeSystem() throws Exception
133    {
134  1 ComponentManager componentManager = new EmbeddableComponentManager();
135   
136    // Only load the minimal number of components required for the test framework, for both performance reasons
137    // and for avoiding having to declare dependencies such as HttpServletRequest.
138  1 ComponentAnnotationLoader loader = new ComponentAnnotationLoader();
139  1 List<ComponentDeclaration> componentDeclarations = new ArrayList<>();
140  1 componentDeclarations.add(new ComponentDeclaration(DefaultStringEntityReferenceResolver.class.getName()));
141  1 componentDeclarations.add(new ComponentDeclaration(DefaultStringEntityReferenceSerializer.class.getName()));
142  1 componentDeclarations.add(new ComponentDeclaration(RelativeStringEntityReferenceResolver.class.getName()));
143  1 componentDeclarations.add(new ComponentDeclaration(DefaultEntityReferenceProvider.class.getName()));
144  1 componentDeclarations.add(new ComponentDeclaration(DefaultModelConfiguration.class.getName()));
145  1 componentDeclarations.add(new ComponentDeclaration(DefaultSymbolScheme.class.getName()));
146  1 loader.initialize(componentManager, AbstractTest.class.getClassLoader(), componentDeclarations);
147   
148  1 TestUtils.initializeComponent(componentManager);
149    }
150   
 
151  92 toggle protected Link getFirstLinkByRelation(LinkCollection linkCollection, String relation)
152    {
153  92 if (linkCollection.getLinks() == null) {
154  0 return null;
155    }
156   
157  92 for (Link link : linkCollection.getLinks()) {
158  247 if (link.getRel().equals(relation)) {
159  76 return link;
160    }
161    }
162   
163  16 return null;
164    }
165   
 
166  0 toggle protected List<Link> getLinksByRelation(LinkCollection linkCollection, String relation)
167    {
168  0 List<Link> result = new ArrayList<Link>();
169   
170  0 if (linkCollection.getLinks() == null) {
171  0 return result;
172    }
173   
174  0 for (Link link : linkCollection.getLinks()) {
175  0 if (link.getRel().equals(relation)) {
176  0 result.add(link);
177    }
178    }
179   
180  0 return result;
181    }
182   
 
183  185 toggle protected String getBaseURL()
184    {
185  185 return this.testUtils.rest().getBaseURL();
186    }
187   
 
188  232 toggle protected String getFullUri(Class<?> resourceClass)
189    {
190  232 return this.testUtils.rest().createUri(resourceClass, null).toString();
191    }
192   
193    public abstract void testRepresentation() throws Exception;
194   
 
195  890 toggle protected GetMethod executeGet(String uri) throws Exception
196    {
197  890 HttpClient httpClient = new HttpClient();
198   
199  890 GetMethod getMethod = new GetMethod(uri);
200  890 getMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
201  890 httpClient.executeMethod(getMethod);
202   
203  890 return getMethod;
204    }
205   
 
206  3 toggle protected GetMethod executeGet(String uri, String userName, String password) throws Exception
207    {
208  3 HttpClient httpClient = new HttpClient();
209  3 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
210  3 httpClient.getParams().setAuthenticationPreemptive(true);
211   
212  3 GetMethod getMethod = new GetMethod(uri);
213  3 getMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
214  3 httpClient.executeMethod(getMethod);
215   
216  3 return getMethod;
217    }
218   
 
219  1 toggle protected PostMethod executePostXml(String uri, Object object) throws Exception
220    {
221  1 HttpClient httpClient = new HttpClient();
222   
223  1 PostMethod postMethod = new PostMethod(uri);
224  1 postMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
225   
226  1 StringWriter writer = new StringWriter();
227  1 marshaller.marshal(object, writer);
228   
229  1 RequestEntity entity =
230    new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
231  1 postMethod.setRequestEntity(entity);
232   
233  1 httpClient.executeMethod(postMethod);
234   
235  1 return postMethod;
236    }
237   
 
238  18 toggle protected PostMethod executePostXml(String uri, Object object, String userName, String password) throws Exception
239    {
240  18 HttpClient httpClient = new HttpClient();
241  18 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
242  18 httpClient.getParams().setAuthenticationPreemptive(true);
243   
244  18 PostMethod postMethod = new PostMethod(uri);
245  18 postMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
246   
247  18 StringWriter writer = new StringWriter();
248  18 marshaller.marshal(object, writer);
249   
250  18 RequestEntity entity =
251    new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
252  18 postMethod.setRequestEntity(entity);
253   
254  18 httpClient.executeMethod(postMethod);
255   
256  18 return postMethod;
257    }
258   
 
259  1 toggle protected PostMethod executePost(String uri, InputStream is, String userName, String password) throws Exception
260    {
261  1 HttpClient httpClient = new HttpClient();
262  1 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
263  1 httpClient.getParams().setAuthenticationPreemptive(true);
264   
265  1 PostMethod postMethod = new PostMethod(uri);
266  1 postMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
267   
268  1 RequestEntity entity = new InputStreamRequestEntity(is);
269  1 postMethod.setRequestEntity(entity);
270   
271  1 httpClient.executeMethod(postMethod);
272   
273  1 return postMethod;
274    }
275   
 
276  1 toggle protected PostMethod executePost(String uri, String string, String mediaType, String userName, String password)
277    throws Exception
278    {
279  1 HttpClient httpClient = new HttpClient();
280  1 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
281  1 httpClient.getParams().setAuthenticationPreemptive(true);
282   
283  1 PostMethod postMethod = new PostMethod(uri);
284  1 postMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
285   
286  1 RequestEntity entity = new StringRequestEntity(string, mediaType, "UTF-8");
287  1 postMethod.setRequestEntity(entity);
288   
289  1 httpClient.executeMethod(postMethod);
290   
291  1 return postMethod;
292    }
293   
 
294  6 toggle protected PostMethod executePostForm(String uri, NameValuePair[] nameValuePairs, String userName, String password)
295    throws Exception
296    {
297  6 HttpClient httpClient = new HttpClient();
298  6 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
299  6 httpClient.getParams().setAuthenticationPreemptive(true);
300   
301  6 PostMethod postMethod = new PostMethod(uri);
302  6 postMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
303  6 postMethod.addRequestHeader("Content-type", MediaType.APPLICATION_WWW_FORM.toString());
304   
305  6 postMethod.setRequestBody(nameValuePairs);
306   
307  6 httpClient.executeMethod(postMethod);
308   
309  6 return postMethod;
310    }
311   
 
312  2 toggle protected PutMethod executePutXml(String uri, Object object) throws Exception
313    {
314  2 HttpClient httpClient = new HttpClient();
315   
316  2 PutMethod putMethod = new PutMethod(uri);
317  2 putMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
318   
319  2 StringWriter writer = new StringWriter();
320  2 marshaller.marshal(object, writer);
321   
322  2 RequestEntity entity =
323    new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
324  2 putMethod.setRequestEntity(entity);
325   
326  2 httpClient.executeMethod(putMethod);
327   
328  2 return putMethod;
329    }
330   
 
331  20 toggle protected PutMethod executePutXml(String uri, Object object, String userName, String password) throws Exception
332    {
333  20 HttpClient httpClient = new HttpClient();
334  20 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
335  20 httpClient.getParams().setAuthenticationPreemptive(true);
336   
337  20 PutMethod putMethod = new PutMethod(uri);
338  20 putMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
339   
340  20 StringWriter writer = new StringWriter();
341  20 marshaller.marshal(object, writer);
342   
343  20 RequestEntity entity =
344    new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
345  20 putMethod.setRequestEntity(entity);
346   
347  20 httpClient.executeMethod(putMethod);
348   
349  20 return putMethod;
350    }
351   
 
352  2 toggle protected PutMethod executePut(String uri, String string, String mediaType) throws Exception
353    {
354  2 HttpClient httpClient = new HttpClient();
355   
356  2 PutMethod putMethod = new PutMethod(uri);
357  2 RequestEntity entity = new StringRequestEntity(string, mediaType, "UTF-8");
358  2 putMethod.setRequestEntity(entity);
359   
360  2 httpClient.executeMethod(putMethod);
361   
362  2 return putMethod;
363    }
364   
 
365  21 toggle protected PutMethod executePut(String uri, String string, String mediaType, String userName, String password)
366    throws Exception
367    {
368  21 HttpClient httpClient = new HttpClient();
369  21 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
370  21 httpClient.getParams().setAuthenticationPreemptive(true);
371   
372  21 PutMethod putMethod = new PutMethod(uri);
373  21 RequestEntity entity = new StringRequestEntity(string, mediaType, "UTF-8");
374  21 putMethod.setRequestEntity(entity);
375   
376  21 httpClient.executeMethod(putMethod);
377   
378  21 return putMethod;
379    }
380   
 
381  3 toggle protected DeleteMethod executeDelete(String uri) throws Exception
382    {
383  3 HttpClient httpClient = new HttpClient();
384  3 DeleteMethod deleteMethod = new DeleteMethod(uri);
385  3 httpClient.executeMethod(deleteMethod);
386   
387  3 return deleteMethod;
388    }
389   
 
390  4 toggle protected DeleteMethod executeDelete(String uri, String userName, String password) throws Exception
391    {
392  4 HttpClient httpClient = new HttpClient();
393  4 httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
394  4 httpClient.getParams().setAuthenticationPreemptive(true);
395   
396  4 DeleteMethod deleteMethod = new DeleteMethod(uri);
397  4 httpClient.executeMethod(deleteMethod);
398   
399  4 return deleteMethod;
400    }
401   
 
402  221 toggle protected String getWiki() throws Exception
403    {
404  221 GetMethod getMethod = executeGet(getFullUri(WikisResource.class));
405  221 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
406   
407  221 Wikis wikis = (Wikis) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
408  221 Assert.assertTrue(wikis.getWikis().size() > 0);
409   
410  221 return wikis.getWikis().get(0).getName();
411    }
412   
 
413  0 toggle protected String getContentFromURI(String uri) throws Exception
414    {
415  0 GetMethod getMethod = executeGet(uri);
416  0 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
417   
418  0 return getMethod.getResponseBodyAsString();
419    }
420   
 
421  337 toggle protected void checkLinks(LinkCollection linkCollection) throws Exception
422    {
423  337 if (linkCollection.getLinks() != null) {
424  337 for (Link link : linkCollection.getLinks()) {
425  458 GetMethod getMethod = executeGet(link.getHref());
426  458 if (getMethod.getStatusCode() != HttpStatus.SC_UNAUTHORIZED) {
427  458 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
428    }
429    }
430    }
431    }
432   
 
433  185 toggle protected String buildURI(Class<?> resource, Object... pathParameters) throws Exception
434    {
435  185 return Utils.createURI(new URI(getBaseURL()), resource, pathParameters).toString();
436    }
437   
 
438  0 toggle private Page getPage(String wikiName, List<String> spaceName, String pageName) throws Exception
439    {
440  0 String uri = buildURI(PageResource.class, wikiName, spaceName, pageName).toString();
441   
442  0 GetMethod getMethod = executeGet(uri);
443   
444  0 return (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
445    }
446   
 
447  0 toggle protected String getPageContent(String wikiName, List<String> spaceName, String pageName) throws Exception
448    {
449  0 Page page = getPage(wikiName, spaceName, pageName);
450   
451  0 return page.getContent();
452    }
453   
 
454  0 toggle protected int setPageContent(String wikiName, List<String> spaceName, String pageName, String content)
455    throws Exception
456    {
457  0 String uri = buildURI(PageResource.class, wikiName, spaceName, pageName).toString();
458   
459  0 PutMethod putMethod = executePut(uri, content, javax.ws.rs.core.MediaType.TEXT_PLAIN,
460    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
461   
462  0 int code = putMethod.getStatusCode();
463  0 Assert.assertTrue(String.format("Failed to set page content, %s", getHttpMethodInfo(putMethod)),
464    code == HttpStatus.SC_ACCEPTED || code == HttpStatus.SC_CREATED);
465   
466  0 return code;
467    }
468   
 
469  954 toggle protected String getHttpMethodInfo(HttpMethod method) throws Exception
470    {
471  954 return String.format("\nName: %s\nURI: %s\nStatus code: %d\nStatus text: %s", method.getName(), method.getURI(),
472    method.getStatusCode(), method.getStatusText());
473    }
474   
 
475  4 toggle protected String getAttachmentsInfo(Attachments attachments)
476    {
477  4 StringBuffer sb = new StringBuffer();
478  4 sb.append(String.format("Attachments: %d\n", attachments.getAttachments().size()));
479  4 for (Attachment attachment : attachments.getAttachments()) {
480  4 sb.append(String.format("* %s\n", attachment.getName()));
481    }
482   
483  4 return sb.toString();
484    }
485   
 
486  0 toggle protected String getPagesInfo(Pages pages)
487    {
488  0 StringBuffer sb = new StringBuffer();
489  0 sb.append(String.format("Pages: %d\n", pages.getPageSummaries().size()));
490  0 for (PageSummary pageSummary : pages.getPageSummaries()) {
491  0 sb.append(String.format("* %s\n", pageSummary.getFullName()));
492    }
493   
494  0 return sb.toString();
495    }
496   
 
497  5 toggle protected void createPage(List<String> spaces, String pageName, String content) throws Exception
498    {
499  5 String uri = buildURI(PageResource.class, getWiki(), spaces, pageName);
500   
501  5 Page page = this.objectFactory.createPage();
502  5 page.setContent(content);
503   
504  5 PutMethod putMethod = executePutXml(uri, page, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(),
505    TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
506  5 Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());
507    }
508   
 
509  17 toggle protected boolean createPageIfDoesntExist(List<String> spaces, String pageName, String content) throws Exception
510    {
511  17 String uri = buildURI(PageResource.class, getWiki(), spaces, pageName);
512   
513  17 GetMethod getMethod = executeGet(uri);
514   
515  17 if (getMethod.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
516  5 createPage(spaces, pageName, content);
517   
518  5 getMethod = executeGet(uri);
519  5 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
520   
521  5 return true;
522    }
523   
524  12 return false;
525    }
526   
 
527  78 toggle protected String getTestMethodName()
528    {
529  78 return this.testName.getMethodName();
530    }
531   
 
532  83 toggle protected String getTestClassName()
533    {
534  83 return getClass().getSimpleName();
535    }
536    }