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 .IOnenoteResourceCollectionPage ;
10+ import com .microsoft .graph .extensions .IOnenoteSectionCollectionPage ;
11+ import com .microsoft .graph .extensions .ISectionGroupCollectionPage ;
12+ import com .microsoft .graph .extensions .Notebook ;
13+ import com .microsoft .graph .extensions .Onenote ;
14+ import com .microsoft .graph .extensions .OnenoteOperation ;
15+ import com .microsoft .graph .extensions .OnenotePage ;
16+ import com .microsoft .graph .extensions .OnenotePagePreview ;
17+ import com .microsoft .graph .extensions .OnenoteResource ;
18+ import com .microsoft .graph .extensions .OnenoteSection ;
19+ import com .microsoft .graph .extensions .SectionGroup ;
20+ import com .microsoft .graph .extensions .User ;
21+ import com .microsoft .graph .options .HeaderOption ;
22+ import com .microsoft .graph .options .Option ;
23+ import com .microsoft .graph .options .QueryOption ;
24+
25+ import org .junit .*;
26+
27+ import java .io .BufferedReader ;
28+ import java .io .ByteArrayInputStream ;
29+ import java .io .InputStream ;
30+ import java .io .InputStreamReader ;
31+ import java .math .BigInteger ;
32+ import java .nio .charset .StandardCharsets ;
33+ import java .security .SecureRandom ;
34+ import java .util .ArrayList ;
35+ import java .util .List ;
36+
37+ //@Suppress
38+ public class OneNoteTests extends AndroidTestCase {
39+
40+ private TestBase testBase ;
41+ private Notebook testNotebook ;
42+ private OnenoteSection testSection ;
43+ private OnenotePage testPage ;
44+ private Notebook testNotebook2 ;
45+ private SectionGroup testSectionGroup2 ;
46+
47+ @ Before
48+ public void setUp () {
49+ testBase = new TestBase ();
50+ testNotebook = testBase .graphClient .getMe ().getOnenote ().getNotebooks ("1-525fe350-0199-4c02-879d-e5b142ae8632" ).buildRequest ().get ();
51+ 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 );
53+
54+ // For copy scenarios
55+ testNotebook2 = testBase .graphClient .getMe ().getOnenote ().getNotebooks ("1-491df90f-b45b-477f-b297-032f000e6f1e" ).buildRequest ().get ();
56+ testSectionGroup2 = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook2 .id ).getSectionGroups ().buildRequest ().get ().getCurrentPage ().get (0 );
57+ }
58+
59+ @ Test
60+ public void testGetNotebookData () {
61+ INotebookCollectionPage books = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().buildRequest ().get ();
62+ assertNotNull (books );
63+
64+ // Get pages from the OneNote object
65+ IOnenotePageCollectionPage pages = testBase .graphClient .getMe ().getOnenote ().getPages ().buildRequest ().get ();
66+ assertNotNull (pages );
67+
68+ // Get sections from a specific notebook
69+ IOnenoteSectionCollectionPage notebookSections = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSections ().buildRequest ().get ();
70+ assertNotNull (notebookSections );
71+
72+ // Get sections from the OneNote object
73+ IOnenoteSectionCollectionPage sections = testBase .graphClient .getMe ().getOnenote ().getSections ().buildRequest ().get ();
74+ assertNotNull (sections );
75+
76+ // Get section groups from a specific notebook
77+ ISectionGroupCollectionPage notebookGroups = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSectionGroups ().buildRequest ().get ();
78+ assertNotNull (notebookGroups );
79+
80+ // Get section groups from the OneNote object
81+ ISectionGroupCollectionPage groups = testBase .graphClient .getMe ().getOnenote ().getSectionGroups ().buildRequest ().get ();
82+ assertNotNull (groups );
83+
84+ // Get pages from a specific section
85+ IOnenotePageCollectionPage sectionPages = testBase .graphClient .getMe ().getOnenote ().getSections (sections .getCurrentPage ().get (0 ).id ).getPages ().buildRequest ().get ();
86+ assertNotNull (sectionPages );
87+ }
88+
89+ @ Test
90+ public void testODataQueries () {
91+ INotebookCollectionPage books = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().buildRequest ().expand ("sections" ).get ();
92+ Notebook book = books .getCurrentPage ().get (0 );
93+ assertNotNull (book .sections );
94+
95+ INotebookCollectionPage idBooks = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().buildRequest ().select ("id" ).get ();
96+ Notebook idBook = books .getCurrentPage ().get (0 );
97+ assertNotNull (idBook .id );
98+
99+ IOnenotePageCollectionPage pages = testBase .graphClient .getMe ().getOnenote ().getPages ().buildRequest ().select ("title" ).get ();
100+ OnenotePage page = pages .getCurrentPage ().get (0 );
101+ assertNotNull (page .title );
102+
103+ List <Option > options = new ArrayList <Option >();
104+ options .add (new QueryOption ("count" , "true" ));
105+ INotebookCollectionPage countedBooks = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().buildRequest (options ).get ();
106+ assert (countedBooks .getRawObject ().get ("@odata.count" ).getAsInt () > 0 );
107+
108+ List <Option > pageLevelOptions = new ArrayList <Option >();
109+ pageLevelOptions .add (new QueryOption ("pagelevel" , "true" ));
110+ IOnenotePageCollectionPage pageLevelPages = testBase .graphClient .getMe ().getOnenote ().getSections (testSection .id ).getPages ().buildRequest (pageLevelOptions ).get ();
111+ assertNotNull (pageLevelPages .getCurrentPage ().get (0 ).level );
112+ }
113+
114+ @ Test
115+ public void testRecentNotebooks () {
116+ INotebookGetRecentNotebooksCollectionPage books = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().getGetRecentNotebooks (true ).buildRequest ().get ();
117+ assertNotNull (books );
118+
119+ INotebookGetRecentNotebooksCollectionPage noPersonalBooks = testBase .graphClient .getMe ().getOnenote ().getNotebooks ().getGetRecentNotebooks (false ).buildRequest ().get ();
120+ assertNotNull (noPersonalBooks );
121+ }
122+
123+ @ Test
124+ public void testGetPageContent () {
125+ InputStream pageStream = testBase .graphClient .getMe ().getOnenote ().getPages (testPage .id ).getContent ().buildRequest ().get ();
126+
127+ BufferedReader r = new BufferedReader (new InputStreamReader (pageStream ));
128+ StringBuilder content = new StringBuilder ();
129+ String line ;
130+
131+ try {
132+ while ((line = r .readLine ()) != null ) {
133+ content .append (line ).append ('\n' );
134+ }
135+ } catch (Exception e ) {
136+ assertEquals (0 ,1 );
137+ }
138+ assertNotNull (content );
139+
140+ //Hardcoding for now since it requires parsing out of the page
141+ String resourceId = "1-ff7e33cbba1a4cda8f7313abe32420a1!1-db1705d5-fc30-4b3b-8dfb-191981428c65" ;
142+ InputStream resourceStream = testBase .graphClient .getMe ().getOnenote ().getResources (resourceId ).getContent ().buildRequest ().get ();
143+
144+ r = new BufferedReader (new InputStreamReader (resourceStream ));
145+ content = new StringBuilder ();
146+
147+ try {
148+ while ((line = r .readLine ()) != null ) {
149+ content .append (line ).append ('\n' );
150+ }
151+ } catch (Exception e ) {
152+ assertEquals (0 ,1 );
153+ }
154+ assertNotNull (content );
155+
156+ }
157+
158+ @ Test
159+ public void testGetPreview () {
160+ OnenotePagePreview preview = testBase .graphClient .getMe ().getOnenote ().getPages (testPage .id ).getPreview ().buildRequest ().get ();
161+ assertNotNull (preview );
162+ }
163+
164+ @ Test
165+ // Currently there is no way to delete notebooks, sections, or pages
166+ public void testPostToNotebook () {
167+ SectionGroup sectionGroup = new SectionGroup ();
168+ sectionGroup .displayName = "Test Section Group" ;
169+
170+ SectionGroup newSectionGroup = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSectionGroups ().buildRequest ().post (sectionGroup );
171+ assertEquals (sectionGroup .displayName , newSectionGroup .displayName );
172+
173+ OnenoteSection section = new OnenoteSection ();
174+ section .displayName = "New Test Section" ;
175+
176+ OnenoteSection newSection = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook .id ).getSections ().buildRequest ().post (section );
177+ assertEquals (section .displayName , newSection .displayName );
178+
179+ String content = "<html><head><title>Test Title</title></head><body>Test body</body></html>" ;
180+
181+ byte [] pageStream = content .getBytes ();
182+ List <Option > options = new ArrayList <Option >();
183+ options .add (new HeaderOption ("Content-Type" , "application/xhtml+xml" ));
184+ OnenotePage newPage = testBase .graphClient .getMe ().getOnenote ().getSections (newSection .id ).getPages ().buildRequest (options ).post (pageStream );
185+ assertEquals ("Test Title" , newPage .title );
186+
187+ testBase .graphClient .getMe ().getOnenote ().getPages (newPage .id ).buildRequest ().delete ();
188+
189+ // testBase.graphClient.getMe().getOnenote().getSections(newSection.id).buildRequest().delete();
190+ }
191+
192+ @ Test
193+ public void testCopyTo (){
194+ OnenoteOperation operation1 = testBase .graphClient .getMe ().getOnenote ().getSections (testSection .id ).getCopyToNotebook (testNotebook2 .id , null , null ).buildRequest ().post ();
195+ assertNotNull (operation1 );
196+
197+ OnenoteOperation operation = testBase .graphClient .getMe ().getOnenote ().getSections (testSection .id ).getCopyToSectionGroup (testSectionGroup2 .id , null , null ).buildRequest ().post ();
198+ assertNotNull (operation );
199+
200+ OnenoteSection section = new OnenoteSection ();
201+ section .displayName = "Test Copy Section" ;
202+ OnenoteSection newSection = testBase .graphClient .getMe ().getOnenote ().getNotebooks (testNotebook2 .id ).getSections ().buildRequest ().post (section );
203+ operation = testBase .graphClient .getMe ().getOnenote ().getPages (testPage .id ).getCopyToSection (newSection .id , null ).buildRequest ().post ();
204+ assertNotNull (operation );
205+
206+ operation = testBase .graphClient .getMe ().getOnenote ().getOperations (operation1 .id ).buildRequest ().get ();
207+ assertFalse (operation1 .status .equals ("not started" ));
208+ }
209+
210+ @ Test
211+ public void testMultipartMimetype (){
212+ 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 );
251+ }
252+ }
0 commit comments