mirror of https://github.com/tasks/tasks
GtasksList wraps StoreObject
parent
bbaabf8770
commit
eb59cde024
@ -0,0 +1,100 @@
|
|||||||
|
package com.todoroo.astrid.gtasks;
|
||||||
|
|
||||||
|
import com.google.api.client.util.DateTime;
|
||||||
|
import com.google.api.services.tasks.model.TaskList;
|
||||||
|
import com.google.api.services.tasks.model.TaskLists;
|
||||||
|
import com.todoroo.astrid.dao.Database;
|
||||||
|
import com.todoroo.astrid.dao.StoreObjectDao;
|
||||||
|
import com.todoroo.astrid.test.DatabaseTestCase;
|
||||||
|
|
||||||
|
import org.tasks.makers.RemoteGtaskListMaker;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static com.natpryce.makeiteasy.MakeItEasy.with;
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
|
||||||
|
import static org.tasks.makers.GtaskListMaker.LAST_SYNC;
|
||||||
|
import static org.tasks.makers.GtaskListMaker.NAME;
|
||||||
|
import static org.tasks.makers.GtaskListMaker.REMOTE_ID;
|
||||||
|
import static org.tasks.makers.GtaskListMaker.newGtaskList;
|
||||||
|
import static org.tasks.makers.RemoteGtaskListMaker.newRemoteList;
|
||||||
|
|
||||||
|
public class GtasksListServiceTest extends DatabaseTestCase {
|
||||||
|
|
||||||
|
@Inject Database database;
|
||||||
|
|
||||||
|
private StoreObjectDao storeObjectDao;
|
||||||
|
private GtasksListService gtasksListService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp() {
|
||||||
|
super.setUp();
|
||||||
|
storeObjectDao = spy(new StoreObjectDao(database));
|
||||||
|
gtasksListService = new GtasksListService(storeObjectDao);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCreateNewList() {
|
||||||
|
setLists(newRemoteList(
|
||||||
|
with(RemoteGtaskListMaker.REMOTE_ID, "1"),
|
||||||
|
with(RemoteGtaskListMaker.NAME, "Default")));
|
||||||
|
|
||||||
|
verify(storeObjectDao).persist(newGtaskList(
|
||||||
|
with(REMOTE_ID, "1"),
|
||||||
|
with(NAME, "Default")));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetListByRemoteId() {
|
||||||
|
GtasksList list = newGtaskList(with(REMOTE_ID, "1"));
|
||||||
|
storeObjectDao.createNew(list.getStoreObject());
|
||||||
|
|
||||||
|
assertEquals(list, gtasksListService.getList("1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetListReturnsNullWhenNotFound() {
|
||||||
|
assertNull(gtasksListService.getList("1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDeleteMissingList() {
|
||||||
|
storeObjectDao.createNew(newGtaskList(with(REMOTE_ID, "1")).getStoreObject());
|
||||||
|
|
||||||
|
setLists(newRemoteList(with(RemoteGtaskListMaker.REMOTE_ID, "2")));
|
||||||
|
|
||||||
|
verify(storeObjectDao).delete(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testUpdateListName() {
|
||||||
|
storeObjectDao.createNew(newGtaskList(
|
||||||
|
with(REMOTE_ID, "1"),
|
||||||
|
with(NAME, "oldName")).getStoreObject());
|
||||||
|
|
||||||
|
setLists(newRemoteList(
|
||||||
|
with(RemoteGtaskListMaker.REMOTE_ID, "1"),
|
||||||
|
with(RemoteGtaskListMaker.NAME, "newName")));
|
||||||
|
|
||||||
|
assertEquals("newName", storeObjectDao.getGtasksList(1).getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNewListLastSyncIsZero() {
|
||||||
|
setLists(new TaskList().setId("1"));
|
||||||
|
|
||||||
|
assertEquals(0L, gtasksListService.getList("1").getLastSync());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNewListNeedsUpdate() {
|
||||||
|
TaskList taskList = new TaskList().setId("1").setTitle("Default").setUpdated(new DateTime(currentTimeMillis()));
|
||||||
|
|
||||||
|
setLists(taskList);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
asList(newGtaskList(with(REMOTE_ID, "1"), with(LAST_SYNC, 0L))),
|
||||||
|
gtasksListService.getListsToUpdate(new TaskLists().setItems(asList(taskList))));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setLists(TaskList... list) {
|
||||||
|
gtasksListService.updateLists(new TaskLists().setItems(
|
||||||
|
asList(list)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
package org.tasks.makers;
|
||||||
|
|
||||||
|
import com.natpryce.makeiteasy.Instantiator;
|
||||||
|
import com.natpryce.makeiteasy.Property;
|
||||||
|
import com.natpryce.makeiteasy.PropertyLookup;
|
||||||
|
import com.natpryce.makeiteasy.PropertyValue;
|
||||||
|
import com.todoroo.astrid.data.StoreObject;
|
||||||
|
import com.todoroo.astrid.gtasks.GtasksList;
|
||||||
|
|
||||||
|
import static com.natpryce.makeiteasy.Property.newProperty;
|
||||||
|
import static org.tasks.makers.Maker.make;
|
||||||
|
|
||||||
|
public class GtaskListMaker {
|
||||||
|
|
||||||
|
public static final Property<GtasksList, Long> ID = newProperty();
|
||||||
|
public static final Property<GtasksList, Integer> ORDER = newProperty();
|
||||||
|
public static final Property<GtasksList, String> REMOTE_ID = newProperty();
|
||||||
|
public static final Property<GtasksList, Long> LAST_SYNC = newProperty();
|
||||||
|
public static final Property<GtasksList, String> NAME = newProperty();
|
||||||
|
|
||||||
|
public static GtasksList newGtaskList(PropertyValue<? super GtasksList, ?>... properties) {
|
||||||
|
return make(instantiator, properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Instantiator<GtasksList> instantiator = new Instantiator<GtasksList>() {
|
||||||
|
@Override
|
||||||
|
public GtasksList instantiate(final PropertyLookup<GtasksList> lookup) {
|
||||||
|
return new GtasksList(new StoreObject() {{
|
||||||
|
setType(GtasksList.TYPE);
|
||||||
|
setValue(StoreObject.ID, lookup.valueOf(GtaskListMaker.ID, 1L));
|
||||||
|
setValue(StoreObject.ITEM, lookup.valueOf(REMOTE_ID, "1"));
|
||||||
|
setValue(StoreObject.VALUE1, lookup.valueOf(NAME, "Default"));
|
||||||
|
setValue(StoreObject.VALUE2, String.valueOf(lookup.valueOf(ORDER, 0)));
|
||||||
|
setValue(StoreObject.VALUE3, String.valueOf(lookup.valueOf(LAST_SYNC, 0L)));
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
package org.tasks.makers;
|
||||||
|
|
||||||
|
import com.natpryce.makeiteasy.Instantiator;
|
||||||
|
import com.natpryce.makeiteasy.PropertyValue;
|
||||||
|
|
||||||
|
import static com.natpryce.makeiteasy.MakeItEasy.a;
|
||||||
|
|
||||||
|
public class Maker {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> T make(Instantiator<T> instantiator, PropertyValue<? super T, ?>... properties) {
|
||||||
|
return com.natpryce.makeiteasy.MakeItEasy.make(a(instantiator, properties));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package org.tasks.makers;
|
||||||
|
|
||||||
|
import com.google.api.client.util.DateTime;
|
||||||
|
import com.google.api.services.tasks.model.TaskList;
|
||||||
|
import com.natpryce.makeiteasy.Instantiator;
|
||||||
|
import com.natpryce.makeiteasy.Property;
|
||||||
|
import com.natpryce.makeiteasy.PropertyLookup;
|
||||||
|
import com.natpryce.makeiteasy.PropertyValue;
|
||||||
|
|
||||||
|
import static com.natpryce.makeiteasy.Property.newProperty;
|
||||||
|
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
|
||||||
|
import static org.tasks.makers.Maker.make;
|
||||||
|
|
||||||
|
public class RemoteGtaskListMaker {
|
||||||
|
public static final Property<TaskList, String> REMOTE_ID = newProperty();
|
||||||
|
public static final Property<TaskList, String> NAME = newProperty();
|
||||||
|
|
||||||
|
public static TaskList newRemoteList(PropertyValue<? super TaskList, ?>... properties) {
|
||||||
|
return make(instantiator, properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Instantiator<TaskList> instantiator = new Instantiator<TaskList>() {
|
||||||
|
@Override
|
||||||
|
public TaskList instantiate(final PropertyLookup<TaskList> lookup) {
|
||||||
|
return new TaskList()
|
||||||
|
.setId(lookup.valueOf(REMOTE_ID, "1"))
|
||||||
|
.setTitle(lookup.valueOf(NAME, "Default"))
|
||||||
|
.setUpdated(new DateTime(currentTimeMillis()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue