Skip to content

Commit 72b58f6

Browse files
author
Nakul Sabharwal
committed
Attachment serializers, tests and @expose attachments attribute.
1 parent abd03ca commit 72b58f6

File tree

6 files changed

+408
-1
lines changed

6 files changed

+408
-1
lines changed

src/main/java/com/microsoft/graph/models/extensions/Event.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
package com.microsoft.graph.models.extensions;
66

7+
import com.google.gson.annotations.Expose;
8+
import com.google.gson.annotations.SerializedName;
79
import com.microsoft.graph.models.generated.BaseEvent;
10+
import com.microsoft.graph.requests.extensions.AttachmentCollectionPage;
811

912
// This file is available for extending, afterwards please submit a pull request.
1013

1114
/**
1215
* The class for the Event.
1316
*/
1417
public class Event extends BaseEvent {
15-
18+
@SerializedName("attachments")
19+
@Expose
20+
public AttachmentCollectionPage attachments;
1621
}

src/main/java/com/microsoft/graph/models/extensions/Message.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
package com.microsoft.graph.models.extensions;
66

7+
import com.google.gson.annotations.Expose;
8+
import com.google.gson.annotations.SerializedName;
79
import com.microsoft.graph.models.generated.BaseMessage;
10+
import com.microsoft.graph.requests.extensions.AttachmentCollectionPage;
811

912
// This file is available for extending, afterwards please submit a pull request.
1013

