From fcb90ff6566e17e28743a56e78bdfc115f46106e Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Mon, 4 Mar 2019 11:38:09 +0300 Subject: [PATCH] Completed FeatureChecker. --- .../Volo/Abp/Features/AbpFeaturesModule.cs | 9 ++++ .../DefaultValueSettingValueProvider.cs | 22 ++++++++ .../Volo/Abp/Features/NullFeatureStore.cs | 1 + .../Volo/Abp/Settings/NullSettingStore.cs | 1 + .../Volo/Abp/Features/FeatureChecker_Tests.cs | 53 +++++++++++++++++++ .../Volo/Abp/Features/TestFeatureStore.cs | 52 ++++++++++++++++++ 6 files changed, 138 insertions(+) create mode 100644 framework/src/Volo.Abp.Features/Volo/Abp/Features/DefaultValueSettingValueProvider.cs create mode 100644 framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeatureChecker_Tests.cs create mode 100644 framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/TestFeatureStore.cs diff --git a/framework/src/Volo.Abp.Features/Volo/Abp/Features/AbpFeaturesModule.cs b/framework/src/Volo.Abp.Features/Volo/Abp/Features/AbpFeaturesModule.cs index 9179a2ff7b..35da247562 100644 --- a/framework/src/Volo.Abp.Features/Volo/Abp/Features/AbpFeaturesModule.cs +++ b/framework/src/Volo.Abp.Features/Volo/Abp/Features/AbpFeaturesModule.cs @@ -18,6 +18,15 @@ namespace Volo.Abp.Features AutoAddDefinitionProviders(context.Services); } + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.Configure(options => + { + options.ValueProviders.Add(); + options.ValueProviders.Add(); + }); + } + private static void AutoAddDefinitionProviders(IServiceCollection services) { var definitionProviders = new List(); diff --git a/framework/src/Volo.Abp.Features/Volo/Abp/Features/DefaultValueSettingValueProvider.cs b/framework/src/Volo.Abp.Features/Volo/Abp/Features/DefaultValueSettingValueProvider.cs new file mode 100644 index 0000000000..d60a55586a --- /dev/null +++ b/framework/src/Volo.Abp.Features/Volo/Abp/Features/DefaultValueSettingValueProvider.cs @@ -0,0 +1,22 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Features +{ + public class DefaultValueFeatureValueProvider : FeatureValueProvider + { + public const string ProviderName = "Default"; + + public override string Name => ProviderName; + + public DefaultValueFeatureValueProvider(IFeatureStore settingStore) + : base(settingStore) + { + + } + + public override Task GetOrNullAsync(FeatureDefinition setting) + { + return Task.FromResult(setting.DefaultValue); + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Features/Volo/Abp/Features/NullFeatureStore.cs b/framework/src/Volo.Abp.Features/Volo/Abp/Features/NullFeatureStore.cs index 2a4fcee0c6..d9b839a664 100644 --- a/framework/src/Volo.Abp.Features/Volo/Abp/Features/NullFeatureStore.cs +++ b/framework/src/Volo.Abp.Features/Volo/Abp/Features/NullFeatureStore.cs @@ -5,6 +5,7 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.Features { + [Dependency(TryRegister = true)] public class NullFeatureStore : IFeatureStore, ISingletonDependency { public ILogger Logger { get; set; } diff --git a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs index 5906306e93..8699fae972 100644 --- a/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs +++ b/framework/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs @@ -5,6 +5,7 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.Settings { + [Dependency(TryRegister = true)] public class NullSettingStore : ISettingStore, ISingletonDependency { public ILogger Logger { get; set; } diff --git a/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeatureChecker_Tests.cs b/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeatureChecker_Tests.cs new file mode 100644 index 0000000000..bafbb25811 --- /dev/null +++ b/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeatureChecker_Tests.cs @@ -0,0 +1,53 @@ +using Shouldly; +using System.Threading.Tasks; +using Volo.Abp.MultiTenancy; +using Xunit; + +namespace Volo.Abp.Features +{ + public class FeatureChecker_Tests : AbpFeaturesTestBase + { + private readonly IFeatureChecker _featureChecker; + private readonly ICurrentTenant _currentTenant; + + public FeatureChecker_Tests() + { + _featureChecker = GetRequiredService(); + _currentTenant = GetRequiredService(); + } + + [Fact] + public async Task IsEnabledAsync() + { + //Tenant is unknown + (await _featureChecker.IsEnabledAsync("BooleanTestFeature1")).ShouldBeFalse(); + + using (_currentTenant.Change(TestFeatureStore.Tenant1Id)) + { + (await _featureChecker.IsEnabledAsync("BooleanTestFeature1")).ShouldBeTrue(); + } + + using (_currentTenant.Change(TestFeatureStore.Tenant2Id)) + { + (await _featureChecker.IsEnabledAsync("BooleanTestFeature1")).ShouldBeFalse(); + } + } + + [Fact] + public async Task GetOrNullAsync() + { + //Tenant is unknown + (await _featureChecker.GetOrNullAsync("IntegerTestFeature1")).ShouldBe("1"); + + using (_currentTenant.Change(TestFeatureStore.Tenant1Id)) + { + (await _featureChecker.GetOrNullAsync("IntegerTestFeature1")).ShouldBe("1"); + } + + using (_currentTenant.Change(TestFeatureStore.Tenant2Id)) + { + (await _featureChecker.GetOrNullAsync("IntegerTestFeature1")).ShouldBe("34"); + } + } + } +} diff --git a/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/TestFeatureStore.cs b/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/TestFeatureStore.cs new file mode 100644 index 0000000000..2d68b75fda --- /dev/null +++ b/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/TestFeatureStore.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Features +{ + public class TestFeatureStore : IFeatureStore, ISingletonDependency + { + public static Guid Tenant1Id = new Guid("f460fcf7-f944-469a-967b-3b2463323dfe"); + public static Guid Tenant2Id = new Guid("e10428ad-4608-4c34-a304-6f82502156f2"); + + private readonly List _settingRecords; + + public TestFeatureStore() + { + _settingRecords = new List + { + new SettingRecord("BooleanTestFeature1", TenantFeatureValueProvider.ProviderName, Tenant1Id.ToString(), "true"), + new SettingRecord("IntegerTestFeature1", TenantFeatureValueProvider.ProviderName, Tenant2Id.ToString(), "34") + }; + } + + public Task GetOrNullAsync(string name, string providerName, string providerKey) + { + return Task.FromResult( + _settingRecords.FirstOrDefault(sr => + sr.Name == name && + sr.ProviderName == providerName && + sr.ProviderKey == providerKey + )?.Value + ); + } + + private class SettingRecord + { + public string Name { get; } + public string ProviderName { get; } + public string ProviderKey { get; } + public string Value { get; } + + public SettingRecord(string name, string providerName, string providerKey, string value) + { + Name = name; + ProviderName = providerName; + ProviderKey = providerKey; + Value = value; + } + } + } +}