1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
59 |
|
|
60 |
|
@version |
61 |
|
@since |
62 |
|
|
|
|
| 39.5% |
Uncovered Elements: 78 (129) |
Complexity: 11 |
Complexity Density: 0.09 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
77 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 2 |
Complexity Density: 0.12 |
1PASS
|
|
94 |
1 |
@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 |
|
|
125 |
|
|
|
|
| 0% |
Uncovered Elements: 63 (63) |
Complexity: 2 |
Complexity Density: 0.03 |
1PASS
|
|
126 |
0 |
@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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
223 |
2 |
protected String getBaseURL()... |
224 |
|
{ |
225 |
2 |
return String.format("http://localhost:%s%s", port, RELATIVE_REST_API_ENTRYPOINT); |
226 |
|
} |
227 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
228 |
0 |
protected UriBuilder getUriBuilder(Class<?> resource)... |
229 |
|
{ |
230 |
0 |
return UriBuilder.fromUri(getBaseURL()).path(resource); |
231 |
|
} |
232 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
233 |
2 |
protected String getFullUri(Class<?> resourceClass)... |
234 |
|
{ |
235 |
2 |
return String.format("%s%s", getBaseURL(), UriBuilder.fromResource(resourceClass).build()); |
236 |
|
} |
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
238 |
1 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
249 |
1 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
271 |
0 |
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 |
|
} |