Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/TwinCATDriverPageFormSerializationTests.cs
T
Joseph Doherty 6a616a1ab2 test(adminui): migrate AdminUI.Tests to v3 greenfield schema + Batch-1 stubs
Migrate tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests (was 358 build errors) to
the v3 Raw-only Tag schema and the Batch-1 AdminUI stubs. Production untouched.

- Driver-page serialization tests: drop the retired pre-declared Tags editor
  (page *TagRow types + DriverOptions.Tags); multi-device pages keep their
  Devices editor (ToOptions(devices)). Enum-serialization round-trip coverage
  (CpuType/ModbusFamily/MelsecFamily/AbCipPlcFamily/AbLegacyPlcFamily/FocasCncSeries)
  preserved intact.
- UnsTreeTestDb: reseed to v3 (Device->Tag raw slice, no EquipmentId/DriverInstanceId
  on Tag, no Namespace).
- UnsTreeService tag tests: assert Batch-1 stubs (CreateTag/UpdateTag refusal,
  empty tag/tag-driver lists); DeleteTag stays live against a raw Tag.
- Equipment #122 guard retained (validates input driver vs line cluster); drop the
  retired persisted-binding assertions + NamespaceId seeds. Area/Line #122 driver-
  orphan guard retired -> assert cross-cluster move now succeeds.
- DeleteCluster: drop the deleted-Namespace child-check test.
- OpcUaClientTagConfigModel/TagConfigValidator: address key FullName -> nodeId.
- ScriptTagCatalog: project surviving Name/DataType/TagConfig; DriverInstanceId null.
- VirtualTag {{equip}} equipment-tag-derived base is dark -> 3 tests skipped (Batch-3).

Result: build green; 507 passed / 3 skipped / 0 failed.
2026-07-15 21:50:48 -04:00

164 lines
6.2 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,
};
[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 = [],
};
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();
}
[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);
}
[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);
}
[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");
}
[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");
}
[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");
}
// v3 Batch-1 migration: the pre-declared Tags editor (TwinCATDriverPage.TwinCATTagRow +
// TwinCATDriverOptions.Tags) was retired — tags moved to the Raw tree (/raw, Batch 2). The former
// TagRow_* tests and the tag leg of FormModel_ToOptions_* were removed. The multi-device Devices
// editor is RETAINED, so its coverage (DeviceRow_* + the device-list serialization below) stays.
[Fact]
public void FormModel_ToOptions_SerializesDeviceList()
{
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 opts = form.ToOptions(devices);
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");
}
}