using LINGYUN.Abp.Notifications; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; using Shouldly; using System; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using Xunit; namespace LINGYUN.Abp.Notifications.ChannelData.Tests; /// /// 渠道数据(载荷)框架契约测试 /// /// /// /// 本文件只测试**框架契约**本身: 弱类型转换、存在性判定、注册期校验、 /// 优先级排序、JSON 往返。测试用载荷是本文件内定义的 stub 类型。 /// /// /// **具体渠道**的数据测试在各自的测试项目中(先 new 消息对象 → /// IJsonSerializer 序列化 → 渠道侧验证): /// /// LINGYUN.Abp.Notifications.WeChat.Work.Tests /// LINGYUN.Abp.Notifications.Webhook.WeChat.Work.Tests /// LINGYUN.Abp.Notifications.WeChat.MiniProgram.Tests /// LINGYUN.Abp.Notifications.WxPusher.Tests /// LINGYUN.Abp.Notifications.PushPlus.Tests /// /// /// public class NotificationChannelData_Tests { // ---------- 1. 弱类型转换 ---------- [Theory] [InlineData((byte)1)] [InlineData(1)] [InlineData(1L)] [InlineData("1")] [InlineData("\"1\"")] [InlineData(true)] [InlineData("true")] public void Should_Coerce_All_Representations_To_Bool(object stored) { var data = new NotificationData(); data.TrySetData("key", stored); CreateResolver().GetValue(data, "key", false).ShouldBeTrue(); } [Fact] public void Should_Distinguish_Unset_From_Explicit_False() { var unset = new NotificationData(); CreateResolver().GetValue(unset, "key").ShouldBeNull(); var explicitFalse = new NotificationData(); explicitFalse.TrySetData("key", 0); CreateResolver().GetValue(explicitFalse, "key").ShouldBe(false); } [Fact] public void Should_Read_Values_After_Json_RoundTrip() { // 通知数据在持久化/失败重试/跨进程投递后必然经历 JSON 往返 var data = new NotificationData(); data.TrySetData("key", (byte)1); var roundTripped = RoundTrip(data); roundTripped.ExtraProperties["key"].ShouldNotBeOfType(); CreateResolver().GetValue(roundTripped, "key", false).ShouldBeTrue(); } [Fact] public void Should_Match_Keys_Case_Insensitively() { var data = new NotificationData(); data.TrySetData("my-stub:key", "value-1"); var resolver = CreateResolver(); // 精确命中 resolver.GetOrNull(data, "my-stub:key").ShouldBe("value-1"); // 任意大小写都应命中 resolver.GetOrNull(data, "MY-STUB:KEY").ShouldBe("value-1"); resolver.GetOrNull(data, "My-Stub:Key").ShouldBe("value-1"); } // ---------- 2. 存在性判定(声明式) ---------- [Fact] public void Should_Detect_Explicit_Payload() { var data = new NotificationData(); ChannelData().Write(data, new PriorityStubA { A = "a" }); CreateResolver().IsDeclared(data, typeof(PriorityStubA)).ShouldBeTrue(); CreateResolver().IsDeclared(data, typeof(PriorityStubB)).ShouldBeFalse(); } [Fact] public void Should_Not_Treat_Ignored_Properties_As_Payload() { var data = new NotificationData(); // 只写被标记为"仅读取"的属性: 不得视为声明了载荷 data.TrySetData("stubB:optional", "value"); CreateResolver().IsDeclared(data, typeof(PriorityStubB)).ShouldBeFalse(); } [Fact] public void Should_Apply_AnyDeclaredProperty_Presence_Mode() { var resolver = CreateResolver(); var first = new NotificationData(); first.TrySetData("stub-many:first", "v"); resolver.IsDeclared(first, typeof(AnyDeclaredPropertyStub)).ShouldBeTrue(); var second = new NotificationData(); second.TrySetData("stub-many:second", "v"); resolver.IsDeclared(second, typeof(AnyDeclaredPropertyStub)).ShouldBeTrue(); // 声明了按键判定, 但一个键都没写 -> 不存在 resolver.IsDeclared(new NotificationData(), typeof(AnyDeclaredPropertyStub)).ShouldBeFalse(); } [Fact] public void Should_Ignore_Matching_For_Optional_Properties() { var data = new NotificationData(); // 只写"仅读取"的属性 -> 不算载荷 data.TrySetData("stubB:optional", "value"); CreateResolver().IsDeclared(data, typeof(PriorityStubB)).ShouldBeFalse(); // 写标识键 -> 算载荷 var withIdentity = new NotificationData(); withIdentity.TrySetData("stub:b", "value"); CreateResolver().IsDeclared(withIdentity, typeof(PriorityStubB)).ShouldBeTrue(); } [Fact] public void Should_Allow_Always_Present_Payload() { var resolver = CreateResolver(); // Presence = None: 无条件存在 resolver.IsDeclared(new NotificationData(), typeof(AlwaysPresentStub)).ShouldBeTrue(); var payload = resolver.Resolve(new NotificationData()); payload.ShouldNotBeNull(); } // ---------- 3. 注册期校验 ---------- [Fact] public void Should_Throw_When_Presence_Declared_Without_Matchable_Property() { var exception = Should.Throw( () => CreateOptions().Add()); exception.Message.ShouldContain("matchable property"); } [Fact] public void Should_Reject_Missing_Channel_Data_Attribute() { var exception = Should.Throw( () => CreateOptions().Add()); exception.Message.ShouldContain("NotificationChannelDataAttribute"); } [Fact] public void Should_Reject_Duplicate_Priority_In_Same_Provider() { var options = CreateOptions(); options.Add(); var exception = Should.Throw( () => options.Add()); exception.Message.ShouldContain("same priority"); } // ---------- 4. 解析与排序 ---------- [Fact] public void Should_Resolve_Payloads_In_Declared_Priority_Order() { var data = new NotificationData(); ChannelData().Write(data, new PriorityStubB { B = "b" }); ChannelData().Write(data, new PriorityStubA { A = "a" }); var resolved = CreateResolver().Resolve(TestProvider, data); // 只声明了 A 与 B; AnyDeclaredPropertyStub 未写入, AlwaysPresentStub(Presence=None) // 无条件存在, 因此共 3 个 resolved.Count.ShouldBe(3); // Priority: AlwaysPresentStub=400, A=100, B=200 -> 排序后 A, B, Always resolved[0].ShouldBeOfType(); resolved[1].ShouldBeOfType(); resolved[2].ShouldBeOfType(); } [Fact] public void Should_Resolve_Payload_After_Json_RoundTrip() { var data = new NotificationData(); ChannelData().Write(data, new PriorityStubA { A = "卡片标题" }); // 复杂载荷以 JSON 字符串形式存储, 往返后仍必须能还原为强类型 data.ExtraProperties["stub:a"].ShouldBeOfType(); var payload = CreateResolver().Resolve(RoundTrip(data)); payload.ShouldNotBeNull(); payload!.A.ShouldBe("卡片标题"); } [Fact] public void Should_Not_Throw_For_Invalid_Payload_But_Report_Declared() { var data = new NotificationData(); // 复杂载荷以 JSON 字符串存储; 这里写入非法 JSON data.TrySetData("stub:json", "{ this is not json }"); var resolver = CreateResolver(); // IsDeclared 仍为 true(生产者确实写了), Resolve 返回 null(解析失败, 仅记日志) resolver.IsDeclared(data, typeof(JsonPayloadStub)).ShouldBeTrue(); resolver.Resolve(data).ShouldBeNull(); // 诊断信息给出实际命中的键名 resolver.GetKey(data, typeof(JsonPayloadStub)).ShouldBe("stub:json"); } [Fact] public void Should_Resolve_Complex_Payload_After_Json_RoundTrip() { var data = new NotificationData(); ChannelData().Write(data, new JsonPayloadStub { Payload = new StubPayload { Name = "标题", Count = 3 } }); // 复杂类型以 JSON 字符串形式存储 data.ExtraProperties["stub:json"].ShouldBeOfType(); var payload = CreateResolver().Resolve(RoundTrip(data)); payload.ShouldNotBeNull(); payload!.Payload.ShouldNotBeNull(); payload.Payload!.Name.ShouldBe("标题"); payload.Payload.Count.ShouldBe(3); } // ---------- 5. 属性元数据缓存的并发行为 ---------- /// /// 投递路径上每个载荷都会读取属性元数据缓存, 而缓存首次构建要做反射与 /// 表达式编译, 因此"高并发下只构建一次且读路径无锁"是必须保证的属性。 /// [Fact] public void Should_Build_Property_Metadata_Once_Under_Concurrency() { var payloadTypes = new[] { typeof(JsonPayloadStub), typeof(PriorityStubA), typeof(PriorityStubB), typeof(AnyDeclaredPropertyStub), typeof(AlwaysPresentStub) }; // 每个类型并发取多次, 覆盖"多线程同时首次构建同一类型"的竞争 var results = new NotificationChannelDataProperty[payloadTypes.Length * 64][]; Parallel.For(0, results.Length, index => { results[index] = NotificationChannelDataPropertyCache.GetProperties( payloadTypes[index % payloadTypes.Length]); }); foreach (var payloadType in payloadTypes) { var expected = NotificationChannelDataPropertyCache.GetProperties(payloadType); var observed = results .Where(x => x.Length == expected.Length) .Where(x => x.Select(p => p.Key).SequenceEqual(expected.Select(p => p.Key))) .ToArray(); // 所有并发调用必须看到同一份元数据(同一实例), 证明只构建了一次 observed.ShouldNotBeEmpty(); observed.ShouldAllBe(x => ReferenceEquals(x, expected)); } } [Fact] public void Should_Return_Same_Instance_On_Repeated_Calls() { var first = NotificationChannelDataPropertyCache.GetProperties(typeof(JsonPayloadStub)); var second = NotificationChannelDataPropertyCache.GetProperties(typeof(JsonPayloadStub)); ReferenceEquals(first, second).ShouldBeTrue(); } // ---------- 6. 测试用载荷 ---------- private const string TestProvider = "stub"; [NotificationChannelData(TestProvider, Priority = 100)] private class PriorityStubA : NotificationChannelData { [NotificationChannelProperty("stub:a")] public string? A { get; set; } } [NotificationChannelData(TestProvider, Priority = 200)] private class PriorityStubB : NotificationChannelData { [NotificationChannelProperty("stub:b")] public string? B { get; set; } [NotificationChannelProperty("stubB:optional", IgnoreWhenMatching = true)] public string? Optional { get; set; } } [NotificationChannelData(TestProvider, Priority = 100)] private class DuplicatePriorityStub : NotificationChannelData { [NotificationChannelProperty("stub:dup")] public string? Value { get; set; } } [NotificationChannelData(TestProvider, Priority = 300, Presence = NotificationChannelDataPresenceMode.AnyDeclaredProperty)] private class AnyDeclaredPropertyStub : NotificationChannelData { [NotificationChannelProperty("stub-many:first")] public string? First { get; set; } [NotificationChannelProperty("stub-many:second")] public string? Second { get; set; } } [NotificationChannelData(TestProvider, Priority = 400, Presence = NotificationChannelDataPresenceMode.None)] private class AlwaysPresentStub : NotificationChannelData { [NotificationChannelProperty("stub-always:value")] public string? Value { get; set; } } [NotificationChannelData(TestProvider, Priority = 500, Presence = NotificationChannelDataPresenceMode.DeclaredProperties)] private class MissingMatchablePropertyStub : NotificationChannelData { // 没有任何带 [NotificationChannelProperty] 的属性 public string? Value { get; set; } } /// /// 复杂载荷(以 JSON 字符串存储) /// [NotificationChannelData(TestProvider, Priority = 600)] private class JsonPayloadStub : NotificationChannelData { [NotificationChannelProperty("stub:json")] public StubPayload? Payload { get; set; } } private class StubPayload { public string? Name { get; set; } public int Count { get; set; } } private class NoAttributeStub : NotificationChannelData { public string? Value { get; set; } } // ---------- 辅助 ---------- private static AbpNotificationChannelDataOptions CreateOptions() { // 选项是纯数据: 存在性判定为纯静态, 注册时无需解析任何服务 return new AbpNotificationChannelDataOptions(); } private static NotificationChannelDataResolver CreateResolver() { var services = new ServiceCollection(); services.AddLogging(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.PostConfigure(options => { options.Add(); options.Add(); options.Add(); options.Add(); options.Add(); }); var provider = services.BuildServiceProvider(); return (NotificationChannelDataResolver)provider.GetRequiredService(); } private static INotificationChannelDataSerializer ChannelData() { var services = new ServiceCollection(); services.AddLogging(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); return services.BuildServiceProvider().GetRequiredService(); } private static NotificationData RoundTrip(NotificationData data) { var json = JsonSerializer.Serialize(data.ExtraProperties); return new NotificationData { ExtraProperties = JsonSerializer.Deserialize(json)! }; } }