Merge remote-tracking branch 'origin/master' into dataMining

This commit is contained in:
Lennart Hensler committed 2014-03-09 15:58:34 +01:00
commit 0e272885ac
27 files changed
+410 -177

No files matched your search

@@ -176,7 +176,7 @@ public class DomainObjectFactoryImpl implements DomainObjectFactory {
private TimePoint loadTimePoint(DBObject object, FieldNames field) {
TimePoint result = null;
Number timePointAsNumber = (Number) object.get(field.name());
if(timePointAsNumber != null) {
if (timePointAsNumber != null) {
result = new MillisecondsTimePoint(timePointAsNumber.longValue());
}
return result;
@@ -106,13 +106,13 @@ public class MongoObjectFactoryImpl implements MongoObjectFactory {
}
private void storeTimePoint(TimePoint timePoint, DBObject result, FieldNames field) {
if(timePoint != null) {
if (timePoint != null) {
result.put(field.name(), timePoint.asMillis());
}
}
private void storeTimed(Timed timed, DBObject result) {
if(timed.getTimePoint() != null) {
if (timed.getTimePoint() != null) {
storeTimePoint(timed.getTimePoint(), result, FieldNames.TIME_AS_MILLIS);
}
}
@@ -809,10 +809,10 @@ public class LeaderboardScoringAndRankingTest extends AbstractLeaderboardTest {
leaderboardHighPoint.getFleet(null));
assertEquals(16, leaderboardHighPoint.getCompetitorsFromBestToWorst(later).size());
assertEquals(new Double(16), leaderboardHighPoint.getTotalPoints(competitors[0], later));
FlexibleLeaderboard leaderboardHighPointESSOverall = new FlexibleLeaderboardImpl("Test ESS Highpoint Overall", new ThresholdBasedResultDiscardingRuleImpl(/* discarding thresholds */ new int[0]),
new HighPointExtremeSailingSeriesOverall(), null);
leaderboardHighPointESSOverall.addRace(new MockedTrackedRaceWithStartTimeAndRanks(now, Arrays.asList(competitors)), "R1", /* medalRace */ false,
leaderboardHighPointESSOverall.getFleet(null));
LeaderboardGroup leaderboardGroup = new LeaderboardGroupImpl("ESS", "ESS", /* displayGroupsInReverseOrder */ false,
Arrays.asList(new Leaderboard[] { leaderboardHighPoint10LastBreaksTie }));
LeaderboardGroupMetaLeaderboard leaderboardHighPointESSOverall = new LeaderboardGroupMetaLeaderboard(
leaderboardGroup, new HighPointExtremeSailingSeriesOverall(), new ThresholdBasedResultDiscardingRuleImpl(/* discarding thresholds */ new int[0]));
assertEquals(16, leaderboardHighPointESSOverall.getCompetitorsFromBestToWorst(later).size());
assertEquals(new Double(10), leaderboardHighPointESSOverall.getTotalPoints(competitors[0], later));
assertEquals(new Double(9), leaderboardHighPointESSOverall.getTotalPoints(competitors[1], later));
@@ -884,34 +884,43 @@ public class LeaderboardScoringAndRankingTest extends AbstractLeaderboardTest {
public void testOverallLeaderboardWithESSHighPointScoringAndTieBreakInLastRegattaOfLeaderboardGroup() throws NoWindException {
Competitor[] c = createCompetitors(12).toArray(new Competitor[0]);
// 10 9 8 7 6 5 4 3 2 1 1 1
Competitor[] regattaRace1 = new Competitor[] { c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10], c[11] };
Competitor[] regattaRace2 = new Competitor[] { c[1], c[10], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[11], c[0] };
Competitor[] regattaRace3 = new Competitor[] { c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[11], c[10], c[0] };
Competitor[] regattaRace1_1 = new Competitor[] { c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10], c[11] };
Competitor[] regattaRace2_1 = new Competitor[] { c[1], c[10], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[11], c[0] };
Competitor[] regattaRace1_2 = new Competitor[] { c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10], c[11] };
Competitor[] regattaRace2_2 = new Competitor[] { c[1], c[10], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[11], c[0] };
Competitor[] regattaRace3_2 = new Competitor[] { c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[11], c[10], c[0] };
TimePoint now = MillisecondsTimePoint.now();
TimePoint later = new MillisecondsTimePoint(now.asMillis()+1000);
FlexibleLeaderboard leaderboard1 = new FlexibleLeaderboardImpl("Leaderboard 1", new ThresholdBasedResultDiscardingRuleImpl(/* discarding thresholds */ new int[0]),
new HighPointFirstGets10LastBreaksTie(), null);
leaderboard1.addRace(new MockedTrackedRaceWithStartTimeAndRanks(now, Arrays.asList(regattaRace1)), "R1", /* medalRace */ false,
leaderboard1.addRace(new MockedTrackedRaceWithStartTimeAndRanks(now, Arrays.asList(regattaRace1_1)), "R1", /* medalRace */ false,
leaderboard1.getFleet(null));
leaderboard1.addRace(new MockedTrackedRaceWithStartTimeAndRanks(now, Arrays.asList(regattaRace2)), "R2", /* medalRace */ false,
leaderboard1.getFleet(null));
leaderboard1.addRace(new MockedTrackedRaceWithStartTimeAndRanks(now, Arrays.asList(regattaRace3)), "R3", /* medalRace */ false,
leaderboard1.addRace(new MockedTrackedRaceWithStartTimeAndRanks(now, Arrays.asList(regattaRace2_1)), "R2", /* medalRace */ false,
leaderboard1.getFleet(null));
FlexibleLeaderboard leaderboard2 = new FlexibleLeaderboardImpl("Leaderboard 2", new ThresholdBasedResultDiscardingRuleImpl(/* discarding thresholds */ new int[0]),
new HighPointFirstGets10LastBreaksTie(), null);
leaderboard2.addRace(new MockedTrackedRaceWithStartTimeAndRanks(now, Arrays.asList(regattaRace1_2)), "R1", /* medalRace */ false,
leaderboard2.getFleet(null));
leaderboard2.addRace(new MockedTrackedRaceWithStartTimeAndRanks(now, Arrays.asList(regattaRace2_2)), "R2", /* medalRace */ false,
leaderboard2.getFleet(null));
leaderboard2.addRace(new MockedTrackedRaceWithStartTimeAndRanks(now, Arrays.asList(regattaRace3_2)), "R3", /* medalRace */ false,
leaderboard2.getFleet(null));
// [C2, C3, C4, C5, C6, C7, C1, C11, C8, C9, C10, C12]
// [29, 25, 22, 19, 16, 12, 13, 11, 10, 7, 4, 3]
List<Competitor> rankedCompetitorsInLeaderboard = leaderboard1.getCompetitorsFromBestToWorst(later);
List<Competitor> rankedCompetitorsInLeaderboard = leaderboard2.getCompetitorsFromBestToWorst(later);
List<Double> scoresForCompetitors = new ArrayList<Double>();
for (Competitor competitor : rankedCompetitorsInLeaderboard) {
scoresForCompetitors.add(leaderboard1.getTotalPoints(competitor, later));
scoresForCompetitors.add(leaderboard2.getTotalPoints(competitor, later));
}
assertEquals(12, rankedCompetitorsInLeaderboard.size());
assertEquals(c[1], rankedCompetitorsInLeaderboard.get(0));
assertEquals(29, leaderboard1.getTotalPoints(c[1], later), 0.000000001);
assertEquals(7, leaderboard1.getTotalPoints(c[8], later), 0.000000001);
assertEquals(4, leaderboard1.getTotalPoints(c[9], later), 0.000000001);
assertEquals(3, leaderboard1.getTotalPoints(c[11], later), 0.000000001);
assertEquals(29, leaderboard2.getTotalPoints(c[1], later), 0.000000001);
assertEquals(7, leaderboard2.getTotalPoints(c[8], later), 0.000000001);
assertEquals(4, leaderboard2.getTotalPoints(c[9], later), 0.000000001);
assertEquals(3, leaderboard2.getTotalPoints(c[11], later), 0.000000001);
LeaderboardGroup leaderboardGroup = new LeaderboardGroupImpl("Leaderboard Group ESS Overall", "Leaderboard Group", false, Arrays.asList(leaderboard1));
LeaderboardGroup leaderboardGroup = new LeaderboardGroupImpl("Leaderboard Group ESS Overall", "Leaderboard Group", false,
Arrays.asList(leaderboard1, leaderboard2));
leaderboardGroup.setOverallLeaderboard(new LeaderboardGroupMetaLeaderboard(leaderboardGroup, new HighPointExtremeSailingSeriesOverall(),
new ThresholdBasedResultDiscardingRuleImpl(new int[0])));
leaderboardGroup.getOverallLeaderboard().setSuppressed(c[c.length-1], true);
@@ -9,6 +9,7 @@ import com.sap.sailing.domain.base.Competitor;
import com.sap.sailing.domain.base.RaceColumn;
import com.sap.sailing.domain.base.Regatta;
import com.sap.sailing.domain.common.MaxPointsReason;
import com.sap.sailing.domain.common.NoWindException;
import com.sap.sailing.domain.common.ScoringSchemeType;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.domain.common.impl.Util.Pair;
@@ -98,10 +99,11 @@ public interface ScoringScheme extends Serializable {
boolean isValidInTotalScore(Leaderboard leaderboard, RaceColumn raceColumn, TimePoint at);
/**
* Some scoring schemes are applied to {@link LeaderboardGroupMetaLeaderboard} instances. These
* instances of a leaderboard are based on other leaderboards grouped in a {@link LeaderboardGroup}.
* It can happen that the {@link ScoringScheme} needs to have look at the total points of the other leaderboards
* in that group. The ordering of the list containing the total points matches the order in the group.
* Some scoring schemes are applied to {@link LeaderboardGroupMetaLeaderboard} instances. These instances of a
* leaderboard are based on other leaderboards grouped in a {@link LeaderboardGroup}. It can happen that the
* {@link ScoringScheme} needs to have a look at the total points of the other leaderboards in that group. The
* ordering of the list containing the total points matches the order in the group.
* @throws NoWindException
*/
int compareByLatestRegattaInMetaLeaderboard(List<Double> o1TotalPointsForAllOtherLeaderboardsOrdered, List<Double> o2TotalPointsForAllOtherLeaderboardsOrdered);
int compareByLatestRegattaInMetaLeaderboard(Leaderboard leaderboard, Competitor o1, Competitor o2, TimePoint timePoint) throws NoWindException;
}
@@ -182,4 +182,10 @@ public abstract class AbstractScoringSchemeImpl implements ScoringScheme {
}
return numberOfCompetitorsInBetterFleets;
}
@Override
public int compareByLatestRegattaInMetaLeaderboard(Leaderboard leaderboard, Competitor o1, Competitor o2,
TimePoint timePoint) throws NoWindException {
return 0;
}
}
@@ -1,6 +1,5 @@
package com.sap.sailing.domain.leaderboard.impl;
import java.util.List;
import java.util.concurrent.Callable;
import com.sap.sailing.domain.base.Competitor;
@@ -78,11 +77,5 @@ public class HighPoint extends AbstractScoringSchemeImpl {
public boolean isValidInTotalScore(Leaderboard leaderboard, RaceColumn raceColumn, TimePoint at) {
return true;
}
@Override
public int compareByLatestRegattaInMetaLeaderboard(List<Double> o1TotalPointsForAllOtherLeaderboardsOrdered,
List<Double> o2TotalPointsForAllOtherLeaderboardsOrdered) {
return 0;
}
}
@@ -1,18 +1,25 @@
package com.sap.sailing.domain.leaderboard.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.Callable;
import com.sap.sailing.domain.base.Competitor;
import com.sap.sailing.domain.base.RaceColumn;
import com.sap.sailing.domain.common.NoWindException;
import com.sap.sailing.domain.common.ScoringSchemeType;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.domain.common.impl.Util;
import com.sap.sailing.domain.common.impl.Util.Pair;
import com.sap.sailing.domain.leaderboard.Leaderboard;
import com.sap.sailing.domain.leaderboard.MetaLeaderboard;
import com.sap.sailing.domain.leaderboard.NumberOfCompetitorsInLeaderboardFetcher;
/**
* A variant of the {@link HighPoint} scoring scheme which breaks ties differently and which assigns a score of 10 to
* the winner of a regatta, and one less for each subsequent position. This scheme is used particularly by the Extreme
* Sailing Series' overall leaderboard.
* Sailing Series' overall leaderboard and can only be applied to {@link MetaLeaderboard}s.
* <p>
*
* From the Notices of Race: "13.5: If there is a tie in the Series score between two or more boats at any time, the tie
@@ -96,27 +103,45 @@ public class HighPointExtremeSailingSeriesOverall extends HighPoint {
}
/**
* If a tie still remains, it shall be broken in
* favour of the boat that had the better place at the last Regatta sailed.
* Notice of Race (NOR) section 13.5 specifies for the series score: "If a tie still remains, it shall be broken in
* favor of the boat that had the better place at the last Regatta sailed."
* @throws NoWindException
*/
@Override
public int compareByLatestRegattaInMetaLeaderboard(List<Double> o1TotalPointsForAllOtherLeaderboardsOrdered,
List<Double> o2TotalPointsForAllOtherLeaderboardsOrdered) {
int result = 0;
if (o1TotalPointsForAllOtherLeaderboardsOrdered.size() == o2TotalPointsForAllOtherLeaderboardsOrdered.size()) {
for (int i=o1TotalPointsForAllOtherLeaderboardsOrdered.size()-1;i==0;i--) {
Double o1PointsForLeaderboard = o1TotalPointsForAllOtherLeaderboardsOrdered.get(i);
Double o2PointsForLeaderboard = o2TotalPointsForAllOtherLeaderboardsOrdered.get(i);
public int compareByLatestRegattaInMetaLeaderboard(Leaderboard leaderboard,
Competitor o1, Competitor o2, TimePoint timePoint) throws NoWindException {
assert leaderboard instanceof MetaLeaderboard;
// compare by last regatta if this leaderboard is a meta leaderboard
final int result;
if (leaderboard instanceof MetaLeaderboard) {
MetaLeaderboard overallLeaderboard = (MetaLeaderboard) leaderboard;
List<Double> o1PointsInLeaderboardsOfTheGroup = new ArrayList<Double>();
List<Double> o2PointsInLeaderboardsOfTheGroup = new ArrayList<Double>();
List<Leaderboard> randomAccessLeaderboardList = new ArrayList<Leaderboard>(Util.size(overallLeaderboard.getLeaderboards()));
Util.addAll(overallLeaderboard.getLeaderboards(), randomAccessLeaderboardList);
for (Leaderboard leaderboardInOverall : overallLeaderboard.getLeaderboards()) {
o1PointsInLeaderboardsOfTheGroup.add(leaderboardInOverall.getTotalPoints(o1, timePoint));
o2PointsInLeaderboardsOfTheGroup.add(leaderboardInOverall.getTotalPoints(o2, timePoint));
}
int localResult = 0;
for (ListIterator<Leaderboard> reverseLeaderbaordIterator=randomAccessLeaderboardList.listIterator(randomAccessLeaderboardList.size());
reverseLeaderbaordIterator.hasPrevious(); ) {
Leaderboard leaderboardInOverall = reverseLeaderbaordIterator.previous();
final Double o1PointsForLeaderboard = leaderboardInOverall.getTotalPoints(o1, timePoint);
final Double o2PointsForLeaderboard = leaderboardInOverall.getTotalPoints(o2, timePoint);
if (o1PointsForLeaderboard != null && o2PointsForLeaderboard != null) {
// we're in a scheme where points never get 0 so we can safely assume
// that the last total points that are no 0 are the ones that we want to
// look at. We also assume that the ordering matches the one in the group
if (o1PointsForLeaderboard > 0 && o2PointsForLeaderboard > 0) {
result = -o1PointsForLeaderboard.compareTo(o2PointsForLeaderboard);
localResult = -o1PointsForLeaderboard.compareTo(o2PointsForLeaderboard);
break;
}
}
}
result = localResult;
} else {
result = 0;
}
return result;
}
@@ -10,12 +10,12 @@ import java.util.Map;
import com.sap.sailing.domain.base.Competitor;
import com.sap.sailing.domain.base.Fleet;
import com.sap.sailing.domain.base.RaceColumn;
import com.sap.sailing.domain.common.NoWindError;
import com.sap.sailing.domain.common.NoWindException;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.domain.common.impl.Util.Pair;
import com.sap.sailing.domain.leaderboard.Leaderboard;
import com.sap.sailing.domain.leaderboard.ScoringScheme;
import com.sap.sailing.domain.leaderboard.meta.LeaderboardGroupMetaLeaderboard;
/**
* Compares two competitors that occur in a {@link Leaderboard#getCompetitors()} set in the context of the
@@ -162,19 +162,11 @@ public class LeaderboardTotalRankComparator implements Comparator<Competitor> {
if (result == 0) {
// compare by last race:
result = scoringScheme.compareByLastRace(o1Scores, o2Scores, nullScoresAreBetter);
if (result == 0 && (getLeaderboard() instanceof LeaderboardGroupMetaLeaderboard)) {
// compare by last regatta if this leaderboard is a meta leaderboard
LeaderboardGroupMetaLeaderboard overallLeaderboard = (LeaderboardGroupMetaLeaderboard) getLeaderboard();
List<Double> o1PointsInLeaderboardsOfTheGroup = new ArrayList<Double>();
List<Double> o2PointsInLeaderboardsOfTheGroup = new ArrayList<Double>();
if (result == 0) {
try {
for (Leaderboard leaderboardInOverall : overallLeaderboard.getLeaderboards()) {
o1PointsInLeaderboardsOfTheGroup.add(leaderboardInOverall.getTotalPoints(o1, timePoint));
o2PointsInLeaderboardsOfTheGroup.add(leaderboardInOverall.getTotalPoints(o2, timePoint));
}
result = scoringScheme.compareByLatestRegattaInMetaLeaderboard(o1PointsInLeaderboardsOfTheGroup, o2PointsInLeaderboardsOfTheGroup);
result = scoringScheme.compareByLatestRegattaInMetaLeaderboard(getLeaderboard(), o1, o2, timePoint);
} catch (NoWindException e) {
e.printStackTrace();
throw new NoWindError(e);
}
}
}
@@ -1,6 +1,5 @@
package com.sap.sailing.domain.leaderboard.impl;
import java.util.List;
import java.util.concurrent.Callable;
import com.sap.sailing.domain.base.Competitor;
@@ -65,10 +64,4 @@ public class LowPoint extends AbstractScoringSchemeImpl {
public boolean isValidInTotalScore(Leaderboard leaderboard, RaceColumn raceColumn, TimePoint at) {
return true;
}
@Override
public int compareByLatestRegattaInMetaLeaderboard(List<Double> o1TotalPointsForAllOtherLeaderboardsOrdered,
List<Double> o2TotalPointsForAllOtherLeaderboardsOrdered) {
return 0;
}
}
@@ -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,11 +24,15 @@ 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);
return new EventBaseImpl(name, new MillisecondsTimePoint(startDate != null ? startDate.longValue() : 0), new MillisecondsTimePoint(endDate != null ? endDate.longValue() : 1),
venue, true, id);
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";