Add cross-platform OPC UA client stack: shared library, CLI tool, and Avalonia UI
Implements Client.Shared (IOpcUaClientService with connection lifecycle, failover, browse, read/write, subscriptions, alarms, history, redundancy), Client.CLI (8 CliFx commands mirroring tools/opcuacli-dotnet), and Client.UI (Avalonia desktop app with tree browser, read/write, subscriptions, alarms, and history tabs). All three target .NET 10 and are covered by 249 unit tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
146
tests/ZB.MOM.WW.LmxOpcUa.Client.CLI.Tests/BrowseCommandTests.cs
Normal file
146
tests/ZB.MOM.WW.LmxOpcUa.Client.CLI.Tests/BrowseCommandTests.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.CLI.Commands;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.CLI.Tests.Fakes;
|
||||
using ZB.MOM.WW.LmxOpcUa.Client.Shared.Models;
|
||||
using BrowseResult = ZB.MOM.WW.LmxOpcUa.Client.Shared.Models.BrowseResult;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Client.CLI.Tests;
|
||||
|
||||
public class BrowseCommandTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Execute_PrintsBrowseResults()
|
||||
{
|
||||
var fakeService = new FakeOpcUaClientService
|
||||
{
|
||||
BrowseResults = new List<BrowseResult>
|
||||
{
|
||||
new BrowseResult("ns=2;s=Obj1", "Object1", "Object", true),
|
||||
new BrowseResult("ns=2;s=Var1", "Variable1", "Variable", false),
|
||||
new BrowseResult("ns=2;s=Meth1", "Method1", "Method", false)
|
||||
}
|
||||
};
|
||||
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
||||
var command = new BrowseCommand(factory)
|
||||
{
|
||||
Url = "opc.tcp://localhost:4840"
|
||||
};
|
||||
|
||||
using var console = TestConsoleHelper.CreateConsole();
|
||||
await command.ExecuteAsync(console);
|
||||
|
||||
var output = TestConsoleHelper.GetOutput(console);
|
||||
output.ShouldContain("[Object] Object1 (NodeId: ns=2;s=Obj1)");
|
||||
output.ShouldContain("[Variable] Variable1 (NodeId: ns=2;s=Var1)");
|
||||
output.ShouldContain("[Method] Method1 (NodeId: ns=2;s=Meth1)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Execute_BrowsesFromSpecifiedNode()
|
||||
{
|
||||
var fakeService = new FakeOpcUaClientService
|
||||
{
|
||||
BrowseResults = new List<BrowseResult>()
|
||||
};
|
||||
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
||||
var command = new BrowseCommand(factory)
|
||||
{
|
||||
Url = "opc.tcp://localhost:4840",
|
||||
NodeId = "ns=2;s=StartNode"
|
||||
};
|
||||
|
||||
using var console = TestConsoleHelper.CreateConsole();
|
||||
await command.ExecuteAsync(console);
|
||||
|
||||
fakeService.BrowseNodeIds.Count.ShouldBe(1);
|
||||
fakeService.BrowseNodeIds[0].ShouldNotBeNull();
|
||||
fakeService.BrowseNodeIds[0]!.Identifier.ShouldBe("StartNode");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Execute_DefaultBrowsesFromNull()
|
||||
{
|
||||
var fakeService = new FakeOpcUaClientService
|
||||
{
|
||||
BrowseResults = new List<BrowseResult>()
|
||||
};
|
||||
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
||||
var command = new BrowseCommand(factory)
|
||||
{
|
||||
Url = "opc.tcp://localhost:4840"
|
||||
};
|
||||
|
||||
using var console = TestConsoleHelper.CreateConsole();
|
||||
await command.ExecuteAsync(console);
|
||||
|
||||
fakeService.BrowseNodeIds.Count.ShouldBe(1);
|
||||
fakeService.BrowseNodeIds[0].ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Execute_NonRecursive_BrowsesSingleLevel()
|
||||
{
|
||||
var fakeService = new FakeOpcUaClientService
|
||||
{
|
||||
BrowseResults = new List<BrowseResult>
|
||||
{
|
||||
new BrowseResult("ns=2;s=Child", "Child", "Object", true)
|
||||
}
|
||||
};
|
||||
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
||||
var command = new BrowseCommand(factory)
|
||||
{
|
||||
Url = "opc.tcp://localhost:4840",
|
||||
Depth = 5 // Should be ignored without recursive flag
|
||||
};
|
||||
|
||||
using var console = TestConsoleHelper.CreateConsole();
|
||||
await command.ExecuteAsync(console);
|
||||
|
||||
// Only the root level browse should happen, not child
|
||||
fakeService.BrowseNodeIds.Count.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Execute_Recursive_BrowsesChildren()
|
||||
{
|
||||
var fakeService = new FakeOpcUaClientService();
|
||||
// Override browse to return children only on first call
|
||||
// We can't easily do this with the simple fake, but the default returns results with HasChildren=true
|
||||
// which will trigger child browse with recursive=true, depth=2
|
||||
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
||||
var command = new BrowseCommand(factory)
|
||||
{
|
||||
Url = "opc.tcp://localhost:4840",
|
||||
Recursive = true,
|
||||
Depth = 2
|
||||
};
|
||||
|
||||
using var console = TestConsoleHelper.CreateConsole();
|
||||
await command.ExecuteAsync(console);
|
||||
|
||||
// Root browse + child browse (for Node1 which HasChildren=true)
|
||||
fakeService.BrowseNodeIds.Count.ShouldBeGreaterThan(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Execute_DisconnectsInFinally()
|
||||
{
|
||||
var fakeService = new FakeOpcUaClientService
|
||||
{
|
||||
BrowseResults = new List<BrowseResult>()
|
||||
};
|
||||
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
||||
var command = new BrowseCommand(factory)
|
||||
{
|
||||
Url = "opc.tcp://localhost:4840"
|
||||
};
|
||||
|
||||
using var console = TestConsoleHelper.CreateConsole();
|
||||
await command.ExecuteAsync(console);
|
||||
|
||||
fakeService.DisconnectCalled.ShouldBeTrue();
|
||||
fakeService.DisposeCalled.ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user