feat(centralui+dcl): Test Bindings popup — one-shot live read of bound tags

Adds a Test Bindings button to the Connection Bindings table on the Configure
Instance page that opens a modal showing the live current value of every bound
attribute. Reuses the routing path that the OPC UA tag browser landed on:

  Central:  TestBindingsDialog → IBindingTester → CommunicationService
            → ReadTagValuesCommand → SiteEnvelope (Ask)
  Site:     SiteCommunicationActor → DeploymentManagerActor singleton
            → DataConnectionManagerActor → child DataConnectionActor
            → _adapter.ReadBatchAsync

Split mirrors the browse handler:
  • Manager owns ConnectionNotFound (only it sees the per-site connection set).
  • Child owns ConnectionNotConnected (pre-call status check, never stash —
    read is interactive design-time), Timeout (OperationCanceledException),
    ServerError (any other exception). Per-tag failures from ReadBatchAsync
    become failure TagReadOutcomes without aborting the batch.

CentralUI:
  • IBindingTester / BindingTester — Design-role guard via HasClaim against
    JwtTokenService.RoleClaimType (not IsInRole — see c1e16cf), typed
    transport-failure translation.
  • TestBindingsDialog — ShowAsync(siteId, rows, instanceLabel) method-arg
    pattern (no Razor parameter race; see 2c138b6), groups rows by connection
    and issues one ReadAsync per connection in parallel, per-row error subline
    + per-connection banner, Refresh button re-issues the reads.
  • InstanceConfigure.razor — Test Bindings button next to Save Bindings,
    disabled when no testable rows. OPC UA only today (other protocols have
    no ReadTagValuesCommand wiring yet).

Tests:
  • Commons: ReadTagValuesCommand discovered by ManagementCommandRegistry.
  • DataConnectionLayer: unknown connection → ConnectionNotFound,
    not-connected adapter → ConnectionNotConnected (ReadBatchAsync NOT called),
    success-path mapping (Good/Bad + per-tag error), cancellation → Timeout.
  • CentralUI: register IBindingTester (and the previously-missing
    IOpcUaBrowseService) on the existing InstanceConfigureAuditDrillinTests
    Bunit container so the page renders cleanly with the new dialog.
