View Javadoc

1   package it.com.atlassian.rest.multipart;
2   
3   import com.atlassian.plugins.rest.multipart.FilePartObject;
4   import com.atlassian.plugins.rest.multipart.FilePartObjects;
5   import com.atlassian.rest.jersey.client.WebResourceFactory;
6   import org.apache.commons.httpclient.HttpClient;
7   import org.apache.commons.httpclient.methods.PostMethod;
8   import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
9   import org.apache.commons.httpclient.methods.multipart.FilePart;
10  import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
11  import org.apache.commons.httpclient.methods.multipart.Part;
12  import org.apache.commons.httpclient.methods.multipart.PartSource;
13  import org.apache.commons.httpclient.methods.multipart.StringPart;
14  import org.apache.commons.httpclient.params.HttpMethodParams;
15  import org.junit.Before;
16  import org.junit.Test;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  
21  import javax.xml.bind.JAXBContext;
22  import javax.xml.bind.Unmarshaller;
23  
24  import static junit.framework.Assert.*;
25  
26  /**
27   */
28  public class MultipartResourceTest
29  {
30      private HttpClient client;
31  
32      @Before
33      public void setUp()
34      {
35          client = new HttpClient();
36      }
37  
38      @Test
39      public void testUploadSingleFile() throws Exception
40      {
41          FilePartObject filePartObject = new FilePartObject("file", false, "filename.txt", "text/plain", "Hello world!!");
42          FilePartObject result = (FilePartObject) uploadAndUnmarshalFileParts("single", filePartObject);
43          assertFilePartEquals(filePartObject, result);
44      }
45  
46      @Test
47      public void testUploadSingleSimpleField() throws Exception
48      {
49          FilePartObject filePartObject = new FilePartObject("file", true, null, null, "Hello world!!");
50          FilePartObject result = (FilePartObject) uploadAndUnmarshalFileParts("single", filePartObject);
51          assertFilePartEquals(filePartObject, result);
52      }
53  
54      @Test
55      public void testUploadMultiple() throws Exception
56      {
57          FilePartObject filePartObject1 = new FilePartObject("file", false, "file1.txt", "text/plain", "Hello first world!");
58          FilePartObject filePartObject2 = new FilePartObject("file", false, "file2.txt", "text/plain", "Hello second world!");
59  
60          FilePartObjects result = (FilePartObjects) uploadAndUnmarshalFileParts("multiple", filePartObject1, filePartObject2);
61          assertEquals(2, result.getFileParts().size());
62          Iterator<FilePartObject> iter = result.getFileParts().iterator();
63          assertFilePartEquals(filePartObject1, iter.next());
64          assertFilePartEquals(filePartObject2, iter.next());
65      }
66  
67      @Test
68      public void testMaxFileSizeUnder() throws Exception
69      {
70          FilePartObject filePartObject = new FilePartObject("file", false, "file.txt", "text/plain", "Safe");
71          FilePartObjects result = (FilePartObjects) uploadAndUnmarshalFileParts("config", filePartObject);
72          assertEquals(1, result.getFileParts().size());
73          assertFilePartEquals(filePartObject, result.getFileParts().iterator().next());
74      }
75  
76      @Test
77      public void testMaxFileSizeExceeded() throws Exception
78      {
79          // The config resource is configured for maximum file size of 10 characters
80          FilePartObject filePartObject = new FilePartObject("file", false, "file.txt", "text/plain", "This is longer than 10 characters");
81          PostMethod method = uploadFileParts("config", filePartObject);
82          assertEquals(404, method.getStatusCode());
83          assertTrue(method.getResponseBodyAsString().contains("10"));
84      }
85  
86      @Test
87      public void testMaxSizeExceeded() throws Exception
88      {
89          // The config resource is configured for maximum overall request size of 1000 characters.  This should yield
90          // 1161 characters due to all the multipart headers
91          PostMethod method = uploadFileParts("config", new FilePartObject("file", false, "file.txt", "text/plain", "Safe"),
92                  new FilePartObject("file", false, "file.txt", "text/plain", "Safe"),
93                  new FilePartObject("file", false, "file.txt", "text/plain", "Safe"),
94                  new FilePartObject("file", false, "file.txt", "text/plain", "Safe"),
95                  new FilePartObject("file", false, "file.txt", "text/plain", "Safe"),
96                  new FilePartObject("file", false, "file.txt", "text/plain", "Safe"));
97          assertEquals(404, method.getStatusCode());
98          assertTrue(method.getResponseBodyAsString().contains("1000"));
99      }
100 
101     @Test
102     public void testInterceptorInvoked() throws Exception
103     {
104         FilePartObject filePartObject = new FilePartObject("file", false, "abc.txt", "text/plain", "Upload");
105         PostMethod method = uploadFileParts("fileName", filePartObject);
106         assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><message><message>abc.txt: Bob</message></message>", method.getResponseBodyAsString());
107     }
108 
109     private static void assertFilePartEquals(FilePartObject o1, FilePartObject o2)
110     {
111         assertEquals("Field name", o1.getFieldName(), o2.getFieldName());
112         assertEquals("Is field", o1.isField(), o2.isField());
113         assertEquals("File name", o1.getFilename(), o2.getFilename());
114         assertEquals("File contents", o1.getValue(), o2.getValue());
115     }
116 
117     private PostMethod uploadFileParts(String to, FilePartObject... fileParts) throws Exception
118     {
119         ArrayList<Part> parts = new ArrayList<Part>();
120         for (FilePartObject o : fileParts)
121         {
122             if (o.isField())
123             {
124                 StringPart stringPart = new StringPart(o.getFieldName(), o.getValue());
125                 parts.add(stringPart);
126             }
127             else
128             {
129                 PartSource partSource = new ByteArrayPartSource(o.getFilename(), o.getValue().getBytes("UTF-8"));
130                 FilePart filePart = new FilePart(o.getFieldName(), partSource, o.getContentType(), "UTF-8");
131                 parts.add(filePart);
132             }
133         }
134 
135         PostMethod method = new PostMethod(WebResourceFactory.getUriBuilder().path("rest").path("refimpl").path(WebResourceFactory.REST_VERSION).path("multipart").path(to).build().toString() + ".xml");
136         MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), new HttpMethodParams());
137         method.setRequestEntity(requestEntity);
138         client.executeMethod(method);
139         return method;
140     }
141     private Object uploadAndUnmarshalFileParts(String to, FilePartObject... fileParts) throws Exception
142     {
143         PostMethod method = uploadFileParts(to, fileParts);
144         Unmarshaller unmarshaller = JAXBContext.newInstance(FilePartObject.class, FilePartObjects.class).createUnmarshaller();
145         return unmarshaller.unmarshal(method.getResponseBodyAsStream());
146     }
147 }