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

File TagsResourceTest.java

 

Code metrics

10
73
3
1
203
151
8
0.11
24.33
3
2.67

Classes

Class Line # Actions
TagsResourceTest 51 73 0% 8 5
0.9418604494.2%
 

Contributing tests

This file is covered by 3 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.UUID;
23   
24    import javax.ws.rs.core.MediaType;
25   
26    import org.apache.commons.httpclient.HttpStatus;
27    import org.apache.commons.httpclient.NameValuePair;
28    import org.apache.commons.httpclient.methods.GetMethod;
29    import org.apache.commons.httpclient.methods.PostMethod;
30    import org.apache.commons.httpclient.methods.PutMethod;
31    import org.junit.Assert;
32    import org.junit.Test;
33    import org.xwiki.rest.Relations;
34    import org.xwiki.rest.model.jaxb.Link;
35    import org.xwiki.rest.model.jaxb.Page;
36    import org.xwiki.rest.model.jaxb.PageSummary;
37    import org.xwiki.rest.model.jaxb.Pages;
38    import org.xwiki.rest.model.jaxb.Tag;
39    import org.xwiki.rest.model.jaxb.Tags;
40    import org.xwiki.rest.resources.pages.PageResource;
41    import org.xwiki.rest.resources.pages.PageTagsResource;
42    import org.xwiki.rest.resources.tags.PagesForTagsResource;
43    import org.xwiki.rest.resources.tags.TagsResource;
44    import org.xwiki.test.rest.framework.AbstractHttpTest;
45    import org.xwiki.test.rest.framework.TestConstants;
46    import org.xwiki.test.ui.TestUtils;
47   
48    /**
49    * @version $Id: a5e4d3d56bcdfb3c69edd8ffd7fd1684e3d6c0d1 $
50    */
 
51    public class TagsResourceTest extends AbstractHttpTest
52    {
 
53  1 toggle @Override
54    @Test
55    public void testRepresentation() throws Exception
56    {
57  1 String tagName = UUID.randomUUID().toString();
58   
59  1 createPageIfDoesntExist(TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME, "Test");
60   
61  1 GetMethod getMethod = executeGet(
62    buildURI(PageResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME)
63    .toString());
64  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
65   
66  1 Tags tags = objectFactory.createTags();
67  1 Tag tag = objectFactory.createTag();
68  1 tag.setName(tagName);
69  1 tags.getTags().add(tag);
70   
71  1 PutMethod putMethod = executePutXml(
72    buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME)
73    .toString(),
74    tags, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
75  1 Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
76   
77  1 getMethod = executeGet(
78    buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME)
79    .toString());
80  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
81   
82  1 tags = (Tags) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
83  1 boolean found = false;
84  1 for (Tag t : tags.getTags()) {
85  1 if (tagName.equals(t.getName())) {
86  1 found = true;
87  1 break;
88    }
89    }
90  1 Assert.assertTrue(found);
91   
92  1 getMethod = executeGet(buildURI(TagsResource.class, getWiki()).toString());
93  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
94   
95  1 tags = (Tags) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
96  1 found = false;
97  1 for (Tag t : tags.getTags()) {
98  1 if (tagName.equals(t.getName())) {
99  1 found = true;
100  1 break;
101    }
102    }
103  1 Assert.assertTrue(found);
104   
105  1 getMethod = executeGet(buildURI(PagesForTagsResource.class, getWiki(), tagName).toString());
106  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
107   
108  1 Pages pages = (Pages) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
109   
110  1 found = false;
111  1 for (PageSummary pageSummary : pages.getPageSummaries()) {
112  1 if (pageSummary.getFullName()
113    .equals(String.format("%s.%s", TestConstants.TEST_SPACE_NAME.get(0), TestConstants.TEST_PAGE_NAME))) {
114  1 found = true;
115    }
116    }
117  1 Assert.assertTrue(found);
118   
119  1 getMethod = executeGet(
120    buildURI(PageResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME)
121    .toString());
122  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
123   
124  1 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
125  1 Link tagsLink = getFirstLinkByRelation(page, Relations.TAGS);
126  1 Assert.assertNotNull(tagsLink);
127    }
128   
 
129  1 toggle @Test
130    public void testPUTTagsWithTextPlain() throws Exception
131    {
132  1 createPageIfDoesntExist(TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME, "Test");
133   
134  1 String tagName = UUID.randomUUID().toString();
135   
136  1 GetMethod getMethod = executeGet(
137    buildURI(PageResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME)
138    .toString());
139  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
140   
141  1 PutMethod putMethod = executePut(
142    buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME)
143    .toString(),
144    tagName, MediaType.TEXT_PLAIN, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(),
145    TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
146  1 Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
147   
148  1 getMethod = executeGet(
149    buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME)
150    .toString());
151  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
152   
153  1 Tags tags = (Tags) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
154  1 boolean found = false;
155  1 for (Tag t : tags.getTags()) {
156  1 if (tagName.equals(t.getName())) {
157  1 found = true;
158  1 break;
159    }
160    }
161  1 Assert.assertTrue(found);
162    }
163   
 
164  1 toggle @Test
165    public void testPUTTagsFormUrlEncoded() throws Exception
166    {
167  1 createPageIfDoesntExist(TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME, "Test");
168   
169  1 String tagName = UUID.randomUUID().toString();
170   
171  1 GetMethod getMethod = executeGet(
172    buildURI(PageResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME)
173    .toString());
174  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
175   
176  1 NameValuePair[] nameValuePairs = new NameValuePair[1];
177  1 nameValuePairs[0] = new NameValuePair("tags", tagName);
178   
179  1 PostMethod postMethod =
180    executePostForm(
181    String.format("%s?method=PUT",
182    buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME,
183    TestConstants.TEST_PAGE_NAME).toString()),
184    nameValuePairs, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(),
185    TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
186  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_ACCEPTED, postMethod.getStatusCode());
187   
188  1 getMethod = executeGet(
189    buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME)
190    .toString());
191  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
192   
193  1 Tags tags = (Tags) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
194  1 boolean found = false;
195  1 for (Tag t : tags.getTags()) {
196  1 if (tagName.equals(t.getName())) {
197  1 found = true;
198  1 break;
199    }
200    }
201  1 Assert.assertTrue(found);
202    }
203    }