1+ package com .microsoft .graph .functional ;
2+
3+ import android .test .AndroidTestCase ;
4+ import android .test .suitebuilder .annotation .Suppress ;
5+
6+ import com .microsoft .graph .extensions .INotebookCollectionPage ;
7+ import com .microsoft .graph .extensions .INotebookGetRecentNotebooksCollectionPage ;
8+ import com .microsoft .graph .extensions .IOnenotePageCollectionPage ;
9+ import com .microsoft .graph .extensions .IOnenoteSectionCollectionPage ;
10+ import com .microsoft .graph .extensions .ISectionGroupCollectionPage ;
11+ import com .microsoft .graph .extensions .Notebook ;
12+ import com .microsoft .graph .extensions .OnenoteOperation ;
13+ import com .microsoft .graph .extensions .OnenotePage ;
14+ import com .microsoft .graph .extensions .OnenotePagePreview ;
15+ import com .microsoft .graph .extensions .OnenotePatchActionType ;
16+ import com .microsoft .graph .extensions .OnenotePatchContentCommand ;
17+ import com .microsoft .graph .extensions .OnenotePatchInsertPosition ;
18+ import com .microsoft .graph .extensions .OnenoteSection ;
19+ import com .microsoft .graph .extensions .SectionGroup ;
20+ import com .microsoft .graph .options .HeaderOption ;
21+ import com .microsoft .graph .options .Option ;
22+ import com .microsoft .graph .options .QueryOption ;
23+
24+ import org .junit .*;
25+
26+ import java .io .BufferedReader ;
27+ import java .io .ByteArrayOutputStream ;
28+ import java .io .InputStream ;
29+ import java .io .InputStreamReader ;
30+ import java .math .BigInteger ;
31+ import java .security .SecureRandom ;
32+ import java .util .ArrayList ;
33+ import java .util .List ;
34+
35+ @ Suppress
36+ public class OneNoteTests extends AndroidTestCase {
37+
38+ private TestBase testBase ;
39+ private Notebook testNotebook ;
40+ private OnenoteSection testSection ;
41+ private OnenotePage testPage ;
42+ private Notebook testNotebook2 ;
43+ private SectionGroup testSectionGroup2 ;
44+
45+ @ Before
46+ public void setUp () {
47+ testBase = new TestBase ();
48+ testNotebook = testBase .graphClient .getMe ().getOnenote ().getNotebooks ("1-525fe350-0199-4c02-879d-e5b142ae8632" ).buildRequest ().get ();
49+ testSection = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSections ().buildRequest ().get ().getCurrentPage ().get (0 );
50+ testPage = testBase .graphClient .getMe ().getOnenote ().getPages ().buildRequest ().get ().getCurrentPage ().get (0 );
51+
52+ // For copy scenarios
53+ testNotebook2 = testBase .graphClient .getMe ().getOnenote ().getNotebooks ("1-491df90f-b45b-477f-b297-032f000e6f1e" ).buildRequest ().get ();
54+ testSectionGroup2 = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook2 .id ).getSectionGroups ().buildRequest ().get ().getCurrentPage ().get (0 );
55+ }
56+
57+ @ Test
58+ public void testGetNotebookData () {
59+ INotebookCollectionPage books = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().buildRequest ().get ();
60+ assertNotNull (books );
61+
62+ // Get pages from the OneNote object
63+ IOnenotePageCollectionPage pages = testBase .graphClient .getMe ().getOnenote ().getPages ().buildRequest ().get ();
64+ assertNotNull (pages );
65+
66+ // Get sections from a specific notebook
67+ IOnenoteSectionCollectionPage notebookSections = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSections ().buildRequest ().get ();
68+ assertNotNull (notebookSections );
69+
70+ // Get sections from the OneNote object
71+ IOnenoteSectionCollectionPage sections = testBase .graphClient .getMe ().getOnenote ().getSections ().buildRequest ().get ();
72+ assertNotNull (sections );
73+
74+ // Get section groups from a specific notebook
75+ ISectionGroupCollectionPage notebookGroups = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSectionGroups ().buildRequest ().get ();
76+ assertNotNull (notebookGroups );
77+
78+ // Get section groups from the OneNote object
79+ ISectionGroupCollectionPage groups = testBase .graphClient .getMe ().getOnenote ().getSectionGroups ().buildRequest ().get ();
80+ assertNotNull (groups );
81+
82+ // Get pages from a specific section
83+ IOnenotePageCollectionPage sectionPages = testBase .graphClient .getMe ().getOnenote ().getSections (sections .getCurrentPage ().get (0 ).id ).getPages ().buildRequest ().get ();
84+ assertNotNull (sectionPages );
85+ }
86+
87+ @ Test
88+ public void testODataQueries () {
89+ INotebookCollectionPage books = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().buildRequest ().expand ("sections" ).get ();
90+ Notebook book = books .getCurrentPage ().get (0 );
91+ assertNotNull (book .sections );
92+
93+ INotebookCollectionPage idBooks = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().buildRequest ().select ("id" ).get ();
94+ Notebook idBook = books .getCurrentPage ().get (0 );
95+ assertNotNull (idBook .id );
96+
97+ IOnenotePageCollectionPage pages = testBase .graphClient .getMe ().getOnenote ().getPages ().buildRequest ().select ("title" ).get ();
98+ OnenotePage page = pages .getCurrentPage ().get (0 );
99+ assertNotNull (page .title );
100+
101+ List <Option > options = new ArrayList <Option >();
102+ options .add (new QueryOption ("count" , "true" ));
103+ INotebookCollectionPage countedBooks = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().buildRequest (options ).get ();
104+ assert (countedBooks .getRawObject ().get ("@odata.count" ).getAsInt () > 0 );
105+
106+ List <Option > pageLevelOptions = new ArrayList <Option >();
107+ pageLevelOptions .add (new QueryOption ("pagelevel" , "true" ));
108+ IOnenotePageCollectionPage pageLevelPages = testBase .graphClient .getMe ().getOnenote ().getSections (testSection .id ).getPages ().buildRequest (pageLevelOptions ).get ();
109+ assertNotNull (pageLevelPages .getCurrentPage ().get (0 ).level );
110+ }
111+
112+ @ Test
113+ public void testRecentNotebooks () {
114+ INotebookGetRecentNotebooksCollectionPage books = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().getGetRecentNotebooks (true ).buildRequest ().get ();
115+ assertNotNull (books );
116+
117+ INotebookGetRecentNotebooksCollectionPage noPersonalBooks = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().getGetRecentNotebooks (false ).buildRequest ().get ();
118+ assertNotNull (noPersonalBooks );
119+ }
120+
121+ @ Test
122+ public void testGetPageContent () {
123+ InputStream pageStream = testBase .graphClient .getMe ().getOnenote ().getPages (testPage .id ).getContent ().buildRequest ().get ();
124+
125+ BufferedReader r = new BufferedReader (new InputStreamReader (pageStream ));
126+ StringBuilder content = new StringBuilder ();
127+ String line ;
128+
129+ try {
130+ while ((line = r .readLine ()) != null ) {
131+ content .append (line ).append ('\n' );
132+ }
133+ } catch (Exception e ) {
134+ assertEquals (0 ,1 );
135+ }
136+ assertNotNull (content );
137+
138+ //Hardcoding for now since it requires parsing out of the page
139+ String resourceId = "1-ff7e33cbba1a4cda8f7313abe32420a1!1-db1705d5-fc30-4b3b-8dfb-191981428c65" ;
140+ InputStream resourceStream = testBase .graphClient .getMe ().getOnenote ().getResources (resourceId ).getContent ().buildRequest ().get ();
141+
142+ r = new BufferedReader (new InputStreamReader (resourceStream ));
143+ content = new StringBuilder ();
144+
145+ try {
146+ while ((line = r .readLine ()) != null ) {
147+ content .append (line ).append ('\n' );
148+ }
149+ } catch (Exception e ) {
150+ assertEquals (0 ,1 );
151+ }
152+ assertNotNull (content );
153+
154+ }
155+
156+ @ Test
157+ public void testGetPreview () {
158+ OnenotePagePreview preview = testBase .graphClient .getMe ().getOnenote ().getPages (testPage .id ).getPreview ().buildRequest ().get ();
159+ assertNotNull (preview );
160+ }
161+
162+ @ Test
163+ // Currently there is no way to delete notebooks, sections, or pages
164+ public void testPostToNotebook () {
165+ SectionGroup sectionGroup = new SectionGroup ();
166+ sectionGroup .displayName = "Test Section Group" ;
167+
168+ SectionGroup newSectionGroup = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSectionGroups ().buildRequest ().post (sectionGroup );
169+ assertEquals (sectionGroup .displayName , newSectionGroup .displayName );
170+
171+ OnenoteSection section = new OnenoteSection ();
172+ section .displayName = "New Test Section" ;
173+
174+ OnenoteSection newSection = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSections ().buildRequest ().post (section );
175+ assertEquals (section .displayName , newSection .displayName );
176+
177+ String content = "<html><head><title>Test Title</title></head><body>Test body</body></html>" ;
178+
179+ byte [] pageStream = content .getBytes ();
180+ List <Option > options = new ArrayList <Option >();
181+ options .add (new HeaderOption ("Content-Type" , "application/xhtml+xml" ));
182+ OnenotePage newPage = testBase .graphClient .getMe ().getOnenote ().getSections (newSection .id ).getPages ().buildRequest (options ).post (pageStream );
183+ assertEquals ("Test Title" , newPage .title );
184+
185+ testBase .graphClient .getMe ().getOnenote ().getPages (newPage .id ).buildRequest ().delete ();
186+
187+ // testBase.graphClient.getMe().getOnenote().getSections(newSection.id).buildRequest().delete();
188+ }
189+
190+ @ Test
191+ public void testCopyTo (){
192+ OnenoteOperation operation1 = testBase .graphClient .getMe ().getOnenote ().getSections (testSection .id ).getCopyToNotebook (testNotebook2 .id , null , null ).buildRequest ().post ();
193+ assertNotNull (operation1 );
194+
195+ OnenoteOperation operation = testBase .graphClient .getMe ().getOnenote ().getSections (testSection .id ).getCopyToSectionGroup (testSectionGroup2 .id , null , null ).buildRequest ().post ();
196+ assertNotNull (operation );
197+
198+ OnenoteSection section = new OnenoteSection ();
199+ section .displayName = "Test Copy Section" ;
200+ OnenoteSection newSection = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook2 .id ).getSections ().buildRequest ().post (section );
201+ operation = testBase .graphClient .getMe ().getOnenote ().getPages (testPage .id ).getCopyToSection (newSection .id , null ).buildRequest ().post ();
202+ assertNotNull (operation );
203+
204+ operation = testBase .graphClient .getMe ().getOnenote ().getOperations (operation1 .id ).buildRequest ().get ();
205+ assertFalse (operation1 .status .equals ("not started" ));
206+ }
207+
208+ @ Test
209+ public void testMultipartPost (){
210+ String multipartBoundary = "part_" + new BigInteger (130 , new SecureRandom ()).toString ();
211+ try {
212+ String html =
213+ "--" + multipartBoundary + "\r \n " +
214+ "Content-Disposition:form-data; name=\" Presentation\" " + "\r \n " +
215+ "Content-Type: text/html" + "\r \n " +
216+ "\r \n " +
217+ "<!DOCTYPE html>\r \n " +
218+ "<html lang=\" en-US\" >\r \n " +
219+ "<head>\r \n " +
220+ "<title>Test Multipart Page</title>\r \n " +
221+ "<meta name=\" created\" content=\" 2001-01-01T01:01+0100\" >\r \n " +
222+ "</head>\r \n " +
223+ "<body>\r \n " +
224+ "<p>\r \n " +
225+ "<img src=\" name:image\" />\r \n " +
226+ "</p>\r \n " +
227+ "<p>\r \n " +
228+ "<object data=\" name:attachment\" data-attachment=\" document.pdf\" /></p>\r \n " +
229+ "\r \n " +
230+ "</body>\r \n " +
231+ "</html>\r \n " +
232+ "\r \n " +
233+ "--" + multipartBoundary + "\r \n " +
234+ "Content-Disposition:form-data; name=\" image\" \r \n " +
235+ "Content-Type: image/jpeg\r \n \r \n " ;
236+ String doc = "\r \n \r \n " +
237+ "--" + multipartBoundary + "\r \n " +
238+ "Content-Disposition:form-data; name=\" attachment\" \r \n " +
239+ "Content-Type:application/pdf\r \n \r \n " ;
240+
241+ String end = "\r \n \r \n " +
242+ "--" + multipartBoundary + "--" ;
243+
244+ InputStream imageStream = this .getClass ().getClassLoader ().getResourceAsStream ("hamilton.jpg" );
245+ byte [] imgArray = getByteArray (imageStream );
246+
247+ InputStream docStream = this .getClass ().getClassLoader ().getResourceAsStream ("document.pdf" );
248+ byte [] docArray = getByteArray (docStream );
249+
250+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
251+ out .write (html .getBytes ());
252+ out .write (imgArray );
253+ out .write (doc .getBytes ());
254+ out .write (docArray );
255+ out .write (end .getBytes ());
256+
257+ byte finalData [] = out .toByteArray ();
258+
259+ List <Option > options = new ArrayList <Option >();
260+ options .add (new HeaderOption ("Content-Type" , "multipart/form-data; boundary=\" " + multipartBoundary + "\" " ));
261+ OnenotePage newPage = testBase .graphClient .getMe ().getOnenote ().getSections (testSection .id ).getPages ().buildRequest (options ).post (finalData );
262+ assertNotNull (newPage );
263+ } catch (Exception e ) {
264+ Assert .fail ("Unable to write to output stream" );
265+ }
266+ }
267+
268+ @ Test
269+ public void testPatchContent () {
270+ List <OnenotePatchContentCommand > commands = new ArrayList <>();
271+ OnenotePatchContentCommand command = new OnenotePatchContentCommand ();
272+ command .target = "body" ;
273+ command .action = OnenotePatchActionType .Append ;
274+ command .position = OnenotePatchInsertPosition .After ;
275+ command .content = "<img src=\" https://en.wikipedia.org/wiki/File:Alexander_Hamilton_portrait_by_John_Trumbull_1806.jpg\" alt=\" New image from a URL\" />" ;
276+ commands .add (command );
277+ testBase .graphClient .getMe ().getOnenote ().getPages (testPage .id ).getOnenotePatchContent (commands ).buildRequest ().post ();
278+ }
279+
280+ public byte [] getByteArray (InputStream in ) {
281+ try {
282+ ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
283+ int nRead ;
284+ byte [] data = new byte [16384 ];
285+ while ((nRead = in .read (data , 0 , data .length )) != -1 ) {
286+ buffer .write (data , 0 , nRead );
287+ }
288+ buffer .flush ();
289+ return buffer .toByteArray ();
290+ } catch (Exception e ) {
291+ Assert .fail ("Unable to open document" );
292+ }
293+ return null ;
294+ }
295+ }
0 commit comments