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

File CommentsResourceTest.java

 

Code metrics

2
61
7
1
195
126
8
0.13
8.71
7
1.14

Classes

Class Line # Actions
CommentsResourceTest 46 61 0% 8 4
0.9428571594.3%
 

Contributing tests

This file is covered by 6 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.util.Arrays;
23    import java.util.List;
24   
25    import javax.ws.rs.core.MediaType;
26   
27    import org.apache.commons.httpclient.HttpStatus;
28    import org.apache.commons.httpclient.NameValuePair;
29    import org.apache.commons.httpclient.methods.GetMethod;
30    import org.apache.commons.httpclient.methods.PostMethod;
31    import org.junit.Assert;
32    import org.junit.Before;
33    import org.junit.Test;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.rest.Relations;
36    import org.xwiki.rest.model.jaxb.Comment;
37    import org.xwiki.rest.model.jaxb.Comments;
38    import org.xwiki.rest.model.jaxb.History;
39    import org.xwiki.rest.model.jaxb.HistorySummary;
40    import org.xwiki.rest.model.jaxb.Page;
41    import org.xwiki.rest.resources.comments.CommentsResource;
42    import org.xwiki.rest.resources.pages.PageHistoryResource;
43    import org.xwiki.test.rest.framework.AbstractHttpTest;
44    import org.xwiki.test.ui.TestUtils;
45   
 
46    public class CommentsResourceTest extends AbstractHttpTest
47    {
48    private String wikiName;
49   
50    private List<String> spaces;
51   
52    private String pageName;
53   
54    private DocumentReference reference;
55   
 
56  6 toggle @Before
57    @Override
58    public void setUp() throws Exception
59    {
60  6 super.setUp();
61   
62  6 this.wikiName = getWiki();
63  6 this.spaces = Arrays.asList(getTestClassName());
64  6 this.pageName = getTestMethodName();
65   
66  6 this.reference = new DocumentReference(this.wikiName, this.spaces, this.pageName);
67   
68    // Create a clean test page.
69  6 this.testUtils.rest().delete(this.reference);
70  6 this.testUtils.rest().savePage(this.reference);
71    }
72   
 
73  1 toggle @Override
74    @Test
75    public void testRepresentation() throws Exception
76    {
77    /* Everything is done in test methods */
78    }
79   
 
80  1 toggle @Test
81    public void testPOSTComment() throws Exception
82    {
83  1 String commentsUri = buildURI(CommentsResource.class, getWiki(), this.spaces, this.pageName).toString();
84   
85  1 GetMethod getMethod = executeGet(commentsUri);
86  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
87   
88  1 Comments comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
89   
90  1 int numberOfComments = comments.getComments().size();
91   
92  1 Comment comment = objectFactory.createComment();
93  1 comment.setText("Comment");
94   
95  1 PostMethod postMethod = executePostXml(commentsUri, comment, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(),
96    TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
97  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
98   
99  1 getMethod = executeGet(commentsUri);
100  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
101   
102  1 comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
103   
104  1 Assert.assertEquals(numberOfComments + 1, comments.getComments().size());
105    }
106   
 
107  1 toggle @Test
108    public void testPOSTCommentWithTextPlain() throws Exception
109    {
110  1 String commentsUri = buildURI(CommentsResource.class, getWiki(), this.spaces, this.pageName).toString();
111   
112  1 GetMethod getMethod = executeGet(commentsUri);
113  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
114   
115  1 Comments comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
116   
117  1 int numberOfComments = comments.getComments().size();
118   
119  1 PostMethod postMethod = executePost(commentsUri, "Comment", MediaType.TEXT_PLAIN,
120    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
121  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
122   
123  1 getMethod = executeGet(commentsUri);
124  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
125   
126  1 comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
127   
128  1 Assert.assertEquals(numberOfComments + 1, comments.getComments().size());
129    }
130   
 
131  1 toggle @Test
132    public void testGETComment() throws Exception
133    {
134  1 String commentsUri = buildURI(CommentsResource.class, getWiki(), this.spaces, this.pageName).toString();
135   
136  1 GetMethod getMethod = executeGet(commentsUri);
137  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
138   
139  1 Comments comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
140   
141  1 for (Comment comment : comments.getComments()) {
142  0 checkLinks(comment);
143    }
144    }
145   
 
146  1 toggle @Test
147    public void testGETCommentsAtPreviousVersions() throws Exception
148    {
149  1 String pageHistoryUri = buildURI(PageHistoryResource.class, getWiki(), this.spaces, this.pageName).toString();
150   
151  1 GetMethod getMethod = executeGet(pageHistoryUri);
152  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
153   
154  1 History history = (History) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
155   
156  1 for (HistorySummary historySummary : history.getHistorySummaries()) {
157  1 getMethod = executeGet(getFirstLinkByRelation(historySummary, Relations.PAGE).getHref());
158  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
159   
160  1 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
161   
162  1 if (getFirstLinkByRelation(page, Relations.COMMENTS) != null) {
163  0 getMethod = executeGet(getFirstLinkByRelation(page, Relations.COMMENTS).getHref());
164  0 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
165    }
166    }
167    }
168   
 
169  1 toggle @Test
170    public void testPOSTCommentFormUrlEncoded() throws Exception
171    {
172  1 String commentsUri = buildURI(CommentsResource.class, getWiki(), this.spaces, this.pageName).toString();
173   
174  1 GetMethod getMethod = executeGet(commentsUri);
175  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
176   
177  1 Comments comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
178   
179  1 int numberOfComments = comments.getComments().size();
180   
181  1 NameValuePair[] nameValuePairs = new NameValuePair[1];
182  1 nameValuePairs[0] = new NameValuePair("text", "Comment");
183   
184  1 PostMethod postMethod = executePostForm(commentsUri, nameValuePairs,
185    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
186  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
187   
188  1 getMethod = executeGet(commentsUri);
189  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
190   
191  1 comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
192   
193  1 Assert.assertEquals(numberOfComments + 1, comments.getComments().size());
194    }
195    }