public async Task SynchronizeForThemeAndSubtheme_BricksetApiServiceReturnsListOfSets_AllSetsAreSaved()
        {
            var themesList           = JsonConvert.DeserializeObject <List <Themes> >(GetResultFileFromResource(Constants.JsonFileGetThemes));
            var testTheme            = themesList.First(themes => themes.Theme == Constants.TestThemeArchitecture);
            var yearsList            = JsonConvert.DeserializeObject <List <Years> >(GetResultFileFromResource(Constants.JsonFileGetYears));
            var subthemesList        = JsonConvert.DeserializeObject <List <Subthemes> >(GetResultFileFromResource(Constants.JsonFileGetSubthemes));
            var setsList             = JsonConvert.DeserializeObject <List <Sets> >(GetResultFileFromResource(Constants.JsonFileGetSets));
            var testSet              = setsList.First(set => set.SetId == Constants.TestSetId);
            var additionalImagesList = JsonConvert.DeserializeObject <List <SetImage> >(GetResultFileFromResource(Constants.JsonFileGetAdditionalImages));
            var instructionsList     = JsonConvert.DeserializeObject <List <Instructions> >(GetResultFileFromResource(Constants.JsonFileGetInstructions));
            var testSubtheme         = subthemesList.First(bricksetSubtheme => bricksetSubtheme.Subtheme == testSet.Subtheme);

            var theme = testTheme.ToTheme();

            theme.SetCountPerYear = yearsList.ToYearSetCountEnumerable().ToList();

            _themeRepository.AddOrUpdate(theme);

            foreach (var subthemeItem in subthemesList)
            {
                var subthemeTheme = subthemeItem.ToSubtheme();
                subthemeTheme.Theme = theme;

                _subthemeRepository.AddOrUpdate(subthemeTheme);
            }

            var subtheme = testSubtheme.ToSubtheme();

            subtheme.Theme = theme;

            var bricksetApiService = Substitute.For <IBricksetApiService>();

            bricksetApiService
            .GetSets(Arg.Any <GetSetsParameters>())
            .Returns(ret => setsList.Where(setFromList => setFromList.Year == ((GetSetsParameters)ret.Args()[0]).Year &&
                                           setFromList.Theme == ((GetSetsParameters)ret.Args()[0]).Theme &&
                                           setFromList.Subtheme == ((GetSetsParameters)ret.Args()[0]).Subtheme));
            bricksetApiService
            .GetAdditionalImages(Arg.Is <ParameterSetId>(parameter => parameter.SetID == testSet.SetId))
            .Returns(additionalImagesList);
            bricksetApiService
            .GetInstructions(Arg.Is <ParameterSetId>(parameter => parameter.SetID == testSet.SetId))
            .Returns(instructionsList);

            var setSynchronizer = CreateTarget(bricksetApiService);

            await setSynchronizer.Synchronize(string.Empty, theme, subtheme).ConfigureAwait(false);

            var expectedSets = setsList.Where(bricksetSets => bricksetSets.Subtheme == subtheme.Name).ToList();

            Check.That(_setRepository.All()).CountIs(expectedSets.Count);
            var persistedSet = _setRepository.Get(testSet.SetId);

            Check.That(persistedSet.Images).CountIs(additionalImagesList.Count);
            Check.That(persistedSet.Instructions).CountIs(instructionsList.Count);
        }
