Files
lmxopcua/tests/Client/ZB.MOM.WW.OtOpcUa.Client.CLI.Tests/ReadCommandTests.cs
T

215 lines
8.5 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
{
[Theory]
[InlineData(0)]
[InlineData(5000)]
[InlineData(2147483647)]
public async Task Execute_ForwardsMaximumAge(double milliseconds)
{
var service = new FakeOpcUaClientService();
var command = new ReadCommand(new FakeOpcUaClientServiceFactory(service))
{
Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Test", MaxAgeMilliseconds = milliseconds
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
if (milliseconds == 0) service.LastMaximumAge.ShouldBeNull();
else service.LastMaximumAge.ShouldBe(TimeSpan.FromMilliseconds(milliseconds));
}
[Theory]
[InlineData(-1)]
[InlineData(2147483648d)]
[InlineData(double.NaN)]
[InlineData(double.PositiveInfinity)]
public async Task Execute_RejectsInvalidMaximumAge(double milliseconds)
{
var service = new FakeOpcUaClientService();
var command = new ReadCommand(new FakeOpcUaClientServiceFactory(service))
{
Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Test", MaxAgeMilliseconds = milliseconds
};
using var console = TestConsoleHelper.CreateConsole();
await Should.ThrowAsync<CliFx.Exceptions.CommandException>(async () => await command.ExecuteAsync(console));
service.ReadNodeIds.ShouldBeEmpty();
}
/// <summary>lmxopcua#921: <c>--attribute Historizing</c> reads that attribute (not the value) and labels the output.</summary>
[Fact]
public async Task Execute_ReadsTheRequestedAttribute()
{
var service = new FakeOpcUaClientService { ReadValueResult = new DataValue(new Variant(false), StatusCodes.Good) };
var command = new ReadCommand(new FakeOpcUaClientServiceFactory(service))
{
Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Test", Attribute = "historizing"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
service.ReadAttributes.ShouldBe([(new NodeId("Test", 2), Attributes.Historizing)]);
service.ReadNodeIds.ShouldBeEmpty();
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Attribute: historizing");
output.ShouldContain("Value: False");
}
/// <summary>An unknown attribute, or <c>--max-age</c> with a non-Value attribute, is refused before connecting.</summary>
[Theory]
[InlineData("Bogus", 0)]
[InlineData("Historizing", 5000)]
public async Task Execute_RejectsAnUnknownAttributeOrMaxAgeOffTheValue(string attribute, double maxAge)
{
var service = new FakeOpcUaClientService();
var command = new ReadCommand(new FakeOpcUaClientServiceFactory(service))
{
Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Test", Attribute = attribute, MaxAgeMilliseconds = maxAge
};
using var console = TestConsoleHelper.CreateConsole();
await Should.ThrowAsync<CliFx.Exceptions.CommandException>(async () => await command.ExecuteAsync(console));
service.ReadAttributes.ShouldBeEmpty();
service.ReadNodeIds.ShouldBeEmpty();
}
/// <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).AddTicks(1234567);
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:");
output.ShouldContain("Source Time: " + sourceTime.ToString("O", System.Globalization.CultureInfo.InvariantCulture));
output.ShouldContain("Server Time: " + serverTime.ToString("O", System.Globalization.CultureInfo.InvariantCulture));
}
/// <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].IdentifierAsString.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));
}
}