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.ArrayList; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.UUID; |
28 |
|
|
29 |
|
import javax.ws.rs.core.MediaType; |
30 |
|
|
31 |
|
import org.apache.commons.httpclient.Header; |
32 |
|
import org.apache.commons.httpclient.HttpClient; |
33 |
|
import org.apache.commons.httpclient.HttpStatus; |
34 |
|
import org.apache.commons.httpclient.UsernamePasswordCredentials; |
35 |
|
import org.apache.commons.httpclient.auth.AuthScope; |
36 |
|
import org.apache.commons.httpclient.methods.DeleteMethod; |
37 |
|
import org.apache.commons.httpclient.methods.GetMethod; |
38 |
|
import org.apache.commons.httpclient.methods.PostMethod; |
39 |
|
import org.apache.commons.httpclient.methods.PutMethod; |
40 |
|
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; |
41 |
|
import org.apache.commons.httpclient.methods.multipart.FilePart; |
42 |
|
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; |
43 |
|
import org.apache.commons.httpclient.methods.multipart.Part; |
44 |
|
import org.junit.Assert; |
45 |
|
import org.junit.Before; |
46 |
|
import org.junit.Test; |
47 |
|
import org.xwiki.model.reference.DocumentReference; |
48 |
|
import org.xwiki.rest.Relations; |
49 |
|
import org.xwiki.rest.model.jaxb.Attachment; |
50 |
|
import org.xwiki.rest.model.jaxb.Attachments; |
51 |
|
import org.xwiki.rest.resources.attachments.AttachmentHistoryResource; |
52 |
|
import org.xwiki.rest.resources.attachments.AttachmentResource; |
53 |
|
import org.xwiki.rest.resources.attachments.AttachmentsAtPageVersionResource; |
54 |
|
import org.xwiki.rest.resources.attachments.AttachmentsResource; |
55 |
|
import org.xwiki.test.rest.framework.AbstractHttpTest; |
56 |
|
import org.xwiki.test.ui.TestUtils; |
57 |
|
|
|
|
| 74.5% |
Uncovered Elements: 40 (157) |
Complexity: 19 |
Complexity Density: 0.15 |
|
58 |
|
public class AttachmentsResourceTest extends AbstractHttpTest |
59 |
|
{ |
60 |
|
private String wikiName; |
61 |
|
|
62 |
|
private List<String> spaces; |
63 |
|
|
64 |
|
private String pageName; |
65 |
|
|
66 |
|
private DocumentReference reference; |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
68 |
8 |
@Before... |
69 |
|
@Override |
70 |
|
public void setUp() throws Exception |
71 |
|
{ |
72 |
8 |
super.setUp(); |
73 |
|
|
74 |
8 |
this.wikiName = getWiki(); |
75 |
8 |
this.spaces = Arrays.asList(getTestClassName()); |
76 |
8 |
this.pageName = getTestMethodName(); |
77 |
|
|
78 |
8 |
this.reference = new DocumentReference(this.wikiName, this.spaces, this.pageName); |
79 |
|
|
80 |
|
|
81 |
8 |
this.testUtils.rest().delete(this.reference); |
82 |
8 |
this.testUtils.rest().savePage(this.reference); |
83 |
|
} |
84 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
85 |
1 |
@Override... |
86 |
|
@Test |
87 |
|
public void testRepresentation() throws Exception |
88 |
|
{ |
89 |
|
|
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
92 |
1 |
@Test... |
93 |
|
public void testPUTGETAttachments() throws Exception |
94 |
|
{ |
95 |
|
|
96 |
1 |
String randomStr = String.format("%s.txt", UUID.randomUUID()); |
97 |
|
|
98 |
1 |
putAttachmentFilename(randomStr, "random"); |
99 |
1 |
putAttachmentFilename("my attach.txt", "space"); |
100 |
1 |
putAttachmentFilename("^caret.txt", "caret"); |
101 |
1 |
putAttachmentFilename("#pound.txt", "pound"); |
102 |
1 |
putAttachmentFilename("%percent.txt", "percent"); |
103 |
1 |
putAttachmentFilename("{brace}.txt", "braces"); |
104 |
1 |
putAttachmentFilename("[bracket].txt", "brackets"); |
105 |
|
|
106 |
1 |
putAttachmentFilename("plus+plus.txt", "plus"); |
107 |
|
|
108 |
|
|
109 |
1 |
String attachmentsUri = buildURIForThisPage(AttachmentsResource.class); |
110 |
1 |
GetMethod getMethod = executeGet(attachmentsUri); |
111 |
1 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); |
112 |
|
|
113 |
1 |
Attachments attachments = (Attachments) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); |
114 |
1 |
Assert.assertEquals(8, attachments.getAttachments().size()); |
115 |
|
} |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
117 |
8 |
protected void putAttachmentFilename(String attachmentName, String type) throws Exception... |
118 |
|
{ |
119 |
8 |
String content = "ATTACHMENT CONTENT"; |
120 |
8 |
String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName); |
121 |
|
|
122 |
8 |
GetMethod getMethod = executeGet(attachmentURI); |
123 |
8 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_NOT_FOUND, getMethod.getStatusCode()); |
124 |
|
|
125 |
8 |
PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN, |
126 |
|
TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword()); |
127 |
8 |
Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode()); |
128 |
|
|
129 |
8 |
getMethod = executeGet(attachmentURI); |
130 |
8 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); |
131 |
|
|
132 |
8 |
Assert.assertEquals(content, getMethod.getResponseBodyAsString()); |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
135 |
1 |
@Test... |
136 |
|
public void testPUTAttachmentNoRights() throws Exception |
137 |
|
{ |
138 |
1 |
String attachmentName = String.format("%s.txt", UUID.randomUUID()); |
139 |
1 |
String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName); |
140 |
|
|
141 |
1 |
String content = "ATTACHMENT CONTENT"; |
142 |
|
|
143 |
1 |
GetMethod getMethod = executeGet(attachmentURI); |
144 |
1 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_NOT_FOUND, getMethod.getStatusCode()); |
145 |
|
|
146 |
1 |
PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN); |
147 |
1 |
Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_UNAUTHORIZED, putMethod.getStatusCode()); |
148 |
|
} |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
150 |
1 |
@Test... |
151 |
|
public void testDELETEAttachment() throws Exception |
152 |
|
{ |
153 |
1 |
String attachmentName = String.format("%d.txt", System.currentTimeMillis()); |
154 |
1 |
String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName); |
155 |
1 |
String content = "ATTACHMENT CONTENT"; |
156 |
|
|
157 |
1 |
PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN, |
158 |
|
TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword()); |
159 |
1 |
Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode()); |
160 |
|
|
161 |
1 |
GetMethod getMethod = executeGet(attachmentURI); |
162 |
1 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); |
163 |
|
|
164 |
1 |
DeleteMethod deleteMethod = executeDelete(attachmentURI, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), |
165 |
|
TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword()); |
166 |
1 |
Assert.assertEquals(getHttpMethodInfo(deleteMethod), HttpStatus.SC_NO_CONTENT, deleteMethod.getStatusCode()); |
167 |
|
|
168 |
1 |
getMethod = executeGet(attachmentURI); |
169 |
1 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_NOT_FOUND, getMethod.getStatusCode()); |
170 |
|
} |
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
172 |
1 |
@Test... |
173 |
|
public void testDELETEAttachmentNoRights() throws Exception |
174 |
|
{ |
175 |
1 |
String attachmentName = String.format("%d.txt", System.currentTimeMillis()); |
176 |
1 |
String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName); |
177 |
|
|
178 |
1 |
String content = "ATTACHMENT CONTENT"; |
179 |
|
|
180 |
1 |
PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN, |
181 |
|
TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword()); |
182 |
1 |
Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode()); |
183 |
|
|
184 |
1 |
DeleteMethod deleteMethod = executeDelete(attachmentURI); |
185 |
1 |
Assert.assertEquals(getHttpMethodInfo(deleteMethod), HttpStatus.SC_UNAUTHORIZED, deleteMethod.getStatusCode()); |
186 |
|
|
187 |
1 |
GetMethod getMethod = executeGet(attachmentURI); |
188 |
1 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); |
189 |
|
} |
190 |
|
|
|
|
| 0% |
Uncovered Elements: 39 (39) |
Complexity: 7 |
Complexity Density: 0.26 |
3FAIL
|
|
191 |
0 |
@Test... |
192 |
|
public void testGETAttachmentsAtPageVersion() throws Exception |
193 |
|
{ |
194 |
0 |
final int NUMBER_OF_ATTACHMENTS = 4; |
195 |
0 |
String[] attachmentNames = new String[NUMBER_OF_ATTACHMENTS]; |
196 |
0 |
String[] pageVersions = new String[NUMBER_OF_ATTACHMENTS]; |
197 |
|
|
198 |
0 |
for (int i = 0; i < NUMBER_OF_ATTACHMENTS; i++) { |
199 |
0 |
attachmentNames[i] = String.format("%s.txt", UUID.randomUUID()); |
200 |
|
} |
201 |
|
|
202 |
0 |
String content = "ATTACHMENT CONTENT"; |
203 |
|
|
204 |
|
|
205 |
0 |
for (int i = 0; i < NUMBER_OF_ATTACHMENTS; i++) { |
206 |
0 |
String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentNames[i]); |
207 |
|
|
208 |
0 |
PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN, |
209 |
|
TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword()); |
210 |
0 |
Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode()); |
211 |
|
|
212 |
0 |
Attachment attachment = (Attachment) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream()); |
213 |
0 |
pageVersions[i] = attachment.getPageVersion(); |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
0 |
for (int i = 0; i < NUMBER_OF_ATTACHMENTS; i++) { |
219 |
0 |
String attachmentsUri = buildURIForThisPage(AttachmentsAtPageVersionResource.class, pageVersions[i]); |
220 |
0 |
GetMethod getMethod = executeGet(attachmentsUri); |
221 |
0 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); |
222 |
|
|
223 |
0 |
Attachments attachments = (Attachments) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
0 |
for (int j = 0; j <= i; j++) { |
230 |
0 |
boolean found = false; |
231 |
0 |
for (Attachment attachment : attachments.getAttachments()) { |
232 |
0 |
if (attachment.getName().equals(attachmentNames[j])) { |
233 |
0 |
if (attachment.getPageVersion().equals(pageVersions[i])) { |
234 |
0 |
found = true; |
235 |
0 |
break; |
236 |
|
} |
237 |
|
} |
238 |
|
} |
239 |
0 |
Assert.assertTrue(String.format("%s is not present in attachments list of the page at version %s", |
240 |
|
attachmentNames[j], pageVersions[i]), found); |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
0 |
for (Attachment attachment : attachments.getAttachments()) { |
245 |
0 |
checkLinks(attachment); |
246 |
|
} |
247 |
|
} |
248 |
|
} |
249 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 3 |
Complexity Density: 0.14 |
1PASS
|
|
250 |
1 |
@Test... |
251 |
|
public void testGETAttachmentVersions() throws Exception |
252 |
|
{ |
253 |
1 |
final int NUMBER_OF_VERSIONS = 4; |
254 |
1 |
String attachmentName = String.format("%s.txt", UUID.randomUUID().toString()); |
255 |
|
|
256 |
1 |
Map<String, String> versionToContentMap = new HashMap<String, String>(); |
257 |
|
|
258 |
|
|
259 |
5 |
for (int i = 0; i < NUMBER_OF_VERSIONS; i++) { |
260 |
4 |
String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName); |
261 |
4 |
String content = String.format("CONTENT %d", i); |
262 |
4 |
PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN, |
263 |
|
TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword()); |
264 |
4 |
if (i == 0) { |
265 |
1 |
Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode()); |
266 |
|
} else { |
267 |
3 |
Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode()); |
268 |
|
} |
269 |
|
|
270 |
4 |
Attachment attachment = (Attachment) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream()); |
271 |
|
|
272 |
4 |
versionToContentMap.put(attachment.getVersion(), content); |
273 |
|
} |
274 |
|
|
275 |
1 |
String attachmentsUri = buildURIForThisPage(AttachmentHistoryResource.class, attachmentName); |
276 |
1 |
GetMethod getMethod = executeGet(attachmentsUri); |
277 |
1 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); |
278 |
|
|
279 |
1 |
Attachments attachments = (Attachments) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); |
280 |
1 |
Assert.assertEquals(NUMBER_OF_VERSIONS, attachments.getAttachments().size()); |
281 |
|
|
282 |
1 |
for (Attachment attachment : attachments.getAttachments()) { |
283 |
4 |
getMethod = executeGet(getFirstLinkByRelation(attachment, Relations.ATTACHMENT_DATA).getHref()); |
284 |
4 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); |
285 |
|
|
286 |
4 |
Assert.assertEquals(versionToContentMap.get(attachment.getVersion()), getMethod.getResponseBodyAsString()); |
287 |
|
} |
288 |
|
} |
289 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
290 |
1 |
@Test... |
291 |
|
public void testPOSTAttachment() throws Exception |
292 |
|
{ |
293 |
1 |
final String attachmentName = String.format("%s.txt", UUID.randomUUID()); |
294 |
1 |
final String content = "ATTACHMENT CONTENT"; |
295 |
|
|
296 |
1 |
String attachmentsUri = buildURIForThisPage(AttachmentsResource.class, attachmentName); |
297 |
|
|
298 |
1 |
HttpClient httpClient = new HttpClient(); |
299 |
1 |
httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials( |
300 |
|
TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword())); |
301 |
1 |
httpClient.getParams().setAuthenticationPreemptive(true); |
302 |
|
|
303 |
1 |
Part[] parts = new Part[1]; |
304 |
|
|
305 |
1 |
ByteArrayPartSource baps = new ByteArrayPartSource(attachmentName, content.getBytes()); |
306 |
1 |
parts[0] = new FilePart(attachmentName, baps); |
307 |
|
|
308 |
1 |
PostMethod postMethod = new PostMethod(attachmentsUri); |
309 |
1 |
MultipartRequestEntity mpre = new MultipartRequestEntity(parts, postMethod.getParams()); |
310 |
1 |
postMethod.setRequestEntity(mpre); |
311 |
1 |
httpClient.executeMethod(postMethod); |
312 |
1 |
Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode()); |
313 |
|
|
314 |
1 |
this.unmarshaller.unmarshal(postMethod.getResponseBodyAsStream()); |
315 |
|
|
316 |
1 |
Header location = postMethod.getResponseHeader("location"); |
317 |
|
|
318 |
1 |
GetMethod getMethod = executeGet(location.getValue()); |
319 |
1 |
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); |
320 |
|
|
321 |
1 |
Assert.assertEquals(content, getMethod.getResponseBodyAsString()); |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
@param |
329 |
|
@param |
330 |
|
@return |
331 |
|
@throws |
332 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
333 |
23 |
protected String buildURIForThisPage(Class<?> resource, Object... args) throws Exception... |
334 |
|
{ |
335 |
23 |
List<Object> pathElements = new ArrayList<Object>(); |
336 |
23 |
pathElements.add(this.wikiName); |
337 |
23 |
pathElements.add(this.spaces); |
338 |
23 |
pathElements.add(this.pageName); |
339 |
23 |
pathElements.addAll(Arrays.asList(args)); |
340 |
|
|
341 |
23 |
return super.buildURI(resource, pathElements.toArray()); |
342 |
|
} |
343 |
|
} |