1114
/**
1215
* The class for the Message.
1316
*/
1417
public class Message extends BaseMessage {
18+
@SerializedName("attachments")
19+
@Expose
20+
public AttachmentCollectionPage attachments;
1521

1622
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2017 Microsoft Corporation
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
// ------------------------------------------------------------------------------
22+
23+
package com.microsoft.graph.serializer;
24+
import java.util.Arrays;
25+
import java.util.Calendar;
26+
import java.util.List;
27+
28+
import com.google.gson.JsonArray;
29+
import com.google.gson.JsonElement;
30+
import com.google.gson.JsonObject;
31+
import com.google.gson.JsonParseException;
32+
import com.google.gson.JsonParser;
33+
import com.microsoft.graph.logger.ILogger;
34+
import com.microsoft.graph.models.extensions.Attachment;
35+
import com.microsoft.graph.models.extensions.Contact;
36+
import com.microsoft.graph.models.extensions.Event;
37+
import com.microsoft.graph.models.extensions.FileAttachment;
38+
import com.microsoft.graph.models.extensions.ItemAttachment;
39+
import com.microsoft.graph.models.extensions.Message;
40+
import com.microsoft.graph.models.extensions.ReferenceAttachment;
41+
import com.microsoft.graph.requests.extensions.AttachmentCollectionPage;
42+
import com.microsoft.graph.requests.generated.BaseAttachmentCollectionResponse;
43+
44+
public class AttachmentCollectionPageSerializer {
45+
46+
private static JsonParser parser;
47+
private static DefaultSerializer serializer;
48+
49+
/**
50+
* Not available for instantiation
51+
*/
52+
private AttachmentCollectionPageSerializer() {
53+
}
54+
55+
/**
56+
* Serializes an AttachmentCollectionPage
57+
*
58+
* @param src the AttachmentCollectionPage variable for serialization
59+
* @param logger the logger
60+
* @return JsonElement of AttachmentCollectionPage
61+
*/
62+
public static JsonElement serialize(final AttachmentCollectionPage src, final ILogger logger) {
63+
if(src == null) {
64+
return null;
65+
}
66+
JsonArray jsonArray = new JsonArray();
67+
List<Attachment> attachments = src.getCurrentPage();
68+
serializer = new DefaultSerializer(logger);
69+
String json;
70+
JsonObject jsonObject;
71+
parser = new JsonParser();
72+
for(Attachment attachment : attachments) {
73+
jsonObject = new JsonObject();
74+
json = serializer.serializeObject(attachment);
75+
jsonObject = (JsonObject)parser.parse(json);
76+
jsonArray.add(jsonObject);
77+
}
78+
return jsonArray;
79+
}
80+
81+
/**
82+
* Deserializes the JsonElement
83+
*
84+
* @param json the source AttachmentCollectionPage's Json
85+
* @param logger the logger
86+
* @throws JsonParseException the parse exception
87+
* @return the deserialized AttachmentCollectionPage
88+
*/
89+
public static AttachmentCollectionPage deserialize(final JsonElement json, final ILogger logger) throws JsonParseException {
90+
if (json == null) {
91+
return null;
92+
}
93+
final BaseAttachmentCollectionResponse response = new BaseAttachmentCollectionResponse();
94+
serializer = new DefaultSerializer(logger);
95+
final JsonObject[] sourceArray = serializer.deserializeObject(json.toString(), JsonObject[].class);
96+
final Attachment[] array = new Attachment[sourceArray.length];
97+
for (int i = 0; i < sourceArray.length; i++) {
98+
array[i] = serializer.deserializeObject(sourceArray[i].toString(), Attachment.class);
99+
array[i].setRawObject(serializer, sourceArray[i]);
100+
}
101+
response.value = Arrays.asList(array);
102+
return new AttachmentCollectionPage(response, null);
103+
}
104+
}

src/main/java/com/microsoft/graph/serializer/GsonFactory.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.gson.JsonSerializer;
3434
import com.microsoft.graph.logger.ILogger;
3535
import com.microsoft.graph.models.extensions.DateOnly;
36+
import com.microsoft.graph.requests.extensions.AttachmentCollectionPage;
3637

3738
import java.lang.reflect.Type;
3839
import java.text.ParseException;
@@ -209,6 +210,25 @@ public Duration deserialize(final JsonElement json,
209210
}
210211
}
211212
};
213+
214+
final JsonSerializer<AttachmentCollectionPage> attachmentCollectionPageSerializer = new JsonSerializer<AttachmentCollectionPage>() {
215+
@Override
216+
public JsonElement serialize(final AttachmentCollectionPage src,
217+
final Type typeOfSrc,
218+
final JsonSerializationContext context) {
219+
return AttachmentCollectionPageSerializer.serialize(src, logger);
220+
}
221+
};
222+
223+
final JsonDeserializer<AttachmentCollectionPage> attachmentCollectionPageDeserializer = new JsonDeserializer<AttachmentCollectionPage>() {
224+
@Override
225+
public AttachmentCollectionPage deserialize(final JsonElement json,
226+
final Type typeOfT,
227+
final JsonDeserializationContext context) throws JsonParseException {
228+
return AttachmentCollectionPageSerializer.deserialize(json, logger);
229+
}
230+
};
231+
212232
return new GsonBuilder()
213233
.excludeFieldsWithoutExposeAnnotation()
214234
.registerTypeAdapter(Calendar.class, calendarJsonSerializer)
@@ -223,6 +243,8 @@ public Duration deserialize(final JsonElement json,
223243
.registerTypeAdapter(EnumSet.class, enumSetJsonDeserializer)
224244
.registerTypeAdapter(Duration.class, durationJsonSerializer)
225245
.registerTypeAdapter(Duration.class, durationJsonDeserializer)
246+
.registerTypeAdapter(AttachmentCollectionPage.class, attachmentCollectionPageSerializer)
247+
.registerTypeAdapter(AttachmentCollectionPage.class, attachmentCollectionPageDeserializer)
226248
.registerTypeAdapterFactory(new FallbackTypeAdapterFactory(logger))
227249
.create();
228250
}

src/test/java/com/microsoft/graph/functional/OutlookTests.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import static org.junit.Assert.assertFalse;
44
import static org.junit.Assert.assertNotNull;
55

6+
import java.io.ByteArrayOutputStream;
7+
import java.io.File;
8+
import java.io.FileInputStream;
9+
import java.io.InputStream;
610
import java.util.ArrayList;
11+
import java.util.Arrays;
712

