Skip to content

Commit 6ccb13a

Browse files
author
Nakul Sabharwal
committed
Added unit tests for createPartHeader, used StringBuilder for constructing header's value-parameter string and restructured conditions in createPartHeader.
1 parent 45ec13b commit 6ccb13a

File tree

3 files changed

+144
-67
lines changed

3 files changed

+144
-67
lines changed

src/main/java/com/microsoft/graph/models/extensions/Multipart.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.security.SecureRandom;
99
import java.util.Map;
1010

11+
import com.google.common.annotations.VisibleForTesting;
1112
import com.microsoft.graph.options.HeaderOption;
1213

1314
/**
@@ -79,26 +80,28 @@ private void writePartData(String partContent, byte[] byteArray) throws IOExcept
7980
out.write(returnContent.getBytes(MULTIPART_ENCODING));
8081
}
8182

82-
private String createPartHeader(String name, String contentType, String filename) {
83-
String partContent = addBoundary();
84-
if(filename != null && name != null) {
85-
String partContentWithNameAndFilename = "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" + RETURN + "Content-Type:%s" + RETURN + RETURN;
86-
partContent += String.format(partContentWithNameAndFilename, name, filename, contentType);
87-
}
88-
else if(filename != null) {
89-
String partContentWithFilename = "Content-Disposition: form-data; filename=\"%s\"" + RETURN + "Content-Type:%s" + RETURN + RETURN;
90-
partContent += String.format(partContentWithFilename, filename, contentType);
91-
}
92-
else if(name != null) {
93-
String partContentWithNameAndContentType = "Content-Disposition: form-data; name=\"%s\"" + RETURN + "Content-Type:%s" + RETURN + RETURN;
94-
partContent += String.format(partContentWithNameAndContentType, name, contentType);
95-
}
96-
else {
97-
String partContentWithContentType = "Content-Disposition: form-data" + RETURN + "Content-Type:%s" + RETURN + RETURN;
98-
partContent += String.format(partContentWithContentType, contentType);
83+
/**
84+
* Create content headers value and parameter
85+
* @param name The content header name
86+
* @param contentType The content header Content-Type
87+
* @param filename The content header filename
88+
* @return content header value and parameter string
89+
*/
90+
@VisibleForTesting String createPartHeader(String name, String contentType, String filename) {
91+
StringBuilder partContent = new StringBuilder(addBoundary());
92+
partContent.append("Content-Disposition: form-data");
93+
if(filename != null) {
94+
if(name != null)
95+
partContent.append("; name=\"").append(name).append("\"; filename=\"").append(filename).append("\"");
96+
else
97+
partContent.append("; filename=\"").append(filename).append("\"");
9998
}
100-
101-
return partContent;
99+
else if(name != null)
100+
partContent.append("; name=\"").append(name).append("\"");
101+
if(contentType != null)
102+
partContent.append(RETURN).append("Content-Type: ").append(contentType);
103+
partContent.append(RETURN).append(RETURN);
104+
return partContent.toString();
102105
}
103106

