Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Services/ConnectionHealthQueryServiceTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

163 lines
7.2 KiB
C#

using NSubstitute;
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Health;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Services;
/// <summary>
/// Unit tests for <see cref="ConnectionHealthQueryService"/>, which projects
/// the site health reports' name-keyed <see cref="SiteHealthReport.DataConnectionStatuses"/>
/// onto a connection-id → <see cref="ConnectionHealth"/> map for the design
/// DataConnections page. The site repository + health aggregator are substituted so
/// the name→id resolution + missing-report / unknown-name tolerance are exercised
/// without any EF Core or cluster traffic.
/// </summary>
public sealed class ConnectionHealthQueryServiceTests
{
private readonly ISiteRepository _siteRepo = Substitute.For<ISiteRepository>();
private readonly ICentralHealthAggregator _aggregator = Substitute.For<ICentralHealthAggregator>();
private ConnectionHealthQueryService CreateService() => new(_siteRepo, _aggregator);
private void SeedRepos(IEnumerable<Site> sites, IEnumerable<DataConnection> connections)
{
_siteRepo.GetAllSitesAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<Site>>(sites.ToList()));
_siteRepo.GetAllDataConnectionsAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<DataConnection>>(connections.ToList()));
}
private static SiteHealthState StateWith(
string siteIdentifier,
params (string Name, ConnectionHealth Health)[] statuses)
{
var dict = statuses.ToDictionary(s => s.Name, s => s.Health);
return new SiteHealthState
{
SiteId = siteIdentifier,
IsOnline = true,
LatestReport = MakeReport(siteIdentifier, dict),
};
}
private static SiteHealthReport MakeReport(
string siteIdentifier,
IReadOnlyDictionary<string, ConnectionHealth> statuses) =>
new(
SiteId: siteIdentifier,
SequenceNumber: 1,
ReportTimestamp: DateTimeOffset.UtcNow,
DataConnectionStatuses: statuses,
TagResolutionCounts: new Dictionary<string, TagResolutionStatus>(),
ScriptErrorCount: 0,
AlarmEvaluationErrorCount: 0,
StoreAndForwardBufferDepths: new Dictionary<string, int>(),
DeadLetterCount: 0,
DeployedInstanceCount: 0,
EnabledInstanceCount: 0,
DisabledInstanceCount: 0);
/// <summary>Verifies that connection names in a health report are mapped to connection ids.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Maps_ConnectionNameStatuses_ToConnectionIds()
{
var site = new Site("Plant-A", "plant-a") { Id = 1 };
var plc1 = new DataConnection("PLC-1", "OpcUa", 1) { Id = 100 };
var rtu9 = new DataConnection("RTU-9", "Custom", 1) { Id = 200 };
SeedRepos(new[] { site }, new[] { plc1, rtu9 });
_aggregator.GetSiteState("plant-a").Returns(
StateWith("plant-a",
("PLC-1", ConnectionHealth.Connected),
("RTU-9", ConnectionHealth.Disconnected)));
var map = await CreateService().GetConnectionHealthAsync();
Assert.Equal(ConnectionHealth.Connected, map[100]);
Assert.Equal(ConnectionHealth.Disconnected, map[200]);
Assert.Equal(2, map.Count);
}
/// <summary>Verifies that a site without a health report yields no entries.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task MissingReport_YieldsNoEntriesForThatSite()
{
var site = new Site("Plant-A", "plant-a") { Id = 1 };
var plc1 = new DataConnection("PLC-1", "OpcUa", 1) { Id = 100 };
SeedRepos(new[] { site }, new[] { plc1 });
// No state tracked for the site yet (just-started central / no report).
_aggregator.GetSiteState("plant-a").Returns((SiteHealthState?)null);
var map = await CreateService().GetConnectionHealthAsync();
Assert.Empty(map);
}
/// <summary>Verifies that a site state without a latest report yields no entries.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task StateWithoutLatestReport_YieldsNoEntries()
{
var site = new Site("Plant-A", "plant-a") { Id = 1 };
var plc1 = new DataConnection("PLC-1", "OpcUa", 1) { Id = 100 };
SeedRepos(new[] { site }, new[] { plc1 });
// Heartbeat-only state: site is tracked but has not sent a full report.
_aggregator.GetSiteState("plant-a").Returns(
new SiteHealthState { SiteId = "plant-a", IsOnline = true, LatestReport = null });
var map = await CreateService().GetConnectionHealthAsync();
Assert.Empty(map);
}
/// <summary>Verifies that a connection name that cannot be resolved is skipped.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task UnknownConnectionName_IsSkipped()
{
var site = new Site("Plant-A", "plant-a") { Id = 1 };
var plc1 = new DataConnection("PLC-1", "OpcUa", 1) { Id = 100 };
SeedRepos(new[] { site }, new[] { plc1 });
// Report references a connection name with no matching DataConnection row
// (e.g. a connection deleted centrally but still live at the site).
_aggregator.GetSiteState("plant-a").Returns(
StateWith("plant-a",
("PLC-1", ConnectionHealth.Connected),
("GHOST", ConnectionHealth.Error)));
var map = await CreateService().GetConnectionHealthAsync();
Assert.Equal(ConnectionHealth.Connected, map[100]);
Assert.Single(map);
}
/// <summary>Verifies that connection names are resolved per site rather than globally.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ResolvesNamesPerSite_NotGloballyAmbiguous()
{
// Two sites each own a connection with the same name. The name→id resolution
// must be scoped to the report's own site so the right id is stamped.
var siteA = new Site("Plant-A", "plant-a") { Id = 1 };
var siteB = new Site("Plant-B", "plant-b") { Id = 2 };
var aPlc = new DataConnection("PLC-1", "OpcUa", 1) { Id = 100 };
var bPlc = new DataConnection("PLC-1", "OpcUa", 2) { Id = 200 };
SeedRepos(new[] { siteA, siteB }, new[] { aPlc, bPlc });
_aggregator.GetSiteState("plant-a").Returns(
StateWith("plant-a", ("PLC-1", ConnectionHealth.Connected)));
_aggregator.GetSiteState("plant-b").Returns(
StateWith("plant-b", ("PLC-1", ConnectionHealth.Connecting)));
var map = await CreateService().GetConnectionHealthAsync();
Assert.Equal(ConnectionHealth.Connected, map[100]);
Assert.Equal(ConnectionHealth.Connecting, map[200]);
}
}