813
import javax.xml.datatype.DatatypeFactory;
914
import javax.xml.datatype.Duration;
@@ -12,16 +17,26 @@
1217
import org.junit.Ignore;
1318
import org.junit.Test;
1419

20+
import com.microsoft.graph.models.extensions.Attendee;
1521
//import com.microsoft.graph.extensions.IDirectoryDeletedItemsCollectionPage;
1622
import com.microsoft.graph.models.extensions.AttendeeBase;
23+
import com.microsoft.graph.models.extensions.Contact;
24+
import com.microsoft.graph.models.extensions.DateTimeTimeZone;
1725
import com.microsoft.graph.models.extensions.EmailAddress;
26+
import com.microsoft.graph.models.extensions.Event;
27+
import com.microsoft.graph.models.extensions.FileAttachment;
28+
import com.microsoft.graph.models.extensions.ItemAttachment;
29+
import com.microsoft.graph.models.extensions.ItemBody;
1830
import com.microsoft.graph.models.extensions.MeetingTimeSuggestionsResult;
1931
import com.microsoft.graph.models.extensions.Message;
2032
import com.microsoft.graph.models.extensions.Recipient;
2133
import com.microsoft.graph.models.extensions.User;
34+
import com.microsoft.graph.models.generated.BodyType;
2235
import com.microsoft.graph.options.QueryOption;
36+
import com.microsoft.graph.requests.extensions.AttachmentCollectionPage;
2337
import com.microsoft.graph.requests.extensions.IMessageCollectionPage;
2438
import com.microsoft.graph.requests.extensions.IUserCollectionPage;
39+
import com.microsoft.graph.requests.generated.BaseAttachmentCollectionResponse;
2540

