forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneNoteTests.java
More file actions
542 lines (477 loc) · 18 KB
/
OneNoteTests.java
File metadata and controls
542 lines (477 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package com.microsoft.graph.functional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import com.microsoft.graph.models.extensions.Multipart;
import com.microsoft.graph.models.extensions.Notebook;
import com.microsoft.graph.models.extensions.OnenoteOperation;
import com.microsoft.graph.models.extensions.OnenotePage;
import com.microsoft.graph.models.extensions.OnenotePagePreview;
import com.microsoft.graph.models.extensions.OnenotePatchContentCommand;
import com.microsoft.graph.models.extensions.OnenoteSection;
import com.microsoft.graph.models.extensions.SectionGroup;
import com.microsoft.graph.models.generated.OnenotePatchActionType;
import com.microsoft.graph.models.generated.OnenotePatchInsertPosition;
import com.microsoft.graph.options.HeaderOption;
import com.microsoft.graph.options.Option;
import com.microsoft.graph.options.QueryOption;
import com.microsoft.graph.requests.extensions.INotebookCollectionPage;
import com.microsoft.graph.requests.extensions.INotebookGetRecentNotebooksCollectionPage;
import com.microsoft.graph.requests.extensions.IOnenotePageCollectionPage;
import com.microsoft.graph.requests.extensions.IOnenotePageCollectionRequest;
import com.microsoft.graph.requests.extensions.IOnenotePageCollectionRequestBuilder;
import com.microsoft.graph.requests.extensions.IOnenoteRequestBuilder;
import com.microsoft.graph.requests.extensions.IOnenoteSectionCollectionPage;
import com.microsoft.graph.requests.extensions.ISectionGroupCollectionPage;
import com.microsoft.graph.requests.extensions.OnenotePageCollectionRequest;
/**
* Tests for OneNote API functionality
*/
@Ignore
public class OneNoteTests {
private IOnenoteRequestBuilder orb;
private Notebook testNotebook;
private Notebook testNotebook2;
private OnenotePage testPage;
private OnenoteSection testSection;
private SectionGroup testSectionGroup2;
private final String HTML_ENCODING= "US-ASCII";
@Before
public void setUp() {
TestBase testBase = new TestBase();
orb = testBase.graphClient.me().onenote();
// Get a pre-made notebook (notebooks cannot currently be deleted)
testNotebook = orb
.notebooks("1-21b1f1d4-7e43-41e8-9acf-7e0b241eeab2")
.buildRequest()
.get();
// Get the first section in the test notebook
testSection = orb
.notebooks(testNotebook.id)
.sections()
.buildRequest()
.get()
.getCurrentPage()
.get(0);
// Get the first page in the test notebook
testPage = orb
.pages()
.buildRequest()
.get()
.getCurrentPage()
.get(0);
// For copy scenarios
testNotebook2 = orb
.notebooks("1-b929a992-3d48-45e4-80db-793995ce452e")
.buildRequest()
.get();
testSectionGroup2 = orb
.notebooks(testNotebook2.id)
.sectionGroups()
.buildRequest()
.get()
.getCurrentPage()
.get(0);
}
/**
* Tests for basic GET requests
*/
@Test
public void testGetNotebookData() {
// Get notebooks
INotebookCollectionPage books = orb
.notebooks()
.buildRequest()
.get();
assertNotNull(books);
// Get pages from the OneNote object
IOnenotePageCollectionPage pages = orb
.pages()
.buildRequest()
.get();
assertNotNull(pages);
// Get sections from a specific notebook
IOnenoteSectionCollectionPage notebookSections = orb
.notebooks(testNotebook.id)
.sections()
.buildRequest()
.get();
assertNotNull(notebookSections);
// Get sections from the OneNote object
IOnenoteSectionCollectionPage sections = orb
.sections()
.buildRequest()
.get();
assertNotNull(sections);
// Get section groups from a specific notebook
ISectionGroupCollectionPage notebookGroups = orb
.notebooks(testNotebook.id)
.sectionGroups()
.buildRequest()
.get();
assertNotNull(notebookGroups);
// Get section groups from the OneNote object
ISectionGroupCollectionPage groups = orb
.sectionGroups()
.buildRequest()
.get();
assertNotNull(groups);
// Get pages from a specific section
IOnenotePageCollectionPage sectionPages = orb
.sections(sections.getCurrentPage().get(0).id)
.pages()
.buildRequest()
.get();
assertNotNull(sectionPages);
}
/**
* Tests for query options
*/
@Test
public void testODataQueries() {
// Test Expand
INotebookCollectionPage books = orb
.notebooks()
.buildRequest()
.expand("sections")
.get();
Notebook book = books.getCurrentPage().get(0);
assertNotNull(book.sections);
// Test Select on notebook
INotebookCollectionPage idBooks = orb
.notebooks()
.buildRequest()
.select("id")
.get();
Notebook idBook = idBooks.getCurrentPage().get(0);
assertNotNull(idBook.id);
// Test Select on page
IOnenotePageCollectionPage pages = orb
.pages()
.buildRequest()
.select("title")
.get();
OnenotePage page = pages.getCurrentPage().get(0);
assertNotNull(page.title);
// Test Count on notebooks
List<Option> options = new ArrayList<Option>();
options.add(new QueryOption("count", "true"));
INotebookCollectionPage countedBooks = orb
.notebooks()
.buildRequest(options)
.get();
assert(countedBooks.getRawObject().get("@odata.count").getAsInt() > 0);
// Test PageLevel on pages
List<QueryOption> pageLevelOptions = new ArrayList<QueryOption>();
pageLevelOptions.add(new QueryOption("pagelevel", "true"));
IOnenotePageCollectionPage pageLevelPages = orb
.sections(testSection.id)
.pages()
.buildRequest(pageLevelOptions)
.get();
assertNotNull(pageLevelPages.getCurrentPage().get(0).level);
}
/**
* Test get recent notebooks
*/
@Test
public void testRecentNotebooks() {
INotebookGetRecentNotebooksCollectionPage books = orb
.notebooks()
.getRecentNotebooks(true)
.buildRequest()
.get();
assertNotNull(books);
INotebookGetRecentNotebooksCollectionPage noPersonalBooks = orb
.notebooks()
.getRecentNotebooks(false)
.buildRequest()
.get();
assertNotNull(noPersonalBooks);
}
/**
* Test get page content
*/
@Test
public void testGetPageContent() {
// Get the page data stream
InputStream pageStream = orb
.pages(testPage.id)
.content()
.buildRequest()
.get();
// Read the stream into a String
BufferedReader r = new BufferedReader(new InputStreamReader(pageStream));
StringBuilder content = new StringBuilder();
String line;
try {
while ((line = r.readLine()) != null) {
content.append(line).append('\n');
}
} catch (Exception e) {
fail("Failed to parse content");
}
assertNotNull(content);
// Hard coding for now since it requires parsing out of the page
String resourceId = "1-ea3e97bf6a5c4dff9e8126014b205298!1-756de5a1-cfe7-4306-bf28-27282cad173b";
// Get the page stream data
InputStream resourceStream = orb
.resources(resourceId)
.content()
.buildRequest()
.get();
// Read the stream into a String
r = new BufferedReader(new InputStreamReader(resourceStream));
content = new StringBuilder();
try {
while ((line = r.readLine()) != null) {
content.append(line).append('\n');
}
} catch (Exception e) {
fail("Failed to parse content");
}
assertNotNull(content);
}
/**
* Test get the page preview
*/
@Test
public void testGetPreview() {
OnenotePagePreview preview = orb
.pages(testPage.id)
.preview()
.buildRequest()
.get();
assertNotNull(preview);
}
/**
* Test posting a page stream to a page
* @throws InterruptedException, UnsupportedEncodingException
*/
@Test
public void testPostToNotebook() throws InterruptedException, UnsupportedEncodingException {
SectionGroup sectionGroupData = new SectionGroup();
// Currently, there is no way to delete sections or section groups, so let's create a random one
int randInt = Integer.MIN_VALUE + (int)(Math.random() * ((Integer.MAX_VALUE- Integer.MIN_VALUE) + 1000));
sectionGroupData.displayName = "Test Section Group" + randInt;
SectionGroup sectionGroup = orb
.notebooks(testNotebook.id)
.sectionGroups()
.buildRequest()
.post(sectionGroupData);
assertEquals(sectionGroupData.displayName, sectionGroup.displayName);
// Create a new random section
OnenoteSection sectionData = new OnenoteSection();
sectionData.displayName = "New Test Section " + randInt;
OnenoteSection section = orb
.notebooks(testNotebook.id)
.sections()
.buildRequest()
.post(sectionData);
assertEquals(sectionData.displayName, section.displayName);
// Test HTML content
String content = "<html><head><title>Test Title</title></head><body>Test body</body></html>";
byte[] pageStream = content.getBytes(HTML_ENCODING);
List<Option> options = new ArrayList<Option>();
options.add(new HeaderOption("Content-Type", "application/xhtml+xml"));
OnenotePage page = orb
.sections(section.id)
.pages()
.buildRequest(options)
.post(pageStream);
assertEquals("Test Title", page.title);
//Ensure that the page exists before we delete it
Thread.sleep(2000);
// Clean up after the test
orb.pages(page.id).buildRequest().delete();
}
/**
* Test copy operations
* TODO: Accessing header data from successful calls is currently not supported
*/
@Ignore
@Test
public void testCopyTo(){
// Test copy to notebook
OnenoteOperation notebookCopy = orb
.sections(testSection.id)
.copyToNotebook(testNotebook2.id, null, null, "TODOsiteCollectionId", "TODOsiteId")
.buildRequest()
.post();
assertNotNull(notebookCopy);
// Test status update
notebookCopy = orb
.operations(notebookCopy.id)
.buildRequest()
.get();
assertFalse(notebookCopy.status.equals("not started"));
// Test copy to section group
OnenoteOperation copySectionGroup = orb
.sections(testSection.id)
.copyToSectionGroup(testSectionGroup2.id, null, null, "TODOsiteCollectionId", "TODOsiteId")
.buildRequest()
.post();
assertNotNull(copySectionGroup);
// Test copy to section
OnenoteSection sectionData = new OnenoteSection();
sectionData.displayName = "Test Copy Section";
OnenoteSection section = orb
.notebooks(testNotebook2.id)
.sections()
.buildRequest()
.post(sectionData);
OnenoteOperation copySection = orb
.pages(testPage.id)
.copyToSection(section.id, null, "TODOsiteCollectionId", "TODOsiteId")
.buildRequest()
.post();
assertNotNull(copySection);
}
/**
* Test posting multipart content to a page
*/
@Test
public void testMultipartPost(){
try {
Multipart multipart = new Multipart();
String htmlContent = "<!DOCTYPE html>\r\n" +
"<html lang=\"en-US\">\r\n" +
"<head>\r\n" +
"<title>Test Multipart Page</title>\r\n" +
"<meta name=\"created\" content=\"2001-01-01T01:01+0100\">\r\n" +
"</head>\r\n" +
"<body>\r\n" +
"<p>\r\n" +
"<img src=\"name:image\" />\r\n" +
"</p>\r\n" +
"<p>\r\n" +
"<object data=\"name:attachment\" data-attachment=\"document.pdf\" /></p>\r\n" +
"\r\n" +
"</body>\r\n" +
"</html>";
File imgFile = new File("src/test/resources/hamilton.jpg");
File pdfFile = new File("src/test/resources/document.pdf");
multipart.addHtmlPart("Presentation", htmlContent.getBytes(HTML_ENCODING));
multipart.addFilePart("hamilton", "image/jpg", imgFile);
multipart.addFilePart("metadata", "application/pdf", pdfFile);
// Add multipart request header
List<Option> options = new ArrayList<Option>();
options.add(multipart.header());
// Post the multipart content
OnenotePage page = orb
.sections(testSection.id)
.pages()
.buildRequest(options)
.post(multipart.content());
assertNotNull(page);
} catch (Exception e) {
fail("Unable to write to output stream");
}
}
/**
* Test posting multipart content to a page
*/
@Test
public void testMultipartPostWithHeadersMap() throws Exception{
Multipart multipart = new Multipart();
String htmlContent = "<!DOCTYPE html>\r\n" +
"<html lang=\"en-US\">\r\n" +
"<head>\r\n" +
"<title>Test Multipart Page</title>\r\n" +
"<meta name=\"created\" content=\"2001-01-01T01:01+0100\">\r\n" +
"</head>\r\n" +
"<body>\r\n" +
"<p>\r\n" +
"<img src=\"name:image\" />\r\n" +
"</p>\r\n" +
"<p>\r\n" +
"<object data=\"name:attachment\" data-attachment=\"document.pdf\" /></p>\r\n" +
"\r\n" +
"</body>\r\n" +
"</html>";
File imgFile = new File("src/test/resources/hamilton.jpg");
File pdfFile = new File("src/test/resources/document.pdf");
Map<String, String> htmlHeaderMap = new HashMap<>();
Map<String, String> contentDispMap = new HashMap<>();
contentDispMap.put("name","Presentation" );
htmlHeaderMap.put("Content-Disposition", Multipart.createContentHeaderValue("form-data", contentDispMap));
htmlHeaderMap.put("Content-Type", Multipart.createContentHeaderValue("text/html", null));
multipart.addPart(htmlHeaderMap, htmlContent.getBytes(HTML_ENCODING));
InputStream fileStream = new FileInputStream(imgFile);
multipart.addFormData("hamilton", "image/jpg", getByteArray(fileStream));
multipart.addFilePart("metadata", "application/pdf", pdfFile);
// Add multipart request header
List<Option> options = new ArrayList<Option>();
options.add(multipart.header());
// Post the multipart content
IOnenotePageCollectionRequestBuilder pageReq = orb
.sections(testSection.id)
.pages();
String expectedRequestUrl = "https://graph.microsoft.com/v1.0/me/onenote/sections/"+testSection.id+"/pages";
assertEquals(expectedRequestUrl, pageReq.getRequestUrl());
IOnenotePageCollectionRequest request = pageReq.buildRequest(options);
assertNotNull(request);
OnenotePageCollectionRequest pageCollectionReq = (OnenotePageCollectionRequest)request;
List<HeaderOption> headeroption = pageCollectionReq.getHeaders();
assertEquals("Content-Type", headeroption.get(0).getName());
String expectedHeaderValue = "multipart/form-data; boundary=\""+multipart.getBoundary()+"\"";
assertEquals(expectedHeaderValue, headeroption.get(0).getValue().toString());
assertNotNull(multipart.content());
OnenotePage page = request.post(multipart.content());
assertNotNull(page);
}
/**
* Test patching a page's content
* TODO: Add an Assert here
*/
@Test
public void testPatchContent() {
List<OnenotePatchContentCommand> commands = new ArrayList<>();
OnenotePatchContentCommand command = new OnenotePatchContentCommand();
command.target = "body";
command.action = OnenotePatchActionType.APPEND;
command.position = OnenotePatchInsertPosition.AFTER;
command.content = "<img src=\"https://en.wikipedia.org/wiki/File:Alexander_Hamilton_portrait_by_John_Trumbull_1806.jpg\" alt=\"New image from a URL\" />";
commands.add(command);
orb
.pages(testPage.id)
.onenotePatchContent(commands)
.buildRequest()
.post();
}
/**
* Helper method to convert an InputStream to a byte[]
* @param in The input stream to convert
* @return The byte[]
*/
public byte[] getByteArray(InputStream in) {
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
} catch (Exception e) {
fail("Unable to open document");
}
return null;
}
}