public void AppSettingOverridesGlobalAndEnvSettingsTest()
        {
            var envScope = ConfigSettingScope.Create("DEV");
            var appScope = ConfigSettingScope.Create("DEV", "APP1");

            var all = new[]
            {
                new ConfigSetting("a", "1"),
                new ConfigSetting("b", "2"),
                new ConfigSetting("c", "3"),

                new ConfigSetting("a", "11")
                {
                    Scope = envScope
                },

                new ConfigSetting("a", "111")
                {
                    Scope = appScope
                },
            };

            var effectiveGlobal = EffectiveSettingsEvaluator.GetEffectiveAsDict(all, ConfigSettingScope.Global);
            var effectiveForEnv = EffectiveSettingsEvaluator.GetEffectiveAsDict(all, envScope);
            var effectiveForApp = EffectiveSettingsEvaluator.GetEffectiveAsDict(all, appScope);

            Assert.AreEqual("1", effectiveGlobal["a"].Definition);
            Assert.AreEqual("11", effectiveForEnv["a"].Definition);
            Assert.AreEqual("111", effectiveForApp["a"].Definition);
        }
        public void AppOnlyBoundSettingTest()
        {
            var envScope = ConfigSettingScope.ForEnvironment("DEV");
            var appScope = ConfigSettingScope.ForApp("APP1");

            var all = new[]
            {
                new ConfigSetting("a", "1"),
                new ConfigSetting("b", "2"),
                new ConfigSetting("c", "3"),

                new ConfigSetting("a", "4")
                {
                    Scope = envScope,
                },

                new ConfigSetting("a", "5")
                {
                    Scope = appScope,
                },
            };

            var effectiveGlobal = EffectiveSettingsEvaluator.GetEffectiveAsDict(all, ConfigSettingScope.Global);
            var effectiveForEnv = EffectiveSettingsEvaluator.GetEffectiveAsDict(all, envScope);
            var effectiveForApp = EffectiveSettingsEvaluator.GetEffectiveAsDict(all, appScope);

            Assert.AreEqual("1", effectiveGlobal["a"].Definition);
            Assert.AreEqual("4", effectiveForEnv["a"].Definition);
            Assert.AreEqual("5", effectiveForApp["a"].Definition);
        }
        public void EnvironmentsDoNotInterferWithEachOtherTest()
        {
            var env1Scope = ConfigSettingScope.Create("DEV");
            var env2Scope = ConfigSettingScope.Create("QA");

            var all = new[]
            {
                new ConfigSetting("a", "1"),
                new ConfigSetting("b", "2"),
                new ConfigSetting("c", "3"),

                new ConfigSetting("a", "4")
                {
                    Scope = env1Scope
                },

                new ConfigSetting("a", "5")
                {
                    Scope = env2Scope
                },
            };

            var effectiveForEnv1 = EffectiveSettingsEvaluator.GetEffectiveAsDict(all, env1Scope);
            var effectiveForEnv2 = EffectiveSettingsEvaluator.GetEffectiveAsDict(all, env2Scope);

            Assert.AreEqual("4", effectiveForEnv1["a"].Definition);
            Assert.AreEqual("5", effectiveForEnv2["a"].Definition);
        }
Exemple #4
0
        public void EnvBoundApplicableToBothEnvAndAppBoundTest()
        {
            var envBound    = ConfigSettingScope.Create("DEV", null);
            var envAppBound = ConfigSettingScope.Create("DEV", "APP2");

            Assert.IsTrue(envBound.IsApplicableTo(envAppBound));
            Assert.IsFalse(envAppBound.IsApplicableTo(envBound));
        }
Exemple #5
0
        public void AppBoundNotApplicableToEnvBoundAndTheOppositeTest()
        {
            var envBound = ConfigSettingScope.Create("DEV", null);
            var appBound = ConfigSettingScope.Create(null, "APP2");

            Assert.IsFalse(envBound.IsApplicableTo(appBound));
            Assert.IsFalse(appBound.IsApplicableTo(envBound));
        }