Пример #2
0
        public async Task Synchronize_BricksetApiServiceReturnsListOfSubthemes_AllSubthemesAreSaved()
        {
            var testTheme     = JsonConvert.DeserializeObject <List <Themes> >(GetResultFileFromResource(Constants.JsonFileGetThemes)).First(themes => themes.Theme == Constants.TestThemeArchitecture);
            var yearsList     = JsonConvert.DeserializeObject <List <Years> >(GetResultFileFromResource(Constants.JsonFileGetYears));
            var subthemesList = JsonConvert.DeserializeObject <List <Subthemes> >(GetResultFileFromResource(Constants.JsonFileGetSubthemes));

            var theme = testTheme.ToTheme();

            theme.SetCountPerYear = yearsList.ToYearSetCountEnumerable().ToList();

            _themeRepository.AddOrUpdate(theme);

            var bricksetApiService = Substitute.For <IBricksetApiService>();

            bricksetApiService
            .GetSubthemes(Arg.Any <ParameterTheme>())
            .Returns(subthemesList);

            var subthemeSynchronizer = CreateTarget(bricksetApiService);

            var subthemes = await subthemeSynchronizer.Synchronize(string.Empty, theme).ConfigureAwait(false);

            Check.That(subthemes).CountIs(subthemesList.Count);
            Check.That(_subthemeRepository.All()).CountIs(subthemesList.Count);
        }
        public async Task SynchronizeBricksetPrimaryUser_SynchronizationTimestampNotSetAndHasRemoteSets_UpdatesLocalSetsAndUpdatesUserSynchronizationTimestamp()
        {
            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];

            testSetOwned.Collection = new SetCollection
            {
                Owned    = true,
                QtyOwned = 2
            };

            var ownedTheme = themesList.First(theme => theme.Theme == testSetOwned.Theme).ToTheme();

            ownedTheme = _themeRepository.AddOrUpdate(ownedTheme);

            var ownedSubtheme = subthemesList.First(subtheme => subtheme.Theme == testSetOwned.Theme && subtheme.Subtheme == testSetOwned.Subtheme).ToSubtheme();

            ownedSubtheme.Theme = ownedTheme;

            ownedSubtheme = _subthemeRepository.AddOrUpdate(ownedSubtheme);

            var ownedSet = testSetOwned.ToSet();

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

            _setRepository.AddOrUpdate(ownedSet);

            var testSetWanted = setsList[1];

            testSetWanted.Collection = new SetCollection
            {
                Wanted = true
            };

            var wantedTheme = themesList.First(theme => theme.Theme == testSetWanted.Theme).ToTheme();

            wantedTheme = wantedTheme.Name == ownedTheme.Name
                ? ownedTheme
                : _themeRepository.AddOrUpdate(wantedTheme);

            var wantedSubtheme = subthemesList.First(subtheme => subtheme.Theme == testSetWanted.Theme && subtheme.Subtheme == testSetWanted.Subtheme).ToSubtheme();

            wantedSubtheme.Theme = wantedTheme;

            wantedSubtheme       = wantedSubtheme.Name == ownedSubtheme.Name && wantedSubtheme.Theme.Name == ownedSubtheme.Theme.Name
                ? wantedSubtheme = ownedSubtheme
                : _subthemeRepository.AddOrUpdate(wantedSubtheme);

            var wantedSet = testSetWanted.ToSet();

            wantedSet.Theme    = wantedTheme;
            wantedSet.Subtheme = wantedSubtheme;

            _setRepository.AddOrUpdate(wantedSet);

            var bricksetApiService = Substitute.For <IBricksetApiService>();

            bricksetApiService
            .GetSets(Arg.Is <GetSetsParameters>(parameter => parameter.Owned.Value))
            .Returns(new List <Sets> {
                testSetOwned
            });

            bricksetApiService
            .GetSets(Arg.Is <GetSetsParameters>(parameter => parameter.Wanted.Value))
            .Returns(new List <Sets> {
                testSetWanted
            });

            var userSynchronizer = CreateTarget(bricksetApiService);

            await userSynchronizer.SynchronizeBricksetPrimaryUser(apiKey, testUser, userHash).ConfigureAwait(false);

            var user = _bricksetUserRepository.Get(testUser);

            Check.That(user.Sets).Not.IsEmpty();
            Check.That(user.Sets.Where(userSet => userSet.Set.SetId == testSetOwned.SetId && userSet.Owned == testSetOwned.Collection.Owned && userSet.QuantityOwned == testSetOwned.Collection.QtyOwned)).Not.IsEmpty();
            Check.That(user.Sets.Where(userSet => userSet.Set.SetId == testSetWanted.SetId && userSet.Wanted == testSetWanted.Collection.Wanted)).Not.IsEmpty();
            Check.That(user.UserSynchronizationTimestamp).HasAValue();
        }
Пример #4
0
        public void AddOrUpdate_NullTheme_ReturnsNull()
        {
            var theme = _themeRepository.AddOrUpdate(null);

            Check.That(theme).IsNull();
        }
        public async Task <IEnumerable <Theme> > Synchronize(string apiKey)
        {
            _messageHub.Publish(new ThemeSynchronizerStart());

            var themeList = new List <Theme>();

            try
            {
                var getThemesParameters = new ParameterApiKey
                {
                    ApiKey = apiKey
                };

                var bricksetThemes = (await _bricksetApiService.GetThemes(getThemesParameters).ConfigureAwait(false)).ToList();

                _messageHub.Publish(new ThemesAcquired {
                    Count = bricksetThemes.Count
                });

                foreach (var bricksetTheme in bricksetThemes)
                {
                    _messageHub.Publish(new SynchronizingThemeStart {
                        Theme = bricksetTheme.Theme
                    });

                    try
                    {
                        var theme = bricksetTheme.ToTheme();

                        var getYearsParameters = new ParameterTheme
                        {
                            ApiKey = apiKey,
                            Theme  = bricksetTheme.Theme
                        };

                        theme.SetCountPerYear = (await _bricksetApiService.GetYears(getYearsParameters).ConfigureAwait(false))
                                                .ToYearSetCountEnumerable()
                                                .ToList();

                        var persistedTheme = _themeRepository.Get(theme.Name);

                        if (persistedTheme != null)
                        {
                            theme.Id = persistedTheme.Id;
                        }

                        _themeRepository.AddOrUpdate(theme);

                        themeList.Add(theme);
                    }
                    catch (Exception ex)
                    {
                        _messageHub.Publish(new SynchronizingThemeException {
                            Theme = bricksetTheme.Theme, Exception = ex
                        });
                    }

                    _messageHub.Publish(new SynchronizingThemeEnd {
                        Theme = bricksetTheme.Theme
                    });
                }
            }
            catch (Exception ex)
            {
                _messageHub.Publish(new ThemeSynchronizerException {
                    Exception = ex
                });
            }

            _messageHub.Publish(new ThemeSynchronizerEnd());

            return(themeList);
        }