a0dccbf8ee
Code-review follow-ups: the page-coverage test now asserts every *DriverPage declares a _jsonOpts serializer (so a new page that serialises config a different way fails the guard, not just converter removal); clarify that 40 == (int)S7CpuType.S71500 in the numeric-throws test.
70 lines
3.8 KiB
C#
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(9, "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");
|
|
}
|
|
}
|