added implementation and test case for TrackeRace.getCenterOfCourse

This commit is contained in:
Axel Uhl committed 2014-07-25 16:38:30 +02:00
1 parent 6edd663fdf
commit 154b0de382
8 files changed
+136 -3

No files matched your search

@@ -0,0 +1,77 @@
package com.sap.sailing.domain.test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import com.sap.sailing.domain.base.Course;
import com.sap.sailing.domain.base.Mark;
import com.sap.sailing.domain.base.RaceDefinition;
import com.sap.sailing.domain.base.Waypoint;
import com.sap.sailing.domain.base.impl.MarkImpl;
import com.sap.sailing.domain.base.impl.WaypointImpl;
import com.sap.sailing.domain.common.Position;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.domain.common.impl.DegreePosition;
import com.sap.sailing.domain.common.impl.MillisecondsTimePoint;
import com.sap.sailing.domain.tracking.DynamicGPSFixTrack;
import com.sap.sailing.domain.tracking.GPSFix;
import com.sap.sailing.domain.tracking.TrackedRace;
import com.sap.sailing.domain.tracking.impl.DynamicGPSFixTrackImpl;
import com.sap.sailing.domain.tracking.impl.DynamicTrackedRaceImpl;
import com.sap.sailing.domain.tracking.impl.GPSFixImpl;
/**
* Tests the calculation of the center of the course, particularly the
* {@link TrackedRace#getCenterOfCourse(com.sap.sailing.domain.common.TimePoint)} method.
*
* @author Axel Uhl (D043530)
*
*/
public class TrackedRaceCenterTest {
private Mark mark1;
private Mark mark2;
private Waypoint wp1;
private Waypoint wp2;
private DynamicGPSFixTrack<Mark, GPSFix> mark1Track;
private DynamicGPSFixTrack<Mark, GPSFix> mark2Track;
private DynamicTrackedRaceImpl trackedRace;
@Before
public void setUp() {
trackedRace = mock(DynamicTrackedRaceImpl.class);
when(trackedRace.getCenterOfCourse(Matchers.any(TimePoint.class))).thenCallRealMethod();
when(trackedRace.getApproximatePosition(Matchers.any(Waypoint.class), Matchers.any(TimePoint.class))).thenCallRealMethod();
RaceDefinition race = mock(RaceDefinition.class);
when(trackedRace.getRace()).thenReturn(race);
Course course = mock(Course.class);
when(race.getCourse()).thenReturn(course);
mark1 = new MarkImpl("1");
mark2 = new MarkImpl("2");
wp1 = new WaypointImpl(mark1);
wp2 = new WaypointImpl(mark2);
when(course.getWaypoints()).thenReturn(Arrays.asList(new Waypoint[] { wp1, wp2 }));
mark1Track = new DynamicGPSFixTrackImpl<Mark>(mark1, /* millisecondsOverWhichToAverage */ 10);
mark2Track = new DynamicGPSFixTrackImpl<Mark>(mark1, /* millisecondsOverWhichToAverage */ 10);
when(trackedRace.getOrCreateTrack(mark1)).thenReturn(mark1Track);
when(trackedRace.getOrCreateTrack(mark2)).thenReturn(mark2Track);
}
@Test
public void testSimpleAverage() {
MillisecondsTimePoint now = MillisecondsTimePoint.now();
Position mark1Pos = new DegreePosition(10, 10);
mark1Track.add(new GPSFixImpl(mark1Pos, now));
Position mark2Pos = new DegreePosition(20, 10);
mark2Track.add(new GPSFixImpl(mark2Pos, now));
Position center = trackedRace.getCenterOfCourse(now);
assertEquals(15, center.getLatDeg(), 0.00001);
assertEquals(10, center.getLngDeg(), 0.00001);
}
}
@@ -1006,4 +1006,10 @@ public class MockedTrackedRace implements DynamicTrackedRace {
}
@Override
public Position getCenterOfCourse(TimePoint at) {
// TODO Auto-generated method stub
return null;
}
}
@@ -653,4 +653,10 @@ public class MockedTrackedRaceWithStartTimeAndRanks implements TrackedRace {
}
@Override
public Position getCenterOfCourse(TimePoint at) {
// TODO Auto-generated method stub
return null;
}
}
@@ -703,4 +703,9 @@ public interface TrackedRace extends Serializable {
GPSFixStore getGPSFixStore();
/**
* Computes the center point of the course's marks at the given time point.
*/
Position getCenterOfCourse(TimePoint at);
}
@@ -3086,4 +3086,29 @@ public abstract class TrackedRaceImpl extends TrackedRaceWithWindEssentials impl
public GPSFixStore getGPSFixStore() {
return gpsFixStore;
}
@Override
public Position getCenterOfCourse(TimePoint at) {
int count = 0;
ScalablePosition sum = null;
for (Waypoint waypoint : getRace().getCourse().getWaypoints()) {
final Position waypointPosition = getApproximatePosition(waypoint, at);
if (waypointPosition != null) {
ScalablePosition p = new ScalablePosition(waypointPosition);
if (sum == null) {
sum = p;
} else {
sum = sum.add(p);
}
}
}
final Position result;
if (sum == null) {
result = null;
} else {
result = sum.divide(count);
}
return result;
}
}
@@ -1007,4 +1007,10 @@ public class MockedTrackedRace implements DynamicTrackedRace {
}
@Override
public Position getCenterOfCourse(TimePoint at) {
// TODO Auto-generated method stub
return null;
}
}
@@ -1302,7 +1302,7 @@ public class SailingServiceImpl extends ProxiedRemoteServiceServlet implements S
* {@link WindInfoForRaceDTO#getCombinedWindOnLegMiddle(int)}.
*/
private WindInfoForRaceDTO getAveragedWindInfo(TimePoint from, long millisecondsStepWidth, int numberOfFixes,
Collection<String> windSourceTypeNames, TrackedRace trackedRace, boolean onlyUpToNewestEvent,
Collection<String> windSourceTypeNames, final TrackedRace trackedRace, boolean onlyUpToNewestEvent,
boolean includeCombinedWindForAllLegMiddles) {
WindInfoForRaceDTO result = null;
if (trackedRace != null) {
@@ -1320,12 +1320,14 @@ public class SailingServiceImpl extends ProxiedRemoteServiceServlet implements S
Util.addAll(trackedRace.getWindSources(), windSourcesToDeliver);
final WindSource combinedWindSource = new WindSourceImpl(WindSourceType.COMBINED);
windSourcesToDeliver.add(combinedWindSource);
for (WindSource windSource : windSourcesToDeliver) {
for (final WindSource windSource : windSourcesToDeliver) {
// TODO consider parallelizing
if (windSourceTypeNames == null || windSourceTypeNames.contains(windSource.getType().name())) {
WindTrackInfoDTO windTrackInfoDTO = createWindTrackInfoDTO(from, millisecondsStepWidth,
numberOfFixes, trackedRace, onlyUpToNewestEvent, newestEvent, windSource,
new PositionAtTimeProvider() { @Override public Position getPosition(TimePoint at) { return null; }});
new PositionAtTimeProvider() { @Override public Position getPosition(TimePoint at) {
return windSource == combinedWindSource ? trackedRace.getCenterOfCourse(at) : null;
}});
windTrackInfoDTOs.put(windSource, windTrackInfoDTO);
}
}
@@ -605,4 +605,10 @@ public class DummyTrackedRace extends TrackedRaceWithWindEssentials {
}
@Override
public Position getCenterOfCourse(TimePoint at) {
// TODO Auto-generated method stub
return null;
}
}