using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport; using ZB.MOM.WW.ScadaBridge.Commons.Types.Templates; using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport; using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase; using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories; using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Services; namespace ZB.MOM.WW.ScadaBridge.Transport.IntegrationTests.Import; /// /// WP2 task 2.6 — template Overwrite and the composition rewire. /// TRN-4: a bundle template with no compositions skipped the clear, so the target's /// slots survived an Overwrite the preview said would remove them. /// TRN-5: the rewire deleted a removed slot's row without its slot-owned derived /// template, leaving an orphan with a dangling OwnerCompositionId. /// public sealed class CompositionOverwriteTests : IDisposable { private readonly ServiceProvider _provider; /// Builds an in-memory configuration database plus the Transport services. public CompositionOverwriteTests() { var services = new ServiceCollection(); services.AddSingleton(new ConfigurationBuilder().AddInMemoryCollection().Build()); var dbName = $"CompositionOverwriteTests_{Guid.NewGuid()}"; services.AddDbContext(opts => opts .UseInMemoryDatabase(dbName) .ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning))); services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddTransport(); _provider = services.BuildServiceProvider(); } /// Releases the resources held by this instance. public void Dispose() => _provider.Dispose(); /// TRN-4: Overwrite with a composition-less bundle template removes the target's slots. /// A task that represents the asynchronous operation. [Fact] public async Task Overwrite_with_no_bundle_compositions_removes_target_compositions() { await SeedAsync(ctx => { ctx.Templates.Add(new Template("B")); ctx.Templates.Add(new Template("A")); }); var bundle = await ExportAllTemplatesAsync(); // Target: A now composes B as b1. await SeedAsync(ctx => { var a = ctx.Templates.Single(t => t.Name == "A"); var b = ctx.Templates.Single(t => t.Name == "B"); ctx.TemplateCompositions.Add(new TemplateComposition("b1") { TemplateId = a.Id, ComposedTemplateId = b.Id }); }); await ImportOverwriteAsync(bundle, "A", "B"); await using var scope = _provider.CreateAsyncScope(); var ctx = scope.ServiceProvider.GetRequiredService(); var aId = await ctx.Templates.Where(t => t.Name == "A").Select(t => t.Id).SingleAsync(); Assert.False(await ctx.TemplateCompositions.AnyAsync(c => c.TemplateId == aId)); } /// /// TRN-5: target owner has slot S1 with a slot-owned derived template; the bundle /// owner has only S2. Overwrite removes S1 AND its derived template. /// /// A task that represents the asynchronous operation. [Fact] public async Task Overwrite_removing_a_slot_deletes_its_slot_owned_derived_template() { // Source: A composes B directly as S2. await SeedAsync(ctx => { ctx.Templates.Add(new Template("B")); ctx.Templates.Add(new Template("A")); }); await SeedAsync(ctx => { var a = ctx.Templates.Single(t => t.Name == "A"); var b = ctx.Templates.Single(t => t.Name == "B"); ctx.TemplateCompositions.Add(new TemplateComposition("S2") { TemplateId = a.Id, ComposedTemplateId = b.Id }); }); var bundle = await ExportAllTemplatesAsync(); // Target: drop S2; A instead owns slot S1 whose composed template is a // slot-owned derived template of B. await SeedAsync(ctx => { ctx.TemplateCompositions.RemoveRange(ctx.TemplateCompositions); }); int derivedId = 0; await SeedAsync(ctx => { var a = ctx.Templates.Single(t => t.Name == "A"); var b = ctx.Templates.Single(t => t.Name == "B"); var derived = new Template("A.S1") { ParentTemplateId = b.Id, IsDerived = true }; ctx.Templates.Add(derived); ctx.SaveChanges(); var slot = new TemplateComposition("S1") { TemplateId = a.Id, ComposedTemplateId = derived.Id }; ctx.TemplateCompositions.Add(slot); ctx.SaveChanges(); derived.OwnerCompositionId = slot.Id; derivedId = derived.Id; }); await ImportOverwriteAsync(bundle, "A", "B"); await using var scope = _provider.CreateAsyncScope(); var ctx = scope.ServiceProvider.GetRequiredService(); var aId = await ctx.Templates.Where(t => t.Name == "A").Select(t => t.Id).SingleAsync(); var slots = await ctx.TemplateCompositions.Where(c => c.TemplateId == aId).Select(c => c.InstanceName).ToListAsync(); Assert.Equal(new[] { "S2" }, slots); Assert.False(await ctx.Templates.AnyAsync(t => t.Id == derivedId), "the removed slot's derived template must be deleted with it"); } private async Task SeedAsync(Action seed) { await using var scope = _provider.CreateAsyncScope(); var ctx = scope.ServiceProvider.GetRequiredService(); seed(ctx); await ctx.SaveChangesAsync(); } private async Task ExportAllTemplatesAsync() { await using var scope = _provider.CreateAsyncScope(); var exporter = scope.ServiceProvider.GetRequiredService(); var ctx = scope.ServiceProvider.GetRequiredService(); var templateIds = await ctx.Templates.Select(t => t.Id).ToListAsync(); var selection = new ExportSelection( TemplateIds: templateIds, SharedScriptIds: Array.Empty(), ExternalSystemIds: Array.Empty(), DatabaseConnectionIds: Array.Empty(), NotificationListIds: Array.Empty(), SmtpConfigurationIds: Array.Empty(), ApiMethodIds: Array.Empty(), IncludeDependencies: false); var stream = await exporter.ExportAsync(selection, user: "alice", sourceEnvironment: "dev", passphrase: null, cancellationToken: CancellationToken.None); using var ms = new MemoryStream(); await stream.CopyToAsync(ms); return ms.ToArray(); } private async Task ImportOverwriteAsync(byte[] bundle, params string[] templateNames) { Guid sessionId; await using (var scope = _provider.CreateAsyncScope()) { var importer = scope.ServiceProvider.GetRequiredService(); using var input = new MemoryStream(bundle, writable: false); sessionId = (await importer.LoadAsync(input, passphrase: null)).SessionId; } await using (var scope = _provider.CreateAsyncScope()) { var importer = scope.ServiceProvider.GetRequiredService(); await importer.ApplyAsync(sessionId, templateNames.Select(n => new ImportResolution("Template", n, ResolutionAction.Overwrite, null)).ToList(), user: "bob"); } } }