making locally GWT-compilable again; addressed part of bug 2223 by making AddWaypoint/RemoveWaypoint idempotent, including a test that asserts this

This commit is contained in:
Axel Uhl committed 2014-08-29 18:08:52 +02:00
1 parent 08ab552f04
commit a4af7c1128
12 files changed
+202 -25

No files matched your search

@@ -21,4 +21,5 @@ Require-Bundle: com.sap.sailing.server,
org.json.simple;bundle-version="1.1.0",
org.junit4;bundle-version="4.8.2",
org.hamcrest;bundle-version="1.1.0",
com.sap.sse.common
com.sap.sse.common,
org.mockito.mockito-core;bundle-version="1.9.5"
@@ -0,0 +1,91 @@
package com.sap.sailing.server.replication.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.sap.sailing.domain.base.Course;
import com.sap.sailing.domain.base.RaceDefinition;
import com.sap.sailing.domain.base.Waypoint;
import com.sap.sailing.domain.base.impl.CourseImpl;
import com.sap.sailing.domain.base.impl.MarkImpl;
import com.sap.sailing.domain.base.impl.WaypointImpl;
import com.sap.sailing.domain.common.RegattaAndRaceIdentifier;
import com.sap.sailing.domain.common.RegattaNameAndRaceName;
import com.sap.sailing.server.RacingEventService;
import com.sap.sailing.server.operationaltransformation.AddWaypoint;
import com.sap.sailing.server.operationaltransformation.RemoveWaypoint;
import com.sap.sse.common.Util;
/**
* See also bug 2223 (http://bugzilla.sapsailing.com/bugzilla/show_bug.cgi?id=2223). The application of
* {@link AddWaypoint} and {@link RemoveWaypoint} needs to be idempotent so that when the initial load
* already contains the serialized results of the application of these operations on the master, the operations
* must simply have no effect on the replica.
*
* @author Axel Uhl (D043530)
*
*/
public class IdempotencyForWaypointOperationsTest {
private RacingEventService service;
private RaceDefinition race;
private Course course;
@Before
public void setUp() {
service = mock(RacingEventService.class);
race = mock(RaceDefinition.class);
when(service.getRace(any(RegattaAndRaceIdentifier.class))).thenReturn(race);
final Iterable<Waypoint> waypoints = Arrays.<Waypoint>asList(
new WaypointImpl(new MarkImpl("M1")),
new WaypointImpl(new MarkImpl("M2")),
new WaypointImpl(new MarkImpl("M3")),
new WaypointImpl(new MarkImpl("M4")),
new WaypointImpl(new MarkImpl("M5")),
new WaypointImpl(new MarkImpl("M6"))
);
course = new CourseImpl("Course name", waypoints);
when(race.getCourse()).thenReturn(course);
}
@Test
public void testCourseIsAlright() {
assertEquals(6, Util.size(course.getWaypoints()));
}
@Test
public void testAddWaypointIsIdempotent() throws Exception {
final WaypointImpl newWaypoint = new WaypointImpl(new MarkImpl("M1.5"));
final List<Waypoint> waypointsAfterAdd = new ArrayList<>();
for (Waypoint wp : course.getWaypoints()) {
waypointsAfterAdd.add(wp);
}
waypointsAfterAdd.add(1, newWaypoint);
AddWaypoint op = new AddWaypoint(new RegattaNameAndRaceName("abc", "def"), 1, newWaypoint, waypointsAfterAdd);
op.internalApplyTo(service);
final Iterable<Waypoint> waypointsAfterFirstAdd = course.getWaypoints();
assertSame(newWaypoint, Util.get(waypointsAfterFirstAdd, 1));
op.internalApplyTo(service);
assertEquals(waypointsAfterFirstAdd, course.getWaypoints());
}
@Test
public void testRemoveWaypointIsIdempotent() throws Exception {
int oldNumberOfWaypoints = Util.size(course.getWaypoints());
RemoveWaypoint op = new RemoveWaypoint(new RegattaNameAndRaceName("abc", "def"), 1, Util.get(course.getWaypoints(), 1));
op.internalApplyTo(service);
final Iterable<Waypoint> waypointsAfterFirstRemove = course.getWaypoints();
assertEquals(oldNumberOfWaypoints-1, Util.size(waypointsAfterFirstRemove));
op.internalApplyTo(service);
assertEquals(waypointsAfterFirstRemove, course.getWaypoints());
}
}