using System.Text.Json; using System.Text.Json.Serialization; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests; public sealed class OpcUaClientDriverPageFormSerializationTests { private static readonly JsonSerializerOptions _opts = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = false, }; [Fact] public void RoundTrip_PreservesKnownFields() { var original = new OpcUaClientDriverOptions { EndpointUrl = "opc.tcp://plc.internal:4840", ApplicationUri = "urn:plc:OtOpcUa:GatewayClient", SessionName = "MySession", SecurityMode = OpcUaSecurityMode.SignAndEncrypt, SecurityPolicy = OpcUaSecurityPolicy.Basic256Sha256, AuthType = OpcUaAuthType.Username, Username = "operator", Password = "s3cr3t", PerEndpointConnectTimeout = TimeSpan.FromSeconds(5), Timeout = TimeSpan.FromSeconds(20), SessionTimeout = TimeSpan.FromSeconds(180), KeepAliveInterval = TimeSpan.FromSeconds(10), ReconnectPeriod = TimeSpan.FromSeconds(15), AutoAcceptCertificates = true, BrowseRoot = "i=85", MaxDiscoveredNodes = 5000, MaxBrowseDepth = 6, TargetNamespaceKind = OpcUaTargetNamespaceKind.SystemPlatform, ProbeTimeoutSeconds = 20, }; var json = JsonSerializer.Serialize(original, _opts); var back = JsonSerializer.Deserialize(json, _opts); back.ShouldNotBeNull(); back.EndpointUrl.ShouldBe("opc.tcp://plc.internal:4840"); back.ApplicationUri.ShouldBe("urn:plc:OtOpcUa:GatewayClient"); back.SessionName.ShouldBe("MySession"); back.SecurityMode.ShouldBe(OpcUaSecurityMode.SignAndEncrypt); back.SecurityPolicy.ShouldBe(OpcUaSecurityPolicy.Basic256Sha256); back.AuthType.ShouldBe(OpcUaAuthType.Username); back.Username.ShouldBe("operator"); back.Password.ShouldBe("s3cr3t"); back.PerEndpointConnectTimeout.ShouldBe(TimeSpan.FromSeconds(5)); back.Timeout.ShouldBe(TimeSpan.FromSeconds(20)); back.SessionTimeout.ShouldBe(TimeSpan.FromSeconds(180)); back.KeepAliveInterval.ShouldBe(TimeSpan.FromSeconds(10)); back.ReconnectPeriod.ShouldBe(TimeSpan.FromSeconds(15)); back.AutoAcceptCertificates.ShouldBeTrue(); back.BrowseRoot.ShouldBe("i=85"); back.MaxDiscoveredNodes.ShouldBe(5000); back.MaxBrowseDepth.ShouldBe(6); back.TargetNamespaceKind.ShouldBe(OpcUaTargetNamespaceKind.SystemPlatform); back.ProbeTimeoutSeconds.ShouldBe(20); } [Fact] public void Deserialize_DropsUnknownFields() { var jsonWithExtra = """{"unknownField":"old-value","probeTimeoutSeconds":20}"""; var optsWithSkip = new JsonSerializerOptions(_opts) { UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, }; var back = JsonSerializer.Deserialize(jsonWithExtra, optsWithSkip); back.ShouldNotBeNull(); back.ProbeTimeoutSeconds.ShouldBe(20); } }