Exemple #6
0
        public void GlobalIsApplicableToEverythingTest()
        {
            ConfigSettingScope global = ConfigSettingScope.Global;

            Assert.IsTrue(global.IsApplicableTo(ConfigSettingScope.Global));
            Assert.IsTrue(global.IsApplicableTo(ConfigSettingScope.Create("DEV")));
            Assert.IsTrue(global.IsApplicableTo(ConfigSettingScope.Create("DEV", "APP1")));
            Assert.IsTrue(global.IsApplicableTo(ConfigSettingScope.Create("DEV", "APP1", "INST1")));
        }
        public IDictionary <string, string> GetAllInterpolated(
            string environment = null, string app = null, string appInstance = null)
        {
            var targetScope = ConfigSettingScope.Create(environment, app, appInstance);
            var ocs         = new ObjectConfigStore(_config);
            var client      = new ConfigClient(ocs, targetScope);

            return(client.GetAll());
        }
Exemple #8
0
        public void DifferentAppScopesNotApplicableToEachOtherTest()
        {
            var app1Scope = ConfigSettingScope.Create("DEV", "APP1");
            var app2Scope = ConfigSettingScope.Create("DEV", "APP2");

            Assert.IsFalse(app1Scope.IsApplicableTo(app2Scope));
            Assert.IsFalse(app2Scope.IsApplicableTo(app1Scope));

            // the same with unspecified env
            app1Scope = ConfigSettingScope.Create(null, "APP1");
            app2Scope = ConfigSettingScope.Create(null, "APP2");

            Assert.IsFalse(app1Scope.IsApplicableTo(app2Scope));
            Assert.IsFalse(app2Scope.IsApplicableTo(app1Scope));
        }
Exemple #9
0
        public void BasicScopingTest()
        {
            var ocs = new ObjectConfigStore(() => new Config()
            {
                Settings = new[]
                {
                    new ConfigSetting("a", "1"),
                    new ConfigSetting("b", "2")
                    {
                        Scope = ConfigSettingScope.ForApp("X")
                    },
                    new ConfigSetting("b", "3")
                    {
                        Scope = ConfigSettingScope.ForApp("Y")
                    },
                }
            });

            #region Global
            var globalClient = new ConfigClient(ocs, ConfigSettingScope.Global);

            Assert.AreEqual(1, globalClient.GetAll().Count);
            Assert.AreEqual("1", globalClient.Get("a"));
            #endregion

            #region App X
            var appXClient = new ConfigClient(ocs, ConfigSettingScope.ForApp("X"));

            Assert.AreEqual(2, appXClient.GetAll().Count);
            Assert.AreEqual("1", appXClient.Get("a"));
            Assert.AreEqual("2", appXClient.Get("b"));
            #endregion

            #region App Y
            var appYClient = new ConfigClient(ocs, ConfigSettingScope.ForApp("Y"));

            Assert.AreEqual(2, appYClient.GetAll().Count);
            Assert.AreEqual("1", appYClient.Get("a"));
            Assert.AreEqual("3", appYClient.Get("b"));
            #endregion
        }
Exemple #10
0
        public static Dictionary <string, ConfigSetting> GetEffectiveAsDict(
            IEnumerable <ConfigSetting> allSettings, ConfigSettingScope targetScope)
        {
            // algorithm:
            // 1. create map for effective settings
            // 2. for each x in settings
            //      add x to map
            //
            //  if the item with key is already in map there,
            //      then override if scope priority is bigger
            //      otherwise skip

            var effective = new Dictionary <string, ConfigSetting>();

            foreach (var item in allSettings)
            {
                if (item.Scope.IsApplicableTo(targetScope))
                {
                    if (!effective.ContainsKey(item.Key))
                    {
                        effective[item.Key] = item;
                    }
                    else // override if bigger priority
                    {
                        if (item.Scope.Priority == effective[item.Key].Scope.Priority)
                        {
                            throw new BetterConfigException($"Ambiguity between following two settings found: '{item}' and '{effective[item.Key]}'");
                        }

                        if (item.Scope.Priority > effective[item.Key].Scope.Priority)
                        {
                            effective[item.Key] = item;
                        }
                    }
                }
            }

            return(effective);
        }
 public ConfigClientBase(ConfigStoreBase configStore, ConfigSettingScope scope)
 {
     ConfigStore = configStore;
     Scope       = scope;
 }
Exemple #12
0
 public static IEnumerable <ConfigSetting> GetEffective(
     IEnumerable <ConfigSetting> allSettings, ConfigSettingScope targetScope)
 {
     return(GetEffectiveAsDict(allSettings, targetScope).Values);
 }