public static ITaskRepository GetTaskRepository(AuthenticationToken authToken) { if (authToken == null) { throw new ArgumentNullException(nameof(authToken)); } EnforceInitialization(); var urlBuilderFactory = new TasksUrlBuilderFactory(ApiKey, _signatureGenerator); var urlFactory = new TasksUrlFactory(urlBuilderFactory); var taskApiClient = new TaskApiClient(urlFactory, ApiClient, authToken); var responseParser = new Http.Api.Tasks.ResponseParser(GetLocationRepository(authToken)); var taskTreeBuilder = new TaskTreeBuilder(); var taskCache = new InMemoryTaskCache(taskTreeBuilder); var syncTracker = new InMemorySyncTracker(); return(new TaskRepository(taskApiClient, responseParser, taskCache, syncTracker)); }
public async Task GetTasksByListIdAsync_HasTasksFromOtherLists_GetsOtherListsTooUsingLastSync() { // Setup const string masterListId = "My Master List ID"; const string firstForeignListId = "First Foreign List ID"; const string secondForeignListId = "Second Foreign List ID"; var masterLastSync = DateTime.Parse("2018-01-23 12:34:56"); var firstForeignLastSync = masterLastSync.Subtract(TimeSpan.FromSeconds(1)); var secondForeignLastSync = masterLastSync.Add(TimeSpan.FromSeconds(1)); var testSyncTracker = new InMemorySyncTracker(); testSyncTracker.SetLastSync(masterListId, masterLastSync); testSyncTracker.SetLastSync(firstForeignListId, firstForeignLastSync); testSyncTracker.SetLastSync(secondForeignListId, secondForeignLastSync); var masterResponseData = new GetListResponseData(); var firstForeignResponseData = new GetListResponseData(); var secondForeignResponseData = new GetListResponseData(); var fakeApiClient = Substitute.For <ITaskApiClient>(); fakeApiClient.GetTasksByListIdAsync(masterListId).Returns(Task.FromResult(masterResponseData)); fakeApiClient.GetTasksByListIdAsync(firstForeignListId, firstForeignLastSync).Returns(Task.FromResult(firstForeignResponseData)); fakeApiClient.GetTasksByListIdAsync(secondForeignListId, secondForeignLastSync).Returns(Task.FromResult(secondForeignResponseData)); var firstTask = Substitute.For <IRtmTask>(); firstTask.ListId.Returns(firstForeignListId); var secondTask = Substitute.For <IRtmTask>(); secondTask.ListId.Returns(secondForeignListId); var masterTaskList = new List <IRtmTask> { firstTask, secondTask }; var firstForeignTaskList = new List <IRtmTask> { firstTask, secondTask }; var secondForeignTaskList = new List <IRtmTask> { firstTask, secondTask }; var masterDeletedTaskList = new List <IRtmTask>(); var firstForeignDeletedTaskList = new List <IRtmTask>(); var secondForeignDeletedTaskList = new List <IRtmTask>(); var fakeResponseParser = Substitute.For <IResponseParser>(); fakeResponseParser.GetTasks(masterResponseData).Returns(masterTaskList); fakeResponseParser.GetDeletedTasks(masterResponseData).Returns(masterDeletedTaskList); fakeResponseParser.GetTasks(firstForeignResponseData).Returns(firstForeignTaskList); fakeResponseParser.GetDeletedTasks(firstForeignResponseData).Returns(firstForeignDeletedTaskList); fakeResponseParser.GetTasks(secondForeignResponseData).Returns(secondForeignTaskList); fakeResponseParser.GetDeletedTasks(secondForeignResponseData).Returns(secondForeignDeletedTaskList); IList <IRtmTask> expectedTaskList = new List <IRtmTask>(); var mockTaskCache = Substitute.For <ITaskCache>(); mockTaskCache.GetAllAsync(masterListId).Returns(Task.FromResult(expectedTaskList)); // Execute var taskRepository = GetItemUnderTest(fakeApiClient, fakeResponseParser, mockTaskCache, testSyncTracker); var actual = await taskRepository.GetTasksByListIdAsync(masterListId).ConfigureAwait(false); // Verify Assert.AreSame(expectedTaskList, actual); #pragma warning disable 4014 mockTaskCache.DidNotReceive().ClearAsync(); mockTaskCache.Received(1).AddOrReplaceAsync(masterListId, masterTaskList, true); mockTaskCache.DidNotReceive().RemoveAsync(masterDeletedTaskList); mockTaskCache.Received(1).AddOrReplaceAsync(firstForeignListId, firstForeignTaskList); mockTaskCache.Received(1).RemoveAsync(firstForeignDeletedTaskList); mockTaskCache.Received(1).AddOrReplaceAsync(secondForeignListId, secondForeignTaskList); mockTaskCache.Received(1).RemoveAsync(secondForeignDeletedTaskList); #pragma warning restore 4014 }