JSON serialization/deserialization of EventBase description/logoImagURL/sponsorImageURLs/officialWebsiteURL

This commit is contained in:
Axel Uhl committed 2014-06-14 11:16:19 +02:00
1 parent 42202bc996
commit 89d8d4c7ba
2 files changed
+42

No files matched your search

@@ -6,6 +6,7 @@ import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.UUID;
@@ -35,9 +36,12 @@ public class EventWithNullStartAndEndDataJsonSerializerTest {
protected final UUID expectedId = UUID.randomUUID();
protected final String expectedName = "ab";
protected final String expectedDescription = "cd";
protected final TimePoint expectedStartDate = null;
protected final TimePoint expectedEndDate = null;
protected final Venue expectedVenue = new VenueImpl("Expected Venue");
protected final URL expectedOfficialWebsiteURL;
protected final URL expectedLogoImageURL;
protected final LeaderboardGroupBase expectedLeaderboardGroup = mock(LeaderboardGroupBase.class);
protected final Iterable<LeaderboardGroupBase> expectedLeaderboardGroups = Collections.singleton(expectedLeaderboardGroup);
@@ -46,6 +50,11 @@ public class EventWithNullStartAndEndDataJsonSerializerTest {
protected EventBaseJsonDeserializer deserializer;
protected EventBase event;
public EventWithNullStartAndEndDataJsonSerializerTest() throws MalformedURLException {
expectedOfficialWebsiteURL = new URL("http://official.website.com");
expectedLogoImageURL = new URL("http://official.logo.com/logo.png");
}
@Before
public void setUp() {
// Event and its basic attributes ...
@@ -56,11 +65,15 @@ public class EventWithNullStartAndEndDataJsonSerializerTest {
event = mock(EventBase.class);
when(event.getId()).thenReturn(expectedId);
when(event.getName()).thenReturn(expectedName);
when(event.getDescription()).thenReturn(expectedDescription);
when(event.getOfficialWebsiteURL()).thenReturn(expectedOfficialWebsiteURL);
when(event.getLogoImageURL()).thenReturn(expectedLogoImageURL);
when(event.getStartDate()).thenReturn(expectedStartDate);
when(event.getEndDate()).thenReturn(expectedEndDate);
when(event.getVenue()).thenReturn(expectedVenue);
when(event.getImageURLs()).thenReturn(Collections.<URL>emptySet());
when(event.getVideoURLs()).thenReturn(Collections.<URL>emptySet());
when(event.getSponsorImageURLs()).thenReturn(Collections.<URL>emptySet());
doReturn(expectedLeaderboardGroups).when(event).getLeaderboardGroups();
// ... and the serializer itself.
@@ -73,6 +86,7 @@ public class EventWithNullStartAndEndDataJsonSerializerTest {
JSONObject result = serializer.serialize(event);
assertEquals(expectedId, UUID.fromString(result.get(EventBaseJsonSerializer.FIELD_ID).toString()));
assertEquals(expectedName, result.get(EventBaseJsonSerializer.FIELD_NAME));
assertEquals(expectedDescription, result.get(EventBaseJsonSerializer.FIELD_DESCRIPTION));
assertNull(result.get(EventBaseJsonSerializer.FIELD_START_DATE));
assertNull(result.get(EventBaseJsonSerializer.FIELD_END_DATE));
}
@@ -83,6 +97,9 @@ public class EventWithNullStartAndEndDataJsonSerializerTest {
final EventBase deserializedEvent = deserializer.deserialize(result);
assertEquals(expectedId, deserializedEvent.getId());
assertEquals(expectedName, deserializedEvent.getName());
assertEquals(expectedDescription, deserializedEvent.getDescription());
assertEquals(expectedOfficialWebsiteURL, deserializedEvent.getOfficialWebsiteURL());
assertEquals(expectedLogoImageURL, deserializedEvent.getLogoImageURL());
LeaderboardGroupBase deserializedLg = deserializedEvent.getLeaderboardGroups().iterator().next();
assertEquals(expectedLeaderboardGroup.getName(), deserializedLg.getName());
assertEquals(expectedLeaderboardGroup.getDescription(), deserializedLg.getDescription());
@@ -31,6 +31,9 @@ public class EventBaseJsonDeserializer implements JsonDeserializer<EventBase> {
public EventBase deserialize(JSONObject object) throws JsonDeserializationException {
UUID id = UUID.fromString((String) object.get(EventBaseJsonSerializer.FIELD_ID));
String name = object.get(EventBaseJsonSerializer.FIELD_NAME).toString();
String description = object.get(EventBaseJsonSerializer.FIELD_DESCRIPTION).toString();
String officialWebsiteURLAsString = object.get(EventBaseJsonSerializer.FIELD_OFFICIAL_WEBSITE_URL).toString();
String logoImageURLAsString = object.get(EventBaseJsonSerializer.FIELD_LOGO_IMAGE_URL).toString();
Number startDate = (Number) object.get(EventBaseJsonSerializer.FIELD_START_DATE);
Number endDate = (Number) object.get(EventBaseJsonSerializer.FIELD_END_DATE);
final Venue venue;
@@ -49,6 +52,21 @@ public class EventBaseJsonDeserializer implements JsonDeserializer<EventBase> {
}
EventBaseImpl result = new StrippedEventImpl(name, startDate == null ? null : new MillisecondsTimePoint(startDate.longValue()),
endDate == null ? null : new MillisecondsTimePoint(endDate.longValue()), venue, /* is public */ true, id, leaderboardGroups);
result.setDescription(description);
if (officialWebsiteURLAsString != null) {
try {
result.setOfficialWebsiteURL(new URL(officialWebsiteURLAsString));
} catch (MalformedURLException e) {
throw new JsonDeserializationException("Error deserializing official website URL for event "+name, e);
}
}
if (logoImageURLAsString != null) {
try {
result.setLogoImageURL(new URL(logoImageURLAsString));
} catch (MalformedURLException e) {
throw new JsonDeserializationException("Error deserializing logo image URL for event "+name, e);
}
}
if (object.get(EventBaseJsonSerializer.FIELD_IMAGE_URLS) != null) {
try {
result.setImageURLs(getURLsFromStrings(Helpers.getNestedArraySafe(object, EventBaseJsonSerializer.FIELD_IMAGE_URLS)));
@@ -63,6 +81,13 @@ public class EventBaseJsonDeserializer implements JsonDeserializer<EventBase> {
throw new JsonDeserializationException("Error deserializing video URLs for event "+name, e);
}
}
if (object.get(EventBaseJsonSerializer.FIELD_SPONSOR_IMAGE_URLS) != null) {
try {
result.setSponsorImageURLs(getURLsFromStrings(Helpers.getNestedArraySafe(object, EventBaseJsonSerializer.FIELD_SPONSOR_IMAGE_URLS)));
} catch (MalformedURLException e) {
throw new JsonDeserializationException("Error deserializing sponsor image URLs for event "+name, e);
}
}
return result;
}