bd6c0b4d3d
Add missing <returns>/<param>/<summary>/<typeparam> tags and clean up misused inheritdoc across 481 files so the documented API surface is complete. Documentation-only (zero code lines changed). The 131 remaining findings are inheritdoc-style warnings deliberately left to preserve hand-written implementation rationale (plan-decision notes, race-condition explanations).
152 lines
7.0 KiB
C#
152 lines
7.0 KiB
C#
using System.Collections.Concurrent;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Opc.Ua.Server;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
|
|
|
/// <summary>
|
|
/// #85 — verifies <see cref="Phase7Applier.MaterialiseHierarchy"/> builds the UNS
|
|
/// Area/Line/Equipment folder tree through <see cref="IOpcUaAddressSpaceSink.EnsureFolder"/>.
|
|
/// One pure unit test (recording sink) confirms ordering + parenting; one boot-verify test
|
|
/// drives a real <see cref="OtOpcUaNodeManager"/> and inspects the resulting predefined-node
|
|
/// count to prove the folders land in the SDK address space.
|
|
/// </summary>
|
|
public sealed class Phase7ApplierHierarchyTests : IDisposable
|
|
{
|
|
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
|
|
|
private readonly string _pkiRoot = Path.Combine(
|
|
Path.GetTempPath(),
|
|
$"otopcua-pki-{Guid.NewGuid():N}");
|
|
|
|
/// <summary>Verifies that MaterialiseHierarchy creates areas, lines, and equipment with correct parent relationships.</summary>
|
|
[Fact]
|
|
public void MaterialiseHierarchy_creates_areas_then_lines_then_equipment_with_correct_parents()
|
|
{
|
|
var sink = new RecordingFolderSink();
|
|
var applier = new Phase7Applier(sink, NullLogger<Phase7Applier>.Instance);
|
|
|
|
var composition = new Phase7CompositionResult(
|
|
UnsAreas: new[] { new UnsAreaProjection("area-1", "Plant North") },
|
|
UnsLines: new[] { new UnsLineProjection("line-1", "area-1", "Cell A") },
|
|
EquipmentNodes: new[] { new EquipmentNode("eq-1", "Pump-1", "line-1") },
|
|
DriverInstancePlans: Array.Empty<DriverInstancePlan>(),
|
|
ScriptedAlarmPlans: Array.Empty<ScriptedAlarmPlan>(),
|
|
GalaxyTags: Array.Empty<GalaxyTagPlan>());
|
|
|
|
applier.MaterialiseHierarchy(composition);
|
|
|
|
var calls = sink.Calls;
|
|
calls.Count.ShouldBe(3);
|
|
calls[0].ShouldBe(("area-1", null, "Plant North"));
|
|
calls[1].ShouldBe(("line-1", "area-1", "Cell A"));
|
|
calls[2].ShouldBe(("eq-1", "line-1", "Pump-1"));
|
|
}
|
|
|
|
/// <summary>Verifies that orphan equipment without a parent line appears under root.</summary>
|
|
[Fact]
|
|
public void MaterialiseHierarchy_orphan_equipment_hangs_under_root()
|
|
{
|
|
var sink = new RecordingFolderSink();
|
|
var applier = new Phase7Applier(sink, NullLogger<Phase7Applier>.Instance);
|
|
|
|
var composition = new Phase7CompositionResult(
|
|
UnsAreas: Array.Empty<UnsAreaProjection>(),
|
|
UnsLines: Array.Empty<UnsLineProjection>(),
|
|
EquipmentNodes: new[] { new EquipmentNode("eq-orphan", "Orphan", UnsLineId: "") },
|
|
DriverInstancePlans: Array.Empty<DriverInstancePlan>(),
|
|
ScriptedAlarmPlans: Array.Empty<ScriptedAlarmPlan>(),
|
|
GalaxyTags: Array.Empty<GalaxyTagPlan>());
|
|
|
|
applier.MaterialiseHierarchy(composition);
|
|
|
|
sink.Calls.Single().ShouldBe(("eq-orphan", null, "Orphan"));
|
|
}
|
|
|
|
/// <summary>Verifies that MaterialiseHierarchy creates folder nodes in a real SDK node manager.</summary>
|
|
/// <returns>A task that represents the asynchronous test operation.</returns>
|
|
[Fact]
|
|
public async Task MaterialiseHierarchy_against_real_SDK_node_manager_creates_folder_nodes()
|
|
{
|
|
await using var host = new OpcUaApplicationHost(
|
|
new OpcUaApplicationHostOptions
|
|
{
|
|
ApplicationName = "OtOpcUa.Hierarchy",
|
|
ApplicationUri = $"urn:OtOpcUa.Hierarchy:{Guid.NewGuid():N}",
|
|
OpcUaPort = AllocateFreePort(),
|
|
PublicHostname = "localhost",
|
|
PkiStoreRoot = _pkiRoot,
|
|
},
|
|
NullLogger<OpcUaApplicationHost>.Instance);
|
|
|
|
var sdkServer = new OtOpcUaSdkServer();
|
|
await host.StartAsync(sdkServer, Ct);
|
|
sdkServer.NodeManager.ShouldNotBeNull();
|
|
|
|
var sink = new SdkAddressSpaceSink(sdkServer.NodeManager!);
|
|
var applier = new Phase7Applier(sink, NullLogger<Phase7Applier>.Instance);
|
|
|
|
applier.MaterialiseHierarchy(new Phase7CompositionResult(
|
|
UnsAreas: new[] { new UnsAreaProjection("area-A", "Area A"), new UnsAreaProjection("area-B", "Area B") },
|
|
UnsLines: new[] { new UnsLineProjection("line-1", "area-A", "Line 1") },
|
|
EquipmentNodes: new[] { new EquipmentNode("eq-1", "Eq 1", "line-1"), new EquipmentNode("eq-2", "Eq 2", "line-1") },
|
|
DriverInstancePlans: Array.Empty<DriverInstancePlan>(),
|
|
ScriptedAlarmPlans: Array.Empty<ScriptedAlarmPlan>(),
|
|
GalaxyTags: Array.Empty<GalaxyTagPlan>()));
|
|
|
|
sdkServer.NodeManager!.FolderCount.ShouldBe(5); // 2 areas + 1 line + 2 equipment
|
|
|
|
// Idempotent: re-applying with the same composition doesn't create duplicates.
|
|
applier.MaterialiseHierarchy(new Phase7CompositionResult(
|
|
UnsAreas: new[] { new UnsAreaProjection("area-A", "Area A"), new UnsAreaProjection("area-B", "Area B") },
|
|
UnsLines: new[] { new UnsLineProjection("line-1", "area-A", "Line 1") },
|
|
EquipmentNodes: new[] { new EquipmentNode("eq-1", "Eq 1", "line-1"), new EquipmentNode("eq-2", "Eq 2", "line-1") },
|
|
DriverInstancePlans: Array.Empty<DriverInstancePlan>(),
|
|
ScriptedAlarmPlans: Array.Empty<ScriptedAlarmPlan>(),
|
|
GalaxyTags: Array.Empty<GalaxyTagPlan>()));
|
|
|
|
sdkServer.NodeManager!.FolderCount.ShouldBe(5);
|
|
}
|
|
|
|
private static int AllocateFreePort()
|
|
{
|
|
using var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
|
|
listener.Start();
|
|
var port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;
|
|
listener.Stop();
|
|
return port;
|
|
}
|
|
|
|
/// <summary>Disposes of resources allocated by this test class.</summary>
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(_pkiRoot))
|
|
{
|
|
try { Directory.Delete(_pkiRoot, recursive: true); }
|
|
catch { /* best-effort */ }
|
|
}
|
|
}
|
|
|
|
private sealed class RecordingFolderSink : IOpcUaAddressSpaceSink
|
|
{
|
|
private readonly ConcurrentQueue<(string NodeId, string? Parent, string DisplayName)> _calls = new();
|
|
/// <summary>Gets the list of EnsureFolder calls recorded by this sink.</summary>
|
|
public List<(string NodeId, string? Parent, string DisplayName)> Calls => _calls.ToList();
|
|
|
|
/// <inheritdoc />
|
|
public void WriteValue(string nodeId, object? value, OpcUaQuality quality, DateTime sourceTimestampUtc) { }
|
|
/// <inheritdoc />
|
|
public void WriteAlarmState(string alarmNodeId, bool active, bool acknowledged, DateTime sourceTimestampUtc) { }
|
|
/// <inheritdoc />
|
|
public void EnsureFolder(string folderNodeId, string? parentNodeId, string displayName)
|
|
=> _calls.Enqueue((folderNodeId, parentNodeId, displayName));
|
|
/// <inheritdoc />
|
|
public void EnsureVariable(string variableNodeId, string? parentFolderNodeId, string displayName, string dataType) { }
|
|
/// <inheritdoc />
|
|
public void RebuildAddressSpace() { }
|
|
}
|
|
}
|