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

File AttachmentsResourceTest.java

 
testGETAttachmentsAtPageVersion: 955822b8-580d-48b5-8517-62e669d71323.txt is not present i...
 

Code metrics

16
130
11
1
343
235
19
0.15
11.82
11
1.73

Classes

Class Line # Actions
AttachmentsResourceTest 58 130 0% 19 40
0.745222974.5%
 

Contributing tests

This file is covered by 8 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.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   
 
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   
 
68  8 toggle @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    // Create a clean test page.
81  8 this.testUtils.rest().delete(this.reference);
82  8 this.testUtils.rest().savePage(this.reference);
83    }
84   
 
85  1 toggle @Override
86    @Test
87    public void testRepresentation() throws Exception
88    {
89    /* Everything is done in test methods. */
90    }
91   
 
92  1 toggle @Test
93    public void testPUTGETAttachments() throws Exception
94    {
95    /* Test normal random UUID method */
96  1 String randomStr = String.format("%s.txt", UUID.randomUUID());
97    /* Test filenames requiring url encoding */
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    /** Causes XWIKI-7874 **/
106  1 putAttachmentFilename("plus+plus.txt", "plus");
107   
108    // Now get all the attachments.
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   
 
117  8 toggle 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   
 
135  1 toggle @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   
 
150  1 toggle @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   
 
172  1 toggle @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   
 
191  0 toggle @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    /* Create NUMBER_OF_ATTACHMENTS attachments */
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    // For each page version generated, check that the attachments that are supposed to be there are actually there.
217    // We do the following: at pageVersion[i] we check that all attachmentNames[0..i] are there.
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    * Check that all attachmentNames[0..i] are present in the list of attachments of page at version
227    * pageVersions[i]
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 Test failure here 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    /* Check links */
244  0 for (Attachment attachment : attachments.getAttachments()) {
245  0 checkLinks(attachment);
246    }
247    }
248    }
249   
 
250  1 toggle @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    /* Create NUMBER_OF_ATTACHMENTS attachments */
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   
 
290  1 toggle @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    * Creates a URI to access the specified resource with the given path elements. The wiki, space and page path
326    * elements are added by this method so you can skip them.
327    *
328    * @param resource the resource that needs to be accessed
329    * @param args the path elements
330    * @return an URI to access the specified resource with the given path elements
331    * @throws Exception if encoding the path elements fails
332    */
 
333  23 toggle 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    }