From 7cb33a57f369dc7b4eaacc0d45d7cebc2a3996df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 18 Aug 2022 08:57:04 +0300 Subject: [PATCH] Added FeaturesSimpleStateCheckerSerializerContributor Also refactored other contributors and tests --- ...SimpleStateCheckerSerializerContributor.cs | 6 +- ...SimpleStateCheckerSerializerContributor.cs | 56 +++++++++++++++++++ .../RequireFeaturesSimpleStateChecker.cs | 10 ++-- ...SimpleStateCheckerSerializerContributor.cs | 6 +- ...tateCheckerSerializerContributor_Tests.cs} | 0 ...StateCheckerSerializerContributor_Tests.cs | 46 +++++++++++++++ ...StateCheckerSerializerContributor_Tests.cs | 4 +- 7 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 framework/src/Volo.Abp.Features/Volo/Abp/Features/FeaturesSimpleStateCheckerSerializerContributor.cs rename framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/{GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs => AuthenticatedSimpleStateCheckerSerializerContributor_Tests.cs} (100%) create mode 100644 framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeaturesSimpleStateCheckerSerializerContributor_Tests.cs diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/AuthenticatedSimpleStateCheckerSerializerContributor.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/AuthenticatedSimpleStateCheckerSerializerContributor.cs index 97b3ae9b0f..cb52f5f7ac 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/AuthenticatedSimpleStateCheckerSerializerContributor.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/AuthenticatedSimpleStateCheckerSerializerContributor.cs @@ -8,6 +8,8 @@ public class AuthenticatedSimpleStateCheckerSerializerContributor : ISimpleStateCheckerSerializerContributor, ISingletonDependency { + public const string CheckerShortName = "A"; + public string SerializeToJson(ISimpleStateChecker checker) where TState : IHasSimpleStateCheckers { @@ -17,7 +19,7 @@ public class AuthenticatedSimpleStateCheckerSerializerContributor : } var jsonObject = new JsonObject { - ["T"] = "A" + ["T"] = CheckerShortName }; return jsonObject.ToJsonString(); @@ -26,7 +28,7 @@ public class AuthenticatedSimpleStateCheckerSerializerContributor : public ISimpleStateChecker Deserialize(JsonObject jsonObject) where TState : IHasSimpleStateCheckers { - if (jsonObject["T"]?.ToString() != "A") + if (jsonObject["T"]?.ToString() != CheckerShortName) { return null; } diff --git a/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeaturesSimpleStateCheckerSerializerContributor.cs b/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeaturesSimpleStateCheckerSerializerContributor.cs new file mode 100644 index 0000000000..7510cb4a24 --- /dev/null +++ b/framework/src/Volo.Abp.Features/Volo/Abp/Features/FeaturesSimpleStateCheckerSerializerContributor.cs @@ -0,0 +1,56 @@ +using System.Linq; +using System.Text.Json.Nodes; +using Volo.Abp.DependencyInjection; +using Volo.Abp.SimpleStateChecking; + +namespace Volo.Abp.Features; + +public class FeaturesSimpleStateCheckerSerializerContributor : + ISimpleStateCheckerSerializerContributor, + ISingletonDependency +{ + public const string CheckerShortName = "F"; + + public string SerializeToJson(ISimpleStateChecker checker) + where TState : IHasSimpleStateCheckers + { + if (checker is not RequireFeaturesSimpleStateChecker featuresSimpleStateChecker) + { + return null; + } + + var jsonObject = new JsonObject { + ["T"] = CheckerShortName, + ["A"] = featuresSimpleStateChecker.RequiresAll + }; + + var nameArray = new JsonArray(); + foreach (var featureName in featuresSimpleStateChecker.FeatureNames) + { + nameArray.Add(featureName); + } + + jsonObject["N"] = nameArray; + return jsonObject.ToJsonString(); + } + + public ISimpleStateChecker Deserialize(JsonObject jsonObject) + where TState : IHasSimpleStateCheckers + { + if (jsonObject["T"]?.ToString() != CheckerShortName) + { + return null; + } + + var nameArray = jsonObject["N"] as JsonArray; + if (nameArray == null) + { + throw new AbpException("'N' is not an array in the serialized state checker! JsonObject: " + jsonObject.ToJsonString()); + } + + return new RequireFeaturesSimpleStateChecker( + (bool?)jsonObject["A"] ?? false, + nameArray.Select(x => x.ToString()).ToArray() + ); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Features/Volo/Abp/Features/RequireFeaturesSimpleStateChecker.cs b/framework/src/Volo.Abp.Features/Volo/Abp/Features/RequireFeaturesSimpleStateChecker.cs index 1b437e623a..625d097441 100644 --- a/framework/src/Volo.Abp.Features/Volo/Abp/Features/RequireFeaturesSimpleStateChecker.cs +++ b/framework/src/Volo.Abp.Features/Volo/Abp/Features/RequireFeaturesSimpleStateChecker.cs @@ -7,8 +7,8 @@ namespace Volo.Abp.Features; public class RequireFeaturesSimpleStateChecker : ISimpleStateChecker where TState : IHasSimpleStateCheckers { - private readonly string[] _featureNames; - private readonly bool _requiresAll; + public string[] FeatureNames { get; } + public bool RequiresAll { get; } public RequireFeaturesSimpleStateChecker(params string[] featureNames) : this(true, featureNames) @@ -19,13 +19,13 @@ public class RequireFeaturesSimpleStateChecker : ISimpleStateChecker IsEnabledAsync(SimpleStateCheckerContext context) { var featureChecker = context.ServiceProvider.GetRequiredService(); - return await featureChecker.IsEnabledAsync(_requiresAll, _featureNames); + return await featureChecker.IsEnabledAsync(RequiresAll, FeatureNames); } } diff --git a/framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/GlobalFeaturesSimpleStateCheckerSerializerContributor.cs b/framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/GlobalFeaturesSimpleStateCheckerSerializerContributor.cs index 00f72e423d..d140bbcc78 100644 --- a/framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/GlobalFeaturesSimpleStateCheckerSerializerContributor.cs +++ b/framework/src/Volo.Abp.GlobalFeatures/Volo/Abp/GlobalFeatures/GlobalFeaturesSimpleStateCheckerSerializerContributor.cs @@ -9,6 +9,8 @@ public class GlobalFeaturesSimpleStateCheckerSerializerContributor : ISimpleStateCheckerSerializerContributor, ISingletonDependency { + public const string CheckerShortName = "G"; + public string SerializeToJson(ISimpleStateChecker checker) where TState : IHasSimpleStateCheckers { @@ -18,7 +20,7 @@ public class GlobalFeaturesSimpleStateCheckerSerializerContributor : } var jsonObject = new JsonObject { - ["T"] = "GF", + ["T"] = CheckerShortName, ["A"] = globalFeaturesSimpleStateChecker.RequiresAll }; @@ -35,7 +37,7 @@ public class GlobalFeaturesSimpleStateCheckerSerializerContributor : public ISimpleStateChecker Deserialize(JsonObject jsonObject) where TState : IHasSimpleStateCheckers { - if (jsonObject["T"]?.ToString() != "GF") + if (jsonObject["T"]?.ToString() != CheckerShortName) { return null; } diff --git a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AuthenticatedSimpleStateCheckerSerializerContributor_Tests.cs similarity index 100% rename from framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs rename to framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/AuthenticatedSimpleStateCheckerSerializerContributor_Tests.cs diff --git a/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeaturesSimpleStateCheckerSerializerContributor_Tests.cs b/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeaturesSimpleStateCheckerSerializerContributor_Tests.cs new file mode 100644 index 0000000000..28f7bb0b5e --- /dev/null +++ b/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeaturesSimpleStateCheckerSerializerContributor_Tests.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using System.Text.Json.Nodes; +using Shouldly; +using Volo.Abp.SimpleStateChecking; +using Xunit; + +namespace Volo.Abp.Features; + +public class FeaturesSimpleStateCheckerSerializerContributor_Tests +{ + [Fact] + public void Should_Serialize_RequireGlobalFeaturesSimpleStateChecker() + { + var serializer = new FeaturesSimpleStateCheckerSerializerContributor(); + + var result = serializer.SerializeToJson( + new RequireFeaturesSimpleStateChecker( + "FeatureA", + "FeatureB" + ) + ); + + result.ShouldBe("{\"T\":\"F\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); + } + + [Fact] + public void Should_Deserialize_RequireGlobalFeaturesSimpleStateChecker() + { + var serializer = new FeaturesSimpleStateCheckerSerializerContributor(); + + var jsonObject = (JsonObject)JsonNode.Parse("{\"T\":\"F\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); + var checker = serializer.Deserialize(jsonObject); + + checker.ShouldBeOfType>(); + var globalFeaturesSimpleStateChecker = checker as RequireFeaturesSimpleStateChecker; + globalFeaturesSimpleStateChecker.ShouldNotBeNull(); + globalFeaturesSimpleStateChecker.RequiresAll.ShouldBeTrue(); + globalFeaturesSimpleStateChecker.FeatureNames[0].ShouldBe("FeatureA"); + globalFeaturesSimpleStateChecker.FeatureNames[1].ShouldBe("FeatureB"); + } + + private class MyState : IHasSimpleStateCheckers + { + public List> StateCheckers { get; } = new(); + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.GlobalFeatures.Tests/Volo/Abp/GlobalFeatures/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs b/framework/test/Volo.Abp.GlobalFeatures.Tests/Volo/Abp/GlobalFeatures/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs index 46064bd11e..c4878abca3 100644 --- a/framework/test/Volo.Abp.GlobalFeatures.Tests/Volo/Abp/GlobalFeatures/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs +++ b/framework/test/Volo.Abp.GlobalFeatures.Tests/Volo/Abp/GlobalFeatures/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs @@ -20,7 +20,7 @@ public class GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests ) ); - result.ShouldBe("{\"T\":\"GF\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); + result.ShouldBe("{\"T\":\"G\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); } [Fact] @@ -28,7 +28,7 @@ public class GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests { var serializer = new GlobalFeaturesSimpleStateCheckerSerializerContributor(); - var jsonObject = (JsonObject)JsonNode.Parse("{\"T\":\"GF\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); + var jsonObject = (JsonObject)JsonNode.Parse("{\"T\":\"G\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); var checker = serializer.Deserialize(jsonObject); checker.ShouldBeOfType>();