Skip to content

Commit 52d0872

Browse files
committed
Added ConvertCalendarTest
1 parent 3cc0309 commit 52d0872

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.aspose.email.cloud.sdk.invoker;
2+
3+
import com.google.gson.JsonDeserializationContext;
4+
import com.google.gson.JsonDeserializer;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonParseException;
7+
8+
import java.lang.reflect.Type;
9+
import java.text.ParseException;
10+
import java.text.SimpleDateFormat;
11+
import java.util.Date;
12+
import java.util.TimeZone;
13+
14+
public class DateDeserializer implements JsonDeserializer<Date> {
15+
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
16+
throws JsonParseException {
17+
String date = json.getAsString();
18+
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
19+
format.setTimeZone(TimeZone.getDefault());
20+
21+
try {
22+
return format.parse(date);
23+
} catch (ParseException exp) {
24+
return null;
25+
}
26+
}
27+
}

src/main/java/com/aspose/email/cloud/sdk/invoker/JSON.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.gson.*;
3333

3434
import java.lang.reflect.Type;
35+
import java.util.Date;
3536

3637
/**
3738
* JSON helper class.
@@ -44,6 +45,7 @@ public class JSON {
4445
GsonBuilder builder = new GsonBuilder();
4546
builder.registerTypeAdapter(BaseObject.class, new TypeDeriveAdapter<BaseObject>("type"));
4647
Derive.registerAdapters(builder);
48+
builder.registerTypeAdapter(Date.class, new DateDeserializer());
4749
gsonExt = builder.create();
4850
}
4951
public static String serialize(Object object)

src/test/java/com/aspose/email/cloud/sdk/api/EmailApiTests.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,47 @@ public void emailClientMultiAccountTest() throws ApiException {
414414
multiAccount.getSendAccount().getCredentials().getDiscriminator());
415415
}
416416

417+
@Test(groups = {"pipeline"})
418+
public void ConvertCalendarTest() throws ApiException, UnsupportedEncodingException {
419+
final String location = "Some location";
420+
//Create DTO with specified location:
421+
CalendarDto calendarDto = new CalendarDto()
422+
.location(location)
423+
.summary("Some summary")
424+
.description("Some description")
425+
.startDate(Calendar.getInstance().getTime())
426+
.endDate(Calendar.getInstance().getTime())
427+
.organizer(new MailAddress().address("organizer@aspose.com"))
428+
.attendees(Collections.singletonList(new MailAddress().address("attendee@aspose.com")));
429+
//We can convert this DTO to a MAPI or ICS file:
430+
byte[] mapiBytes = api.convertCalendarModelToFile(
431+
new ConvertCalendarModelToFileRequestData(
432+
"Msg", calendarDto));
433+
/*
434+
// mapiBytes can be saved as a calendar.msg file:
435+
try (FileOutputStream stream = new FileOutputStream("calendar.msg")){
436+
stream.write(mapiBytes);
437+
}
438+
*/
439+
440+
//Let's convert this bytes to an ICS file:
441+
byte[] icsBytes = api.convertCalendar(new ConvertCalendarRequestData("Ics", mapiBytes));
442+
/*
443+
//icsBytes can be saved as a calendar.ics file:
444+
try (FileOutputStream stream = new FileOutputStream("calendar.ics")){
445+
stream.write(icsBytes);
446+
}
447+
*/
448+
//ICS is a text format. We can convert icsBytes to a string and check that it
449+
//contains specified location as a substring:
450+
String calendarContent = new String(icsBytes, "UTF-8");
451+
assert calendarContent.contains(location);
452+
//We can also convert file bytes back to a CalendarDto
453+
CalendarDto dto = api.getCalendarFileAsModel(
454+
new GetCalendarFileAsModelRequestData(icsBytes));
455+
assert location.equals(dto.getLocation());
456+
}
457+
417458
private String createCalendar() throws ApiException {
418459
Calendar startDate = Calendar.getInstance();
419460
return createCalendar(startDate);

0 commit comments

Comments
 (0)