66import java .io .ByteArrayOutputStream ;
77import java .io .File ;
88import java .io .FileInputStream ;
9+ import java .io .FileNotFoundException ;
10+ import java .io .IOException ;
911import java .io .InputStream ;
1012import java .util .ArrayList ;
1113import java .util .Arrays ;
14+ import java .util .List ;
1215
1316import javax .xml .datatype .DatatypeFactory ;
1417import javax .xml .datatype .Duration ;
1720import org .junit .Ignore ;
1821import org .junit .Test ;
1922
23+ import com .microsoft .graph .concurrency .ChunkedUploadProvider ;
24+ import com .microsoft .graph .concurrency .IProgressCallback ;
25+ import com .microsoft .graph .core .ClientException ;
26+ import com .microsoft .graph .models .extensions .AttachmentItem ;
2027import com .microsoft .graph .models .extensions .Attendee ;
2128import com .microsoft .graph .models .extensions .AttendeeBase ;
2229import com .microsoft .graph .models .extensions .Contact ;
2936import com .microsoft .graph .models .extensions .MeetingTimeSuggestionsResult ;
3037import com .microsoft .graph .models .extensions .Message ;
3138import com .microsoft .graph .models .extensions .Recipient ;
39+ import com .microsoft .graph .models .extensions .UploadSession ;
3240import com .microsoft .graph .models .extensions .User ;
41+ import com .microsoft .graph .models .generated .AttachmentType ;
3342import com .microsoft .graph .models .generated .BodyType ;
3443import com .microsoft .graph .options .QueryOption ;
3544import com .microsoft .graph .requests .extensions .AttachmentCollectionPage ;
@@ -94,8 +103,20 @@ public void testSendDraft() {
94103
95104 //Attempt to identify the sent message via randomly generated subject
96105 String draftSubject = "Draft Test Message " + Double .toString (Math .random ()*1000 );
106+ Message newMessage = createDraftMessage (testBase , draftSubject );
107+
108+ //Send the drafted message
109+ testBase .graphClient .me ().mailFolders ("Drafts" ).messages (newMessage .id ).send ().buildRequest ().post ();
97110
98- User me = testBase .graphClient .me ().buildRequest ().get ();
111+ java .util .List <QueryOption > options = new ArrayList <QueryOption >();
112+ QueryOption o = new QueryOption ("$filter" , "subject eq '" + draftSubject + "'" );
113+ options .add (o );
114+ //Check that the sent message exists on the server
115+ IMessageCollectionPage mcp = testBase .graphClient .me ().messages ().buildRequest (options ).get ();
116+ assertFalse (mcp .getCurrentPage ().isEmpty ());
117+ }
118+ private Message createDraftMessage (TestBase testBase , String draftSubject ) {
119+ User me = testBase .graphClient .me ().buildRequest ().get ();
99120 Recipient r = new Recipient ();
100121 EmailAddress address = new EmailAddress ();
101122 address .address = me .mail ;
@@ -108,18 +129,8 @@ public void testSendDraft() {
108129 message .isDraft = true ;
109130
110131 //Save the message as a draft
111- Message newMessage = testBase .graphClient .me ().messages ().buildRequest ().post (message );
112- //Send the drafted message
113- testBase .graphClient .me ().mailFolders ("Drafts" ).messages (newMessage .id ).send ().buildRequest ().post ();
114-
115- java .util .List <QueryOption > options = new ArrayList <QueryOption >();
116- QueryOption o = new QueryOption ("$filter" , "subject eq '" + draftSubject + "'" );
117- options .add (o );
118- //Check that the sent message exists on the server
119- IMessageCollectionPage mcp = testBase .graphClient .me ().messages ().buildRequest (options ).get ();
120- assertFalse (mcp .getCurrentPage ().isEmpty ());
121- }
122-
132+ return testBase .graphClient .me ().messages ().buildRequest ().post (message );
133+ }
123134 @ Test
124135 public void sendEmailWithAttachment () throws Exception {
125136 TestBase testBase = new TestBase ();
@@ -249,4 +260,58 @@ public static byte[] getByteArray(InputStream in) {
249260 }
250261 return null ;
251262 }
263+
264+ IProgressCallback <AttachmentItem > callback = new IProgressCallback <AttachmentItem > () {
265+ @ Override
266+ public void progress (final long current , final long max ) {
267+ //Check progress
268+ }
269+ @ Override
270+ public void success (final AttachmentItem result ) {
271+ //Handle the successful response
272+ Assert .assertNotNull (result );
273+ }
274+
275+ @ Override
276+ public void failure (final ClientException ex ) {
277+ //Handle the failed upload
278+ Assert .fail ("Upload session failed" );
279+ }
280+ };
281+ @ Test
282+ public void testSendDraftWithLargeAttachements () throws FileNotFoundException , IOException {
283+ TestBase testBase = new TestBase ();
284+
285+ //Attempt to identify the sent message via randomly generated subject
286+ String draftSubject = "Draft Test Message " + Double .toString (Math .random ()*1000 );
287+ Message newMessage = createDraftMessage (testBase , draftSubject );
288+
289+ File file = new File ("src/test/resources/largefile10M.blob" );
290+
291+ AttachmentItem attachmentItem = new AttachmentItem ();
292+ attachmentItem .attachmentType = AttachmentType .FILE ;
293+ attachmentItem .name = file .getName ();
294+ attachmentItem .size = file .length ();
295+ attachmentItem .contentType = "application/octet-stream" ;
296+
297+ InputStream fileStream = OutlookTests .class .getClassLoader ().getResourceAsStream ("largefile10M.blob" );
298+
299+ long streamSize = attachmentItem .size ;
300+
301+ UploadSession uploadSession = testBase .graphClient .me ()
302+ .messages (newMessage .id )
303+ .attachments ()
304+ .createUploadSession (attachmentItem )
305+ .buildRequest ()
306+ .post ();
307+
308+ ChunkedUploadProvider <AttachmentItem > chunkedUploadProvider = new ChunkedUploadProvider <>(uploadSession , testBase .graphClient , fileStream ,
309+ streamSize , AttachmentItem .class );
310+
311+ // Do the upload
312+ chunkedUploadProvider .upload (callback );
313+
314+ //Send the drafted message
315+ testBase .graphClient .me ().mailFolders ("Drafts" ).messages (newMessage .id ).send ().buildRequest ().post ();
316+ }
252317}
0 commit comments