From a77832e8b169559a1d91fb53299d016551de269e Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Fri, 7 Mar 2014 18:10:07 +0100 Subject: [PATCH] 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. --- .../test/EventDataJsonSerializerTest.java | 174 +++++++++--------- ...NullStartAndEndDataJsonSerializerTest.java | 81 ++++++++ .../impl/EventBaseJsonDeserializer.java | 9 +- .../test/EventReplicationTest.java | 20 ++ 4 files changed, 199 insertions(+), 85 deletions(-) mode change 100644 => 100755 java/com.sap.sailing.server.gateway.serialization.test/src/com/sap/sailing/server/gateway/serialization/test/EventDataJsonSerializerTest.java create mode 100755 java/com.sap.sailing.server.gateway.serialization.test/src/com/sap/sailing/server/gateway/serialization/test/EventWithNullStartAndEndDataJsonSerializerTest.java diff --git a/java/com.sap.sailing.server.gateway.serialization.test/src/com/sap/sailing/server/gateway/serialization/test/EventDataJsonSerializerTest.java b/java/com.sap.sailing.server.gateway.serialization.test/src/com/sap/sailing/server/gateway/serialization/test/EventDataJsonSerializerTest.java old mode 100644 new mode 100755 index 96228540111..5f50c5e8747 --- a/java/com.sap.sailing.server.gateway.serialization.test/src/com/sap/sailing/server/gateway/serialization/test/EventDataJsonSerializerTest.java +++ b/java/com.sap.sailing.server.gateway.serialization.test/src/com/sap/sailing/server/gateway/serialization/test/EventDataJsonSerializerTest.java @@ -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 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 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()); + } + +} diff --git a/java/com.sap.sailing.server.gateway.serialization.test/src/com/sap/sailing/server/gateway/serialization/test/EventWithNullStartAndEndDataJsonSerializerTest.java b/java/com.sap.sailing.server.gateway.serialization.test/src/com/sap/sailing/server/gateway/serialization/test/EventWithNullStartAndEndDataJsonSerializerTest.java new file mode 100755 index 00000000000..f39a8522978 --- /dev/null +++ b/java/com.sap.sailing.server.gateway.serialization.test/src/com/sap/sailing/server/gateway/serialization/test/EventWithNullStartAndEndDataJsonSerializerTest.java @@ -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 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()); + } +} diff --git a/java/com.sap.sailing.server.gateway.serialization/src/com/sap/sailing/server/gateway/deserialization/impl/EventBaseJsonDeserializer.java b/java/com.sap.sailing.server.gateway.serialization/src/com/sap/sailing/server/gateway/deserialization/impl/EventBaseJsonDeserializer.java index ac22446b25f..e5d07c7a124 100644 --- a/java/com.sap.sailing.server.gateway.serialization/src/com/sap/sailing/server/gateway/deserialization/impl/EventBaseJsonDeserializer.java +++ b/java/com.sap.sailing.server.gateway.serialization/src/com/sap/sailing/server/gateway/deserialization/impl/EventBaseJsonDeserializer.java @@ -24,8 +24,13 @@ public class EventBaseJsonDeserializer implements JsonDeserializer { 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); } diff --git a/java/com.sap.sailing.server.replication.test/src/com/sap/sailing/server/replication/test/EventReplicationTest.java b/java/com.sap.sailing.server.replication.test/src/com/sap/sailing/server/replication/test/EventReplicationTest.java index c87566ac66d..cea1f3c6a48 100644 --- a/java/com.sap.sailing.server.replication.test/src/com/sap/sailing/server/replication/test/EventReplicationTest.java +++ b/java/com.sap.sailing.server.replication.test/src/com/sap/sailing/server/replication/test/EventReplicationTest.java @@ -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 regattas = new ArrayList(); + 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";