104107
/**
@@ -123,7 +126,7 @@ public static String createContentHeaderValue(String contentValue, Map<String, S
123126
*/
124127
private String createPartHeader(Map<String, String> headers) {
125128
String partContent = addBoundary();
126-
String defaultPartContent = "Content-Disposition: form-data;" + RETURN + "Content-Type:" + contentType + RETURN + RETURN;
129+
String defaultPartContent = "Content-Disposition: form-data;" + RETURN + "Content-Type: " + contentType + RETURN + RETURN;
127130

128131
if(headers != null) {
129132
for(Map.Entry<String,String> entry : headers.entrySet())

src/test/java/com/microsoft/graph/functional/OneNoteTests.java

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -439,53 +439,49 @@ public void testMultipartPost(){
439439
* Test posting multipart content to a page
440440
*/
441441
@Test
442-
public void testMultipartPostWithHeadersMap(){
443-
try {
444-
Multipart multipart = new Multipart();
445-
446-
String htmlContent = "<!DOCTYPE html>\r\n" +
447-
"<html lang=\"en-US\">\r\n" +
448-
"<head>\r\n" +
449-
"<title>Test Multipart Page</title>\r\n" +
450-
"<meta name=\"created\" content=\"2001-01-01T01:01+0100\">\r\n" +
451-
"</head>\r\n" +
452-
"<body>\r\n" +
453-
"<p>\r\n" +
454-
"<img src=\"name:image\" />\r\n" +
455-
"</p>\r\n" +
456-
"<p>\r\n" +
457-
"<object data=\"name:attachment\" data-attachment=\"document.pdf\" /></p>\r\n" +
458-
"\r\n" +
459-
"</body>\r\n" +
460-
"</html>";
461-
File imgFile = new File("src/test/resources/hamilton.jpg");
462-
File pdfFile = new File("src/test/resources/document.pdf");
463-
464-
Map<String, String> htmlHeaderMap = new HashMap<>();
465-
Map<String, String> contentDispMap = new HashMap<>();
466-
contentDispMap.put("name","Presentation" );
467-
htmlHeaderMap.put("Content-Disposition", Multipart.createContentHeaderValue("form-data", contentDispMap));
468-
htmlHeaderMap.put("Content-Type", Multipart.createContentHeaderValue("text/html", null));
469-
multipart.addPart(htmlHeaderMap, htmlContent.getBytes(HTML_ENCODING));
470-
471-
InputStream fileStream = new FileInputStream(imgFile);
472-
multipart.addFormData("hamilton", "image/jpg", getByteArray(fileStream));
473-
multipart.addFilePart("metadata", "application/pdf", pdfFile);
474-
475-
// Add multipart request header
476-
List<Option> options = new ArrayList<Option>();
477-
options.add(multipart.header());
478-
479-
// Post the multipart content
480-
OnenotePage page = orb
481-
.sections(testSection.id)
482-
.pages()
483-
.buildRequest(options)
484-
.post(multipart.content());
485-
assertNotNull(page);
486-
} catch (Exception e) {
487-
fail("Unable to write to output stream");
488-
}
442+
public void testMultipartPostWithHeadersMap() throws Exception{
443+
Multipart multipart = new Multipart();
444+
445+
String htmlContent = "<!DOCTYPE html>\r\n" +
446+
"<html lang=\"en-US\">\r\n" +
447+
"<head>\r\n" +
448+
"<title>Test Multipart Page</title>\r\n" +
449+
"<meta name=\"created\" content=\"2001-01-01T01:01+0100\">\r\n" +
450+
"</head>\r\n" +
451+
"<body>\r\n" +
452+
"<p>\r\n" +
453+
"<img src=\"name:image\" />\r\n" +
454+
"</p>\r\n" +
455+
"<p>\r\n" +
456+
"<object data=\"name:attachment\" data-attachment=\"document.pdf\" /></p>\r\n" +
457+
"\r\n" +
458+
"</body>\r\n" +
459+
"</html>";
460+
File imgFile = new File("src/test/resources/hamilton.jpg");
461+
File pdfFile = new File("src/test/resources/document.pdf");
462+
463+
Map<String, String> htmlHeaderMap = new HashMap<>();
464+
Map<String, String> contentDispMap = new HashMap<>();
465+
contentDispMap.put("name","Presentation" );
466+
htmlHeaderMap.put("Content-Disposition", Multipart.createContentHeaderValue("form-data", contentDispMap));
467+
htmlHeaderMap.put("Content-Type", Multipart.createContentHeaderValue("text/html", null));
468+
multipart.addPart(htmlHeaderMap, htmlContent.getBytes(HTML_ENCODING));
469+
470+
InputStream fileStream = new FileInputStream(imgFile);
471+
multipart.addFormData("hamilton", "image/jpg", getByteArray(fileStream));
472+
multipart.addFilePart("metadata", "application/pdf", pdfFile);
473+
474+
// Add multipart request header
475+
List<Option> options = new ArrayList<Option>();
476+
options.add(multipart.header());
477+
478+
// Post the multipart content
479+
OnenotePage page = orb
480+
.sections(testSection.id)
481+
.pages()
482+
.buildRequest(options)
483+
.post(multipart.content());
484+
assertNotNull(page);
489485
}
490486

491487
/**
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.microsoft.graph.models.extensions;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Before;
6+
import org.junit.Ignore;
7+
import org.junit.Test;
8+
9+
/**
10+
* Tests for Multipart functionality
11+
*/
12+
@Ignore
13+
public class MultipartTests {
14+
15+
private Multipart multipart;
16+
17+
@Before
18+
public void setUp() {
19+
multipart = new Multipart();
20+
}
21+
22+
@Test
23+
public void testCreatePartHeaderWithNameContenttypeFilename() {
24+
String actual = multipart.createPartHeader("hamilton", "image/jpg", "hamilton.jpg");
25+
String expected = "--"+multipart.getBoundary()+"\r\nContent-Disposition: form-data; name=\"hamilton\"; filename=\"hamilton.jpg\"\r\nContent-Type: image/jpg\r\n\r\n";
26+
assertEquals(expected, actual);
27+
}
28+
29+
@Test
30+
public void testCreatePartHeaderWithContenttypeFilename() {
31+
String actual = multipart.createPartHeader(null, "image/jpg", "hamilton.jpg");
32+
String expected = "--"+multipart.getBoundary()+"\r\nContent-Disposition: form-data; filename=\"hamilton.jpg\"\r\nContent-Type: image/jpg\r\n\r\n";
33+
assertEquals(expected, actual);
34+
}
35+
36+
@Test
37+
public void testCreatePartHeaderWithNameContenttype() {
38+
String actual = multipart.createPartHeader("hamilton", "image/jpg", null);
39+
String expected = "--"+multipart.getBoundary()+"\r\nContent-Disposition: form-data; name=\"hamilton\"\r\nContent-Type: image/jpg\r\n\r\n";
40+
assertEquals(expected, actual);
41+
}
42+
43+
@Test
44+
public void testCreatePartHeaderWithContenttype() {
45+
String actual = multipart.createPartHeader(null, "image/jpg", null);
46+
String expected = "--"+multipart.getBoundary()+"\r\nContent-Disposition: form-data\r\nContent-Type: image/jpg\r\n\r\n";
47+
assertEquals(expected, actual);
48+
}
49+
50+
@Test
51+
public void testCreatePartHeader() {
52+
String actual = multipart.createPartHeader(null, null, null);
53+
String expected = "--"+multipart.getBoundary()+"\r\nContent-Disposition: form-data\r\n\r\n";
54+
System.out.println(actual);
55+
assertEquals(expected, actual);
56+
}
57+
58+
@Test
59+
public void testCreatePartHeaderWithNameFilename() {
60+
String actual = multipart.createPartHeader("hamilton", null, "hamilton.jpg");
61+
String expected = "--"+multipart.getBoundary()+"\r\nContent-Disposition: form-data; name=\"hamilton\"; filename=\"hamilton.jpg\"\r\n\r\n";
62+
assertEquals(expected, actual);
63+
}
64+
65+
@Test
66+
public void testCreatePartHeaderWithName() {
67+
String actual = multipart.createPartHeader("hamilton", null, null);
68+
String expected = "--"+multipart.getBoundary()+"\r\nContent-Disposition: form-data; name=\"hamilton\"\r\n\r\n";
69+
assertEquals(expected, actual);
70+
}
71+
72+
@Test
73+
public void testCreatePartHeaderWithFilename() {
74+
String actual = multipart.createPartHeader(null, null, "hamilton.jpg");
75+
String expected = "--"+multipart.getBoundary()+"\r\nContent-Disposition: form-data; filename=\"hamilton.jpg\"\r\n\r\n";
76+
assertEquals(expected, actual);
77+
}
78+
}

0 commit comments

Comments
 (0)