2641
@Ignore
2742
public class OutlookTests {
@@ -105,4 +120,134 @@ public void testSendDraft() {
105120
IMessageCollectionPage mcp = testBase.graphClient.me().messages().buildRequest(options).get();
106121
assertFalse(mcp.getCurrentPage().isEmpty());
107122
}
123+
124+
@Test
125+
public void sendEmailWithAttachment() throws Exception{
126+
TestBase testBase = new TestBase();
127+
Message message = getMessage();
128+
message.hasAttachments = true;
129+
BaseAttachmentCollectionResponse response = new BaseAttachmentCollectionResponse();
130+
response.value = Arrays.asList(getFileAttachment(),getItemAttachmentWithEvent(),getItemAttachmentWithContact());
131+
message.attachments = new AttachmentCollectionPage(response, null);
132+
testBase.graphClient.me().sendMail(message, true).buildRequest().post();
133+
}
134+
135+
@Test
136+
public void uploadEmailAsDraftWithAttachmentThenSend() {
137+
TestBase testBase = new TestBase();
138+
Message message = getMessage();
139+
Message m = testBase.graphClient.me().messages().buildRequest().post(message);
140+
assertNotNull(m);
141+
testBase.graphClient.me().messages(m.id).send().buildRequest().post();
142+
}
143+
144+
@Test
145+
public void sendEventWithAttachment() throws Exception{
146+
TestBase testBase = new TestBase();
147+
Event event = getEvent();
148+
event.body = getItemBody();
149+
event.hasAttachments = true;
150+
BaseAttachmentCollectionResponse response = new BaseAttachmentCollectionResponse();
151+
response.value = Arrays.asList(getFileAttachment(),getItemAttachmentWithContact());
152+
event.attachments = new AttachmentCollectionPage(response, null);
153+
Event eventResponse = testBase.graphClient.me().events().buildRequest().post(event);
154+
assertNotNull(eventResponse);
155+
}
156+
157+
private ItemBody getItemBody() {
158+
ItemBody itemBody = new ItemBody();
159+
itemBody.content = "<html><head></head><body> test body </body></html>";
160+
itemBody.contentType = BodyType.HTML;
161+
return itemBody;
162+
}
163+
164+
public Message getMessage() {
165+
Message message = new Message();
166+
java.util.List<String> emails = Arrays.asList("test_email@test_domain.com");
167+
java.util.List<Recipient> listReceipient = new ArrayList<>();
168+
for(String email : emails) {
169+
EmailAddress emailAddress = new EmailAddress();
170+
emailAddress.address = email;
171+
Recipient recipient = new Recipient();
172+
recipient.emailAddress = emailAddress;
173+
listReceipient.add(recipient);
174+
}
175+
message.body = getItemBody();
176+
message.toRecipients = listReceipient;
177+
message.subject = "Test Message";
178+
message.id = "1234";
179+
return message;
180+
}
181+
182+
private FileAttachment getFileAttachment() throws Exception{
183+
FileAttachment fileAttachment = new FileAttachment();
184+
fileAttachment.name = "document.pdf";
185+
File pdfFile = new File("src/test/resources/document.pdf");
186+
InputStream fileStream = new FileInputStream(pdfFile);
187+
fileAttachment.contentBytes = getByteArray(fileStream);
188+
fileAttachment.oDataType = "#microsoft.graph.fileAttachment";
189+
fileAttachment.id="54321";
190+
return fileAttachment;
191+
}
192+
193+
private ItemAttachment getItemAttachmentWithEvent() {
194+
ItemAttachment itemAttachmentEvent = new ItemAttachment();
195+
itemAttachmentEvent.oDataType = "#microsoft.graph.itemAttachment";
196+
itemAttachmentEvent.name = "event name";
197+
itemAttachmentEvent.item = getEvent();
198+
itemAttachmentEvent.item.oDataType = "microsoft.graph.event";
199+
return itemAttachmentEvent;
200+
}
201+
202+
private ItemAttachment getItemAttachmentWithContact() {
203+
Contact contact = new Contact();
204+
contact.displayName = "displayname";
205+
contact.mobilePhone="123456890";
206+
ItemAttachment itemAttachmentContact = new ItemAttachment();
207+
itemAttachmentContact.oDataType = "#microsoft.graph.itemAttachment";
208+
itemAttachmentContact.name = "Attachment name";
209+
itemAttachmentContact.item = contact;
210+
itemAttachmentContact.item.oDataType = "microsoft.graph.contact";
211+
return itemAttachmentContact;
212+
}
213+
214+
private Event getEvent() {
215+
Event event = new Event();
216+
java.util.List<String> emails = Arrays.asList("test_email@test_domain.com");
217+
java.util.List<Attendee> attendees = new ArrayList<>();
218+
for(String email : emails) {
219+
EmailAddress emailAddress = new EmailAddress();
220+
emailAddress.address = email;
221+
Attendee attendee = new Attendee();
222+
attendee.emailAddress = emailAddress;
223+
attendees.add(attendee);
224+
}
225+
DateTimeTimeZone start = new DateTimeTimeZone();
226+
start.timeZone = "UTC";
227+
start.dateTime = "2018-10-16T06:15:26.544Z";
228+
DateTimeTimeZone end = new DateTimeTimeZone();
229+
end.timeZone = "UTC";
230+
end.dateTime = "2018-11-18T07:30:26.544Z";
231+
event.start = start;
232+
event.end = end;
233+
event.subject = "Test Event Subject";
234+
event.id = "1234";
235+
return event;
236+
}
237+
238+
public byte[] getByteArray(InputStream in) {
239+
try {
240+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
241+
int nRead;
242+
byte[] data = new byte[16384];
243+
while ((nRead = in.read(data, 0, data.length)) != -1) {
244+
buffer.write(data, 0, nRead);
245+
}
246+
buffer.flush();
247+
return buffer.toByteArray();
248+
} catch (Exception e) {
249+
e.printStackTrace();
250+
}
251+
return null;
252+
}
108253
}

0 commit comments

Comments
 (0)