test: validate grpc data connections with local docker and opcua cli

This commit is contained in:
Joseph Doherty
2026-11-17 22:01:26 -05:00
parent 1df29a3124
commit 29fec57fac
194 changed files with 23327 additions and 740 deletions
@@ -1,3 +1,5 @@
using System.Globalization;
using CliFx.Exceptions;
using Opc.Ua;
using Shouldly;
using Xunit;
@@ -8,6 +10,131 @@ namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Tests;
public class WriteCommandTests
{
/// <summary>Verifies that an explicit UInt16 write does not require read access.</summary>
[Fact]
public async Task Execute_ExplicitUInt16BypassesReadAndWritesUShort()
{
var fakeService = new FakeOpcUaClientService
{
ReadException = new InvalidOperationException("Value read is not authorized.")
};
var command = new WriteCommand(new FakeOpcUaClientServiceFactory(fakeService))
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=WriteOnly",
Value = "65535",
Type = "UInt16"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.ReadNodeIds.ShouldBeEmpty();
fakeService.WriteValues.Count.ShouldBe(1);
fakeService.WriteValues[0].Value.ShouldBeOfType<ushort>().ShouldBe(ushort.MaxValue);
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
/// <summary>Verifies the explicit scalar set and invariant numeric conversion.</summary>
[Theory]
[InlineData("Boolean", "yes", typeof(bool))]
[InlineData("Byte", "255", typeof(byte))]
[InlineData("SByte", "-128", typeof(sbyte))]
[InlineData("Int16", "-32768", typeof(short))]
[InlineData("UInt16", "65535", typeof(ushort))]
[InlineData("Int32", "-2147483648", typeof(int))]
[InlineData("UInt32", "4294967295", typeof(uint))]
[InlineData("Int64", "-9223372036854775808", typeof(long))]
[InlineData("UInt64", "18446744073709551615", typeof(ulong))]
[InlineData("Float", "1.25", typeof(float))]
[InlineData("Double", "1.25", typeof(double))]
[InlineData("String", "operator text", typeof(string))]
[InlineData("uint16", "123", typeof(ushort))]
public async Task Execute_ExplicitScalarUsesRequestedClrType(string type, string value, Type expectedType)
{
var fakeService = new FakeOpcUaClientService
{
ReadException = new InvalidOperationException("Explicit types must not read.")
};
var command = new WriteCommand(new FakeOpcUaClientServiceFactory(fakeService))
{
Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Scalar", Value = value, Type = type
};
var originalCulture = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR");
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
}
fakeService.ReadNodeIds.ShouldBeEmpty();
fakeService.WriteValues.Count.ShouldBe(1);
var writtenValue = fakeService.WriteValues[0].Value;
writtenValue.GetType().ShouldBe(expectedType);
if (expectedType == typeof(bool))
writtenValue.ShouldBe(true);
else
Convert.ToString(writtenValue, CultureInfo.InvariantCulture).ShouldBe(value);
}
/// <summary>Verifies unsupported type names fail locally before connecting or writing.</summary>
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData("DateTime")]
[InlineData("ByteString")]
[InlineData("UInt16[]")]
[InlineData("System.UInt16")]
[InlineData("Unknown")]
public async Task Execute_UnsupportedExplicitTypeRejectsBeforeConnect(string type)
{
var fakeService = new FakeOpcUaClientService();
var command = new WriteCommand(new FakeOpcUaClientServiceFactory(fakeService))
{
Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Scalar", Value = "1", Type = type
};
using var console = TestConsoleHelper.CreateConsole();
var error = await Should.ThrowAsync<CommandException>(async () => await command.ExecuteAsync(console));
error.Message.ShouldContain("--type");
fakeService.ConnectCalled.ShouldBeFalse();
fakeService.ReadNodeIds.ShouldBeEmpty();
fakeService.WriteValues.ShouldBeEmpty();
}
/// <summary>Verifies invalid explicit values fail locally before connecting or writing.</summary>
[Theory]
[InlineData("UInt16", "65536")]
[InlineData("UInt16", "-1")]
[InlineData("UInt16", "not-a-number")]
[InlineData("UInt16", "1.5")]
[InlineData("SByte", "128")]
[InlineData("Boolean", "maybe")]
[InlineData("Double", "1,25")]
public async Task Execute_InvalidExplicitValueRejectsBeforeConnect(string type, string value)
{
var fakeService = new FakeOpcUaClientService();
var command = new WriteCommand(new FakeOpcUaClientServiceFactory(fakeService))
{
Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Scalar", Value = value, Type = type
};
using var console = TestConsoleHelper.CreateConsole();
var error = await Should.ThrowAsync<CommandException>(async () => await command.ExecuteAsync(console));
error.Message.ShouldContain("--value");
fakeService.ConnectCalled.ShouldBeFalse();
fakeService.ReadNodeIds.ShouldBeEmpty();
fakeService.WriteValues.ShouldBeEmpty();
}
/// <summary>Verifies that write command executes successfully.</summary>
[Fact]
public async Task Execute_WritesSuccessfully()
@@ -104,4 +231,4 @@ public class WriteCommandTests
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
}
}