1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
50 |
|
|
|
|
| 94.2% |
Uncovered Elements: 5 (86) |
Complexity: 8 |
Complexity Density: 0.11 |
|
51 |
|
public class TagsResourceTest extends AbstractHttpTest |
52 |
|
{ |
|
|
| 93.6% |
Uncovered Elements: 3 (47) |
Complexity: 4 |
Complexity Density: 0.1 |
1PASS
|
|
53 |
1 |
@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 |
|
|
|
|
| 94.1% |
Uncovered Elements: 1 (17) |
Complexity: 2 |
Complexity Density: 0.13 |
1PASS
|
|
129 |
1 |
@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 |
|
|
|
|
| 94.7% |
Uncovered Elements: 1 (19) |
Complexity: 2 |
Complexity Density: 0.12 |
1PASS
|
|
164 |
1 |
@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 |
|
} |