This commit is contained in:
Joseph Doherty
2026-05-28 13:25:48 -04:00
parent f401a9ea0e
commit 2a7dee4afa
14 changed files with 909 additions and 1 deletions
@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.CentralUI.Auth;
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
@@ -43,6 +44,12 @@ public class InstanceConfigureAuditDrillinTests : BunitContext
Services.AddSingleton(new InstanceService(_templateRepo, Substitute.For<IAuditService>()));
Services.AddSingleton(Substitute.For<IFlatteningPipeline>());
// The page renders <OpcUaBrowserDialog/> and <TestBindingsDialog/> at
// the bottom; their @inject directives need a registered service even
// though this test doesn't open either dialog.
Services.AddSingleton(Substitute.For<IOpcUaBrowseService>());
Services.AddSingleton(Substitute.For<IBindingTester>());
// Auth: a system-wide Deployment user so SiteScope grants everything.
var claims = new[]
{
@@ -0,0 +1,24 @@
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Messages;
/// <summary>
/// Verifies that <see cref="ReadTagValuesCommand"/> is discovered by
/// <see cref="ManagementCommandRegistry"/> so it travels over the management
/// boundary as a known command (resolvable by wire name and round-trippable
/// through <c>GetCommandName</c> / <c>Resolve</c>). Mirrors
/// <see cref="BrowseCommandsRegistryTests"/>.
/// </summary>
public class ReadTagValuesCommandRegistryTests
{
[Fact]
public void Registry_discovers_ReadTagValuesCommand()
{
// GetCommandName throws ArgumentException for any type the registry
// does not contain, so a successful call here is proof of discovery.
var name = ManagementCommandRegistry.GetCommandName(typeof(ReadTagValuesCommand));
Assert.Equal("ReadTagValues", name);
Assert.Equal(typeof(ReadTagValuesCommand), ManagementCommandRegistry.Resolve(name));
}
}
@@ -0,0 +1,174 @@
using Akka.Actor;
using Akka.TestKit.Xunit2;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Protocol;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.DataConnection;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Actors;
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Actors;
/// <summary>
/// Test Bindings (one-shot live read of bound tags): the site-side
/// <see cref="DataConnectionManagerActor"/> + child
/// <see cref="DataConnectionActor"/> together resolve
/// <see cref="ReadTagValuesCommand"/> against the live adapter and surface
/// every outcome as either a per-tag <see cref="TagReadOutcome"/> or a typed
/// connection-level <see cref="ReadTagValuesFailure"/>. The split mirrors the
/// browse handler: the manager owns
/// <see cref="ReadTagValuesFailureKind.ConnectionNotFound"/> (only it knows
/// the per-site connection set); everything else lives in the child where the
/// adapter is held — <see cref="ReadTagValuesFailureKind.ConnectionNotConnected"/>
/// from the pre-call status check, <see cref="ReadTagValuesFailureKind.Timeout"/>
/// / <see cref="ReadTagValuesFailureKind.ServerError"/> from the adapter call.
/// </summary>
public class DataConnectionManagerReadTagValuesHandlerTests : TestKit
{
private readonly IDataConnectionFactory _factory;
private readonly ISiteHealthCollector _healthCollector;
private readonly DataConnectionOptions _options;
public DataConnectionManagerReadTagValuesHandlerTests()
: base(@"akka.loglevel = WARNING")
{
_factory = Substitute.For<IDataConnectionFactory>();
_healthCollector = Substitute.For<ISiteHealthCollector>();
_options = new DataConnectionOptions
{
ReconnectInterval = TimeSpan.FromSeconds(30),
TagResolutionRetryInterval = TimeSpan.FromSeconds(30),
};
}
[Fact]
public void Unknown_connection_name_returns_ConnectionNotFound()
{
var manager = Sys.ActorOf(Props.Create(() =>
new DataConnectionManagerActor(_factory, _options, _healthCollector, null)));
// No CreateConnectionCommand sent — the manager has zero children, so
// any read must be rejected with ConnectionNotFound (only the manager
// has site-level visibility into the connection set).
manager.Tell(new ReadTagValuesCommand("unknown-connection", new[] { "ns=2;s=A" }));
var reply = ExpectMsg<ReadTagValuesResult>();
Assert.NotNull(reply.Failure);
Assert.Equal(ReadTagValuesFailureKind.ConnectionNotFound, reply.Failure!.Kind);
Assert.Empty(reply.Outcomes);
}
[Fact]
public void Adapter_not_connected_returns_ConnectionNotConnected()
{
// Adapter that reports Disconnected — the child actor's pre-call
// status check must short-circuit to ConnectionNotConnected without
// calling ReadBatchAsync (avoids the per-tag "client is not
// connected" noise that the OpcUa adapter would otherwise produce).
var adapter = Substitute.For<IDataConnection>();
adapter.ConnectAsync(Arg.Any<IDictionary<string, string>>(), Arg.Any<CancellationToken>())
.Returns(Task.FromException(new InvalidOperationException("simulated failure")));
adapter.Status.Returns(ConnectionHealth.Disconnected);
_factory.Create("OpcUa", Arg.Any<IDictionary<string, string>>()).Returns(adapter);
var manager = Sys.ActorOf(Props.Create(() =>
new DataConnectionManagerActor(_factory, _options, _healthCollector, null)));
manager.Tell(new CreateConnectionCommand(
"conn-down", "OpcUa", new Dictionary<string, string>(), null, 3));
// Wait for the child actor to spin up; the read handler runs in every
// lifecycle state, so we don't need to wait for a specific Become.
AwaitCondition(
() => _factory.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "Create"),
TimeSpan.FromSeconds(2));
manager.Tell(new ReadTagValuesCommand("conn-down", new[] { "ns=2;s=A" }));
var reply = ExpectMsg<ReadTagValuesResult>(TimeSpan.FromSeconds(3));
Assert.NotNull(reply.Failure);
Assert.Equal(ReadTagValuesFailureKind.ConnectionNotConnected, reply.Failure!.Kind);
Assert.Empty(reply.Outcomes);
// ReadBatchAsync must NOT be called when the status guard short-circuits.
adapter.DidNotReceive().ReadBatchAsync(Arg.Any<IEnumerable<string>>(), Arg.Any<CancellationToken>());
}
[Fact]
public void Success_path_maps_results_to_TagReadOutcomes()
{
var adapter = Substitute.For<IDataConnection>();
adapter.ConnectAsync(Arg.Any<IDictionary<string, string>>(), Arg.Any<CancellationToken>())
.Returns(Task.CompletedTask);
adapter.Status.Returns(ConnectionHealth.Connected);
var ts = new DateTimeOffset(2026, 5, 28, 12, 0, 0, TimeSpan.Zero);
var batch = new Dictionary<string, ReadResult>
{
["ns=2;s=A"] = new ReadResult(true, new TagValue(42.7, QualityCode.Good, ts), null),
// Adapter-reported per-tag failure (e.g. unknown node id): mapped to
// a failure TagReadOutcome with Quality=Bad and Value=null.
["ns=2;s=B"] = new ReadResult(false, null, "BadNodeIdUnknown"),
};
adapter.ReadBatchAsync(Arg.Any<IEnumerable<string>>(), Arg.Any<CancellationToken>())
.Returns(batch);
_factory.Create("OpcUa", Arg.Any<IDictionary<string, string>>()).Returns(adapter);
var manager = Sys.ActorOf(Props.Create(() =>
new DataConnectionManagerActor(_factory, _options, _healthCollector, null)));
manager.Tell(new CreateConnectionCommand(
"conn-ok", "OpcUa", new Dictionary<string, string>(), null, 3));
AwaitCondition(
() => _factory.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "Create"),
TimeSpan.FromSeconds(2));
manager.Tell(new ReadTagValuesCommand("conn-ok", new[] { "ns=2;s=A", "ns=2;s=B" }));
var reply = ExpectMsg<ReadTagValuesResult>(TimeSpan.FromSeconds(3));
Assert.Null(reply.Failure);
Assert.Equal(2, reply.Outcomes.Count);
var aOutcome = reply.Outcomes.Single(o => o.TagPath == "ns=2;s=A");
Assert.True(aOutcome.Success);
Assert.Equal(42.7, aOutcome.Value);
Assert.Equal("Good", aOutcome.Quality);
Assert.Equal(ts, aOutcome.Timestamp);
Assert.Null(aOutcome.ErrorMessage);
var bOutcome = reply.Outcomes.Single(o => o.TagPath == "ns=2;s=B");
Assert.False(bOutcome.Success);
Assert.Null(bOutcome.Value);
Assert.Equal("Bad", bOutcome.Quality);
Assert.Equal("BadNodeIdUnknown", bOutcome.ErrorMessage);
}
[Fact]
public void Adapter_OperationCancelled_returns_Timeout()
{
var adapter = Substitute.For<IDataConnection>();
adapter.ConnectAsync(Arg.Any<IDictionary<string, string>>(), Arg.Any<CancellationToken>())
.Returns(Task.CompletedTask);
adapter.Status.Returns(ConnectionHealth.Connected);
adapter.ReadBatchAsync(Arg.Any<IEnumerable<string>>(), Arg.Any<CancellationToken>())
.Returns(Task.FromException<IReadOnlyDictionary<string, ReadResult>>(
new OperationCanceledException("test cancel")));
_factory.Create("OpcUa", Arg.Any<IDictionary<string, string>>()).Returns(adapter);
var manager = Sys.ActorOf(Props.Create(() =>
new DataConnectionManagerActor(_factory, _options, _healthCollector, null)));
manager.Tell(new CreateConnectionCommand(
"conn-slow", "OpcUa", new Dictionary<string, string>(), null, 3));
AwaitCondition(
() => _factory.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "Create"),
TimeSpan.FromSeconds(2));
manager.Tell(new ReadTagValuesCommand("conn-slow", new[] { "ns=2;s=A" }));
var reply = ExpectMsg<ReadTagValuesResult>(TimeSpan.FromSeconds(3));
Assert.NotNull(reply.Failure);
Assert.Equal(ReadTagValuesFailureKind.Timeout, reply.Failure!.Kind);
Assert.Empty(reply.Outcomes);
}
}