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
252 lines (225 loc) · 9.62 KB
/
Copy pathOutlookTests.java
File metadata and controls
252 lines (225 loc) · 9.62 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
package com.microsoft.graph.functional;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import com.microsoft.graph.models.extensions.Attendee;
import com.microsoft.graph.models.extensions.AttendeeBase;
import com.microsoft.graph.models.extensions.Contact;
import com.microsoft.graph.models.extensions.DateTimeTimeZone;
import com.microsoft.graph.models.extensions.EmailAddress;
import com.microsoft.graph.models.extensions.Event;
import com.microsoft.graph.models.extensions.FileAttachment;
import com.microsoft.graph.models.extensions.ItemAttachment;
import com.microsoft.graph.models.extensions.ItemBody;
import com.microsoft.graph.models.extensions.MeetingTimeSuggestionsResult;
import com.microsoft.graph.models.extensions.Message;
import com.microsoft.graph.models.extensions.Recipient;
import com.microsoft.graph.models.extensions.User;
import com.microsoft.graph.models.generated.BodyType;
import com.microsoft.graph.options.QueryOption;
import com.microsoft.graph.requests.extensions.AttachmentCollectionPage;
import com.microsoft.graph.requests.extensions.IMessageCollectionPage;
import com.microsoft.graph.requests.extensions.IUserCollectionPage;
import com.microsoft.graph.requests.extensions.AttachmentCollectionResponse;
@Ignore
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(message, true).buildRequest().post();
}
@Test
public void testGetFindMeetingTimes() {
TestBase testBase = new TestBase();
// Get the first user in the tenant
User me = testBase.graphClient.me().buildRequest().get();
IUserCollectionPage 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(attendees, null, null, duration, 10, true, false, 10.0).buildRequest().post();
assertNotNull(result);
} catch (Exception e) {
Assert.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);
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
Message newMessage = testBase.graphClient.me().messages().buildRequest().post(message);
//Send the drafted message
testBase.graphClient.me().mailFolders("Drafts").messages(newMessage.id).send().buildRequest().post();
java.util.List<QueryOption> options = new ArrayList<QueryOption>();
QueryOption o = new QueryOption("$filter", "subject eq '" + draftSubject + "'");
options.add(o);
//Check that the sent message exists on the server
IMessageCollectionPage mcp = testBase.graphClient.me().messages().buildRequest(options).get();
assertFalse(mcp.getCurrentPage().isEmpty());
}
@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(message, true).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;
}
}