Skip to content

Commit 637949c

Browse files
authored
Merge pull request #14 from thijskok/revised-attachment-handling
Revised attachment handling
2 parents c6d3a2d + 6523999 commit 637949c

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

src/main/java/com/testmonitor/actions/TestResults.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public ArrayList<TestResult> search(String query) throws IOException, URISyntaxE
100100
* @return The created test result
101101
*/
102102
public TestResult create(TestResult testResult) throws IOException {
103-
JSONObject response = this.connector.post("test-results", testResult.toHttpParams());
103+
JSONObject response = this.connector.multiPartPost("test-results", testResult.toHttpParams(), testResult.getAttachments());
104104

105105
HashMap<String, Object> newTestResult = (HashMap<String, Object>) response.getJSONObject("data").toMap();
106106

@@ -131,7 +131,7 @@ public TestResult update(TestResult testResult) throws IOException {
131131
* @return The test result
132132
*/
133133
public TestResult addAttachment(TestResult testResult, File attachment) throws IOException {
134-
this.connector.postAttachment("test-result/" + testResult.getId() + "/attachments", attachment);
134+
this.connector.postFile("test-result/" + testResult.getId() + "/attachments", "file", attachment);
135135

136136
return testResult;
137137
}

src/main/java/com/testmonitor/api/Connector.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.File;
2727
import java.io.IOException;
2828
import java.net.URISyntaxException;
29+
import java.util.HashMap;
2930
import java.util.List;
3031
import java.util.concurrent.TimeUnit;
3132

@@ -142,21 +143,49 @@ public JSONObject put(String uri, List<NameValuePair> params) throws IOException
142143
return this.request(httpput);
143144
}
144145

146+
/**
147+
* Send a post request using multipart.
148+
*
149+
* @param uri A relative path
150+
* @param params The arguments to post
151+
* @param files The files to post
152+
*
153+
* @return The HTTP response as a JSONObject.
154+
*/
155+
public JSONObject multiPartPost(String uri, List<NameValuePair> params, HashMap<String, File> files) throws IOException {
156+
HttpPost httppost = new HttpPost(this.baseUrl(uri));
157+
158+
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
159+
builder.setMode(HttpMultipartMode.EXTENDED);
160+
161+
// Handle plain post parameters
162+
params.forEach((pair -> builder.addTextBody(pair.getName(), pair.getValue())));
163+
164+
// Handle file attachments
165+
files.forEach((name, file) -> builder.addPart(name, new FileBody(file, ContentType.DEFAULT_BINARY)));
166+
167+
HttpEntity entity = builder.build();
168+
169+
httppost.setEntity(entity);
170+
171+
return this.request(httppost);
172+
}
173+
145174
/**
146175
* Send an attachment.
147176
*
148177
* @param uri A relative path
178+
* @param name Field name
149179
* @param file The file attachment
150180
*
151181
* @return The HTTP response as a JSONObject.
152182
*/
153-
public JSONObject postAttachment(String uri, File file) throws IOException {
183+
public JSONObject postFile(String uri, String name, File file) throws IOException {
154184
HttpPost post = new HttpPost(this.baseUrl(uri));
155-
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
156185

157186
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
158187
builder.setMode(HttpMultipartMode.EXTENDED);
159-
builder.addPart("file", fileBody);
188+
builder.addPart(name, new FileBody(file, ContentType.DEFAULT_BINARY));
160189

161190
HttpEntity entity = builder.build();
162191

src/main/java/com/testmonitor/parsers/TestResultParser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
public class TestResultParser {
99
/**
10-
* Parse a JSONObject in a list of milestones
10+
* Parse a JSONObject in a list of test results
1111
*
12-
* @param response The JSON response of a request
12+
* @param response The JSON response of a reqsuest
1313
*
1414
* @return A parsed list of test results
1515
*/
@@ -27,11 +27,11 @@ public static ArrayList<TestResult> parse(JSONObject response)
2727
}
2828

2929
/**
30-
* Parse a hashmap into a milestone.
30+
* Parse a hashmap into a test result.
3131
*
32-
* @param item the hashmap that contains the milestone data.
32+
* @param item the hashmap that contains the test result data.
3333
*
34-
* @return The parsed milestone
34+
* @return The parsed test result
3535
*/
3636
public static TestResult parse(HashMap<String, Object> item)
3737
{

src/main/java/com/testmonitor/resources/TestResult.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.apache.hc.core5.http.NameValuePair;
44
import org.apache.hc.core5.http.message.BasicNameValuePair;
55

6+
import java.io.File;
67
import java.util.ArrayList;
8+
import java.util.HashMap;
79
import java.util.List;
810

911
public class TestResult {
@@ -19,6 +21,8 @@ public class TestResult {
1921

2022
private Integer testResultCategoryId;
2123

24+
private ArrayList<File> attachments = new ArrayList<File>();
25+
2226
public TestResult setId(Integer id) {
2327
this.id = id;
2428

@@ -109,6 +113,20 @@ public TestResult setDraft(String draft) {
109113
return this;
110114
}
111115

116+
public void addAttachment(File attachment) {
117+
this.attachments.add(attachment);
118+
}
119+
120+
public HashMap<String, File> getAttachments() {
121+
return new HashMap<String, File>() {{
122+
attachments.forEach((file) -> put("file", file));
123+
}};
124+
}
125+
126+
public void clearAttachments() {
127+
this.attachments.clear();
128+
}
129+
112130
public List<NameValuePair> toHttpParams() {
113131
List<NameValuePair> params = new ArrayList<>();
114132

0 commit comments

Comments
 (0)