forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutlookTests.java
More file actions
405 lines (368 loc) · 18 KB
/
Copy pathOutlookTests.java
File metadata and controls
405 lines (368 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
package com.microsoft.graph.functional;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import com.google.gson.JsonObject;
import com.microsoft.graph.tasks.LargeFileUploadTask;
import com.microsoft.graph.tasks.LargeFileUploadResult;
import com.microsoft.graph.tasks.IProgressCallback;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.http.BaseCollectionPage;
import com.microsoft.graph.models.Attachment;
import com.microsoft.graph.models.AttachmentItem;
import com.microsoft.graph.models.Attendee;
import com.microsoft.graph.models.AttendeeBase;
import com.microsoft.graph.models.Contact;
import com.microsoft.graph.models.DateTimeTimeZone;
import com.microsoft.graph.models.EmailAddress;
import com.microsoft.graph.models.Event;
import com.microsoft.graph.models.FileAttachment;
import com.microsoft.graph.models.ItemAttachment;
import com.microsoft.graph.models.ItemBody;
import com.microsoft.graph.models.MeetingTimeSuggestionsResult;
import com.microsoft.graph.models.Message;
import com.microsoft.graph.models.Recipient;
import com.microsoft.graph.models.SingleValueLegacyExtendedProperty;
import com.microsoft.graph.models.UploadSession;
import com.microsoft.graph.models.User;
import com.microsoft.graph.models.AttachmentType;
import com.microsoft.graph.models.BodyType;
import com.microsoft.graph.requests.SingleValueLegacyExtendedPropertyCollectionRequestBuilder;
import com.microsoft.graph.requests.SingleValueLegacyExtendedPropertyCollectionResponse;
import com.microsoft.graph.requests.AttachmentCollectionResponse;
import com.microsoft.graph.requests.AttachmentCollectionPage;
import com.microsoft.graph.requests.MessageCollectionPage;
import com.microsoft.graph.requests.EventCollectionPage;
import com.microsoft.graph.requests.UserCollectionPage;
import com.microsoft.graph.requests.SingleValueLegacyExtendedPropertyCollectionPage;
import com.microsoft.graph.models.UserSendMailParameterSet;
import com.microsoft.graph.options.HeaderOption;
import com.microsoft.graph.models.AttachmentCreateUploadSessionParameterSet;
import com.microsoft.graph.models.UserFindMeetingTimesParameterSet;
import com.microsoft.graph.serializer.DefaultSerializer;
import com.microsoft.graph.logger.ILogger;
import com.microsoft.graph.models.CalendarGetScheduleParameterSet;
import com.microsoft.graph.requests.CalendarGetScheduleCollectionPage;
@Disabled
public class OutlookTests {
@Test
public void testSendMail() {
TestBase testBase = new TestBase();
User me = testBase.graphClient.me().buildRequest().get();
Recipient r = new Recipient();
EmailAddress address = new EmailAddress();
address.address = me.mail;
r.emailAddress = address;
Message message = new Message();
message.subject = "Test E-Mail";
message.from = r;
ArrayList<Recipient> recipients = new ArrayList<Recipient>();
recipients.add(r);
message.toRecipients = recipients;
testBase.graphClient.me().sendMail(UserSendMailParameterSet.newBuilder().withMessage(message).withSaveToSentItems(true).build()).buildRequest().post();
}
@Test
public void testGetFindMeetingTimes() {
TestBase testBase = new TestBase();
// Get the first user in the tenant
User me = testBase.graphClient.me().buildRequest().get();
UserCollectionPage users = testBase.graphClient.users().buildRequest().get();
User tenantUser = users.getCurrentPage().get(0);
//Ensure that the user grabbed is not the logged-in user
if (tenantUser.mail.equals(me.mail)) {
tenantUser = users.getCurrentPage().get(1);
}
ArrayList<AttendeeBase> attendees = new ArrayList<AttendeeBase>();
AttendeeBase attendeeBase = new AttendeeBase();
EmailAddress email = new EmailAddress();
email.address = tenantUser.mail;
attendeeBase.emailAddress = email;
attendees.add(attendeeBase);
try {
DatatypeFactory.newInstance().newDuration("PT30M");
Duration duration = DatatypeFactory.newInstance().newDuration("PT30M");
MeetingTimeSuggestionsResult result = testBase.graphClient.me()
.findMeetingTimes(UserFindMeetingTimesParameterSet.newBuilder()
.withAttendees(attendees)
.withMeetingDuration(duration)
.withMaxCandidates(10)
.withReturnSuggestionReasons(true)
.withMinimumAttendeePercentage(10.0)
.build())
.buildRequest()
.post();
assertNotNull(result);
} catch (Exception e) {
fail("Duration could not be created from String");
}
}
@Test
public void testSendDraft() {
TestBase testBase = new TestBase();
//Attempt to identify the sent message via randomly generated subject
String draftSubject = "Draft Test Message " + Double.toString(Math.random()*1000);
Message newMessage = createDraftMessage(testBase, draftSubject);
//Send the drafted message
testBase.graphClient.me().mailFolders("Drafts").messages(newMessage.id).send().buildRequest().post();
//Check that the sent message exists on the server
MessageCollectionPage mcp = testBase.graphClient.me().messages().buildRequest().filter("subject eq '" + draftSubject + "'").get();
assertFalse(mcp.getCurrentPage().isEmpty());
}
private Message createDraftMessage(TestBase testBase, String draftSubject) {
User me = testBase.graphClient.me().buildRequest().get();
Recipient r = new Recipient();
EmailAddress address = new EmailAddress();
address.address = me.mail;
r.emailAddress = address;
Message message = new Message();
message.subject = draftSubject;
ArrayList<Recipient> recipients = new ArrayList<Recipient>();
recipients.add(r);
message.toRecipients = recipients;
message.isDraft = true;
//Save the message as a draft
return testBase.graphClient.me().messages().buildRequest().post(message);
}
@Test
public void sendEmailWithAttachment() throws Exception{
TestBase testBase = new TestBase();
Message message = getMessage();
message.hasAttachments = true;
AttachmentCollectionResponse response = new AttachmentCollectionResponse();
response.value = Arrays.asList(getFileAttachment(),getItemAttachmentWithEvent(),getItemAttachmentWithContact());
message.attachments = new AttachmentCollectionPage(response, null);
testBase.graphClient.me().sendMail(UserSendMailParameterSet.newBuilder().withMessage(message).withSaveToSentItems(true).build()).buildRequest().post();
}
@Test
public void uploadEmailAsDraftWithAttachmentThenSend() {
TestBase testBase = new TestBase();
Message message = getMessage();
Message m = testBase.graphClient.me().messages().buildRequest().post(message);
assertNotNull(m);
testBase.graphClient.me().messages(m.id).send().buildRequest().post();
}
@Test
public void sendEventWithAttachment() throws Exception{
TestBase testBase = new TestBase();
Event event = getEvent();
event.body = getItemBody();
event.hasAttachments = true;
AttachmentCollectionResponse response = new AttachmentCollectionResponse();
response.value = Arrays.asList(getFileAttachment(),getItemAttachmentWithContact());
event.attachments = new AttachmentCollectionPage(response, null);
Event eventResponse = testBase.graphClient.me().events().buildRequest().post(event);
assertNotNull(eventResponse);
}
private ItemBody getItemBody() {
ItemBody itemBody = new ItemBody();
itemBody.content = "<html><head></head><body> test body </body></html>";
itemBody.contentType = BodyType.HTML;
return itemBody;
}
public Message getMessage() {
Message message = new Message();
java.util.List<String> emails = Arrays.asList("test_email@test_domain.com");
java.util.List<Recipient> listReceipient = new ArrayList<>();
for(String email : emails) {
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = email;
Recipient recipient = new Recipient();
recipient.emailAddress = emailAddress;
listReceipient.add(recipient);
}
message.body = getItemBody();
message.toRecipients = listReceipient;
message.subject = "Test Message";
message.id = "1234";
return message;
}
private FileAttachment getFileAttachment() throws Exception{
FileAttachment fileAttachment = new FileAttachment();
fileAttachment.name = "document.pdf";
File pdfFile = new File("src/test/resources/document.pdf");
InputStream fileStream = new FileInputStream(pdfFile);
fileAttachment.contentBytes = OutlookTests.getByteArray(fileStream);
fileAttachment.oDataType = "#microsoft.graph.fileAttachment";
fileAttachment.id="54321";
return fileAttachment;
}
private ItemAttachment getItemAttachmentWithEvent() {
ItemAttachment itemAttachmentEvent = new ItemAttachment();
itemAttachmentEvent.oDataType = "#microsoft.graph.itemAttachment";
itemAttachmentEvent.name = "event name";
itemAttachmentEvent.item = getEvent();
itemAttachmentEvent.item.oDataType = "microsoft.graph.event";
return itemAttachmentEvent;
}
private ItemAttachment getItemAttachmentWithContact() {
Contact contact = new Contact();
contact.displayName = "displayname";
contact.mobilePhone="123456890";
ItemAttachment itemAttachmentContact = new ItemAttachment();
itemAttachmentContact.oDataType = "#microsoft.graph.itemAttachment";
itemAttachmentContact.name = "Attachment name";
itemAttachmentContact.item = contact;
itemAttachmentContact.item.oDataType = "microsoft.graph.contact";
return itemAttachmentContact;
}
private Event getEvent() {
Event event = new Event();
java.util.List<String> emails = Arrays.asList("test_email@test_domain.com");
java.util.List<Attendee> attendees = new ArrayList<>();
for(String email : emails) {
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = email;
Attendee attendee = new Attendee();
attendee.emailAddress = emailAddress;
attendees.add(attendee);
}
DateTimeTimeZone start = new DateTimeTimeZone();
start.timeZone = "UTC";
start.dateTime = "2018-10-16T06:15:26.544Z";
DateTimeTimeZone end = new DateTimeTimeZone();
end.timeZone = "UTC";
end.dateTime = "2018-11-18T07:30:26.544Z";
event.start = start;
event.end = end;
event.subject = "Test Event Subject";
event.id = "1234";
return event;
}
public static 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) {
e.printStackTrace();
}
return null;
}
final IProgressCallback callback = new IProgressCallback () {
@Override
public void progress(final long current, final long max) {
//Check progress
}
};
@Test
public void testSendDraftWithLargeAttachements() throws FileNotFoundException, IOException {
TestBase testBase = new TestBase();
//Attempt to identify the sent message via randomly generated subject
String draftSubject = "Draft Test Message " + Double.toString(Math.random()*1000);
Message newMessage = createDraftMessage(testBase, draftSubject);
File file = new File("src/test/resources/largefile10M.blob");
AttachmentItem attachmentItem = new AttachmentItem();
attachmentItem.attachmentType = AttachmentType.FILE;
attachmentItem.name = file.getName();
attachmentItem.size = file.length();
attachmentItem.contentType = "application/octet-stream";
InputStream fileStream = OutlookTests.class.getClassLoader().getResourceAsStream("largefile10M.blob");
long streamSize = attachmentItem.size;
UploadSession uploadSession = testBase.graphClient.me()
.messages(newMessage.id)
.attachments()
.createUploadSession(AttachmentCreateUploadSessionParameterSet.newBuilder().withAttachmentItem(attachmentItem).build())
.buildRequest()
.post();
LargeFileUploadTask<AttachmentItem> chunkedUploadProvider = new LargeFileUploadTask<>(uploadSession, testBase.graphClient, fileStream,
streamSize, AttachmentItem.class);
// Do the upload
final LargeFileUploadResult<AttachmentItem> result = chunkedUploadProvider.upload(0, null, callback);
assertNotNull(result);
//Send the drafted message
testBase.graphClient.me().mailFolders("Drafts").messages(newMessage.id).send().buildRequest().post();
}
@Test
public void testSingleValuesExtendedProperties() {
final TestBase testBase = new TestBase();
final EventCollectionPage arrangePage = testBase.graphClient.me().events().buildRequest().top(1).get();
final String eventId = arrangePage.getCurrentPage().get(0).id;
final Event updatedEvent = new Event();
final String uuid = UUID.randomUUID().toString();
final SingleValueLegacyExtendedProperty prop = new SingleValueLegacyExtendedProperty();
prop.id = "String {"+uuid+"} Name fun";
prop.value = "some value";
final SingleValueLegacyExtendedPropertyCollectionResponse response = new SingleValueLegacyExtendedPropertyCollectionResponse();
response.value = new ArrayList<SingleValueLegacyExtendedProperty>();
response.value.add(prop);
updatedEvent.singleValueExtendedProperties = new SingleValueLegacyExtendedPropertyCollectionPage(response, new SingleValueLegacyExtendedPropertyCollectionRequestBuilder(null, null, null));
testBase.graphClient.me().events(eventId).buildRequest().patch(updatedEvent);
final EventCollectionPage page = testBase.graphClient.me()
.events()
.buildRequest()
.expand("singleValueExtendedProperties")
.top(1)
// .filter("singleValueExtendedProperties/Any(ep: ep/id eq '"+prop.id+"' and ep/value eq '"+prop.value+"')")
.get();
assertNotNull(page);
final List<Event> events = page.getCurrentPage();
assertTrue(events.size() == 1);
assertNotNull(events.get(0).singleValueExtendedProperties);
}
@Test
public void testAttachments() throws Exception {
final TestBase testBase = new TestBase();
final AttachmentCollectionPage page = testBase.graphClient
.me()
.messages("AAMkADc5NmMyYjUxLTQ0ZDEtNGM3Yi1iY2JkLTgyZWYwZjgzNDI3NwBGAAAAAADVwiXSJFUqQrTdi_SlUV7QBwCD0ThbORwuS5hfVs_PIdoqAAAAAAENAACD0ThbORwuS5hfVs_PIdoqAAZ6u3D_AAA=")
.attachments()
.buildRequest()
.get();
final List<Attachment> attchs = page.getCurrentPage();
assertEquals(1, attchs.size());
assertTrue(attchs.get(0) instanceof FileAttachment);
}
@Test
public void testGetSchedule() throws Exception {
final TestBase testBase = new TestBase();
final User me = testBase.graphClient.me().buildRequest().select("userPrincipalName").get();
final UserCollectionPage usersPage = testBase.graphClient
.users()
.buildRequest(new HeaderOption("ConsistencyLevel", "eventual"))
.top(1)
.select("userPrincipalName")
.filter("userPrincipalName ne '" + me.userPrincipalName + "'")
.count()
.get();
final List<User> users = usersPage.getCurrentPage();
final DateTimeTimeZone endTime = new DateTimeTimeZone();
endTime.dateTime = OffsetDateTime.now().plusDays(1).plusHours(8).toLocalDateTime().toString();
endTime.timeZone = "Eastern Standard Time";
final DateTimeTimeZone startTime = new DateTimeTimeZone();
startTime.dateTime = OffsetDateTime.now().plusDays(1).toLocalDateTime().toString();
startTime.timeZone = "Eastern Standard Time";
final CalendarGetScheduleParameterSet paramSet = CalendarGetScheduleParameterSet
.newBuilder()
.withSchedules(Arrays.asList(me.userPrincipalName, users.get(0).userPrincipalName))
.withEndTime(endTime)
.withStartTime(startTime)
.withAvailabilityViewInterval(60)
.build();
final CalendarGetScheduleCollectionPage resultPage = testBase.graphClient.me().calendar().getSchedule(paramSet)
.buildRequest().post();
assertNotNull(resultPage);
}
}