public void ShouldFailWhenInvalidFlagTypesExistOnNonEmptyFlagsCollection()
        {
            Mock<ISettingsRepository> settingsRepo = new Mock<ISettingsRepository>();

            _mockSettingsStorageModels.AddRange(new List<SettingsStorageModel>{
                new SettingsStorageModel
                {
                    Category = SettingsManager.FlagsCategoryName,
                    Name = "ServiceBaseAddress",
                    Value = "abbv",
                    UpdatedAt = new DateTime(2015, 1, 1)
                },
                new SettingsStorageModel
                {
                    Category = SettingsManager.FlagsCategoryName,
                    Name = "StartCount",
                    Value = "12",
                    UpdatedAt = new DateTime(2015, 2, 1)

                }});

            settingsRepo.Setup(x => x.ReadSettings(It.IsAny<string>()))
                .Returns<string>(category => _mockSettingsStorageModels
                    .Where(x => x.Category == category));

            var manager = new SettingsManagerPeriodic(settingsRepo.Object);
            Assert.AreEqual(false, manager.IsFlagSet("ServiceBaseAddress"));
            Assert.AreEqual(false, manager.IsFlagSet("StartCount"));
        }
        public void SavingMustWriteNewObjectToRepository()
        {
            var repo = new InMemoryRepository();
            var manager = new SettingsManagerPeriodic(repo){AutoPersistOnCreate = false};
            var settings = manager.Get<Settings>();

            var custom = new CustomSetting()
            {
                Text = "abc"
            };
            var value = repo.Content.Where(i => i.Name == "Custom").Select(i=>i.Value).FirstOrDefault();
            Assert.IsNull(settings.Custom);
            Assert.IsNull(value);

            settings.Custom = custom;
            manager.Save(settings);
            value = repo.Content.First(i => i.Name == "Custom").Value;
            Assert.IsNotNull(value);
            Assert.IsTrue(value.Contains("abc"));
        }
        public void SettingsManagerShouldCollectSetPropertyExceptions()
        {
            var inMemoryRepository = new InMemoryRepository();
            inMemoryRepository.Content.Add(new SettingsStorageModel() { Category = "Settings", Name = "Decimal", Value = "test" });
            inMemoryRepository.Content.Add(new SettingsStorageModel() { Category = "Settings", Name = "Text", Value = "test" });

            var manager = new SettingsManagerPeriodic(inMemoryRepository);

            bool propertyErrorOccurred = false;

            manager.PropertyError += (sender, eventArgs) =>
            {
                propertyErrorOccurred = eventArgs.IsRepeating;
            };

            var settings = manager.Get<Settings>(true);

            Assert.IsTrue(propertyErrorOccurred);
        }
        public void ReadOnlyPropertyMustBeRespected()
        {
            var repo = new InMemoryRepository();
            var manager = new SettingsManagerPeriodic(repo);
            var settings = manager.Get<ReadOnlySettings>();
            settings.Text = "a";
            manager.Save(settings);
            settings = manager.Get<ReadOnlySettings>(true);
            Assert.AreNotEqual("a", settings.Text);

            manager.ChangeAndSave(s => s.Text = "b", settings);
            settings = manager.Get<ReadOnlySettings>(true);
            Assert.AreNotEqual("b", settings.Text);
        }
        public void SavingAndLoadingMustBeSame()
        {
            var repo = new InMemoryRepository();
            var manager = new SettingsManagerPeriodic(repo);
            var settings = manager.Get<Settings>();

            var custom = new CustomSetting()
            {
                Text = "1",
                Ref = new CustomSetting() { Text = "2" }
            };

            settings.Decimal = 0.01m;
            settings.Text = "test";
            settings.BigInteger = UInt64.MaxValue;
            settings.Boolean = true;
            settings.Character = 'X';
            settings.Enum = ConsoleColor.Green;
            settings.Float = float.MaxValue;
            settings.Integer = int.MinValue;
            settings.Time = DateTime.MaxValue;
            settings.TimeSpan = TimeSpan.MaxValue;
            settings.TimeSpan2 = new TimeSpan(0, 1, 2, 3);
            settings.Custom = custom;

            manager.Save(settings);
            settings = manager.Get<Settings>(true);

            Assert.AreEqual(0.01m, settings.Decimal);
            Assert.AreEqual("test", settings.Text);
            Assert.AreEqual(UInt64.MaxValue, settings.BigInteger);
            Assert.AreEqual(true, settings.Boolean);
            Assert.AreEqual('X', settings.Character);
            Assert.AreEqual(ConsoleColor.Green, settings.Enum);
            Assert.AreEqual(float.MaxValue, settings.Float);
            Assert.AreEqual(int.MinValue, settings.Integer);
            Assert.AreEqual(0, (int)(DateTime.MaxValue - settings.Time).TotalSeconds); //compare only to seconds precision
            Assert.AreEqual(TimeSpan.MaxValue, settings.TimeSpan);
            Assert.AreEqual(new TimeSpan(0, 1, 2, 3), settings.TimeSpan2);
            Assert.AreEqual(custom.Text, settings.Custom.Text);
            Assert.AreEqual(custom.Ref.Text, settings.Custom.Ref.Text);
        }
 public void PropertyChangedEventMustFire()
 {
     var repo = new InMemoryRepository();
     var manager = new SettingsManagerPeriodic(repo);
     var settings = manager.Get<Settings>();
     settings.Text = "old";
     string newValue = "";
     string oldValue = "";
     manager.PropertyValueChanged += (sender, args) =>
     {
         args.When<Settings, string>((s) => s.Text, (ov, nv) =>
         {
             oldValue = ov;
             newValue = nv;
         });
     };
     repo.Content.Single(s => s.Name == "Text").Value = "new"; //simulate value change in repository (db)
     manager.Get<Settings>(true);
     Assert.AreEqual("new", newValue);
     Assert.AreEqual("old", oldValue);
 }
 public void PropertyChangedEventMustNotFireWhenPropertyValueNotChanged()
 {
     var repo = new InMemoryRepository();
     var manager = new SettingsManagerPeriodic(repo);
     var settings = manager.Get<Settings>();
     settings.Text = "old";
     var fired = false;
     manager.PropertyValueChanged += (sender, args) =>
     {
         fired = true;
     };
     manager.ChangeAndSave(s => s.Text = "old", settings);
     manager.Get<Settings>(true);
     Assert.IsFalse(fired);
 }
        public void PeriodicReaderShouldReactToFlagChanges()
        {
            Mock<ISettingsRepository> settingsRepo = new Mock<ISettingsRepository>();

            _mockSettingsStorageModels.AddRange(new List<SettingsStorageModel> {
                new SettingsStorageModel
                {
                    Category = SettingsManager.FlagsCategoryName,
                    Name = "X",
                    Value = "true",
                    UpdatedAt = new DateTime(2015, 1, 1)
                }
            });

            settingsRepo.Setup(x => x.ReadSettings(It.IsAny<string>()))
               .Returns<string>(category => _mockSettingsStorageModels);

            settingsRepo.Setup(x => x.ReadSettings(It.IsAny<IList<string>>(), It.IsAny<DateTime?>()))
               .Returns<IList<string>, DateTime?>((categories, lastChangedDate) => _mockSettingsStorageModels);
            var cancellationTokenSource = new CancellationTokenSource();

            var manager = new SettingsManagerPeriodic(settingsRepo.Object);

            var initialValue = manager.IsFlagSet("X");
            manager.StartReadingTask(TimeSpan.FromMilliseconds(10), cancellationTokenSource.Token);
            Thread.Sleep(100);
            var repoItem = _mockSettingsStorageModels.Single(s => s.Name == "X");
            repoItem.Value = "0";
            Thread.Sleep(100);
            Assert.AreEqual(!initialValue, manager.IsFlagSet("X"));
        }
        public void PreriodicReaderMustRaiseError()
        {
            var cts = new CancellationTokenSource();
            var repo = new InMemoryRepository();
            var manager = new SettingsManagerPeriodic(repo);
            var settings = manager.Get<ReadOnlySettings>();
            settings.Text = "a";
            manager.Save(settings);

            var error = false;
            manager.PeriodicReaderError += (s, a) => { error = true; };
            manager.StartReadingTask(TimeSpan.FromMilliseconds(10), cts.Token);
            Thread.Sleep(200);
            Assert.AreEqual(false, error);
        }
 public void PeriodicReaderMustDetectNewFlags()
 {
     var cts = new CancellationTokenSource();
     var repo = new InMemoryRepository();
     var manager = new SettingsManagerPeriodic(repo);
     Assert.AreEqual(false, manager.IsFlagSet("TestFlag"));
     manager.StartReadingTask(TimeSpan.FromMilliseconds(10), cts.Token);
     repo.Content.Add(new SettingsStorageModel() { Category = SettingsManager.FlagsCategoryName, Name = "TestFlag", Value = "True" });
     Thread.Sleep(200);
     Assert.AreEqual(true, manager.IsFlagSet("TestFlag"));
     cts.Cancel();
 }
        public void PeriodicReaderCancel()
        {
            var repo = new InMemoryRepository();
            var manager = new SettingsManagerPeriodic(repo);
            var settings = manager.Get<Settings>();
            var cts = new CancellationTokenSource();
            var task = manager.StartReadingTask(TimeSpan.FromMilliseconds(1), cts.Token);
            int cnt = 0;
            bool cancelEvent = false;
            manager.ReaderExecuting += (sender, args) => cnt++;
            manager.PeriodicReaderCanceled += (sender, args) => cancelEvent = true;

            Thread.Sleep(200);
            cts.Cancel();
            Thread.Sleep(200);
            Assert.IsTrue(cancelEvent);
            Assert.IsTrue(task.IsCanceled);
            Assert.IsTrue(cnt > 0);
        }
        public void FlagShouldNotBeSetOnEmptyFlagsCollection()
        {
            Mock<ISettingsRepository> settingsRepo = new Mock<ISettingsRepository>();

            settingsRepo.Setup(x => x.ReadSettings(It.IsAny<string>()))
                .Returns<string>(category => new List<SettingsStorageModel>().Where(x => x.Category == category));

            var manager = new SettingsManagerPeriodic(settingsRepo.Object);
            Assert.AreEqual(false, manager.IsFlagSet("BiddingSystem"));
        }
        public void EmptyStringsMustWork()
        {
            var repo = new InMemoryRepository();
            var manager = new SettingsManagerPeriodic(repo);
            var settings = manager.Get<Settings>();

            settings.Text = "";
            manager.Save(settings);
            settings = manager.Get<Settings>();
            Assert.AreEqual("", settings.Text);
        }
        public void BasicReading()
        {
            var repo = new InMemoryRepository();
            repo.Content.Add(new SettingsStorageModel() { Category = "Settings", Name = "Decimal", Value = "1.5" });
            repo.Content.Add(new SettingsStorageModel() { Category = "Settings", Name = "Text", Value = "test" });

            var manager = new SettingsManagerPeriodic(repo);
            var settings = manager.Get<Settings>();

            Assert.AreEqual(1.5m, settings.Decimal);
            Assert.AreEqual("test", settings.Text);
        }