public async Task SynchronizeBricksetPrimaryUser_UserSynchronizationTimestampNotSetAndDoesNotHaveRemoteSets_DoesNotUpdateLocalSetsAndUpdatesUserSynchronizationTimestamp() { const string testUser = "******"; _bricksetUserRepository.Add(BricksetUserType.Primary, testUser); var bricksetApiService = Substitute.For <IBricksetApiService>(); bricksetApiService .GetSets(Arg.Is <GetSetsParameters>(parameter => parameter.Owned.Value)) .Returns(new List <Sets>()); bricksetApiService .GetSets(Arg.Is <GetSetsParameters>(parameter => parameter.Wanted.Value)) .Returns(new List <Sets>()); var userSynchronizer = CreateTarget(bricksetApiService); await userSynchronizer.SynchronizeBricksetPrimaryUser(string.Empty, testUser, string.Empty).ConfigureAwait(false); var user = _bricksetUserRepository.Get(testUser); Check.That(user.Sets).IsEmpty(); Check.That(user.UserSynchronizationTimestamp).HasAValue(); }
public async Task SynchronizeBricksetPrimaryUser(string apiKey, string username, string userHash) { _messageHub.Publish(new UserSynchronizerStart { UserType = BricksetUserType.Primary, Username = username }); try { var user = _bricksetUserRepository.Get(username); if (user.UserSynchronizationTimestamp.HasValue) { _messageHub.Publish(new AllMyBricksToBricksetStart()); _messageHub.Publish(new AllMyBricksToBricksetAcquiringSetsStart()); var updatedSetsSinceLastSynchronization = user.Sets.Where(set => set.LastChangeTimestamp > user.UserSynchronizationTimestamp.Value).ToList(); _messageHub.Publish(new AllMyBricksToBricksetAcquiringSetsEnd { Count = updatedSetsSinceLastSynchronization.Count }); updatedSetsSinceLastSynchronization.ForEach(bricksetUserSet => { _messageHub.Publish(new UserSynchronizerSynchronizingSetStart { SetId = bricksetUserSet.Set.SetId }); var setCollectionParameter = new SetCollectionParameters { ApiKey = apiKey, UserHash = userHash, SetID = bricksetUserSet.Set.SetId, QtyOwned = bricksetUserSet.QuantityOwned, Want = bricksetUserSet.Wanted, Own = bricksetUserSet.Owned }; _bricksetApiService.SetCollection(setCollectionParameter); _messageHub.Publish(new UserSynchronizerSynchronizingSetEnd { SetId = bricksetUserSet.Set.SetId }); }); _messageHub.Publish(new AllMyBricksToBricksetEnd()); } _messageHub.Publish(new BricksetToAllMyBricksStart()); _messageHub.Publish(new BricksetToAllMyBricksAcquiringSetsStart()); var bricksetUserSets = await GetAllUserSetsFromBrickset(apiKey, userHash : userHash).ConfigureAwait(false); var allMyBricksUserSetIds = user.Sets.Select(bricksetUserSet => bricksetUserSet.Set.SetId); var bricksetUserSetIds = bricksetUserSets.Select(bricksetUserSet => bricksetUserSet.Set.SetId); var setIdsNotInAllMyBricks = bricksetUserSetIds.Except(allMyBricksUserSetIds).ToList(); _messageHub.Publish(new BricksetToAllMyBricksAcquiringSetsEnd { Count = setIdsNotInAllMyBricks.Count }); foreach (var userSetNotInAllMyBricks in bricksetUserSets.Where(bricksetUserSet => setIdsNotInAllMyBricks.Contains(bricksetUserSet.Set.SetId))) { _messageHub.Publish(new UserSynchronizerSynchronizingSetStart { SetId = userSetNotInAllMyBricks.Set.SetId }); _bricksetUserRepository.AddOrUpdateSet(username, userSetNotInAllMyBricks); _messageHub.Publish(new UserSynchronizerSynchronizingSetEnd { SetId = userSetNotInAllMyBricks.Set.SetId }); } _messageHub.Publish(new BricksetToAllMyBricksEnd()); _bricksetUserRepository.UpdateUserSynchronizationTimestamp(username, DateTimeOffset.Now); } catch (Exception ex) { _messageHub.Publish(new UserSynchronizerException { UserType = BricksetUserType.Primary, Username = username, Exception = ex }); } _messageHub.Publish(new UserSynchronizerEnd { UserType = BricksetUserType.Primary, Username = username }); }
public void Get_InvalidUsername_ReturnsNull(string username) { var bricksetUser = _bricksetUserRepository.Get(username); Check.That(bricksetUser).IsNull(); }