adjusted event (de)serialization tests so that serializer / deserializer are tested in conjunction;

Testing only the serializer doesn't catch deserialization errors for specific attribute states after
serializing them.
This commit is contained in:
Axel Uhl committed 2014-03-07 18:10:07 +01:00
1 parent 304339b8f6
commit a77832e8b1
4 files changed
+199 -85

No files matched your search

@@ -1,83 +1,91 @@
package com.sap.sailing.server.gateway.serialization.test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Date;
import java.util.UUID;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.sap.sailing.domain.base.EventBase;
import com.sap.sailing.domain.base.Venue;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.domain.common.impl.MillisecondsTimePoint;
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
import com.sap.sailing.server.gateway.serialization.impl.EventJsonSerializer;
public class EventDataJsonSerializerTest {
protected final UUID expectedId = UUID.randomUUID();
protected final String expectedName = "ab";
protected final TimePoint expectedStartDate = new MillisecondsTimePoint(new Date());
protected final TimePoint expectedEndDate = new MillisecondsTimePoint(new Date());
protected JsonSerializer<Venue> venueSerializer;
protected EventJsonSerializer serializer;
protected EventBase event;
// see https://groups.google.com/forum/?fromgroups=#!topic/mockito/iMumB0_bpdo
@SuppressWarnings("unchecked")
@Before
public void setUp() {
// Event and its basic attributes ...
event = mock(EventBase.class);
when(event.getId()).thenReturn(expectedId);
when(event.getName()).thenReturn(expectedName);
when(event.getStartDate()).thenReturn(expectedStartDate);
when(event.getEndDate()).thenReturn(expectedEndDate);
// ... and the serializer itself.
venueSerializer = mock(JsonSerializer.class);
serializer = new EventJsonSerializer(venueSerializer);
}
@Test
public void testBasicAttributes() {
JSONObject result = serializer.serialize(event);
assertEquals(
expectedId,
UUID.fromString(result.get(EventJsonSerializer.FIELD_ID).toString()));
assertEquals(
expectedName,
result.get(EventJsonSerializer.FIELD_NAME));
assertEquals(
expectedStartDate,
new MillisecondsTimePoint(((Number) result.get(EventJsonSerializer.FIELD_START_DATE)).longValue()));
assertEquals(
expectedEndDate,
new MillisecondsTimePoint(((Number) result.get(EventJsonSerializer.FIELD_END_DATE)).longValue()));
}
@Test
public void testSerializesVenue() {
// ... venue and its mocked serializer ...
Venue venue = mock(Venue.class);
when(event.getVenue()).thenReturn(venue);
JSONObject expectedVenue = new JSONObject();
when(venueSerializer.serialize(eq(venue))).thenReturn(expectedVenue);
JSONObject result = serializer.serialize(event);
assertEquals(
expectedVenue,
result.get(EventJsonSerializer.FIELD_VENUE));
}
}
package com.sap.sailing.server.gateway.serialization.test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Date;
import java.util.UUID;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.sap.sailing.domain.base.DomainFactory;
import com.sap.sailing.domain.base.EventBase;
import com.sap.sailing.domain.base.Venue;
import com.sap.sailing.domain.base.impl.VenueImpl;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.domain.common.impl.MillisecondsTimePoint;
import com.sap.sailing.server.gateway.deserialization.JsonDeserializationException;
import com.sap.sailing.server.gateway.deserialization.impl.CourseAreaJsonDeserializer;
import com.sap.sailing.server.gateway.deserialization.impl.EventBaseJsonDeserializer;
import com.sap.sailing.server.gateway.deserialization.impl.VenueJsonDeserializer;
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
import com.sap.sailing.server.gateway.serialization.impl.CourseAreaJsonSerializer;
import com.sap.sailing.server.gateway.serialization.impl.EventJsonSerializer;
import com.sap.sailing.server.gateway.serialization.impl.VenueJsonSerializer;
public class EventDataJsonSerializerTest {
protected final UUID expectedId = UUID.randomUUID();
protected final String expectedName = "ab";
protected final TimePoint expectedStartDate = new MillisecondsTimePoint(new Date());
protected final TimePoint expectedEndDate = new MillisecondsTimePoint(new Date());
protected final Venue expectedVenue = new VenueImpl("Expected Venue");
protected JsonSerializer<Venue> venueSerializer;
protected EventJsonSerializer serializer;
protected EventBaseJsonDeserializer deserializer;
protected EventBase event;
// see https://groups.google.com/forum/?fromgroups=#!topic/mockito/iMumB0_bpdo
@Before
public void setUp() {
// Event and its basic attributes ...
event = mock(EventBase.class);
when(event.getId()).thenReturn(expectedId);
when(event.getName()).thenReturn(expectedName);
when(event.getStartDate()).thenReturn(expectedStartDate);
when(event.getEndDate()).thenReturn(expectedEndDate);
when(event.getVenue()).thenReturn(expectedVenue);
// ... and the serializer itself.
serializer = new EventJsonSerializer(new VenueJsonSerializer(new CourseAreaJsonSerializer()));
deserializer = new EventBaseJsonDeserializer(new VenueJsonDeserializer(new CourseAreaJsonDeserializer(DomainFactory.INSTANCE)));
}
@Test
public void testBasicAttributes() {
JSONObject result = serializer.serialize(event);
assertEquals(
expectedId,
UUID.fromString(result.get(EventJsonSerializer.FIELD_ID).toString()));
assertEquals(
expectedName,
result.get(EventJsonSerializer.FIELD_NAME));
assertEquals(
expectedStartDate,
new MillisecondsTimePoint(((Number) result.get(EventJsonSerializer.FIELD_START_DATE)).longValue()));
assertEquals(
expectedEndDate,
new MillisecondsTimePoint(((Number) result.get(EventJsonSerializer.FIELD_END_DATE)).longValue()));
}
@Test
public void testBasicAttributesAfterDeserialization() throws JsonDeserializationException {
final JSONObject result = serializer.serialize(event);
final EventBase deserializedEvent = deserializer.deserialize(result);
assertEquals(expectedId, deserializedEvent.getId());
assertEquals(expectedName, deserializedEvent.getName());
assertEquals(expectedStartDate, deserializedEvent.getStartDate());
assertEquals(expectedEndDate, deserializedEvent.getEndDate());
}
@Test
public void testSerializesVenue() throws JsonDeserializationException {
JSONObject result = serializer.serialize(event);
EventBase event = deserializer.deserialize(result);
assertEquals(expectedVenue.getName(), event.getVenue().getName());
}
}
@@ -0,0 +1,81 @@
package com.sap.sailing.server.gateway.serialization.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.UUID;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.sap.sailing.domain.base.DomainFactory;
import com.sap.sailing.domain.base.EventBase;
import com.sap.sailing.domain.base.Venue;
import com.sap.sailing.domain.base.impl.VenueImpl;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.server.gateway.deserialization.JsonDeserializationException;
import com.sap.sailing.server.gateway.deserialization.impl.CourseAreaJsonDeserializer;
import com.sap.sailing.server.gateway.deserialization.impl.EventBaseJsonDeserializer;
import com.sap.sailing.server.gateway.deserialization.impl.VenueJsonDeserializer;
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
import com.sap.sailing.server.gateway.serialization.impl.CourseAreaJsonSerializer;
import com.sap.sailing.server.gateway.serialization.impl.EventJsonSerializer;
import com.sap.sailing.server.gateway.serialization.impl.VenueJsonSerializer;
public class EventWithNullStartAndEndDataJsonSerializerTest {
protected final UUID expectedId = UUID.randomUUID();
protected final String expectedName = "ab";
protected final TimePoint expectedStartDate = null;
protected final TimePoint expectedEndDate = null;
protected final Venue expectedVenue = new VenueImpl("Expected Venue");
protected JsonSerializer<Venue> venueSerializer;
protected EventJsonSerializer serializer;
protected EventBaseJsonDeserializer deserializer;
protected EventBase event;
@Before
public void setUp() {
// Event and its basic attributes ...
event = mock(EventBase.class);
when(event.getId()).thenReturn(expectedId);
when(event.getName()).thenReturn(expectedName);
when(event.getStartDate()).thenReturn(expectedStartDate);
when(event.getEndDate()).thenReturn(expectedEndDate);
when(event.getVenue()).thenReturn(expectedVenue);
// ... and the serializer itself.
serializer = new EventJsonSerializer(new VenueJsonSerializer(new CourseAreaJsonSerializer()));
deserializer = new EventBaseJsonDeserializer(new VenueJsonDeserializer(new CourseAreaJsonDeserializer(DomainFactory.INSTANCE)));
}
@Test
public void testBasicAttributes() {
JSONObject result = serializer.serialize(event);
assertEquals(expectedId, UUID.fromString(result.get(EventJsonSerializer.FIELD_ID).toString()));
assertEquals(expectedName, result.get(EventJsonSerializer.FIELD_NAME));
assertNull(result.get(EventJsonSerializer.FIELD_START_DATE));
assertNull(result.get(EventJsonSerializer.FIELD_END_DATE));
}
@Test
public void testBasicAttributesAfterDeserialization() throws JsonDeserializationException {
final JSONObject result = serializer.serialize(event);
final EventBase deserializedEvent = deserializer.deserialize(result);
assertEquals(expectedId, deserializedEvent.getId());
assertEquals(expectedName, deserializedEvent.getName());
assertNull(deserializedEvent.getStartDate());
assertNull(deserializedEvent.getEndDate());
}
@Test
public void testSerializesVenue() throws JsonDeserializationException {
JSONObject result = serializer.serialize(event);
EventBase event = deserializer.deserialize(result);
assertEquals(expectedVenue.getName(), event.getVenue().getName());
}
}
@@ -24,8 +24,13 @@ public class EventBaseJsonDeserializer implements JsonDeserializer<EventBase> {
String name = object.get(EventJsonSerializer.FIELD_NAME).toString();
Number startDate = (Number) object.get(EventJsonSerializer.FIELD_START_DATE);
Number endDate = (Number) object.get(EventJsonSerializer.FIELD_END_DATE);
JSONObject venueObject = Helpers.getNestedObjectSafe(object, EventJsonSerializer.FIELD_VENUE);
Venue venue = venueDeserializer.deserialize(venueObject);
final Venue venue;
if (object.get(EventJsonSerializer.FIELD_VENUE) != null) {
JSONObject venueObject = Helpers.getNestedObjectSafe(object, EventJsonSerializer.FIELD_VENUE);
venue = venueDeserializer.deserialize(venueObject);
} else {
venue = null;
}
return new EventBaseImpl(name, startDate == null ? null : new MillisecondsTimePoint(startDate.longValue()),
endDate == null ? null : new MillisecondsTimePoint(endDate.longValue()), venue, true, id);
}
@@ -2,6 +2,7 @@ package com.sap.sailing.server.replication.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.Date;
@@ -39,6 +40,25 @@ public class EventReplicationTest extends AbstractServerReplicationTest {
assertEquals(replicatedEvent.getVenue().getName(), venueName);
}
@Test
public void testEventReplicationWithNullStartAndEndDate() throws InterruptedException {
final String eventName = "ESS Masquat";
final String venueName = "Masquat, Oman";
final boolean isPublic = false;
List<String> regattas = new ArrayList<String>();
regattas.add("Day1");
regattas.add("Day2");
Event masterEvent = master.addEvent(eventName, null, null, venueName, isPublic, UUID.randomUUID());
Thread.sleep(1000);
Event replicatedEvent = replica.getEvent(masterEvent.getId());
assertNotNull(replicatedEvent);
assertEquals(replicatedEvent.getName(), eventName);
assertNull(replicatedEvent.getStartDate());
assertNull(replicatedEvent.getEndDate());
assertEquals(replicatedEvent.getVenue().getName(), venueName);
}
@Test
public void testCourseAreaReplication() throws InterruptedException {
final String eventName = "ESS Singapur";