using System.Text.Json; using System.Text.Json.Serialization; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Driver.Mqtt; namespace ZB.MOM.WW.OtOpcUa.Driver.Mqtt.Tests; public sealed class MqttDriverOptionsTests { private static readonly JsonSerializerOptions J = new() { PropertyNameCaseInsensitive = true, UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, Converters = { new JsonStringEnumConverter() }, }; [Fact] public void Deserialize_SparkplugConfig_ReadsModeAndSubObject() { const string json = """ { "host":"10.100.0.35","port":8883,"useTls":true,"mode":"SparkplugB", "sparkplug":{"groupId":"Plant1","hostId":"h1","requestRebirthOnGap":true} } """; var o = JsonSerializer.Deserialize(json, J)!; o.Mode.ShouldBe(MqttMode.SparkplugB); o.UseTls.ShouldBeTrue(); o.Sparkplug!.GroupId.ShouldBe("Plant1"); o.Plain.ShouldBeNull(); } [Fact] public void Serialize_WritesEnumsAsNames_NotOrdinals() { var s = JsonSerializer.Serialize(new MqttDriverOptions { Mode = MqttMode.Plain }, J); s.ShouldContain("\"Plain\""); s.ShouldNotContain("\"mode\":0"); } }