From dc21cbad539871f21b95934b09e0bb8b718b26a2 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 28 May 2026 09:55:13 -0400 Subject: [PATCH] feat(adminui): AbCip typed driver page --- .../AbCipDriverPageFormSerializationTests.cs | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/AbCipDriverPageFormSerializationTests.cs diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/AbCipDriverPageFormSerializationTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/AbCipDriverPageFormSerializationTests.cs new file mode 100644 index 00000000..1bb34cee --- /dev/null +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/AbCipDriverPageFormSerializationTests.cs @@ -0,0 +1,81 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Shouldly; +using Xunit; +using ZB.MOM.WW.OtOpcUa.Driver.AbCip; + +namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests; + +public sealed class AbCipDriverPageFormSerializationTests +{ + private static readonly JsonSerializerOptions _opts = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = false, + }; + + [Fact] + public void RoundTrip_PreservesKnownFields() + { + var original = new AbCipDriverOptions + { + Timeout = TimeSpan.FromSeconds(5), + EnableControllerBrowse = true, + EnableAlarmProjection = true, + AlarmPollInterval = TimeSpan.FromSeconds(2), + EnableDeclarationOnlyUdtGrouping = true, + Probe = new AbCipProbeOptions + { + Enabled = true, + Interval = TimeSpan.FromSeconds(10), + Timeout = TimeSpan.FromSeconds(3), + ProbeTagPath = "Program:Main.HealthBit", + }, + ProbeTimeoutSeconds = 10, + Devices = + [ + new AbCipDeviceOptions("ab://10.0.0.1/1,0", AbCipPlcFamily.ControlLogix, "PLC-1"), + ], + Tags = + [ + new AbCipTagDefinition("Speed", "ab://10.0.0.1/1,0", "Motor1.Speed", AbCipDataType.Real, Writable: true), + ], + }; + + var json = JsonSerializer.Serialize(original, _opts); + var back = JsonSerializer.Deserialize(json, _opts); + + back.ShouldNotBeNull(); + back.Timeout.ShouldBe(TimeSpan.FromSeconds(5)); + back.EnableControllerBrowse.ShouldBeTrue(); + back.EnableAlarmProjection.ShouldBeTrue(); + back.AlarmPollInterval.ShouldBe(TimeSpan.FromSeconds(2)); + back.EnableDeclarationOnlyUdtGrouping.ShouldBeTrue(); + back.Probe.Enabled.ShouldBeTrue(); + back.Probe.Interval.ShouldBe(TimeSpan.FromSeconds(10)); + back.Probe.Timeout.ShouldBe(TimeSpan.FromSeconds(3)); + back.Probe.ProbeTagPath.ShouldBe("Program:Main.HealthBit"); + back.ProbeTimeoutSeconds.ShouldBe(10); + back.Devices.Count.ShouldBe(1); + back.Devices[0].HostAddress.ShouldBe("ab://10.0.0.1/1,0"); + back.Devices[0].PlcFamily.ShouldBe(AbCipPlcFamily.ControlLogix); + back.Tags.Count.ShouldBe(1); + back.Tags[0].Name.ShouldBe("Speed"); + back.Tags[0].DataType.ShouldBe(AbCipDataType.Real); + } + + [Fact] + public void Deserialize_DropsUnknownFields() + { + var jsonWithExtra = """{"unknownField":"old-value","probeTimeoutSeconds":10}"""; + + var optsWithSkip = new JsonSerializerOptions(_opts) + { + UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, + }; + + var back = JsonSerializer.Deserialize(jsonWithExtra, optsWithSkip); + back.ShouldNotBeNull(); + back.ProbeTimeoutSeconds.ShouldBe(10); + } +}