210 lines
10 KiB
C#
210 lines
10 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Reflection;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
|
|
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;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
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;
|
|
|
|
/// <summary>
|
|
/// Review 2026-10-21 04/P4 + 08/P1: one import used to load the WHOLE template
|
|
/// library (attributes, alarms, scripts with bodies, compositions — tracked) up to
|
|
/// nine times, two of them inside per-template loops, and to look every bundled
|
|
/// instance up one query at a time. These tests count repository calls through a
|
|
/// recording proxy so the per-session budget is pinned.
|
|
/// </summary>
|
|
public sealed class ImportRoundTripBudgetTests : IDisposable
|
|
{
|
|
private readonly ServiceProvider _provider;
|
|
private readonly ConcurrentDictionary<string, int> _calls = new(StringComparer.Ordinal);
|
|
|
|
/// <summary>Initializes the test class with a counting template repository over an in-memory database.</summary>
|
|
public ImportRoundTripBudgetTests()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<Microsoft.AspNetCore.DataProtection.IDataProtectionProvider>(
|
|
new Microsoft.AspNetCore.DataProtection.EphemeralDataProtectionProvider());
|
|
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().AddInMemoryCollection().Build());
|
|
|
|
var dbName = $"ImportRoundTripBudgetTests_{Guid.NewGuid()}";
|
|
services.AddDbContext<ScadaBridgeDbContext>(opts => opts
|
|
.UseInMemoryDatabase(dbName)
|
|
.ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning)));
|
|
|
|
services.AddSingleton<ITemplateGraphWatermark, TemplateGraphWatermark>();
|
|
services.AddScoped<ITemplateEngineRepository>(sp => CountingProxy<ITemplateEngineRepository>.Wrap(
|
|
new TemplateEngineRepository(
|
|
sp.GetRequiredService<ScadaBridgeDbContext>(),
|
|
sp.GetRequiredService<ITemplateGraphWatermark>()),
|
|
_calls));
|
|
services.AddScoped<IExternalSystemRepository, ExternalSystemRepository>();
|
|
services.AddScoped<INotificationRepository, NotificationRepository>();
|
|
services.AddScoped<IInboundApiRepository, InboundApiRepository>();
|
|
services.AddScoped<ISiteRepository, SiteRepository>();
|
|
services.AddScoped<IAuditCorrelationContext, AuditCorrelationContext>();
|
|
services.AddScoped<IAuditService, AuditService>();
|
|
services.AddTransport();
|
|
|
|
_provider = services.BuildServiceProvider();
|
|
}
|
|
|
|
/// <summary>Releases the resources used by this instance.</summary>
|
|
public void Dispose() => _provider.Dispose();
|
|
|
|
private int Calls(string method) => _calls.TryGetValue(method, out var n) ? n : 0;
|
|
|
|
/// <summary>
|
|
/// Seeds a library of templates (a base, a module, and several composing /
|
|
/// derived templates) plus a site with instances, exports the site closure with
|
|
/// its templates, and returns the loaded session.
|
|
/// </summary>
|
|
private async Task<Guid> SeedExportAndLoadAsync(int instanceCount)
|
|
{
|
|
await using (var scope = _provider.CreateAsyncScope())
|
|
{
|
|
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
|
|
var baseT = new Template("Base");
|
|
baseT.Attributes.Add(new TemplateAttribute("Flow") { DataType = DataType.Float, Value = "1.0" });
|
|
var module = new Template("Motor");
|
|
module.Attributes.Add(new TemplateAttribute("Speed") { DataType = DataType.Float, Value = "1.0" });
|
|
ctx.Templates.AddRange(baseT, module);
|
|
await ctx.SaveChangesAsync();
|
|
|
|
var pump = new Template("Pump") { ParentTemplateId = baseT.Id };
|
|
pump.Compositions.Add(new TemplateComposition("motor1") { ComposedTemplateId = module.Id });
|
|
ctx.Templates.Add(pump);
|
|
// Unrelated library templates: the import must not pay for them per item.
|
|
for (var i = 0; i < 5; i++) ctx.Templates.Add(new Template($"Other{i}"));
|
|
var site = new Site("Plant 1", "plant-1") { GrpcNodeAAddress = "http://plant-1-a:8083" };
|
|
ctx.Sites.Add(site);
|
|
await ctx.SaveChangesAsync();
|
|
|
|
for (var i = 0; i < instanceCount; i++)
|
|
{
|
|
ctx.Instances.Add(new Instance($"Pump-{i:00}") { TemplateId = pump.Id, SiteId = site.Id });
|
|
}
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
|
|
Stream bundle;
|
|
await using (var scope = _provider.CreateAsyncScope())
|
|
{
|
|
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
|
|
var siteIds = await ctx.Sites.Select(s => s.Id).ToListAsync();
|
|
bundle = await scope.ServiceProvider.GetRequiredService<IBundleExporter>().ExportAsync(
|
|
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: siteIds),
|
|
user: "alice", sourceEnvironment: "dev", passphrase: null,
|
|
cancellationToken: CancellationToken.None);
|
|
}
|
|
|
|
await using var loadScope = _provider.CreateAsyncScope();
|
|
return (await loadScope.ServiceProvider.GetRequiredService<IBundleImporter>()
|
|
.LoadAsync(bundle, passphrase: null)).SessionId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that preview + apply of a bundle carrying templates and instances
|
|
/// never loads the full template graph, loads the library projection at most once
|
|
/// per phase, and looks instances up in batches rather than per instance.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Preview_and_apply_load_the_template_library_once_per_phase_and_batch_instance_lookups()
|
|
{
|
|
var sessionId = await SeedExportAndLoadAsync(instanceCount: 6);
|
|
_calls.Clear();
|
|
|
|
ImportPreview preview;
|
|
await using (var scope = _provider.CreateAsyncScope())
|
|
{
|
|
preview = await scope.ServiceProvider.GetRequiredService<IBundleImporter>().PreviewAsync(sessionId);
|
|
}
|
|
Assert.DoesNotContain(preview.Items, i => i.Kind == ConflictKind.Blocker);
|
|
|
|
Assert.Equal(0, Calls(nameof(ITemplateEngineRepository.GetAllTemplatesAsync)));
|
|
Assert.Equal(0, Calls(nameof(ITemplateEngineRepository.GetInstanceByUniqueNameAsync)));
|
|
Assert.Equal(0, Calls(nameof(ITemplateEngineRepository.GetTemplateByIdAsync)));
|
|
Assert.True(
|
|
Calls(nameof(ITemplateEngineRepository.GetAllTemplatesForAnalysisAsync))
|
|
+ Calls(nameof(ITemplateEngineRepository.GetTemplateStubsAsync)) <= 2,
|
|
"preview should load the library projection at most once per shape");
|
|
_calls.Clear();
|
|
|
|
var resolutions = preview.Items
|
|
.Where(i => i.EntityType is "Template" or "Instance" or "Site")
|
|
.Select(i => new ImportResolution(i.EntityType, i.Name, ResolutionAction.Overwrite, null))
|
|
.ToList();
|
|
ImportResult result;
|
|
await using (var scope = _provider.CreateAsyncScope())
|
|
{
|
|
result = await scope.ServiceProvider.GetRequiredService<IBundleImporter>()
|
|
.ApplyAsync(sessionId, resolutions, user: "bob");
|
|
}
|
|
|
|
Assert.True(result.Overwritten >= 6 + 3, $"expected the templates and instances to be overwritten, got {result.Overwritten}");
|
|
Assert.Equal(0, Calls(nameof(ITemplateEngineRepository.GetAllTemplatesAsync)));
|
|
Assert.Equal(0, Calls(nameof(ITemplateEngineRepository.GetInstanceByUniqueNameAsync)));
|
|
Assert.True(Calls(nameof(ITemplateEngineRepository.GetAllTemplatesForAnalysisAsync)) <= 1,
|
|
"apply should load the analysis library at most once");
|
|
Assert.True(Calls(nameof(ITemplateEngineRepository.GetInstancesByUniqueNamesAsync)) <= 1,
|
|
"apply should look the bundle's instances up in one batch");
|
|
}
|
|
|
|
/// <summary>Records every interface call by method name, then forwards it to the real implementation.</summary>
|
|
/// <typeparam name="T">The proxied interface.</typeparam>
|
|
public class CountingProxy<T> : DispatchProxy where T : class
|
|
{
|
|
private T _inner = null!;
|
|
private ConcurrentDictionary<string, int> _calls = null!;
|
|
|
|
/// <summary>Creates a counting proxy over <paramref name="inner"/>.</summary>
|
|
/// <param name="inner">The real implementation.</param>
|
|
/// <param name="calls">The shared call counter.</param>
|
|
/// <returns>The proxy.</returns>
|
|
public static T Wrap(T inner, ConcurrentDictionary<string, int> calls)
|
|
{
|
|
var proxy = Create<T, CountingProxy<T>>();
|
|
var self = (CountingProxy<T>)(object)proxy;
|
|
self._inner = inner;
|
|
self._calls = calls;
|
|
return proxy;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override object? Invoke(MethodInfo? targetMethod, object?[]? args)
|
|
{
|
|
_calls.AddOrUpdate(targetMethod!.Name, 1, (_, n) => n + 1);
|
|
try
|
|
{
|
|
return targetMethod.Invoke(_inner, args);
|
|
}
|
|
catch (TargetInvocationException ex) when (ex.InnerException is not null)
|
|
{
|
|
throw ex.InnerException;
|
|
}
|
|
}
|
|
}
|
|
}
|