Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/DriverPageJsonConverterTests.cs
T
Joseph Doherty 0b4b2e4cfd refactor(historian-gateway): retire Wonderware historian projects (gateway is sole backend)
The HistorianGateway driver is now the sole historian read/write+alarm backend, so the
Wonderware sidecar projects are dead code. Removes the 5 Wonderware projects (driver,
.Client, .Client.Contracts, + their 2 test projects) from the solution and tree, and fully
retires the vestigial 'Historian.Wonderware' driver type (UI/probe-only; it had no driver
factory): the Host probe registration, the AdminUI driver-config surface (driver page,
tag-config editor/model/validator entry, address picker/builder, driver-type catalog +
dropdown + edit-router entries), and their tests. Prunes the now-unused Wonderware
connection fields (Host/Port/UseTls/ServerCertThumbprint/SharedSecret) from
AlarmHistorianOptions (keeping Enabled + the SQLite store-and-forward knobs) and refreshes
the stale XML docs that named Wonderware as the production backend.

Claude-Session: https://claude.ai/code/session_012SDSQ3AcaXqPcBtDESBRii
2026-06-26 19:25:21 -04:00

70 lines
3.8 KiB
C#

using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.Drivers;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests;
/// <summary>
/// Regression guard for the systemic driver-config enum-serialization bug found 2026-06-19.
/// Every <c>*DriverPage</c> serialised enum config fields (e.g. S7 <c>CpuType</c>, Modbus
/// <c>DataType</c>/<c>Region</c>, AbCip <c>PlcFamily</c>) as JSON <em>numbers</em> because its
/// private static <c>_jsonOpts</c> had no <see cref="JsonStringEnumConverter"/>. The driver
/// factories, however, deserialise into string-typed DTOs (+ lenient <c>ParseEnum</c>) and
/// <em>throw</em> when binding a JSON number to a <c>string?</c> — so an AdminUI-authored
/// config that contained any enum field produced a blob the driver could not parse, faulting
/// the driver on deploy. The fix makes every page serialise enums as strings (matching the
/// factory + the long-correct OpcUaClient template). This test fails if any driver page loses
/// its string-enum converter again.
/// </summary>
public sealed class DriverPageJsonConverterTests
{
/// <summary>Every concrete <c>*DriverPage</c> in the AdminUI assembly that declares a
/// <c>_jsonOpts</c> config serializer.</summary>
private static IReadOnlyList<Type> DriverPageTypes { get; } =
typeof(S7DriverPage).Assembly.GetTypes()
.Where(t => t.Name.EndsWith("DriverPage", StringComparison.Ordinal) && !t.IsAbstract)
.Where(t => t.GetField("_jsonOpts", BindingFlags.NonPublic | BindingFlags.Static) is not null)
.OrderBy(t => t.Name)
.ToList();
/// <summary>xUnit theory source over the driver-page types discovered by reflection.</summary>
public static TheoryData<Type> DriverPagesWithJsonOpts()
{
var data = new TheoryData<Type>();
foreach (var t in DriverPageTypes)
data.Add(t);
return data;
}
/// <summary>Verifies every driver page's config serializer registers a string-enum converter so
/// enum config fields round-trip as strings (and the driver factory can parse the result).</summary>
/// <param name="pageType">A driver page component type discovered by reflection.</param>
[Theory]
[MemberData(nameof(DriverPagesWithJsonOpts))]
public void Driver_page_json_options_register_string_enum_converter(Type pageType)
{
var field = pageType.GetField("_jsonOpts", BindingFlags.NonPublic | BindingFlags.Static);
var opts = (JsonSerializerOptions)field!.GetValue(null)!;
opts.Converters.OfType<JsonStringEnumConverter>().ShouldNotBeEmpty(
$"{pageType.Name}._jsonOpts must register a JsonStringEnumConverter; otherwise AdminUI-authored " +
"enum config fields serialise as numbers and the string-typed driver factory throws on parse.");
}
/// <summary>Enforces that EVERY concrete <c>*DriverPage</c> routes config serialization through a
/// <c>_jsonOpts</c> field — otherwise a new page that serialised config a different way would slip
/// past the converter guard above. Also a floor check so a rename can't silently shrink the set.</summary>
[Fact]
public void Every_driver_page_uses_a_guarded_jsonOpts_serializer()
{
var allDriverPages = typeof(S7DriverPage).Assembly.GetTypes()
.Where(t => t.Name.EndsWith("DriverPage", StringComparison.Ordinal) && !t.IsAbstract)
.ToList();
allDriverPages.Count.ShouldBeGreaterThanOrEqualTo(8, "reflection should discover the full driver-page fleet");
DriverPageTypes.Count.ShouldBe(allDriverPages.Count,
"every *DriverPage must declare a _jsonOpts config serializer so the string-enum converter guard covers it");
}
}