Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/AbLegacyDriverPageFormSerializationTests.cs
T
2026-05-28 09:57:07 -04:00

82 lines
3.0 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.PlcFamilies;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests;
public sealed class AbLegacyDriverPageFormSerializationTests
{
private static readonly JsonSerializerOptions _opts = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = false,
};
[Fact]
public void RoundTrip_PreservesKnownFields()
{
var original = new AbLegacyDriverOptions
{
Timeout = TimeSpan.FromSeconds(4),
Probe = new AbLegacyProbeOptions
{
Enabled = true,
Interval = TimeSpan.FromSeconds(8),
Timeout = TimeSpan.FromSeconds(3),
ProbeAddress = "N7:0",
},
ProbeTimeoutSeconds = 10,
Devices =
[
new AbLegacyDeviceOptions("10.0.0.10", AbLegacyPlcFamily.Slc500, "PLC-A"),
new AbLegacyDeviceOptions("10.0.0.11", AbLegacyPlcFamily.MicroLogix),
],
Tags =
[
new AbLegacyTagDefinition("Level", "10.0.0.10", "N7:5", AbLegacyDataType.Int, Writable: false),
new AbLegacyTagDefinition("Pump", "10.0.0.10", "B3:0/0", AbLegacyDataType.Bit, Writable: true),
],
};
var json = JsonSerializer.Serialize(original, _opts);
var back = JsonSerializer.Deserialize<AbLegacyDriverOptions>(json, _opts);
back.ShouldNotBeNull();
back.Timeout.ShouldBe(TimeSpan.FromSeconds(4));
back.Probe.Enabled.ShouldBeTrue();
back.Probe.Interval.ShouldBe(TimeSpan.FromSeconds(8));
back.Probe.Timeout.ShouldBe(TimeSpan.FromSeconds(3));
back.Probe.ProbeAddress.ShouldBe("N7:0");
back.ProbeTimeoutSeconds.ShouldBe(10);
back.Devices.Count.ShouldBe(2);
back.Devices[0].HostAddress.ShouldBe("10.0.0.10");
back.Devices[0].PlcFamily.ShouldBe(AbLegacyPlcFamily.Slc500);
back.Devices[0].DeviceName.ShouldBe("PLC-A");
back.Devices[1].PlcFamily.ShouldBe(AbLegacyPlcFamily.MicroLogix);
back.Tags.Count.ShouldBe(2);
back.Tags[0].Name.ShouldBe("Level");
back.Tags[0].Address.ShouldBe("N7:5");
back.Tags[0].DataType.ShouldBe(AbLegacyDataType.Int);
back.Tags[0].Writable.ShouldBeFalse();
back.Tags[1].DataType.ShouldBe(AbLegacyDataType.Bit);
}
[Fact]
public void Deserialize_DropsUnknownFields()
{
var jsonWithExtra = """{"unknownField":"old-value","probeTimeoutSeconds":10}""";
var optsWithSkip = new JsonSerializerOptions(_opts)
{
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip,
};
var back = JsonSerializer.Deserialize<AbLegacyDriverOptions>(jsonWithExtra, optsWithSkip);
back.ShouldNotBeNull();
back.ProbeTimeoutSeconds.ShouldBe(10);
}
}