using System; using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.FeatureManagement.JsonConverters; using Volo.Abp.Json; using Volo.Abp.Validation.StringValues; using Xunit; namespace Volo.Abp.FeatureManagement; public class SystemTextJsonStringValueJsonConverter_Tests : FeatureManagementApplicationTestBase { private readonly IJsonSerializer _jsonSerializer; public SystemTextJsonStringValueJsonConverter_Tests() { _jsonSerializer = GetRequiredService(); } protected override void BeforeAddApplication(IServiceCollection services) { services.PreConfigure(options => { options.ValueValidatorFactory.Add(new ValueValidatorFactory("URL")); }); base.BeforeAddApplication(services); } [Fact] public void Should_Serialize_And_Deserialize() { var featureListDto = new GetFeatureListResultDto { Groups = new List { new FeatureGroupDto { Name = "MyGroup", DisplayName = "MyGroup", Features = new List { new FeatureDto { ValueType = new FreeTextStringValueType { Validator = new BooleanValueValidator() } }, new FeatureDto { ValueType = new SelectionStringValueType { ItemSource = new StaticSelectionStringValueItemSource( new LocalizableSelectionStringValueItem { Value = "TestValue", DisplayText = new LocalizableStringInfo("TestResourceName", "TestName") }), Validator = new AlwaysValidValueValidator() } }, new FeatureDto { ValueType = new ToggleStringValueType { Validator = new NumericValueValidator { MaxValue = 1000, MinValue = 10 } } }, new FeatureDto { Provider = new FeatureProviderDto { Name = "FeatureName", Key = "FeatureKey" } }, new FeatureDto { ValueType = new FreeTextStringValueType { Validator = new UrlValueValidator("https") } } } } } }; var serialized = _jsonSerializer.Serialize(featureListDto, indented: true); var featureListDto2 = _jsonSerializer.Deserialize(serialized); featureListDto2.ShouldNotBeNull(); featureListDto2.Groups[0].Features[0].ValueType.ShouldBeOfType(); featureListDto2.Groups[0].Features[0].ValueType.Validator.ShouldBeOfType(); featureListDto2.Groups[0].Features[1].ValueType.ShouldBeOfType(); featureListDto2.Groups[0].Features[1].ValueType.Validator.ShouldBeOfType(); featureListDto2.Groups[0].Features[1].ValueType.As().ItemSource.Items.ShouldBeOfType(); featureListDto2.Groups[0].Features[1].ValueType.As().ItemSource.Items.ShouldContain(x => x.Value == "TestValue" && x.DisplayText.ResourceName == "TestResourceName" && x.DisplayText.Name == "TestName"); featureListDto2.Groups[0].Features[2].ValueType.ShouldBeOfType(); featureListDto2.Groups[0].Features[2].ValueType.Validator.ShouldBeOfType(); featureListDto2.Groups[0].Features[2].ValueType.Validator.As().MaxValue.ShouldBe(1000); featureListDto2.Groups[0].Features[2].ValueType.Validator.As().MinValue.ShouldBe(10); featureListDto2.Groups[0].Features[3].Provider.Name.ShouldBe("FeatureName"); featureListDto2.Groups[0].Features[3].Provider.Key.ShouldBe("FeatureKey"); featureListDto2.Groups[0].Features[4].ValueType.ShouldBeOfType(); featureListDto2.Groups[0].Features[4].ValueType.Validator.ShouldBeOfType(); featureListDto2.Groups[0].Features[4].ValueType.Validator.As().Scheme.ShouldBe("https"); } }