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
@@ -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}");
}
}