Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests/Browse/GatewayGalaxyPlatformSourceTests.cs
T
Joseph Doherty c4bc1ef309 fix(galaxy): seed platforms with one template filter per request (#864)
The gateway's shared GalaxyHierarchyProjector ANDs template_chain_contains
entries, so the single request carrying both $WinPlatform and $AppEngine
matched no object and the probe watcher always seeded 0 platforms.
GatewayGalaxyPlatformSource now runs one paged sweep per template and
unions/dedupes by tag name. The tests use a fake gateway that applies the
projector's AND semantics (they fail on the old single request).

Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
2026-11-11 12:24:03 -05:00

127 lines
5.8 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.MxGateway.Contracts.Proto.Galaxy;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Browse;
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Browse;
/// <summary>The platform-only query the driver seeds its ScanState probe watcher from.</summary>
public sealed class GatewayGalaxyPlatformSourceTests
{
private static GalaxyObject Obj(string tag, params string[] chain)
{
var o = new GalaxyObject { TagName = tag };
o.TemplateChain.Add(chain);
return o;
}
/// <summary>
/// A fake gateway that applies the request filters with the SAME semantics as the shared
/// <c>GalaxyHierarchyProjector.MatchesFilters</c>: every <c>template_chain_contains</c> token must
/// match (AND, case-insensitive substring over the chain), <c>category_ids</c> is an OR set. Pages
/// the filtered result at the request's page size.
/// </summary>
private sealed class FilteringGateway(params GalaxyObject[] galaxy)
{
public List<DiscoverHierarchyRequest> Requests { get; } = [];
public Task<DiscoverHierarchyReply> Discover(DiscoverHierarchyRequest req, CancellationToken ct)
{
Requests.Add(req.Clone());
var matched = galaxy.Where(o => Matches(o, req)).ToList();
var offset = req.PageToken == "" ? 0 : int.Parse(req.PageToken.Split(':')[1]);
var reply = new DiscoverHierarchyReply();
reply.Objects.Add(matched.Skip(offset).Take(req.PageSize));
if (offset + req.PageSize < matched.Count)
{
reply.NextPageToken = $"{string.Join(",", req.TemplateChainContains)}:{offset + req.PageSize}";
}
return Task.FromResult(reply);
}
private static bool Matches(GalaxyObject obj, DiscoverHierarchyRequest req)
{
if (req.CategoryIds.Count > 0 && !req.CategoryIds.Contains(obj.CategoryId)) return false;
foreach (var filter in req.TemplateChainContains)
{
if (!obj.TemplateChain.Any(t => t.Contains(filter, StringComparison.OrdinalIgnoreCase))) return false;
}
return true;
}
}
/// <summary>
/// #864: against a gateway that ANDs template filters (as the real one does), every deployed
/// <c>$WinPlatform</c> AND every <c>$AppEngine</c> is found — the old single request carrying both
/// tokens matched none. Shape of the VD03 DEV galaxy: 3 platforms, 2 engines.
/// </summary>
[Fact]
public async Task Finds_every_platform_and_engine_against_an_AND_filtering_gateway()
{
var gw = new FilteringGateway(
Obj("CTRX_DEV", "$WinPlatform"),
Obj("DevWinPlatform", "$WinPlatform"),
Obj("JoePlatform", "$JoePlatformTemplate", "$WinPlatform"),
Obj("DevAppEngine", "$AppEngine"),
Obj("JoeAppEngine", "$AppEngine"),
Obj("Tank01", "$UserDefined"),
Obj("Area01", "$Area"));
var names = await GatewayGalaxyPlatformSource.QueryAsync(gw.Discover, pageSize: 50, CancellationToken.None);
names.ShouldBe(["CTRX_DEV", "DevWinPlatform", "JoePlatform", "DevAppEngine", "JoeAppEngine"]);
gw.Requests.ShouldAllBe(r => r.TemplateChainContains.Count == 1 && r.PageSize == 50 && r.IncludeAttributes == false);
gw.Requests.Select(r => r.TemplateChainContains[0]).Distinct().ShouldBe(["$WinPlatform", "$AppEngine"]);
}
/// <summary>Each per-template sweep pages to completion independently; tag names are de-duplicated across sweeps.</summary>
[Fact]
public async Task Each_template_sweep_pages_independently_and_names_are_deduplicated()
{
var gw = new FilteringGateway(
Obj("Plat01", "$WinPlatform"),
Obj("Plat02", "$WinPlatform"),
Obj("Plat03", "$WinPlatform"),
// An object whose chain carries both templates is reported once.
Obj("Hybrid01", "$AppEngine", "$WinPlatform"),
Obj("Eng01", "$AppEngine"),
Obj("Eng02", "$AppEngine"),
Obj("", "$WinPlatform"));
var names = await GatewayGalaxyPlatformSource.QueryAsync(gw.Discover, pageSize: 2, CancellationToken.None);
names.ShouldBe(["Plat01", "Plat02", "Plat03", "Hybrid01", "Eng01", "Eng02"]);
// $WinPlatform: 5 matches (incl. the empty tag) => 3 pages; $AppEngine: 3 matches => 2 pages.
gw.Requests.Count(r => r.TemplateChainContains[0] == "$WinPlatform").ShouldBe(3);
gw.Requests.Count(r => r.TemplateChainContains[0] == "$AppEngine").ShouldBe(2);
}
/// <summary>A gateway that ignores the filter still yields only platforms (replies are re-filtered on the chain).</summary>
[Fact]
public async Task Replies_are_refiltered_on_the_template_chain()
{
Task<DiscoverHierarchyReply> Discover(DiscoverHierarchyRequest req, CancellationToken ct)
{
var reply = new DiscoverHierarchyReply();
reply.Objects.Add([Obj("Plat01", "$WinPlatform"), Obj("Tank01", "$UserDefined"), Obj("Eng01", "$AppEngine")]);
return Task.FromResult(reply);
}
(await GatewayGalaxyPlatformSource.QueryAsync(Discover, pageSize: 50, CancellationToken.None)).ShouldBe(["Plat01", "Eng01"]);
}
/// <summary>A gateway that strips the template chain is trusted to have filtered server-side.</summary>
[Fact]
public async Task Objects_without_a_template_chain_are_kept()
{
Task<DiscoverHierarchyReply> Discover(DiscoverHierarchyRequest req, CancellationToken ct)
{
var reply = new DiscoverHierarchyReply();
reply.Objects.Add(new GalaxyObject { TagName = "Plat01" });
return Task.FromResult(reply);
}
(await GatewayGalaxyPlatformSource.QueryAsync(Discover, pageSize: 0, CancellationToken.None)).ShouldBe(["Plat01"]);
}
}