Added unit test and fixed bug for UserMigrated message

pull/14/head
Sam Bosley 11 years ago
parent 330edc80b0
commit 2e2fea9deb

@ -17,7 +17,7 @@ public class UserMigrated extends ServerToClientMessage {
String newUuid = json.optString("new_user_id"); //$NON-NLS-1$ String newUuid = json.optString("new_user_id"); //$NON-NLS-1$
if (RemoteModel.isValidUuid(newUuid)) { if (RemoteModel.isValidUuid(newUuid)) {
Preferences.setString(ActFmPreferenceService.PREF_USER_ID, newUuid); Preferences.setString(ActFmPreferenceService.PREF_USER_ID, newUuid);
new ConvertSelfUserIdsToZero(); new ConvertSelfUserIdsToZero().execute();
} }
} }

@ -5,19 +5,22 @@ import org.json.JSONObject;
import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.actfm.sync.ActFmSyncThread.ModelType; import com.todoroo.astrid.actfm.sync.ActFmSyncThread.ModelType;
import com.todoroo.astrid.actfm.sync.messages.ChangesHappened; import com.todoroo.astrid.actfm.sync.messages.ChangesHappened;
import com.todoroo.astrid.actfm.sync.messages.NameMaps; import com.todoroo.astrid.actfm.sync.messages.NameMaps;
import com.todoroo.astrid.actfm.sync.messages.ReplayOutstandingEntries; import com.todoroo.astrid.actfm.sync.messages.ReplayOutstandingEntries;
import com.todoroo.astrid.actfm.sync.messages.ServerToClientMessage; import com.todoroo.astrid.actfm.sync.messages.ServerToClientMessage;
import com.todoroo.astrid.actfm.sync.messages.UserMigrated;
import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.SyncFlags; import com.todoroo.astrid.data.SyncFlags;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskOutstanding; import com.todoroo.astrid.data.TaskOutstanding;
public class SyncMessageTest extends NewSyncTestCase { public class SyncMessageTest extends NewSyncTestCase {
public void testTaskChangesHappenedConstructor() { public void testTaskChangesHappenedConstructor() {
Task t = createTask(); Task t = createTask();
try { try {
@ -30,15 +33,15 @@ public class SyncMessageTest extends NewSyncTestCase {
fail("ChangesHappened constructor threw exception " + e); fail("ChangesHappened constructor threw exception " + e);
} }
} }
private static final String MAKE_CHANGES_TITLE = "Made changes to title"; private static final String MAKE_CHANGES_TITLE = "Made changes to title";
private JSONObject getMakeChanges(String... extraChanges) throws JSONException { private JSONObject getMakeChanges(String... extraChanges) throws JSONException {
JSONObject makeChanges = new JSONObject(); JSONObject makeChanges = new JSONObject();
makeChanges.put("type", ServerToClientMessage.TYPE_MAKE_CHANGES); makeChanges.put("type", ServerToClientMessage.TYPE_MAKE_CHANGES);
makeChanges.put("table", NameMaps.TABLE_ID_TASKS); makeChanges.put("table", NameMaps.TABLE_ID_TASKS);
JSONObject changes = new JSONObject(); JSONObject changes = new JSONObject();
changes.put("title", MAKE_CHANGES_TITLE); changes.put("title", MAKE_CHANGES_TITLE);
changes.put("importance", Task.IMPORTANCE_DO_OR_DIE); changes.put("importance", Task.IMPORTANCE_DO_OR_DIE);
if (extraChanges != null) { if (extraChanges != null) {
for (int i = 0; i < extraChanges.length - 1; i+=2) { for (int i = 0; i < extraChanges.length - 1; i+=2) {
@ -47,20 +50,20 @@ public class SyncMessageTest extends NewSyncTestCase {
changes.put(key, value); changes.put(key, value);
} }
} }
makeChanges.put("changes", changes); makeChanges.put("changes", changes);
return makeChanges; return makeChanges;
} }
public void testMakeChangesMakesChanges() { public void testMakeChangesMakesChanges() {
Task t = createTask(); Task t = createTask();
try { try {
JSONObject makeChanges = getMakeChanges(); JSONObject makeChanges = getMakeChanges();
makeChanges.put("uuid", t.getValue(Task.UUID)); makeChanges.put("uuid", t.getValue(Task.UUID));
ServerToClientMessage message = ServerToClientMessage.instantiateMessage(makeChanges); ServerToClientMessage message = ServerToClientMessage.instantiateMessage(makeChanges);
message.processMessage(null); message.processMessage(null);
t = taskDao.fetch(t.getId(), Task.TITLE, Task.IMPORTANCE); t = taskDao.fetch(t.getId(), Task.TITLE, Task.IMPORTANCE);
assertEquals(MAKE_CHANGES_TITLE, t.getValue(Task.TITLE)); assertEquals(MAKE_CHANGES_TITLE, t.getValue(Task.TITLE));
assertEquals(Task.IMPORTANCE_DO_OR_DIE, t.getValue(Task.IMPORTANCE).intValue()); assertEquals(Task.IMPORTANCE_DO_OR_DIE, t.getValue(Task.IMPORTANCE).intValue());
@ -69,33 +72,33 @@ public class SyncMessageTest extends NewSyncTestCase {
fail("JSONException"); fail("JSONException");
} }
} }
public void testMakeChangesMakesNewTasks() { public void testMakeChangesMakesNewTasks() {
try { try {
JSONObject makeChanges = getMakeChanges(); JSONObject makeChanges = getMakeChanges();
makeChanges.put("uuid", "1"); makeChanges.put("uuid", "1");
ServerToClientMessage message = ServerToClientMessage.instantiateMessage(makeChanges); ServerToClientMessage message = ServerToClientMessage.instantiateMessage(makeChanges);
message.processMessage(null); message.processMessage(null);
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID, Task.UUID, Task.TITLE, Task.IMPORTANCE).where(Task.UUID.eq("1"))); TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID, Task.UUID, Task.TITLE, Task.IMPORTANCE).where(Task.UUID.eq("1")));
try { try {
assertEquals(1, cursor.getCount()); assertEquals(1, cursor.getCount());
cursor.moveToFirst(); cursor.moveToFirst();
Task t = new Task(cursor); Task t = new Task(cursor);
assertEquals(MAKE_CHANGES_TITLE, t.getValue(Task.TITLE)); assertEquals(MAKE_CHANGES_TITLE, t.getValue(Task.TITLE));
assertEquals(Task.IMPORTANCE_DO_OR_DIE, t.getValue(Task.IMPORTANCE).intValue()); assertEquals(Task.IMPORTANCE_DO_OR_DIE, t.getValue(Task.IMPORTANCE).intValue());
assertEquals("1", t.getValue(Task.UUID)); assertEquals("1", t.getValue(Task.UUID));
} finally { } finally {
cursor.close(); cursor.close();
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
fail("JSONException"); fail("JSONException");
} }
} }
public void testMakeChangesWithUuidCollision() { public void testMakeChangesWithUuidCollision() {
Task t = createTask(); Task t = createTask();
String oldUuid = t.getUuid(); String oldUuid = t.getUuid();
@ -105,7 +108,7 @@ public class SyncMessageTest extends NewSyncTestCase {
makeChanges.put("uuid", oldUuid); makeChanges.put("uuid", oldUuid);
ServerToClientMessage message = ServerToClientMessage.instantiateMessage(makeChanges); ServerToClientMessage message = ServerToClientMessage.instantiateMessage(makeChanges);
message.processMessage(null); message.processMessage(null);
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.UUID).where(Task.UUID.eq(newUuid))); TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.UUID).where(Task.UUID.eq(newUuid)));
try { try {
assertEquals(1, cursor.getCount()); assertEquals(1, cursor.getCount());
@ -124,20 +127,46 @@ public class SyncMessageTest extends NewSyncTestCase {
fail("JSONException"); fail("JSONException");
} }
} }
public void testReplayOutstandingEntries() { public void testReplayOutstandingEntries() {
Task t = createTask(); Task t = createTask();
t.setValue(Task.TITLE, "change title"); t.setValue(Task.TITLE, "change title");
t.setValue(Task.IMPORTANCE, Task.IMPORTANCE_NONE); t.setValue(Task.IMPORTANCE, Task.IMPORTANCE_NONE);
t.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true); t.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
taskDao.save(t); taskDao.save(t);
new ReplayOutstandingEntries<Task, TaskOutstanding>(Task.class, NameMaps.TABLE_ID_TASKS, taskDao, taskOutstandingDao, false).execute(); new ReplayOutstandingEntries<Task, TaskOutstanding>(Task.class, NameMaps.TABLE_ID_TASKS, taskDao, taskOutstandingDao, false).execute();
t = taskDao.fetch(t.getId(), Task.TITLE, Task.IMPORTANCE); t = taskDao.fetch(t.getId(), Task.TITLE, Task.IMPORTANCE);
assertEquals(SYNC_TASK_TITLE, t.getValue(Task.TITLE)); assertEquals(SYNC_TASK_TITLE, t.getValue(Task.TITLE));
assertEquals(SYNC_TASK_IMPORTANCE, t.getValue(Task.IMPORTANCE).intValue()); assertEquals(SYNC_TASK_IMPORTANCE, t.getValue(Task.IMPORTANCE).intValue());
} }
public void testUserMigratedUpdatesUserIds() {
Task t = createTask("Someone else's", true);
TagData td = createTagData("Someone else's tag", true);
t.setValue(Task.USER_ID, "1");
taskDao.save(t);
td.setValue(TagData.USER_ID, "1");
tagDataDao.saveExisting(td);
JSONObject userMigrated = new JSONObject();
try {
userMigrated.put("type", ServerToClientMessage.TYPE_USER_MIGRATED);
userMigrated.put("new_user_id", "1");
} catch (JSONException e) {
fail("JSONException");
}
new UserMigrated(userMigrated).processMessage(null);
t = taskDao.fetch(t.getId(), Task.USER_ID);
td = tagDataDao.fetch(td.getId(), TagData.USER_ID);
assertEquals("1", ActFmPreferenceService.userId());
assertEquals(Task.USER_ID_SELF, t.getValue(Task.USER_ID));
assertEquals(Task.USER_ID_SELF, td.getValue(TagData.USER_ID));
}
} }

Loading…
Cancel
Save