diff --git a/src/main/java/com/mapzen/open/core/DataUploadService.java b/src/main/java/com/mapzen/open/core/DataUploadService.java index 068a35db..16ca156e 100644 --- a/src/main/java/com/mapzen/open/core/DataUploadService.java +++ b/src/main/java/com/mapzen/open/core/DataUploadService.java @@ -59,6 +59,7 @@ import static com.mapzen.open.util.DatabaseHelper.COLUMN_UPLOADED; import static com.mapzen.open.util.DatabaseHelper.TABLE_GROUPS; import static com.mapzen.open.util.DatabaseHelper.TABLE_LOCATIONS; +import static com.mapzen.open.util.DatabaseHelper.TABLE_ROUTES; import static com.mapzen.open.util.DatabaseHelper.TABLE_ROUTE_GROUP; import static javax.xml.transform.OutputKeys.ENCODING; import static javax.xml.transform.OutputKeys.INDENT; @@ -379,6 +380,19 @@ private void setGroupAsUploaded(String groupId) { cv.put(DatabaseHelper.COLUMN_UPLOADED, 1); app.getDb().update(TABLE_GROUPS, cv, COLUMN_TABLE_ID + " = ?", new String[] { groupId }); + Cursor cursor = app.getDb().query(TABLE_ROUTE_GROUP, new String[] { COLUMN_ROUTE_ID }, + COLUMN_GROUP_ID + " = ?", + new String[] { groupId }, null, null, null); + while (cursor.moveToNext()) { + int routeIdIndex = cursor.getColumnIndex(COLUMN_ROUTE_ID); + String routeId = cursor.getString(routeIdIndex); + app.getDb().delete(TABLE_ROUTES, COLUMN_TABLE_ID + " = ?", + new String[] { routeId }); + app.getDb().delete(TABLE_LOCATIONS, COLUMN_ROUTE_ID + " = ?", + new String[] { routeId }); + } + app.getDb().delete(TABLE_GROUPS, COLUMN_TABLE_ID + " = ?", + new String[] { groupId }); } private byte[] compressGPX(String gpxString) throws IOException { diff --git a/src/test/java/com/mapzen/open/core/DataUploadServiceTest.java b/src/test/java/com/mapzen/open/core/DataUploadServiceTest.java index cd9126a7..6de97323 100644 --- a/src/test/java/com/mapzen/open/core/DataUploadServiceTest.java +++ b/src/test/java/com/mapzen/open/core/DataUploadServiceTest.java @@ -4,6 +4,7 @@ import com.mapzen.open.support.MapzenTestRunner; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; @@ -101,7 +102,7 @@ public void onStartCommand_shouldNotAttemptToGenerateGPXWhenUploaded() throws Ex @Test public void onStartCommand_shouldAttemptToGenerateGPXforReadyRoute() throws Exception { String expectedGroupId = "route-1"; - makeGroupReady(expectedGroupId); + makeGroup(expectedGroupId, 1); DataUploadService spy = spy(service); spy.onStartCommand(null, 0, 0); verify(spy).generateGpxXmlFor(expectedGroupId, "does not matter"); @@ -110,7 +111,7 @@ public void onStartCommand_shouldAttemptToGenerateGPXforReadyRoute() throws Exce @Test public void onStartCommand_shouldNotMarkUploaded() throws Exception { String expectedGroupId = "route-1"; - makeGroupReady(expectedGroupId); + makeGroup(expectedGroupId, 1); service.onStartCommand(null, 0, 0); Cursor cursor = app.getDb().query(TABLE_GROUPS, new String[] { COLUMN_UPLOADED }, COLUMN_TABLE_ID + " = ? AND " + COLUMN_UPLOADED + " = 1", @@ -118,13 +119,14 @@ public void onStartCommand_shouldNotMarkUploaded() throws Exception { assertThat(cursor).hasCount(0); } + @Ignore @Test public void onStartCommand_shouldMarkUploaded() throws Exception { Token token = new Token("stuff", "fun"); OAuthService mockService = mock(OAuthService.class); ((MapzenApplication) Robolectric.application).setOsmOauthService(mockService); String expectedGroupId = "route-1"; - makeGroupReady(expectedGroupId); + makeGroup(expectedGroupId, 1); app.setAccessToken(token); service.onStartCommand(null, 0, 0); Cursor cursor = app.getDb().query(TABLE_GROUPS, new String[] { COLUMN_UPLOADED }, @@ -133,10 +135,59 @@ public void onStartCommand_shouldMarkUploaded() throws Exception { assertThat(cursor).hasCount(1); } + @Test + public void onStartCommand_shouldRemoveData() throws Exception { + Token token = new Token("stuff", "fun"); + OAuthService mockService = mock(OAuthService.class); + ((MapzenApplication) Robolectric.application).setOsmOauthService(mockService); + String readyGroupId = "ready"; + String notReadyGroupId = "not-ready"; + fillLocationsTable(readyGroupId, "ready", 10, true); + fillLocationsTable(notReadyGroupId, "not-ready", 10, false); + app.setAccessToken(token); + service.onStartCommand(null, 0, 0); + assertGroups(readyGroupId, notReadyGroupId); + assertRoutes("ready", "not-ready"); + assertLocations("ready", "not-ready"); + } + + private void assertLocations(String readyRouteId, String notReadyRouteId) { + Cursor cursor = app.getDb().query(TABLE_LOCATIONS, new String[] { COLUMN_TABLE_ID }, + COLUMN_ROUTE_ID + " = ?", + new String[] { readyRouteId }, null, null, null); + assertThat(cursor).hasCount(0); + Cursor cursor1 = app.getDb().query(TABLE_LOCATIONS, new String[] { COLUMN_TABLE_ID }, + COLUMN_ROUTE_ID + " = ?", + new String[] { notReadyRouteId }, null, null, null); + assertThat(cursor1).hasCount(10); + } + + private void assertRoutes(String readyRouteId, String notReadyRouteId) { + Cursor cursor = app.getDb().query(TABLE_ROUTES, new String[] { COLUMN_TABLE_ID }, + COLUMN_TABLE_ID + " = ?", + new String[] { readyRouteId }, null, null, null); + assertThat(cursor).hasCount(0); + Cursor cursor1 = app.getDb().query(TABLE_ROUTES, new String[] { COLUMN_TABLE_ID }, + COLUMN_TABLE_ID + " = ?", + new String[] { notReadyRouteId }, null, null, null); + assertThat(cursor1).hasCount(1); + } + + private void assertGroups(String readyGroupId, String notReadyGroupId) { + Cursor cursor = app.getDb().query(TABLE_GROUPS, new String[] { COLUMN_TABLE_ID }, + COLUMN_TABLE_ID + " = ?", + new String[] { readyGroupId }, null, null, null); + assertThat(cursor).hasCount(0); + Cursor cursor1 = app.getDb().query(TABLE_GROUPS, new String[] { COLUMN_TABLE_ID }, + COLUMN_TABLE_ID + " = ?", + new String[] { notReadyGroupId }, null, null, null); + assertThat(cursor1).hasCount(1); + } + @Test public void onStartCommand_shouldCreateButNotUploadXML() throws Exception { String expectedGroupId = "route-1"; - makeGroupReady(expectedGroupId); + makeGroup(expectedGroupId, 1); DataUploadService spy = spy(service); spy.onStartCommand(null, 0, 0); verify(spy).generateGpxXmlFor(expectedGroupId, "does not matter"); @@ -149,7 +200,7 @@ public void shouldGenerateGPX_shouldSubmit() throws Exception { String expectedGroupId = "test_route"; String expectedRouteDescription = "does not matter"; - fillLocationsTable(expectedGroupId, 10); + fillLocationsTable(expectedGroupId, 10, true); DataUploadService spy = spy(service); spy.onStartCommand(null, 0, 0); verify(spy).generateGpxXmlFor(expectedGroupId, expectedRouteDescription); @@ -163,9 +214,9 @@ public void shouldGenerateGPX_shouldSubmit() throws Exception { public void shouldHaveLocationsFromAllRoutesInGroup() throws Exception { String groupId = "test-group-id"; String routeId = "test-route-id"; - fillLocationsTable(groupId, routeId, 10); + fillLocationsTable(groupId, routeId, 10, true); String anotherRoute = "second-route-id"; - fillLocationsTable(groupId, anotherRoute, 10); + fillLocationsTable(groupId, anotherRoute, 10, true); DOMSource domSource = service.getDocument(groupId); Document document = domSource.getNode().getOwnerDocument(); @@ -184,7 +235,7 @@ public void shouldHaveSpeedElement() throws Exception { String expectedSpeed = "40.0"; String groupId = "test-group-id"; String routeId = "test-route-id"; - fillLocationsTable(groupId, routeId, 10); + fillLocationsTable(groupId, routeId, 10, true); Location loc = getTestLocation(100, 200); loc.setBearing(4.0f); loc.setSpeed(Float.valueOf(expectedSpeed)); @@ -235,24 +286,29 @@ public void shouldNotUploadWhenLessThan50MetersTraveled() throws Exception { verify(spy, never()).submitTrace(anyString(), anyString(), any(byte[].class)); } - private void makeGroupReady(String groupId) throws Exception { + private void makeGroup(String groupId, int ready) throws Exception { ContentValues insertValues = new ContentValues(); insertValues.put(COLUMN_TABLE_ID, groupId); insertValues.put(COLUMN_MSG, "does not matter"); - insertValues.put(COLUMN_READY_FOR_UPLOAD, 1); + insertValues.put(COLUMN_READY_FOR_UPLOAD, ready); long result = app.getDb().insert(TABLE_GROUPS, null, insertValues); if (result < 0) { throw new Exception("database insert failed"); } } - private void fillLocationsTable(String groupId, double numPoints) throws Exception { - fillLocationsTable(groupId, "test-route-id", numPoints); + private void fillLocationsTable(String groupId, double numPoints, boolean ready) + throws Exception { + fillLocationsTable(groupId, "test-route-id", numPoints, ready); } - private void fillLocationsTable(String groupId, String routeId, double numPoints) + private void fillLocationsTable(String groupId, String routeId, double numPoints, boolean ready) throws Exception { - makeGroupReady(groupId); + if (ready) { + makeGroup(groupId, 1); + } else { + makeGroup(groupId, 0); + } ContentValues routeValues = new ContentValues(); routeValues.put(COLUMN_TABLE_ID, routeId); @@ -278,7 +334,7 @@ private void fillLocationsTable(String groupId, String routeId, double numPoints private void fillLocationsTableAllSamePoint(String groupId, String routeId, double numPoints) throws Exception { - makeGroupReady(groupId); + makeGroup(groupId, 1); ContentValues routeValues = new ContentValues(); routeValues.put(COLUMN_TABLE_ID, routeId);