Esempio n. 1
0
        public void AddOrUpdateSet_InvalidUsername_ReturnsNull(string username)
        {
            var set          = ModelsSetup.GetSetUnderTest();
            var bricksetUser = ModelsSetup.GetBricksetUserUnderTest();

            InsertData(set);
            InsertData(bricksetUser);

            var bricksetUserSet = _bricksetUserRepository.AddOrUpdateSet(username, new BricksetUserSet {
                Set = new Set {
                    SetId = set.SetId
                }
            });

            Check.That(bricksetUserSet).IsNull();
        }
Esempio n. 2
0
        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 async Task SynchronizeBricksetPrimaryUser_SynchronizationTimestampSetAndDoesNotHaveSetsToUpdate_DoesNotUpdateRemoteCollection()
        {
            const string apiKey   = "APIKEY";
            const string userHash = "USERHASH";
            const string testUser = "******";

            _bricksetUserRepository.Add(BricksetUserType.Primary, testUser);

            var themesList    = JsonConvert.DeserializeObject <List <Themes> >(GetResultFileFromResource(Constants.JsonFileGetThemes));
            var subthemesList = JsonConvert.DeserializeObject <List <Subthemes> >(GetResultFileFromResource(Constants.JsonFileGetSubthemes));
            var setsList      = JsonConvert.DeserializeObject <List <Sets> >(GetResultFileFromResource(Constants.JsonFileGetSets));

            var testSetOwned  = setsList[0];
            var ownedTheme    = themesList.First(theme => theme.Theme == testSetOwned.Theme).ToTheme();
            var ownedSubtheme = subthemesList.First(subtheme => subtheme.Theme == testSetOwned.Theme && subtheme.Subtheme == testSetOwned.Subtheme).ToSubtheme();

            ownedSubtheme.Theme = ownedTheme;

            var ownedSet = testSetOwned.ToSet();

            ownedSet.Theme    = ownedTheme;
            ownedSet.Subtheme = ownedSubtheme;

            _themeRepository.AddOrUpdate(ownedTheme);
            _subthemeRepository.AddOrUpdate(ownedSubtheme);
            ownedSet = _setRepository.AddOrUpdate(ownedSet);

            var bricksetUserSet = new BricksetUserSet
            {
                Set           = ownedSet,
                Owned         = true,
                QuantityOwned = 2
            };

            _bricksetUserRepository.AddOrUpdateSet(testUser, bricksetUserSet);

            _bricksetUserRepository.UpdateUserSynchronizationTimestamp(testUser, DateTimeOffset.Now.AddSeconds(1));

            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(apiKey, testUser, userHash).ConfigureAwait(false);

            var user = _bricksetUserRepository.Get(testUser);

            await bricksetApiService
            .DidNotReceive()
            .SetCollection(Arg.Any <SetCollectionParameters>()).ConfigureAwait(false);

            Check.That(user.Sets)
            .Not.IsEmpty()
            .And.CountIs(1);
            Check.That(user.Sets[0].Set.SetId).IsEqualTo(bricksetUserSet.Set.SetId);
            Check.That(user.Sets[0].QuantityOwned).IsEqualTo(bricksetUserSet.QuantityOwned);
            Check.That(user.UserSynchronizationTimestamp).HasAValue();
        }