Add Galaxy platform scope filter so multi-node deployments can restrict the OPC UA address space to only objects hosted by the local platform, reducing memory footprint and MXAccess subscription count from the full Galaxy (49 objects / 4206 attributes) down to the local subtree (3 objects / 386 attributes on the dev Galaxy).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
using System.Collections.Generic;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host.Domain;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host.GalaxyRepository;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Tests.GalaxyRepository
|
||||
{
|
||||
public class PlatformScopeFilterTests
|
||||
{
|
||||
// Category constants matching the Galaxy schema.
|
||||
private const int CatPlatform = 1;
|
||||
private const int CatAppEngine = 3;
|
||||
private const int CatUserDefined = 10;
|
||||
private const int CatArea = 13;
|
||||
|
||||
/// <summary>
|
||||
/// Builds a two-platform Galaxy hierarchy for filtering tests.
|
||||
///
|
||||
/// Structure:
|
||||
/// Area1 (id=1, area, parent=0)
|
||||
/// PlatformA (id=10, cat=1, hosted_by=0) ← node "NODEA"
|
||||
/// EngineA (id=20, cat=3, hosted_by=10)
|
||||
/// Obj1 (id=30, cat=10, hosted_by=20)
|
||||
/// Obj2 (id=31, cat=10, hosted_by=20)
|
||||
/// PlatformB (id=11, cat=1, hosted_by=0) ← node "NODEB"
|
||||
/// EngineB (id=21, cat=3, hosted_by=11)
|
||||
/// Obj3 (id=32, cat=10, hosted_by=21)
|
||||
/// Area2 (id=2, area, parent=0)
|
||||
/// Obj4 (id=33, cat=10, hosted_by=21) ← hosted by EngineB
|
||||
/// </summary>
|
||||
private static (List<GalaxyObjectInfo> hierarchy, List<PlatformInfo> platforms) CreateTwoPlatformGalaxy()
|
||||
{
|
||||
var hierarchy = new List<GalaxyObjectInfo>
|
||||
{
|
||||
new() { GobjectId = 1, TagName = "Area1", ContainedName = "Area1", BrowseName = "Area1", ParentGobjectId = 0, IsArea = true, CategoryId = CatArea, HostedByGobjectId = 0 },
|
||||
new() { GobjectId = 10, TagName = "PlatformA", ContainedName = "PlatformA", BrowseName = "PlatformA", ParentGobjectId = 1, IsArea = false, CategoryId = CatPlatform, HostedByGobjectId = 0 },
|
||||
new() { GobjectId = 20, TagName = "EngineA_001", ContainedName = "EngineA", BrowseName = "EngineA", ParentGobjectId = 10, IsArea = false, CategoryId = CatAppEngine, HostedByGobjectId = 10 },
|
||||
new() { GobjectId = 30, TagName = "Obj1_001", ContainedName = "Obj1", BrowseName = "Obj1", ParentGobjectId = 20, IsArea = false, CategoryId = CatUserDefined, HostedByGobjectId = 20 },
|
||||
new() { GobjectId = 31, TagName = "Obj2_001", ContainedName = "Obj2", BrowseName = "Obj2", ParentGobjectId = 20, IsArea = false, CategoryId = CatUserDefined, HostedByGobjectId = 20 },
|
||||
new() { GobjectId = 11, TagName = "PlatformB", ContainedName = "PlatformB", BrowseName = "PlatformB", ParentGobjectId = 1, IsArea = false, CategoryId = CatPlatform, HostedByGobjectId = 0 },
|
||||
new() { GobjectId = 21, TagName = "EngineB_001", ContainedName = "EngineB", BrowseName = "EngineB", ParentGobjectId = 11, IsArea = false, CategoryId = CatAppEngine, HostedByGobjectId = 11 },
|
||||
new() { GobjectId = 32, TagName = "Obj3_001", ContainedName = "Obj3", BrowseName = "Obj3", ParentGobjectId = 21, IsArea = false, CategoryId = CatUserDefined, HostedByGobjectId = 21 },
|
||||
new() { GobjectId = 2, TagName = "Area2", ContainedName = "Area2", BrowseName = "Area2", ParentGobjectId = 0, IsArea = true, CategoryId = CatArea, HostedByGobjectId = 0 },
|
||||
new() { GobjectId = 33, TagName = "Obj4_001", ContainedName = "Obj4", BrowseName = "Obj4", ParentGobjectId = 2, IsArea = false, CategoryId = CatUserDefined, HostedByGobjectId = 21 },
|
||||
};
|
||||
|
||||
var platforms = new List<PlatformInfo>
|
||||
{
|
||||
new() { GobjectId = 10, NodeName = "NODEA" },
|
||||
new() { GobjectId = 11, NodeName = "NODEB" },
|
||||
};
|
||||
|
||||
return (hierarchy, platforms);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Filter_ReturnsOnlyObjectsUnderMatchingPlatform()
|
||||
{
|
||||
var (hierarchy, platforms) = CreateTwoPlatformGalaxy();
|
||||
|
||||
var (filtered, ids) = PlatformScopeFilter.Filter(hierarchy, platforms, "NODEA");
|
||||
|
||||
// Should include: Area1, PlatformA, EngineA, Obj1, Obj2
|
||||
// Should exclude: PlatformB, EngineB, Obj3, Area2, Obj4
|
||||
ids.ShouldContain(1); // Area1 (ancestor of PlatformA)
|
||||
ids.ShouldContain(10); // PlatformA
|
||||
ids.ShouldContain(20); // EngineA
|
||||
ids.ShouldContain(30); // Obj1
|
||||
ids.ShouldContain(31); // Obj2
|
||||
ids.ShouldNotContain(11); // PlatformB
|
||||
ids.ShouldNotContain(21); // EngineB
|
||||
ids.ShouldNotContain(32); // Obj3
|
||||
ids.ShouldNotContain(33); // Obj4
|
||||
ids.ShouldNotContain(2); // Area2 (no local children)
|
||||
filtered.Count.ShouldBe(5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Filter_ReturnsObjectsUnderPlatformB()
|
||||
{
|
||||
var (hierarchy, platforms) = CreateTwoPlatformGalaxy();
|
||||
|
||||
var (filtered, ids) = PlatformScopeFilter.Filter(hierarchy, platforms, "NODEB");
|
||||
|
||||
// Should include: Area1, PlatformB, EngineB, Obj3, Area2, Obj4
|
||||
ids.ShouldContain(1); // Area1 (ancestor of PlatformB)
|
||||
ids.ShouldContain(11); // PlatformB
|
||||
ids.ShouldContain(21); // EngineB
|
||||
ids.ShouldContain(32); // Obj3
|
||||
ids.ShouldContain(2); // Area2 (has Obj4 hosted by EngineB)
|
||||
ids.ShouldContain(33); // Obj4
|
||||
// Should exclude PlatformA's subtree
|
||||
ids.ShouldNotContain(10);
|
||||
ids.ShouldNotContain(20);
|
||||
ids.ShouldNotContain(30);
|
||||
ids.ShouldNotContain(31);
|
||||
filtered.Count.ShouldBe(6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Filter_IsCaseInsensitiveOnNodeName()
|
||||
{
|
||||
var (hierarchy, platforms) = CreateTwoPlatformGalaxy();
|
||||
|
||||
var (filtered, _) = PlatformScopeFilter.Filter(hierarchy, platforms, "nodea");
|
||||
|
||||
filtered.Count.ShouldBe(5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Filter_ReturnsEmptyWhenNoMatchingPlatform()
|
||||
{
|
||||
var (hierarchy, platforms) = CreateTwoPlatformGalaxy();
|
||||
|
||||
var (filtered, ids) = PlatformScopeFilter.Filter(hierarchy, platforms, "UNKNOWN");
|
||||
|
||||
filtered.ShouldBeEmpty();
|
||||
ids.ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Filter_IncludesAncestorAreasForConnectedTree()
|
||||
{
|
||||
// An object nested several levels deep should pull in all ancestor areas.
|
||||
var hierarchy = new List<GalaxyObjectInfo>
|
||||
{
|
||||
new() { GobjectId = 1, TagName = "TopArea", ContainedName = "TopArea", BrowseName = "TopArea", ParentGobjectId = 0, IsArea = true, CategoryId = CatArea, HostedByGobjectId = 0 },
|
||||
new() { GobjectId = 2, TagName = "SubArea", ContainedName = "SubArea", BrowseName = "SubArea", ParentGobjectId = 1, IsArea = true, CategoryId = CatArea, HostedByGobjectId = 0 },
|
||||
new() { GobjectId = 10, TagName = "Plat", ContainedName = "Plat", BrowseName = "Plat", ParentGobjectId = 2, IsArea = false, CategoryId = CatPlatform, HostedByGobjectId = 0 },
|
||||
new() { GobjectId = 20, TagName = "Eng", ContainedName = "Eng", BrowseName = "Eng", ParentGobjectId = 10, IsArea = false, CategoryId = CatAppEngine, HostedByGobjectId = 10 },
|
||||
new() { GobjectId = 30, TagName = "Obj", ContainedName = "Obj", BrowseName = "Obj", ParentGobjectId = 20, IsArea = false, CategoryId = CatUserDefined, HostedByGobjectId = 20 },
|
||||
};
|
||||
var platforms = new List<PlatformInfo> { new() { GobjectId = 10, NodeName = "LOCAL" } };
|
||||
|
||||
var (filtered, ids) = PlatformScopeFilter.Filter(hierarchy, platforms, "LOCAL");
|
||||
|
||||
ids.ShouldContain(1); // TopArea
|
||||
ids.ShouldContain(2); // SubArea
|
||||
ids.ShouldContain(10); // Platform
|
||||
ids.ShouldContain(20); // Engine
|
||||
ids.ShouldContain(30); // Object
|
||||
filtered.Count.ShouldBe(5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Filter_ExcludesAreaWithNoLocalDescendants()
|
||||
{
|
||||
var hierarchy = new List<GalaxyObjectInfo>
|
||||
{
|
||||
new() { GobjectId = 1, TagName = "UsedArea", ContainedName = "UsedArea", BrowseName = "UsedArea", ParentGobjectId = 0, IsArea = true, CategoryId = CatArea, HostedByGobjectId = 0 },
|
||||
new() { GobjectId = 2, TagName = "EmptyArea", ContainedName = "EmptyArea", BrowseName = "EmptyArea", ParentGobjectId = 0, IsArea = true, CategoryId = CatArea, HostedByGobjectId = 0 },
|
||||
new() { GobjectId = 10, TagName = "Plat", ContainedName = "Plat", BrowseName = "Plat", ParentGobjectId = 1, IsArea = false, CategoryId = CatPlatform, HostedByGobjectId = 0 },
|
||||
};
|
||||
var platforms = new List<PlatformInfo> { new() { GobjectId = 10, NodeName = "LOCAL" } };
|
||||
|
||||
var (_, ids) = PlatformScopeFilter.Filter(hierarchy, platforms, "LOCAL");
|
||||
|
||||
ids.ShouldContain(1); // UsedArea (ancestor of Plat)
|
||||
ids.ShouldNotContain(2); // EmptyArea (no local descendants)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FilterAttributes_RetainsOnlyMatchingGobjectIds()
|
||||
{
|
||||
var gobjectIds = new HashSet<int> { 10, 30 };
|
||||
var attributes = new List<GalaxyAttributeInfo>
|
||||
{
|
||||
new() { GobjectId = 10, TagName = "Plat", AttributeName = "Attr1", FullTagReference = "Plat.Attr1" },
|
||||
new() { GobjectId = 20, TagName = "Other", AttributeName = "Attr2", FullTagReference = "Other.Attr2" },
|
||||
new() { GobjectId = 30, TagName = "Obj", AttributeName = "Attr3", FullTagReference = "Obj.Attr3" },
|
||||
};
|
||||
|
||||
var filtered = PlatformScopeFilter.FilterAttributes(attributes, gobjectIds);
|
||||
|
||||
filtered.Count.ShouldBe(2);
|
||||
filtered.ShouldAllBe(a => gobjectIds.Contains(a.GobjectId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Filter_PreservesOriginalOrder()
|
||||
{
|
||||
var (hierarchy, platforms) = CreateTwoPlatformGalaxy();
|
||||
|
||||
var (filtered, _) = PlatformScopeFilter.Filter(hierarchy, platforms, "NODEA");
|
||||
|
||||
// Verify the order matches the original hierarchy order for included items.
|
||||
for (int i = 1; i < filtered.Count; i++)
|
||||
{
|
||||
var prevIndex = hierarchy.FindIndex(o => o.GobjectId == filtered[i - 1].GobjectId);
|
||||
var currIndex = hierarchy.FindIndex(o => o.GobjectId == filtered[i].GobjectId);
|
||||
prevIndex.ShouldBeLessThan(currIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user