2424
2525import org .junit .*;
2626
27+ import java .io .BufferedInputStream ;
2728import java .io .BufferedReader ;
2829import java .io .ByteArrayInputStream ;
30+ import java .io .ByteArrayOutputStream ;
31+ import java .io .DataOutputStream ;
2932import java .io .InputStream ;
3033import java .io .InputStreamReader ;
34+ import java .io .OutputStream ;
3135import java .math .BigInteger ;
36+ import java .net .HttpURLConnection ;
37+ import java .net .URL ;
38+ import java .net .URLConnection ;
3239import java .nio .charset .StandardCharsets ;
3340import java .security .SecureRandom ;
3441import java .util .ArrayList ;
@@ -49,7 +56,7 @@ public void setUp() {
4956 testBase = new TestBase ();
5057 testNotebook = testBase .graphClient .getMe ().getOnenote ().getNotebooks ("1-525fe350-0199-4c02-879d-e5b142ae8632" ).buildRequest ().get ();
5158 testSection = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSections ().buildRequest ().get ().getCurrentPage ().get (0 );
52- testPage = testBase .graphClient .getMe ().getOnenote ().getPages ().buildRequest ().get ().getCurrentPage ().get (0 );
59+ // testPage = testBase.graphClient.getMe().getOnenote().getPages().buildRequest().get().getCurrentPage().get(0);
5360
5461 // For copy scenarios
5562 testNotebook2 = testBase .graphClient .getMe ().getOnenote ().getNotebooks ("1-491df90f-b45b-477f-b297-032f000e6f1e" ).buildRequest ().get ();
@@ -208,45 +215,78 @@ public void testCopyTo(){
208215 }
209216
210217 @ Test
211- public void testMultipartMimetype (){
218+ public void testMultipartPost (){
212219 String multipartBoundary = "part_" + new BigInteger (130 , new SecureRandom ()).toString ();
213- String content = "--" + multipartBoundary + "\n " +
214- "Content-Disposition:form-data; name=\" Presentation\" \n " +
215- "Content-Type: text/html\n " +
216- "\n " +
217- "<!DOCTYPE html>\n " +
218- "<html lang=\" en-US\" >\n " +
219- "<head>\n " +
220- "<title>Page1</title>\n " +
221- "<meta name=\" created\" content=\" 2001-01-01T01:01+0100\" >\n " +
222- "</head>\n " +
223- "<body>\n " +
224- "<p>hello</p>" +
225- // "\t\t<p>\n" +
226- // "\t\t\t<img src=\"name:image\" />\n" +
227- // "\t\t</p>\n" +
228- // "\t\t<p>\n" +
229- // "\t\t\t<object data=\"name:attachment\" data-attachment=\"document.pdf\" />\n" +
230- // "\t\t</p>\n" +
231- "</body>\n " +
232- "</html>\n " +
233- //"\n" +
234- // multipartBoundary + "\n" +
235- // "Content-Disposition:form-data; name=\"image\"\n" +
236- // "Content-type:image/jpeg\n" +
237- // "\n" +
238- // "FromFile=\"C:\\hamilton.jpg\"\n" +
239- // "\n" +
240- // multipartBoundary + "\n" +
241- // "Content-Disposition:form-data; name=\"attachment\"\n" +
242- // "Content-type:application/pdf\n" +
243- // "\n" +
244- // "FromFile=\"C:\\document.pdf\"\n" +
245- // "\n" +
246- "--" + multipartBoundary + "--" ;
247- byte [] pageStream = content .getBytes ();
248- List <Option > options = new ArrayList <Option >();
249- options .add (new HeaderOption ("Content-Type" , "multipart/form-data;boundary=\" " + multipartBoundary + "\" " ));
250- OnenotePage newPage = testBase .graphClient .getMe ().getOnenote ().getSections (testSection .id ).getPages ().buildRequest (options ).post (pageStream );
220+ try {
221+ String html =
222+ "--" + multipartBoundary + "\r \n " +
223+ "Content-Disposition:form-data; name=\" Presentation\" " + "\r \n " +
224+ "Content-Type: text/html" + "\r \n " +
225+ "\r \n " +
226+ "<!DOCTYPE html>\r \n " +
227+ "<html lang=\" en-US\" >\r \n " +
228+ "<head>\r \n " +
229+ "<title>Test Multipart Page</title>\r \n " +
230+ "<meta name=\" created\" content=\" 2001-01-01T01:01+0100\" >\r \n " +
231+ "</head>\r \n " +
232+ "<body>\r \n " +
233+ "<p>\r \n " +
234+ "<img src=\" name:image\" />\r \n " +
235+ "</p>\r \n " +
236+ "<p>\r \n " +
237+ "<object data=\" name:attachment\" data-attachment=\" document.pdf\" /></p>\r \n " +
238+ "\r \n " +
239+ "</body>\r \n " +
240+ "</html>\r \n " +
241+ "\r \n " +
242+ "--" + multipartBoundary + "\r \n " +
243+ "Content-Disposition:form-data; name=\" image\" \r \n " +
244+ "Content-Type: image/jpeg\r \n \r \n " ;
245+ String doc = "\r \n \r \n " +
246+ "--" + multipartBoundary + "\r \n " +
247+ "Content-Disposition:form-data; name=\" attachment\" \r \n " +
248+ "Content-Type:application/pdf\r \n \r \n " ;
249+
250+ String end = "\r \n \r \n " +
251+ "--" + multipartBoundary + "--" ;
252+
253+ InputStream imageStream = this .getClass ().getClassLoader ().getResourceAsStream ("hamilton.jpg" );
254+ byte [] imgArray = getByteArray (imageStream );
255+
256+ InputStream docStream = this .getClass ().getClassLoader ().getResourceAsStream ("document.pdf" );
257+ byte [] docArray = getByteArray (docStream );
258+
259+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
260+ out .write (html .getBytes ());
261+ out .write (imgArray );
262+ out .write (doc .getBytes ());
263+ out .write (docArray );
264+ out .write (end .getBytes ());
265+
266+ byte finalData [] = out .toByteArray ();
267+
268+ List <Option > options = new ArrayList <Option >();
269+ options .add (new HeaderOption ("Content-Type" , "multipart/form-data; boundary=\" " + multipartBoundary + "\" " ));
270+ OnenotePage newPage = testBase .graphClient .getMe ().getOnenote ().getSections (testSection .id ).getPages ().buildRequest (options ).post (finalData );
271+ assertNotNull (newPage );
272+ } catch (Exception e ) {
273+ Assert .fail ("Unable to write to output stream" );
274+ }
275+ }
276+
277+ public byte [] getByteArray (InputStream in ) {
278+ try {
279+ ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
280+ int nRead ;
281+ byte [] data = new byte [16384 ];
282+ while ((nRead = in .read (data , 0 , data .length )) != -1 ) {
283+ buffer .write (data , 0 , nRead );
284+ }
285+ buffer .flush ();
286+ return buffer .toByteArray ();
287+ } catch (Exception e ) {
288+ Assert .fail ("Unable to open document" );
289+ }
290+ return null ;
251291 }
252292}
0 commit comments