ed0881d672
Task 22 (spec section 8 P5), with the controller rulings that exclude PLC-5 and LogixPccc. - Transport is nullable on both option records; null (key absent) means "use the default" and is never serialized. Factory DTO parse: absent -> null; a bad value still fails via ParseEnum. - One resolver per driver (AbCipTransportResolver / AbLegacyTransportResolver), used by the driver constructor, the Test Connect probe and (through the driver) the CLI. AbCip: null -> Native. AbLegacy: null -> Native unless any device is Plc5 or LogixPccc, then the whole driver resolves to Libplctag. Explicit values always win; explicit Native with a Plc5/LogixPccc device logs one Warning at init. - Init log shows configured vs effective, e.g. transport=Native (default). - AdminUI forms: "Default (...)" / "Native (managed)" / "libplctag (fallback)". Default omits the key on save (reverses Task 19's explicit re-emit, which would have pinned every edited driver). - CLIs: --transport default|native|libplctag, default "default" via the resolver, so --plc-type Plc5 with no --transport runs libplctag. - Tests pinned to Libplctag where a default-path runtime was created: found by instrumenting the native factories with the current xUnit test name (one unit test, AbLegacyReinitConfigAdoption; plus the two fixture-gated AbCip probe E2E tests whose exact messages were proven on libplctag). TransportMatrix and the differential harness already pass explicit transports. - Switch/resolver/CLI/form tests are falsified by mutating the resolver (default -> Libplctag, exclusion removed, AbLegacy default -> Libplctag): each mutation turns them red. - Replaces the inline role-name literal in AbCipDriverFactoryExtensions' ParseEnum call with a named constant; RoleAuthorityGuardTests was already red on the base commit for that line. - Docs: AbCip.md / AbLegacy.md default + release note; CLAUDE.md AB section. Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
176 lines
7.4 KiB
C#
176 lines
7.4 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Forms;
|
|
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,
|
|
};
|
|
|
|
private static readonly JsonSerializerOptions TestJsonOpts = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip,
|
|
};
|
|
|
|
[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"),
|
|
],
|
|
};
|
|
|
|
var json = JsonSerializer.Serialize(original, _opts);
|
|
var back = JsonSerializer.Deserialize<AbCipDriverOptions>(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);
|
|
}
|
|
|
|
[Fact]
|
|
public void Deserialize_DropsUnknownFields()
|
|
{
|
|
var jsonWithExtra = """{"unknownField":"old-value","probeTimeoutSeconds":10}""";
|
|
|
|
var optsWithSkip = new JsonSerializerOptions(_opts)
|
|
{
|
|
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip,
|
|
};
|
|
|
|
var back = JsonSerializer.Deserialize<AbCipDriverOptions>(jsonWithExtra, optsWithSkip);
|
|
back.ShouldNotBeNull();
|
|
back.ProbeTimeoutSeconds.ShouldBe(10);
|
|
}
|
|
|
|
// v3 Batch-2 (WP3): the embedded-Devices CollectionEditor + AbCipDeviceRow retired — devices are now
|
|
// separate Device entities authored via the raw-tree DeviceModal (AbCipDeviceModel, HostAddress/PlcFamily
|
|
// into DeviceConfig). The former DeviceRow_* / ValidateDeviceRow_* tests were removed. The channel-config
|
|
// FormModel now lives in AbCipDriverForm; the multi-device Devices array it preserves is covered below.
|
|
|
|
[Fact]
|
|
public void Device_list_survives_form_serialize_round_trip()
|
|
{
|
|
var devices = new List<AbCipDeviceOptions>
|
|
{
|
|
new("ab://10.0.0.1/1,0", AbCipPlcFamily.ControlLogix, "PLC-1"),
|
|
new("ab://10.0.0.2/1,0", AbCipPlcFamily.CompactLogix, "PLC-2"),
|
|
};
|
|
var opts = new AbCipDriverForm.FormModel().ToOptions(devices);
|
|
var json = JsonSerializer.Serialize(opts, TestJsonOpts);
|
|
var back = JsonSerializer.Deserialize<AbCipDriverOptions>(json, TestJsonOpts)!;
|
|
back.Devices.Count.ShouldBe(2);
|
|
back.Devices[0].HostAddress.ShouldBe("ab://10.0.0.1/1,0");
|
|
back.Devices[0].PlcFamily.ShouldBe(AbCipPlcFamily.ControlLogix);
|
|
back.Devices[1].PlcFamily.ShouldBe(AbCipPlcFamily.CompactLogix);
|
|
}
|
|
|
|
// ---- Task 19: Transport (native AB transport switch) ----
|
|
// These drive the form's OWN load/save path (TryDeserialize -> FromOptions -> ToOptions -> Serialize),
|
|
// which uses the production JsonSerializerOptions, so a numeric enum or a dropped field shows up here.
|
|
|
|
[Theory]
|
|
[InlineData(AbCipTransport.Libplctag)]
|
|
[InlineData(AbCipTransport.Native)]
|
|
[InlineData(null)]
|
|
public void Transport_survives_FromOptions_ToOptions(AbCipTransport? transport)
|
|
{
|
|
var original = new AbCipDriverOptions { Transport = transport };
|
|
var back = AbCipDriverForm.FormModel.FromOptions(original).ToOptions([]);
|
|
back.Transport.ShouldBe(transport);
|
|
}
|
|
|
|
[Fact]
|
|
public void Native_transport_serializes_as_a_string()
|
|
{
|
|
var form = new AbCipDriverForm.FormModel { Transport = AbCipTransport.Native };
|
|
var json = AbCipDriverForm.Serialize(form.ToOptions([]));
|
|
json.ShouldContain("\"transport\":\"Native\"");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Native", AbCipTransport.Native)]
|
|
[InlineData("Libplctag", AbCipTransport.Libplctag)]
|
|
public void Editing_an_unrelated_field_preserves_an_explicit_transport(string savedValue, AbCipTransport expected)
|
|
{
|
|
// The regression the review found: FromOptions/ToOptions dropped Transport, so an operator who set
|
|
// a transport was silently reverted the next time they saved ANY other edit.
|
|
var saved = $$"""{"transport":"{{savedValue}}","timeout":"00:00:02","devices":[]}""";
|
|
var loaded = AbCipDriverForm.TryDeserialize(saved);
|
|
loaded.ShouldNotBeNull();
|
|
loaded.Transport.ShouldBe(expected);
|
|
|
|
var form = AbCipDriverForm.FormModel.FromOptions(loaded);
|
|
form.TimeoutSeconds = 7;
|
|
var json = AbCipDriverForm.Serialize(form.ToOptions(loaded.Devices));
|
|
|
|
json.ShouldContain($"\"transport\":\"{savedValue}\"");
|
|
json.ShouldContain("\"timeout\":\"00:00:07\"");
|
|
}
|
|
|
|
[Fact]
|
|
public void A_config_without_transport_loads_as_Default_and_an_unrelated_edit_keeps_the_key_absent()
|
|
{
|
|
// Task 22 reverses Task 19's "re-emit an explicit Libplctag": that would pin every edited driver to
|
|
// a concrete transport and bypass the driver-side default (and AbLegacy's PLC-5 / LogixPccc
|
|
// exclusion). Absent loads as Default (null) and a save that edits something else still omits it.
|
|
var loaded = AbCipDriverForm.TryDeserialize("""{"probeTimeoutSeconds":5}""");
|
|
loaded.ShouldNotBeNull();
|
|
var form = AbCipDriverForm.FormModel.FromOptions(loaded);
|
|
form.Transport.ShouldBeNull();
|
|
|
|
form.TimeoutSeconds = 7;
|
|
var json = AbCipDriverForm.Serialize(form.ToOptions(loaded.Devices));
|
|
json.ShouldNotContain("transport", Case.Insensitive);
|
|
json.ShouldContain("\"timeout\":\"00:00:07\"");
|
|
}
|
|
|
|
[Fact]
|
|
public void Choosing_Default_over_an_explicit_transport_drops_the_key()
|
|
{
|
|
var loaded = AbCipDriverForm.TryDeserialize("""{"transport":"Libplctag"}""");
|
|
loaded.ShouldNotBeNull();
|
|
var form = AbCipDriverForm.FormModel.FromOptions(loaded);
|
|
form.Transport.ShouldBe(AbCipTransport.Libplctag);
|
|
|
|
form.Transport = null; // the "Default (...)" dropdown option
|
|
AbCipDriverForm.Serialize(form.ToOptions(loaded.Devices)).ShouldNotContain("transport", Case.Insensitive);
|
|
}
|
|
}
|