bd6c0b4d3d
Add missing <returns>/<param>/<summary>/<typeparam> tags and clean up misused inheritdoc across 481 files so the documented API surface is complete. Documentation-only (zero code lines changed). The 131 remaining findings are inheritdoc-style warnings deliberately left to preserve hand-written implementation rationale (plan-decision notes, race-condition explanations).
232 lines
9.5 KiB
C#
232 lines
9.5 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests;
|
|
|
|
public sealed class TwinCATDriverPageFormSerializationTests
|
|
{
|
|
private static readonly JsonSerializerOptions _opts = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
WriteIndented = false,
|
|
};
|
|
|
|
/// <summary>Verifies that serializing and deserializing TwinCAT driver options preserves all known fields.</summary>
|
|
[Fact]
|
|
public void RoundTrip_PreservesKnownFields()
|
|
{
|
|
var original = new TwinCATDriverOptions
|
|
{
|
|
Timeout = TimeSpan.FromSeconds(4),
|
|
UseNativeNotifications = false,
|
|
EnableControllerBrowse = true,
|
|
NotificationMaxDelayMs = 50,
|
|
Probe = new TwinCATProbeOptions
|
|
{
|
|
Enabled = false,
|
|
Interval = TimeSpan.FromSeconds(10),
|
|
Timeout = TimeSpan.FromSeconds(3),
|
|
},
|
|
ProbeTimeoutSeconds = 20,
|
|
Devices = [],
|
|
Tags = [],
|
|
};
|
|
|
|
var json = JsonSerializer.Serialize(original, _opts);
|
|
var back = JsonSerializer.Deserialize<TwinCATDriverOptions>(json, _opts);
|
|
|
|
back.ShouldNotBeNull();
|
|
back.Timeout.ShouldBe(TimeSpan.FromSeconds(4));
|
|
back.UseNativeNotifications.ShouldBeFalse();
|
|
back.EnableControllerBrowse.ShouldBeTrue();
|
|
back.NotificationMaxDelayMs.ShouldBe(50);
|
|
back.Probe.ShouldNotBeNull();
|
|
back.Probe.Enabled.ShouldBeFalse();
|
|
back.Probe.Interval.ShouldBe(TimeSpan.FromSeconds(10));
|
|
back.Probe.Timeout.ShouldBe(TimeSpan.FromSeconds(3));
|
|
back.ProbeTimeoutSeconds.ShouldBe(20);
|
|
back.Devices.ShouldBeEmpty();
|
|
back.Tags.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>Verifies that deserializing JSON with unknown fields silently drops the unrecognized members.</summary>
|
|
[Fact]
|
|
public void Deserialize_DropsUnknownFields()
|
|
{
|
|
var jsonWithExtra = """{"unknownField":"old-value","probeTimeoutSeconds":25}""";
|
|
var optsSkip = new JsonSerializerOptions(_opts)
|
|
{
|
|
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip,
|
|
};
|
|
var back = JsonSerializer.Deserialize<TwinCATDriverOptions>(jsonWithExtra, optsSkip);
|
|
back.ShouldNotBeNull();
|
|
back.ProbeTimeoutSeconds.ShouldBe(25);
|
|
}
|
|
|
|
/// <summary>Verifies that the form model round-trip preserves all editable fields.</summary>
|
|
[Fact]
|
|
public void FormModel_RoundTrip_PreservesEditableFields()
|
|
{
|
|
var opts = new TwinCATDriverOptions
|
|
{
|
|
Timeout = TimeSpan.FromSeconds(3),
|
|
UseNativeNotifications = true,
|
|
EnableControllerBrowse = false,
|
|
NotificationMaxDelayMs = 100,
|
|
Probe = new TwinCATProbeOptions
|
|
{
|
|
Enabled = true,
|
|
Interval = TimeSpan.FromSeconds(6),
|
|
Timeout = TimeSpan.FromSeconds(2),
|
|
},
|
|
ProbeTimeoutSeconds = 15,
|
|
};
|
|
|
|
var form = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.FormModel.FromOptions(opts);
|
|
var roundTripped = form.ToOptions([], []);
|
|
|
|
roundTripped.Timeout.ShouldBe(TimeSpan.FromSeconds(3));
|
|
roundTripped.UseNativeNotifications.ShouldBeTrue();
|
|
roundTripped.EnableControllerBrowse.ShouldBeFalse();
|
|
roundTripped.NotificationMaxDelayMs.ShouldBe(100);
|
|
roundTripped.Probe.Enabled.ShouldBeTrue();
|
|
roundTripped.Probe.Interval.ShouldBe(TimeSpan.FromSeconds(6));
|
|
roundTripped.Probe.Timeout.ShouldBe(TimeSpan.FromSeconds(2));
|
|
roundTripped.ProbeTimeoutSeconds.ShouldBe(15);
|
|
}
|
|
|
|
/// <summary>Verifies that a device row round-trip preserves all editable fields.</summary>
|
|
[Fact]
|
|
public void DeviceRow_RoundTrip_PreservesEditableFields()
|
|
{
|
|
var def = new TwinCATDeviceOptions("192.168.0.1.1.1:851", "PLC1");
|
|
|
|
var row = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATDeviceRow.FromDefinition(def);
|
|
var back = row.ToDefinition();
|
|
|
|
back.HostAddress.ShouldBe("192.168.0.1.1.1:851");
|
|
back.DeviceName.ShouldBe("PLC1");
|
|
}
|
|
|
|
/// <summary>Verifies that unedited source fields are carried through after a device row edit.</summary>
|
|
[Fact]
|
|
public void DeviceRow_CarriesThroughUneditedSourceFields()
|
|
{
|
|
// Edit only DeviceName; HostAddress on the source must survive the round-trip via _source.
|
|
var def = new TwinCATDeviceOptions("10.0.0.5.1.1:851", "Original");
|
|
|
|
var row = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATDeviceRow.FromDefinition(def);
|
|
row.DeviceName = "Renamed";
|
|
var back = row.ToDefinition();
|
|
|
|
back.HostAddress.ShouldBe("10.0.0.5.1.1:851");
|
|
back.DeviceName.ShouldBe("Renamed");
|
|
}
|
|
|
|
/// <summary>Verifies that device row validation rejects duplicate host addresses.</summary>
|
|
[Fact]
|
|
public void DeviceRow_ValidateRow_RejectsDuplicateHostAddress()
|
|
{
|
|
var existing = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATDeviceRow.FromDefinition(new TwinCATDeviceOptions("192.168.0.1.1.1:851"));
|
|
var dup = new ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATDeviceRow { HostAddress = "192.168.0.1.1.1:851" };
|
|
|
|
var all = new[] { existing, dup };
|
|
var error = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATDeviceRow.ValidateRow(dup, all, editIndex: 1);
|
|
|
|
error.ShouldNotBeNull();
|
|
error.ShouldContain("Duplicate");
|
|
}
|
|
|
|
/// <summary>Verifies that a tag row round-trip preserves all editable fields.</summary>
|
|
[Fact]
|
|
public void TagRow_RoundTrip_PreservesEditableFields()
|
|
{
|
|
var def = new TwinCATTagDefinition("Speed", "192.168.0.1.1.1:851", "MAIN.rSpeed", TwinCATDataType.Real, Writable: false);
|
|
|
|
var row = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATTagRow.FromDefinition(def);
|
|
var back = row.ToDefinition();
|
|
|
|
back.Name.ShouldBe("Speed");
|
|
back.DeviceHostAddress.ShouldBe("192.168.0.1.1.1:851");
|
|
back.SymbolPath.ShouldBe("MAIN.rSpeed");
|
|
back.DataType.ShouldBe(TwinCATDataType.Real);
|
|
back.Writable.ShouldBeFalse();
|
|
}
|
|
|
|
/// <summary>Verifies that the unedited WriteIdempotent field is carried through after a tag row edit.</summary>
|
|
[Fact]
|
|
public void TagRow_CarriesThroughUneditedWriteIdempotent()
|
|
{
|
|
// WriteIdempotent is not exposed by the editor; it must survive a load→edit→save via _source.
|
|
var def = new TwinCATTagDefinition("Cmd", "192.168.0.1.1.1:851", "GVL.Start", TwinCATDataType.Bool,
|
|
Writable: true, WriteIdempotent: true);
|
|
|
|
var row = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATTagRow.FromDefinition(def);
|
|
row.Name = "CmdRenamed"; // touch an edited field
|
|
var back = row.ToDefinition();
|
|
|
|
back.Name.ShouldBe("CmdRenamed");
|
|
back.WriteIdempotent.ShouldBeTrue();
|
|
}
|
|
|
|
/// <summary>Verifies that tag row validation rejects duplicate tag names (case-insensitive).</summary>
|
|
[Fact]
|
|
public void TagRow_ValidateRow_RejectsDuplicateName()
|
|
{
|
|
var existing = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATTagRow.FromDefinition(
|
|
new TwinCATTagDefinition("Speed", "192.168.0.1.1.1:851", "MAIN.rSpeed", TwinCATDataType.Real));
|
|
var dup = new ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATTagRow { Name = "SPEED" }; // case-insensitive collision
|
|
|
|
var all = new[] { existing, dup };
|
|
var error = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.TwinCATTagRow.ValidateRow(dup, all, editIndex: 1);
|
|
|
|
error.ShouldNotBeNull();
|
|
error.ShouldContain("Duplicate");
|
|
}
|
|
|
|
/// <summary>Verifies that ToOptions serializes device and tag lists correctly.</summary>
|
|
[Fact]
|
|
public void FormModel_ToOptions_SerializesDeviceAndTagLists()
|
|
{
|
|
var form = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers
|
|
.TwinCATDriverPage.FormModel.FromOptions(new TwinCATDriverOptions());
|
|
|
|
var devices = new[] { new TwinCATDeviceOptions("192.168.0.1.1.1:851", "PLC1") };
|
|
var tags = new[]
|
|
{
|
|
new TwinCATTagDefinition("Speed", "192.168.0.1.1.1:851", "MAIN.rSpeed", TwinCATDataType.Real,
|
|
Writable: true, WriteIdempotent: true),
|
|
};
|
|
|
|
var opts = form.ToOptions(devices, tags);
|
|
var json = JsonSerializer.Serialize(opts, _opts);
|
|
var back = JsonSerializer.Deserialize<TwinCATDriverOptions>(json, _opts);
|
|
|
|
back.ShouldNotBeNull();
|
|
back.Devices.Count.ShouldBe(1);
|
|
back.Devices[0].HostAddress.ShouldBe("192.168.0.1.1.1:851");
|
|
back.Devices[0].DeviceName.ShouldBe("PLC1");
|
|
back.Tags.Count.ShouldBe(1);
|
|
back.Tags[0].Name.ShouldBe("Speed");
|
|
back.Tags[0].DeviceHostAddress.ShouldBe("192.168.0.1.1.1:851");
|
|
back.Tags[0].SymbolPath.ShouldBe("MAIN.rSpeed");
|
|
back.Tags[0].DataType.ShouldBe(TwinCATDataType.Real);
|
|
back.Tags[0].Writable.ShouldBeTrue();
|
|
back.Tags[0].WriteIdempotent.ShouldBeTrue();
|
|
}
|
|
}
|