Added FeaturesSimpleStateCheckerSerializerContributor

Also refactored other contributors and tests
pull/13806/head
Halil İbrahim Kalkan 3 years ago
parent 7897fa0dfb
commit 7cb33a57f3

@ -8,6 +8,8 @@ public class AuthenticatedSimpleStateCheckerSerializerContributor :
ISimpleStateCheckerSerializerContributor,
ISingletonDependency
{
public const string CheckerShortName = "A";
public string SerializeToJson<TState>(ISimpleStateChecker<TState> checker)
where TState : IHasSimpleStateCheckers<TState>
{
@ -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<TState> Deserialize<TState>(JsonObject jsonObject)
where TState : IHasSimpleStateCheckers<TState>
{
if (jsonObject["T"]?.ToString() != "A")
if (jsonObject["T"]?.ToString() != CheckerShortName)
{
return null;
}

@ -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<TState>(ISimpleStateChecker<TState> checker)
where TState : IHasSimpleStateCheckers<TState>
{
if (checker is not RequireFeaturesSimpleStateChecker<TState> 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<TState> Deserialize<TState>(JsonObject jsonObject)
where TState : IHasSimpleStateCheckers<TState>
{
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<TState>(
(bool?)jsonObject["A"] ?? false,
nameArray.Select(x => x.ToString()).ToArray()
);
}
}

@ -7,8 +7,8 @@ namespace Volo.Abp.Features;
public class RequireFeaturesSimpleStateChecker<TState> : ISimpleStateChecker<TState>
where TState : IHasSimpleStateCheckers<TState>
{
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<TState> : ISimpleStateChecker<TSt
{
Check.NotNullOrEmpty(featureNames, nameof(featureNames));
_requiresAll = requiresAll;
_featureNames = featureNames;
RequiresAll = requiresAll;
FeatureNames = featureNames;
}
public async Task<bool> IsEnabledAsync(SimpleStateCheckerContext<TState> context)
{
var featureChecker = context.ServiceProvider.GetRequiredService<IFeatureChecker>();
return await featureChecker.IsEnabledAsync(_requiresAll, _featureNames);
return await featureChecker.IsEnabledAsync(RequiresAll, FeatureNames);
}
}

@ -9,6 +9,8 @@ public class GlobalFeaturesSimpleStateCheckerSerializerContributor :
ISimpleStateCheckerSerializerContributor,
ISingletonDependency
{
public const string CheckerShortName = "G";
public string SerializeToJson<TState>(ISimpleStateChecker<TState> checker)
where TState : IHasSimpleStateCheckers<TState>
{
@ -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<TState> Deserialize<TState>(JsonObject jsonObject)
where TState : IHasSimpleStateCheckers<TState>
{
if (jsonObject["T"]?.ToString() != "GF")
if (jsonObject["T"]?.ToString() != CheckerShortName)
{
return null;
}

@ -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<MyState>(
"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<MyState>(jsonObject);
checker.ShouldBeOfType<RequireFeaturesSimpleStateChecker<MyState>>();
var globalFeaturesSimpleStateChecker = checker as RequireFeaturesSimpleStateChecker<MyState>;
globalFeaturesSimpleStateChecker.ShouldNotBeNull();
globalFeaturesSimpleStateChecker.RequiresAll.ShouldBeTrue();
globalFeaturesSimpleStateChecker.FeatureNames[0].ShouldBe("FeatureA");
globalFeaturesSimpleStateChecker.FeatureNames[1].ShouldBe("FeatureB");
}
private class MyState : IHasSimpleStateCheckers<MyState>
{
public List<ISimpleStateChecker<MyState>> StateCheckers { get; } = new();
}
}

@ -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<MyState>(jsonObject);
checker.ShouldBeOfType<RequireGlobalFeaturesSimpleStateChecker<MyState>>();

Loading…
Cancel
Save