public void GetChildren_WithSection_ReturnsAllDescendents() { var configManagerFixture = new ConfigurationManagerFixture(); int numKeys = new Random().Next(1, 10); for (int keyCt = 1; keyCt <= numKeys; keyCt++) { configManagerFixture.WithAppSetting($"Key{keyCt}", $"Value{keyCt}"); configManagerFixture.WithAppSetting($"Section:Key{keyCt}", $"Value{keyCt}"); configManagerFixture.WithAppSetting($"Section:SubSection:Key{keyCt}", $"Value{keyCt}"); configManagerFixture.WithAppSetting($"OtherSection:Key{keyCt}", $"Value{keyCt}"); } IConfigurationManager configManager = configManagerFixture.Build(); var appConfig = new AppConfiguration(configManager); IAppConfigurationSection section = appConfig.GetSection("Section"); int actual = section.GetChildren().Count(); const int numSections = 2; int expected = numKeys * numSections; Assert.That(actual, Is.EqualTo(expected)); }
public TenantAppConfigurationSection(IAppConfigurationRoot root, string key, string tenant) { if (string.IsNullOrEmpty(tenant)) { throw new ArgumentException("Cannot be null or empty.", nameof(tenant)); } this.root = root; this.tenant = tenant; string tenantKey, globalKey; if (tenant == key) { tenantKey = tenant; globalKey = null; } else if (key.StartsWith($"{tenant}{AppConfigurationPath.KeyDelimiter}", StringComparison.OrdinalIgnoreCase)) { tenantKey = key; globalKey = key.Substring(tenant.Length + 1); } else { tenantKey = AppConfigurationPath.Combine(tenant, key); globalKey = key; } tenantSection = root.GetSection(tenantKey); globalSection = globalKey == null ? null : root.GetSection(globalKey); Path = tenantKey; Value = tenantSection.Exists() ? tenantSection.Value : globalSection?.Value; }
public static bool Exists(this IAppConfigurationSection section) { if (section == null) { return(false); } return(section.Value != null || section.GetChildren().Any()); }
private string GetValue(string key) { IAppConfigurationSection tenantKeySection = tenantSection .GetChildren() .FirstOrDefault(section => section.Key.Equals(key, StringComparison.OrdinalIgnoreCase)); if (tenantKeySection != null) { return(tenantKeySection.Value); } return(globalSection?[key]); }
public void GetSection_WithKeyWithNoSection_ReturnsKeyAsSection(string key, string value) { var configManager = new ConfigurationManagerFixture() .WithAppSetting(key, value) .Build(); var appConfig = new AppConfiguration(configManager); IAppConfigurationSection section = appConfig.GetSection(key); string actual = section.Value; Assert.That(actual, Is.EqualTo(value)); }
public void GetSection_WithNotPresentSection_ReturnsEmptySection() { var configManager = new ConfigurationManagerFixture() .WithAppSetting("Section:Key", "Value1") .Build(); var appConfig = new AppConfiguration(configManager); IAppConfigurationSection section = appConfig.GetSection("SectionNotPresent"); bool actual = section.Exists(); Assert.That(actual, Is.False); }
public void GetSection_WithSection_ReturnsSectionWithKeys(string sectionKey, string key, string value) { var configManagerFixture = new ConfigurationManagerFixture(); int numKeys = new Random().Next(1, 10); for (int keyCt = 1; keyCt <= numKeys; keyCt++) { configManagerFixture.WithAppSetting(AppConfigurationPath.Combine(sectionKey, $"{key}{keyCt}"), $"{value}{keyCt}"); } IConfigurationManager configManager = configManagerFixture.Build(); var appConfig = new AppConfiguration(configManager); IAppConfigurationSection section = appConfig.GetSection(sectionKey); int actual = section.GetChildren().Count(); Assert.That(actual, Is.EqualTo(numKeys)); }
public void GetSection_WithSubSection_ReturnsSubSectionWithKeys() { var configManagerFixture = new ConfigurationManagerFixture(); int numKeys = new Random().Next(1, 10); for (int keyCt = 1; keyCt <= numKeys; keyCt++) { configManagerFixture.WithAppSetting(AppConfigurationPath.Combine("Section", $"Key{keyCt}"), $"Value{keyCt}"); configManagerFixture.WithAppSetting(AppConfigurationPath.Combine("Section:SubSection", $"Key{keyCt}"), $"Value{keyCt}"); configManagerFixture.WithAppSetting(AppConfigurationPath.Combine("Section:OtherSubSection", $"Key{keyCt}"), $"Value{keyCt}"); } IConfigurationManager configManager = configManagerFixture.Build(); var appConfig = new AppConfiguration(configManager); IAppConfigurationSection section = appConfig.GetSection("Section"); IAppConfigurationSection subSection = section.GetSection("SubSection"); int actual = subSection.GetChildren().Count(); Assert.That(actual, Is.EqualTo(numKeys)); }