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);
        }
        public void AddOrUpdate_NullSubtheme_ReturnsNull()
        {
            Subtheme subthemeUnderTest = null;

            var subtheme = _subthemeRepository.AddOrUpdate(subthemeUnderTest);

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

            var subthemes = new List <Subtheme>();

            try
            {
                var getSubthemesParameters = new ParameterTheme
                {
                    ApiKey = apiKey,
                    Theme  = theme.Name
                };

                var bricksetSubthemes = (await _bricksetApiService.GetSubthemes(getSubthemesParameters)).ToList();

                _messageHub.Publish(new SubthemesAcquired {
                    Theme = theme.Name, Count = bricksetSubthemes.Count
                });

                foreach (var bricksetSubtheme in bricksetSubthemes)
                {
                    _messageHub.Publish(new SynchronizingSubthemeStart {
                        Theme = theme.Name, Subtheme = bricksetSubtheme.Subtheme
                    });

                    try
                    {
                        var subtheme = bricksetSubtheme.ToSubtheme();

                        subtheme.Theme = theme;

                        var persistedSubtheme = _subthemeRepository.Get(subtheme.Theme.Name, subtheme.Name);

                        if (persistedSubtheme != null)
                        {
                            subtheme.Id = persistedSubtheme.Id;
                        }

                        _subthemeRepository.AddOrUpdate(subtheme);

                        subthemes.Add(subtheme);
                    }
                    catch (Exception ex)
                    {
                        _messageHub.Publish(new SynchronizingSubthemeException {
                            Theme = theme.Name, Subtheme = bricksetSubtheme.Subtheme, Exception = ex
                        });
                    }

                    _messageHub.Publish(new SynchronizingSubthemeEnd {
                        Theme = theme.Name, Subtheme = bricksetSubtheme.Subtheme
                    });
                }
            }
            catch (Exception ex)
            {
                _messageHub.Publish(new SubthemeSynchronizerException {
                    Theme = theme.Name, Exception = ex
                });
            }

            _messageHub.Publish(new SubthemeSynchronizerEnd());

            return(subthemes);
        }
        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();
        }