feat(transport): export site/instance selection end-to-end via CLI + ManagementActor (M8 B4)

This commit is contained in:
Joseph Doherty
2026-06-18 06:14:39 -04:00
parent d7dae24355
commit bdbf5cdab0
16 changed files with 450 additions and 12 deletions
@@ -1,3 +1,4 @@
using System.CommandLine;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests.Commands;
@@ -91,4 +92,49 @@ public class BundleCommandsStreamingTests : IDisposable
{
Assert.Throws<ArgumentException>(() => BundleCommands.StreamBase64ToFile("AAAA", string.Empty));
}
// ---- M8 (B4): --sites / --instances comma-split + flag parsing ----------
[Fact]
public void ParseNameList_CommaSeparated_SplitsAndTrims()
{
var result = BundleCommands.ParseNameList(" North-01 , East-02 ,West-03 ");
Assert.NotNull(result);
Assert.Equal(new[] { "North-01", "East-02", "West-03" }, result);
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void ParseNameList_NullOrBlank_ReturnsNull(string? token)
{
Assert.Null(BundleCommands.ParseNameList(token));
}
[Fact]
public void ParseNameList_EmptyEntries_AreDropped()
{
var result = BundleCommands.ParseNameList("a,,b, ,c");
Assert.Equal(new[] { "a", "b", "c" }, result);
}
[Fact]
public void BundleExport_SitesAndInstancesFlags_ParseWithoutError()
{
var url = new Option<string>("--url") { Recursive = true };
var format = new Option<string>("--format") { Recursive = true };
var username = new Option<string>("--username") { Recursive = true };
var password = new Option<string>("--password") { Recursive = true };
var bundle = BundleCommands.Build(url, format, username, password);
var parse = bundle.Parse(new[]
{
"export", "--output", "/tmp/out.scadabundle",
"--sites", "NORTH-01,East Plant",
"--instances", "NORTH-01.Pump1,NORTH-01.Pump2",
});
Assert.Empty(parse.Errors);
}
}
@@ -1386,6 +1386,15 @@ public class ManagementActorTests : TestKit, IDisposable
.Returns(new List<Template>());
_templateRepo.GetAllSharedScriptsAsync(Arg.Any<CancellationToken>())
.Returns(new List<Commons.Entities.Scripts.SharedScript>());
// M8 (B4): export now also fans out to instances (template repo) and sites
// (site repo) for the site/instance-scoped selection.
_templateRepo.GetAllInstancesAsync(Arg.Any<CancellationToken>())
.Returns(new List<Instance>());
var siteRepo = Substitute.For<ISiteRepository>();
siteRepo.GetAllSitesAsync(Arg.Any<CancellationToken>())
.Returns(new List<Commons.Entities.Sites.Site>());
_services.AddScoped(_ => siteRepo);
var externalRepo = Substitute.For<IExternalSystemRepository>();
externalRepo.GetAllExternalSystemsAsync(Arg.Any<CancellationToken>())
@@ -1494,6 +1503,95 @@ public class ManagementActorTests : TestKit, IDisposable
Assert.Contains("DoesNotExist", response.Error);
}
[Fact]
public void ExportBundleCommand_WithSiteAndInstanceNames_ResolvesToIds()
{
// M8 (B4): --sites accepts SiteIdentifier (preferred) or Name; --instances
// accepts UniqueName. The handler resolves both to surrogate ids and passes
// them on the ExportSelection.
var (exporter, _) = AddBundleSubstitutes();
var siteRepo = (ISiteRepository)_services.BuildServiceProvider()
.GetRequiredService<ISiteRepository>();
siteRepo.GetAllSitesAsync(Arg.Any<CancellationToken>())
.Returns(new List<Commons.Entities.Sites.Site>
{
new("North Plant", "NORTH-01") { Id = 11 },
new("East Plant", "EAST-02") { Id = 22 },
});
_templateRepo.GetAllInstancesAsync(Arg.Any<CancellationToken>())
.Returns(new List<Instance>
{
new("NORTH-01.Pump1") { Id = 101, SiteId = 11, TemplateId = 1 },
new("NORTH-01.Pump2") { Id = 102, SiteId = 11, TemplateId = 1 },
});
Commons.Types.Transport.ExportSelection? captured = null;
exporter.ExportAsync(
Arg.Do<Commons.Types.Transport.ExportSelection>(s => captured = s),
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string?>(),
Arg.Any<CancellationToken>())
.Returns(_ => Task.FromResult<Stream>(new MemoryStream(new byte[] { 1, 2, 3 })));
var cmd = new ExportBundleCommand(
All: false,
TemplateNames: null, SharedScriptNames: null,
ExternalSystemNames: null, DatabaseConnectionNames: null,
NotificationListNames: null, SmtpConfigurationNames: null,
ApiMethodNames: null,
IncludeDependencies: false, Passphrase: null,
SourceEnvironment: "test-env",
// mix the SiteIdentifier and the friendly Name to prove the dual-key match.
SiteNames: new[] { "NORTH-01", "East Plant" },
InstanceNames: new[] { "NORTH-01.Pump1" });
var actor = CreateActor();
actor.Tell(Envelope(cmd, "Designer"));
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(5));
Assert.NotNull(captured);
Assert.Equal(new[] { 11, 22 }, captured!.SiteIds.OrderBy(x => x));
Assert.Equal(new[] { 101 }, captured.InstanceIds);
}
[Fact]
public void ExportBundleCommand_WithAll_IncludesEverySiteAndInstance()
{
// M8 (B4): All=true ignores per-type name lists and includes every site +
// instance, mirroring how All already includes every template/etc.
var (exporter, _) = AddBundleSubstitutes();
var siteRepo = (ISiteRepository)_services.BuildServiceProvider()
.GetRequiredService<ISiteRepository>();
siteRepo.GetAllSitesAsync(Arg.Any<CancellationToken>())
.Returns(new List<Commons.Entities.Sites.Site>
{
new("North Plant", "NORTH-01") { Id = 11 },
new("East Plant", "EAST-02") { Id = 22 },
});
_templateRepo.GetAllInstancesAsync(Arg.Any<CancellationToken>())
.Returns(new List<Instance>
{
new("NORTH-01.Pump1") { Id = 101, SiteId = 11, TemplateId = 1 },
new("EAST-02.Pump9") { Id = 909, SiteId = 22, TemplateId = 1 },
});
Commons.Types.Transport.ExportSelection? captured = null;
exporter.ExportAsync(
Arg.Do<Commons.Types.Transport.ExportSelection>(s => captured = s),
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string?>(),
Arg.Any<CancellationToken>())
.Returns(_ => Task.FromResult<Stream>(new MemoryStream(new byte[] { 1 })));
var actor = CreateActor();
actor.Tell(Envelope(AllExportCommand(), "Designer"));
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(5));
Assert.NotNull(captured);
Assert.Equal(new[] { 11, 22 }, captured!.SiteIds.OrderBy(x => x));
Assert.Equal(new[] { 101, 909 }, captured.InstanceIds.OrderBy(x => x));
}
[Fact]
public void ImportBundleCommand_WithBlockerRow_AbortsBeforeApply()
{
@@ -41,6 +41,9 @@ public sealed class CompositionImportTests : IDisposable
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
// M8: DependencyResolver now injects ISiteRepository to walk the
// site/data-connection/instance closure; register it or activation fails.
services.AddScoped<ISiteRepository, SiteRepository>();
services.AddScoped<IAuditCorrelationContext, AuditCorrelationContext>();
services.AddScoped<IAuditService, AuditService>();
services.AddTransport();
@@ -39,6 +39,9 @@ public sealed class ConflictResolutionTests : IDisposable
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
// M8: DependencyResolver now injects ISiteRepository to walk the
// site/data-connection/instance closure; register it or activation fails.
services.AddScoped<ISiteRepository, SiteRepository>();
services.AddScoped<IAuditCorrelationContext, AuditCorrelationContext>();
services.AddScoped<IAuditService, AuditService>();
services.AddTransport();
@@ -3,8 +3,10 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Scripts;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
@@ -50,11 +52,15 @@ public sealed class BundleExporterTests : IDisposable
services.AddDbContext<ScadaBridgeDbContext>(opts =>
opts.UseInMemoryDatabase(dbName));
// Repositories the resolver pulls from.
// Repositories the resolver pulls from. M8 (B4): the resolver now injects
// ISiteRepository to walk the site/data-connection/instance closure, so it
// must be registered or the BuildServiceProvider-time graph resolution for
// DependencyResolver fails.
services.AddScoped<ITemplateEngineRepository, TemplateEngineRepository>();
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
services.AddScoped<ISiteRepository, SiteRepository>();
// Audit pipeline — AuditService writes via the EF context + reads the
// bundle-import correlation id from the scoped context (null here, since
@@ -277,4 +283,180 @@ public sealed class BundleExporterTests : IDisposable
Assert.Equal("stg", entry.EntityName);
}
}
[Fact]
public async Task ExportAsync_with_site_selection_packs_sites_dataconnections_and_instances()
{
// Arrange: a site with one data connection and one instance (on a template)
// bound to that connection. Selecting the site pulls its data connection +
// all its instances; the instance's binding edge proves the closure walks
// the instance's child collections (review item I3 — proves the aggregate
// wiring actually carries the site/instance arrays into the bundle).
int siteId;
await using (var scope = _provider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
var template = new Template("PumpStation") { Description = "for instance" };
ctx.Templates.Add(template);
var site = new Site("North Plant", "NORTH-01") { Description = "north site" };
ctx.Sites.Add(site);
await ctx.SaveChangesAsync();
siteId = site.Id;
var conn = new DataConnection("PlcA", "OpcUa", site.Id);
ctx.DataConnections.Add(conn);
await ctx.SaveChangesAsync();
var instance = new Instance("NORTH-01.Pump1") { TemplateId = template.Id, SiteId = site.Id };
ctx.Instances.Add(instance);
await ctx.SaveChangesAsync();
ctx.InstanceConnectionBindings.Add(new InstanceConnectionBinding("Flow")
{
InstanceId = instance.Id,
DataConnectionId = conn.Id,
});
await ctx.SaveChangesAsync();
}
// Act: select the site (no central-config entities) with deps on, so the
// site closure also pulls each instance's owning template — the realistic
// "export a whole site" path.
Stream bundleStream;
await using (var scope = _provider.CreateAsyncScope())
{
var exporter = scope.ServiceProvider.GetRequiredService<IBundleExporter>();
var selection = new ExportSelection(
TemplateIds: Array.Empty<int>(),
SharedScriptIds: Array.Empty<int>(),
ExternalSystemIds: Array.Empty<int>(),
DatabaseConnectionIds: Array.Empty<int>(),
NotificationListIds: Array.Empty<int>(),
SmtpConfigurationIds: Array.Empty<int>(),
ApiMethodIds: Array.Empty<int>(),
IncludeDependencies: true,
SiteIds: new[] { siteId });
bundleStream = await exporter.ExportAsync(
selection, user: "carol", sourceEnvironment: "dev",
passphrase: null, cancellationToken: CancellationToken.None);
}
byte[] bundleBytes;
using (var ms = new MemoryStream())
{
await bundleStream.CopyToAsync(ms);
bundleBytes = ms.ToArray();
}
// Assert: manifest summary counts the site/data-connection/instance, and
// the unpacked content carries the actual arrays (I3 — without the
// EntityAggregate wiring these would be empty).
var serializer = _provider.GetRequiredService<BundleSerializer>();
BundleManifest manifest;
byte[] rawContent;
using (var ms = new MemoryStream(bundleBytes, writable: false))
{
manifest = serializer.ReadManifest(ms);
}
using (var ms = new MemoryStream(bundleBytes, writable: false))
{
rawContent = serializer.ReadContentBytes(ms, manifest);
}
Assert.Equal(1, manifest.Summary.Sites);
Assert.Equal(1, manifest.Summary.DataConnections);
Assert.Equal(1, manifest.Summary.Instances);
var content = serializer.UnpackContent(rawContent, manifest, passphrase: null, encryptor: null);
Assert.Single(content.Sites);
Assert.Equal("NORTH-01", content.Sites[0].SiteIdentifier);
Assert.Single(content.DataConnections);
Assert.Equal("PlcA", content.DataConnections[0].Name);
Assert.Single(content.Instances);
Assert.Equal("NORTH-01.Pump1", content.Instances[0].UniqueName);
Assert.Equal("PumpStation", content.Instances[0].TemplateName);
// The instance carries its binding, and the manifest dep-edge resolves the
// owning site by identifier (I1) — Site:NORTH-01, never Site:<rawId>.
Assert.Single(content.Instances[0].ConnectionBindings);
var instanceEntry = Assert.Single(
manifest.Contents,
e => e.Type == "Instance" && e.Name == "NORTH-01.Pump1");
Assert.Contains($"Site:NORTH-01", instanceEntry.DependsOn);
Assert.DoesNotContain(instanceEntry.DependsOn, d => d == $"Site:{siteId}");
}
[Fact]
public async Task ExportAsync_directly_selected_instance_records_owning_site_by_identifier()
{
// I1: a directly-selected instance with IncludeDependencies=false does NOT
// pack its owning site, yet the manifest dep-edge must still read
// Site:<identifier> (resolved via the site-identifier enrichment) rather
// than degrading to Site:<rawId>.
int instanceId;
int siteId;
await using (var scope = _provider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
var template = new Template("Valve") { Description = "for instance" };
ctx.Templates.Add(template);
var site = new Site("South Plant", "SOUTH-09") { Description = "south" };
ctx.Sites.Add(site);
await ctx.SaveChangesAsync();
siteId = site.Id;
var instance = new Instance("SOUTH-09.Valve1") { TemplateId = template.Id, SiteId = site.Id };
ctx.Instances.Add(instance);
await ctx.SaveChangesAsync();
instanceId = instance.Id;
}
Stream bundleStream;
await using (var scope = _provider.CreateAsyncScope())
{
var exporter = scope.ServiceProvider.GetRequiredService<IBundleExporter>();
var selection = new ExportSelection(
TemplateIds: Array.Empty<int>(),
SharedScriptIds: Array.Empty<int>(),
ExternalSystemIds: Array.Empty<int>(),
DatabaseConnectionIds: Array.Empty<int>(),
NotificationListIds: Array.Empty<int>(),
SmtpConfigurationIds: Array.Empty<int>(),
ApiMethodIds: Array.Empty<int>(),
IncludeDependencies: false,
InstanceIds: new[] { instanceId });
bundleStream = await exporter.ExportAsync(
selection, user: "dave", sourceEnvironment: "dev",
passphrase: null, cancellationToken: CancellationToken.None);
}
byte[] bundleBytes;
using (var ms = new MemoryStream())
{
await bundleStream.CopyToAsync(ms);
bundleBytes = ms.ToArray();
}
var serializer = _provider.GetRequiredService<BundleSerializer>();
BundleManifest manifest;
using (var ms = new MemoryStream(bundleBytes, writable: false))
{
manifest = serializer.ReadManifest(ms);
}
// Instance is packed; its owning site is NOT (deps off, site not selected).
Assert.Equal(1, manifest.Summary.Instances);
Assert.Equal(0, manifest.Summary.Sites);
var instanceEntry = Assert.Single(
manifest.Contents,
e => e.Type == "Instance" && e.Name == "SOUTH-09.Valve1");
// I1: the dep-edge reads the portable identifier, not the raw surrogate id.
Assert.Contains("Site:SOUTH-09", instanceEntry.DependsOn);
Assert.DoesNotContain(instanceEntry.DependsOn, d => d == $"Site:{siteId}");
}
}
@@ -61,6 +61,9 @@ public sealed class BundleImporterApplyTests : IDisposable
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
// M8: DependencyResolver now injects ISiteRepository to walk the
// site/data-connection/instance closure; register it or activation fails.
services.AddScoped<ISiteRepository, SiteRepository>();
services.AddScoped<IAuditCorrelationContext, AuditCorrelationContext>();
services.AddScoped<IAuditService, AuditService>();
services.AddTransport();
@@ -39,6 +39,9 @@ public sealed class BundleImporterPreviewTests : IDisposable
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
// M8: DependencyResolver now injects ISiteRepository to walk the
// site/data-connection/instance closure; register it or activation fails.
services.AddScoped<ISiteRepository, SiteRepository>();
services.AddScoped<IAuditCorrelationContext, AuditCorrelationContext>();
services.AddScoped<IAuditService, AuditService>();
services.AddTransport();
@@ -84,6 +84,9 @@ public sealed class BundleImporterRollbackFailureTests : IDisposable
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
// M8: DependencyResolver now injects ISiteRepository to walk the
// site/data-connection/instance closure; register it or activation fails.
services.AddScoped<ISiteRepository, SiteRepository>();
services.AddScoped<IAuditCorrelationContext, AuditCorrelationContext>();
services.AddScoped<IAuditService, AuditService>();
services.AddTransport();
@@ -53,6 +53,9 @@ public sealed class RoundTripTests : IDisposable
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
// M8: DependencyResolver now injects ISiteRepository to walk the
// site/data-connection/instance closure; register it or activation fails.
services.AddScoped<ISiteRepository, SiteRepository>();
services.AddScoped<IAuditCorrelationContext, AuditCorrelationContext>();
services.AddScoped<IAuditService, AuditService>();
services.AddTransport();
@@ -51,6 +51,9 @@ public sealed class SemanticValidatorImportTests : IDisposable
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
// M8: DependencyResolver now injects ISiteRepository to walk the
// site/data-connection/instance closure; register it or activation fails.
services.AddScoped<ISiteRepository, SiteRepository>();
services.AddScoped<IAuditCorrelationContext, AuditCorrelationContext>();
services.AddScoped<IAuditService, AuditService>();
services.AddTransport();
@@ -41,6 +41,9 @@ public sealed class ValidationFailureTests : IDisposable
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
// M8: DependencyResolver now injects ISiteRepository to walk the
// site/data-connection/instance closure; register it or activation fails.
services.AddScoped<ISiteRepository, SiteRepository>();
services.AddScoped<IAuditCorrelationContext, AuditCorrelationContext>();
services.AddScoped<IAuditService, AuditService>();
services.AddTransport();