3d081ad29c
Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
144 lines
5.1 KiB
C#
144 lines
5.1 KiB
C#
using Opc.Ua;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;
|
|
using ZB.MOM.WW.OtOpcUa.Client.CLI.Tests.Fakes;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Tests;
|
|
|
|
public class ReadCommandTests
|
|
{
|
|
/// <summary>Verifies that execute prints the read value.</summary>
|
|
[Fact]
|
|
public async Task Execute_PrintsReadValue()
|
|
{
|
|
var sourceTime = new DateTime(2025, 6, 15, 10, 30, 0, DateTimeKind.Utc);
|
|
var serverTime = new DateTime(2025, 6, 15, 10, 30, 1, DateTimeKind.Utc);
|
|
var fakeService = new FakeOpcUaClientService
|
|
{
|
|
ReadValueResult = new DataValue(
|
|
new Variant("Hello"),
|
|
StatusCodes.Good,
|
|
sourceTime,
|
|
serverTime)
|
|
};
|
|
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
|
var command = new ReadCommand(factory)
|
|
{
|
|
Url = "opc.tcp://localhost:4840",
|
|
NodeId = "ns=2;s=TestNode"
|
|
};
|
|
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
await command.ExecuteAsync(console);
|
|
|
|
var output = TestConsoleHelper.GetOutput(console);
|
|
output.ShouldContain("Node: ns=2;s=TestNode");
|
|
output.ShouldContain("Value: Hello");
|
|
output.ShouldContain("Status:");
|
|
output.ShouldContain("Source Time:");
|
|
output.ShouldContain("Server Time:");
|
|
}
|
|
|
|
/// <summary>Verifies that execute calls read value with correct node ID.</summary>
|
|
[Fact]
|
|
public async Task Execute_CallsReadValueWithCorrectNodeId()
|
|
{
|
|
var fakeService = new FakeOpcUaClientService();
|
|
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
|
var command = new ReadCommand(factory)
|
|
{
|
|
Url = "opc.tcp://localhost:4840",
|
|
NodeId = "ns=2;s=MyVariable"
|
|
};
|
|
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
await command.ExecuteAsync(console);
|
|
|
|
fakeService.ReadNodeIds.Count.ShouldBe(1);
|
|
fakeService.ReadNodeIds[0].Identifier.ShouldBe("MyVariable");
|
|
}
|
|
|
|
/// <summary>Verifies that execute disconnects in finally.</summary>
|
|
[Fact]
|
|
public async Task Execute_DisconnectsInFinally()
|
|
{
|
|
var fakeService = new FakeOpcUaClientService();
|
|
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
|
var command = new ReadCommand(factory)
|
|
{
|
|
Url = "opc.tcp://localhost:4840",
|
|
NodeId = "ns=2;s=TestNode"
|
|
};
|
|
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
await command.ExecuteAsync(console);
|
|
|
|
fakeService.DisconnectCalled.ShouldBeTrue();
|
|
fakeService.DisposeCalled.ShouldBeTrue();
|
|
}
|
|
|
|
/// <summary>Verifies that execute disconnects even on read error.</summary>
|
|
[Fact]
|
|
public async Task Execute_DisconnectsEvenOnReadError()
|
|
{
|
|
var fakeService = new FakeOpcUaClientService
|
|
{
|
|
ReadException = new InvalidOperationException("Read failed")
|
|
};
|
|
var factory = new FakeOpcUaClientServiceFactory(fakeService);
|
|
var command = new ReadCommand(factory)
|
|
{
|
|
Url = "opc.tcp://localhost:4840",
|
|
NodeId = "ns=2;s=TestNode"
|
|
};
|
|
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
await Should.ThrowAsync<InvalidOperationException>(async () => await command.ExecuteAsync(console));
|
|
|
|
fakeService.DisconnectCalled.ShouldBeTrue();
|
|
fakeService.DisposeCalled.ShouldBeTrue();
|
|
}
|
|
|
|
/// <summary>A Bad node status is printed, not thrown (#577).</summary>
|
|
[Fact]
|
|
public async Task Execute_PrintsStatusCodeWhenTheReadFailsWithABadStatus()
|
|
{
|
|
var fakeService = new FakeOpcUaClientService
|
|
{
|
|
ReadException = new ServiceResultException(StatusCodes.BadNodeIdUnknown)
|
|
};
|
|
var command = new ReadCommand(new FakeOpcUaClientServiceFactory(fakeService))
|
|
{
|
|
Url = "opc.tcp://localhost:4840",
|
|
NodeId = "ns=2;s=Missing"
|
|
};
|
|
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
await command.ExecuteAsync(console); // must not throw
|
|
|
|
var output = TestConsoleHelper.GetOutput(console);
|
|
output.ShouldContain("Node: ns=2;s=Missing");
|
|
output.ShouldContain("BadNodeIdUnknown");
|
|
fakeService.DisconnectCalled.ShouldBeTrue(); // positive control: the finally still ran
|
|
}
|
|
|
|
/// <summary>A session-level failure is still an error, not a printed status.</summary>
|
|
[Fact]
|
|
public async Task Execute_StillThrowsOnASessionLevelFailure()
|
|
{
|
|
var fakeService = new FakeOpcUaClientService
|
|
{
|
|
ReadException = new ServiceResultException(StatusCodes.BadConnectionClosed)
|
|
};
|
|
var command = new ReadCommand(new FakeOpcUaClientServiceFactory(fakeService))
|
|
{
|
|
Url = "opc.tcp://localhost:4840",
|
|
NodeId = "ns=2;s=Any"
|
|
};
|
|
|
|
using var console = TestConsoleHelper.CreateConsole();
|
|
await Should.ThrowAsync<ServiceResultException>(async () => await command.ExecuteAsync(console));
|
|
}
|
|
}
|