feat(transport): export site/instance selection end-to-end via CLI + ManagementActor (M8 B4)
This commit is contained in:
@@ -65,6 +65,10 @@ public static class BundleCommands
|
||||
// --api-keys option. Re-create keys and re-grant their method scopes on the
|
||||
// destination via the admin UI/CLI.
|
||||
var apiMethodsOption = NameListOption("--api-methods", "Comma-separated API-method names");
|
||||
// M8 (B4): site/instance-scoped export. Sites accept a SiteIdentifier
|
||||
// (preferred) or friendly Name per token; instances accept a UniqueName.
|
||||
var sitesOption = NameListOption("--sites", "Comma-separated site identifiers or names");
|
||||
var instancesOption = NameListOption("--instances", "Comma-separated instance unique-names");
|
||||
var includeDepsOption = new Option<bool>("--include-dependencies")
|
||||
{
|
||||
Description = "Pull transitive dependencies (referenced shared scripts, parents, composed members) into the bundle.",
|
||||
@@ -88,6 +92,8 @@ public static class BundleCommands
|
||||
cmd.Add(notificationListsOption);
|
||||
cmd.Add(smtpConfigsOption);
|
||||
cmd.Add(apiMethodsOption);
|
||||
cmd.Add(sitesOption);
|
||||
cmd.Add(instancesOption);
|
||||
cmd.Add(includeDepsOption);
|
||||
cmd.Add(sourceEnvOption);
|
||||
|
||||
@@ -110,7 +116,9 @@ public static class BundleCommands
|
||||
ApiMethodNames: result.GetValue(apiMethodsOption),
|
||||
IncludeDependencies: includeDeps,
|
||||
Passphrase: passphrase,
|
||||
SourceEnvironment: sourceEnv);
|
||||
SourceEnvironment: sourceEnv,
|
||||
SiteNames: result.GetValue(sitesOption),
|
||||
InstanceNames: result.GetValue(instancesOption));
|
||||
|
||||
return await CommandHelpers.ExecuteCommandAsync(
|
||||
result, urlOption, formatOption, usernameOption, passwordOption,
|
||||
@@ -359,14 +367,26 @@ public static class BundleCommands
|
||||
{
|
||||
Description = description,
|
||||
CustomParser = arg =>
|
||||
{
|
||||
var token = arg.Tokens.Count == 0 ? null : arg.Tokens[0].Value;
|
||||
if (string.IsNullOrWhiteSpace(token)) return null;
|
||||
return token
|
||||
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.ToArray();
|
||||
},
|
||||
ParseNameList(arg.Tokens.Count == 0 ? null : arg.Tokens[0].Value),
|
||||
};
|
||||
return opt;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Splits a comma-separated CLI value into a trimmed, empty-entry-free name
|
||||
/// list (the shared shape used by every <c>--templates</c>/<c>--sites</c>/…
|
||||
/// option). Returns <c>null</c> for a null/blank token so the management
|
||||
/// command sees "not specified" rather than an empty list. Exposed
|
||||
/// <c>internal</c> so the flag-parse tests can assert the split semantics
|
||||
/// without reaching into the per-command local options.
|
||||
/// </summary>
|
||||
/// <param name="token">The raw comma-separated option value, or <c>null</c>.</param>
|
||||
/// <returns>The parsed name array, or <c>null</c> when the token is null/blank.</returns>
|
||||
internal static IReadOnlyList<string>? ParseNameList(string? token)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(token)) return null;
|
||||
return token
|
||||
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,13 @@ public sealed record ExportBundleCommand(
|
||||
IReadOnlyList<string>? ApiMethodNames,
|
||||
bool IncludeDependencies,
|
||||
string? Passphrase,
|
||||
string SourceEnvironment);
|
||||
string SourceEnvironment,
|
||||
// Additive (M8 B4): site/instance-scoped selection. Sites resolve by
|
||||
// SiteIdentifier (preferred) or Name; instances resolve by UniqueName.
|
||||
// Defaulted null so every existing positional caller keeps compiling; the
|
||||
// handler normalizes null to "select nothing" (or everything under All=true).
|
||||
IReadOnlyList<string>? SiteNames = null,
|
||||
IReadOnlyList<string>? InstanceNames = null);
|
||||
|
||||
/// <summary>
|
||||
/// Bundle body returned as base64-encoded ZIP. <see cref="ByteCount"/> is the
|
||||
|
||||
@@ -2305,6 +2305,7 @@ public class ManagementActor : ReceiveActor
|
||||
var externalRepo = sp.GetRequiredService<IExternalSystemRepository>();
|
||||
var notifRepo = sp.GetRequiredService<INotificationRepository>();
|
||||
var inboundRepo = sp.GetRequiredService<IInboundApiRepository>();
|
||||
var siteRepo = sp.GetRequiredService<ISiteRepository>();
|
||||
|
||||
var templates = await templateRepo.GetAllTemplatesAsync();
|
||||
var sharedScripts = await templateRepo.GetAllSharedScriptsAsync();
|
||||
@@ -2314,6 +2315,10 @@ public class ManagementActor : ReceiveActor
|
||||
var smtpConfigs = await notifRepo.GetAllSmtpConfigurationsAsync();
|
||||
// Inbound API keys are not transported between environments (re-arch C4); only methods.
|
||||
var apiMethods = await inboundRepo.GetAllApiMethodsAsync();
|
||||
// M8 (B4): site/instance-scoped selection. Sites match on SiteIdentifier
|
||||
// (preferred) or Name; instances match on UniqueName.
|
||||
var sites = await siteRepo.GetAllSitesAsync();
|
||||
var instances = await templateRepo.GetAllInstancesAsync();
|
||||
|
||||
int[] ResolveIds<T>(IReadOnlyList<T> all, IReadOnlyList<string>? names,
|
||||
Func<T, string> getName, Func<T, int> getId, string entityType)
|
||||
@@ -2333,6 +2338,33 @@ public class ManagementActor : ReceiveActor
|
||||
return matched;
|
||||
}
|
||||
|
||||
// Sites accept EITHER the SiteIdentifier (preferred) or the friendly Name
|
||||
// per requested token, so this needs a two-key matcher rather than the
|
||||
// single-key ResolveIds above. Under All=true every site is included.
|
||||
int[] ResolveSiteIds()
|
||||
{
|
||||
if (cmd.All) return sites.Select(s => s.Id).ToArray();
|
||||
var names = cmd.SiteNames;
|
||||
if (names is null || names.Count == 0) return Array.Empty<int>();
|
||||
var matchedIds = new List<int>();
|
||||
var unmatched = new List<string>();
|
||||
foreach (var token in names)
|
||||
{
|
||||
var site = sites.FirstOrDefault(s =>
|
||||
string.Equals(s.SiteIdentifier, token, StringComparison.Ordinal))
|
||||
?? sites.FirstOrDefault(s =>
|
||||
string.Equals(s.Name, token, StringComparison.Ordinal));
|
||||
if (site is null) unmatched.Add(token);
|
||||
else matchedIds.Add(site.Id);
|
||||
}
|
||||
if (unmatched.Count > 0)
|
||||
{
|
||||
throw new ManagementCommandException(
|
||||
$"Unknown site identifier/name(s): {string.Join(", ", unmatched.OrderBy(n => n, StringComparer.Ordinal))}.");
|
||||
}
|
||||
return matchedIds.Distinct().ToArray();
|
||||
}
|
||||
|
||||
var selection = new ExportSelection(
|
||||
TemplateIds: ResolveIds(templates, cmd.TemplateNames, t => t.Name, t => t.Id, "template"),
|
||||
SharedScriptIds: ResolveIds(sharedScripts, cmd.SharedScriptNames, s => s.Name, s => s.Id, "shared script"),
|
||||
@@ -2343,7 +2375,11 @@ public class ManagementActor : ReceiveActor
|
||||
// preview row shows the Host value, so the CLI uses Host too.
|
||||
SmtpConfigurationIds: ResolveIds(smtpConfigs, cmd.SmtpConfigurationNames, s => s.Host, s => s.Id, "SMTP configuration"),
|
||||
ApiMethodIds: ResolveIds(apiMethods, cmd.ApiMethodNames, m => m.Name, m => m.Id, "API method"),
|
||||
IncludeDependencies: cmd.IncludeDependencies);
|
||||
IncludeDependencies: cmd.IncludeDependencies,
|
||||
// M8 (B4): site/instance-scoped selection. Under All=true include every
|
||||
// site + instance, mirroring how All includes every template/etc.
|
||||
SiteIds: ResolveSiteIds(),
|
||||
InstanceIds: ResolveIds(instances, cmd.InstanceNames, i => i.UniqueName, i => i.Id, "instance"));
|
||||
|
||||
var exporter = sp.GetRequiredService<IBundleExporter>();
|
||||
await using var stream = await exporter.ExportAsync(
|
||||
|
||||
@@ -73,6 +73,10 @@ public sealed class BundleExporter : IBundleExporter
|
||||
var resolved = await _resolver.ResolveAsync(selection, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// 2. Convert to the wire-shaped DTO (strips EF identity, refs-by-name).
|
||||
// M8 (B4): the resolver's site/dataConnection/instance closure is wired
|
||||
// through via object-initializer — without this the aggregate would
|
||||
// carry the empty defaults and the bundle would ship empty site/
|
||||
// instance arrays even when those were selected (review item I3).
|
||||
var aggregate = new EntityAggregate(
|
||||
TemplateFolders: resolved.TemplateFolders,
|
||||
Templates: resolved.Templates,
|
||||
@@ -83,7 +87,12 @@ public sealed class BundleExporter : IBundleExporter
|
||||
NotificationLists: resolved.NotificationLists,
|
||||
SmtpConfigurations: resolved.SmtpConfigs,
|
||||
// Inbound API keys are not transported between environments (re-arch C4).
|
||||
ApiMethods: resolved.ApiMethods);
|
||||
ApiMethods: resolved.ApiMethods)
|
||||
{
|
||||
Sites = resolved.Sites,
|
||||
DataConnections = resolved.DataConnections,
|
||||
Instances = resolved.Instances,
|
||||
};
|
||||
var contentDto = _entitySerializer.ToBundleContent(aggregate);
|
||||
|
||||
// 3. Per-category summary counts — used for fast humanly-readable preview at import.
|
||||
|
||||
@@ -196,6 +196,23 @@ public sealed class DependencyResolver
|
||||
// in `sites` after closure expansion, so ordering + dependsOn edges resolve names.
|
||||
var siteIdentifierById = sites.Values.ToDictionary(s => s.Id, s => s.SiteIdentifier);
|
||||
|
||||
// I1: when an instance/data-connection is selected directly with
|
||||
// IncludeDependencies=false, its owning site is not packed (so it never
|
||||
// lands in `sites`) and the manifest dep-edge would degrade to the raw id
|
||||
// (Site:<rawId>) via SiteIdentifierOf's fallback. Resolve the identifier
|
||||
// for any referenced-but-unpacked owning site so the manifest reads
|
||||
// Site:<identifier> regardless of the deps flag — readability only, the
|
||||
// site row itself stays out of the bundle.
|
||||
var referencedSiteIds = instances.Values.Select(i => i.SiteId)
|
||||
.Concat(dataConnections.Values.Select(c => c.SiteId))
|
||||
.Distinct()
|
||||
.Where(id => !siteIdentifierById.ContainsKey(id));
|
||||
foreach (var siteId in referencedSiteIds)
|
||||
{
|
||||
var site = await _siteRepository.GetSiteByIdAsync(siteId, ct).ConfigureAwait(false);
|
||||
if (site is not null) siteIdentifierById[site.Id] = site.SiteIdentifier;
|
||||
}
|
||||
|
||||
var orderedSites = sites.Values
|
||||
.OrderBy(s => s.SiteIdentifier, StringComparer.Ordinal)
|
||||
.ToList();
|
||||
|
||||
@@ -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();
|
||||
|
||||
+183
-1
@@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -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();
|
||||
|
||||
+3
@@ -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();
|
||||
|
||||
+3
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user