152 lines
7.4 KiB
C#
152 lines
7.4 KiB
C#
using NSubstitute;
|
|
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.Types.Enums;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Templates;
|
|
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
|
|
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Validation;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.DeploymentManager.Tests;
|
|
|
|
/// <summary>
|
|
/// Pins <c>PreviewAsync</c>: the Create Instance page must see exactly the attribute
|
|
/// set a freshly created, un-overridden instance flattens to — including composed
|
|
/// module members — without any instance row existing.
|
|
/// </summary>
|
|
public class FlatteningPipelinePreviewTests
|
|
{
|
|
private const int TemplateId = 10, ModuleTemplateId = 11, SiteId = 20, InstanceId = 30;
|
|
|
|
private readonly ITemplateEngineRepository _templateRepo = Substitute.For<ITemplateEngineRepository>();
|
|
private readonly ISiteRepository _siteRepo = Substitute.For<ISiteRepository>();
|
|
private readonly ISharedSchemaRepository _sharedSchemaRepo = Substitute.For<ISharedSchemaRepository>();
|
|
private readonly FlatteningPipeline _sut;
|
|
|
|
/// <summary>Initializes the test class with in-memory repositories and the pipeline under test.</summary>
|
|
public FlatteningPipelinePreviewTests()
|
|
{
|
|
_sharedSchemaRepo.ListAsync(Arg.Any<CancellationToken>()).Returns([]);
|
|
_sut = new FlatteningPipeline(_templateRepo, _siteRepo, new FlatteningService(), new ValidationService(),
|
|
new RevisionHashService(), _sharedSchemaRepo, new TemplateGraphWatermark());
|
|
|
|
// Parent template with one direct data-sourced attribute, composing a module with one.
|
|
var module = new Template("Receiver") { Id = ModuleTemplateId };
|
|
module.Attributes.Add(new TemplateAttribute("MoveInFlag")
|
|
{
|
|
DataType = DataType.Boolean,
|
|
DataSourceReference = "MoveInFlag"
|
|
});
|
|
var parent = new Template("Line") { Id = TemplateId };
|
|
parent.Attributes.Add(new TemplateAttribute("Running")
|
|
{
|
|
DataType = DataType.Boolean,
|
|
DataSourceReference = "Running"
|
|
});
|
|
var composition = new TemplateComposition("Left")
|
|
{
|
|
TemplateId = TemplateId,
|
|
ComposedTemplateId = ModuleTemplateId
|
|
};
|
|
|
|
_templateRepo.GetTemplateWithChildrenAsync(TemplateId, Arg.Any<CancellationToken>()).Returns(parent);
|
|
_templateRepo.GetTemplateWithChildrenAsync(ModuleTemplateId, Arg.Any<CancellationToken>()).Returns(module);
|
|
// Compositions ride on the loaded template (GetTemplateWithChildrenAsync Includes them).
|
|
parent.Compositions.Add(composition);
|
|
_templateRepo.GetAllSharedScriptsAsync(Arg.Any<CancellationToken>()).Returns([]);
|
|
_templateRepo.GetInstanceByIdAsync(InstanceId, Arg.Any<CancellationToken>())
|
|
.Returns(new Instance("Line-01") { Id = InstanceId, TemplateId = TemplateId, SiteId = SiteId });
|
|
_siteRepo.GetDataConnectionsBySiteIdAsync(SiteId, Arg.Any<CancellationToken>())
|
|
.Returns([new DataConnection("MxGateway", "MxGateway", SiteId) { Id = 1 }]);
|
|
}
|
|
|
|
/// <summary>Verifies that previewing matches flattening a freshly created instance.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Preview_matches_flatten_of_fresh_instance()
|
|
{
|
|
var preview = await _sut.PreviewAsync(TemplateId, SiteId);
|
|
var real = await _sut.FlattenAndValidateAsync(InstanceId, validateScripts: false);
|
|
|
|
// Result.Error throws on a success, so the message is only read on failure.
|
|
Assert.True(preview.IsSuccess, preview.IsSuccess ? "" : preview.Error);
|
|
Assert.True(real.IsSuccess, real.IsSuccess ? "" : real.Error);
|
|
|
|
var expected = real.Value.Configuration.Attributes
|
|
.Select(a => (a.CanonicalName, a.DefaultDataSourceReference ?? a.DataSourceReference))
|
|
.OrderBy(x => x.Item1, StringComparer.Ordinal).ToList();
|
|
var actual = preview.Value.Attributes
|
|
.Select(a => (a.CanonicalName, a.DefaultDataSourceReference ?? a.DataSourceReference))
|
|
.OrderBy(x => x.Item1, StringComparer.Ordinal).ToList();
|
|
|
|
Assert.Equal(expected, actual);
|
|
Assert.Contains(actual, a => a.Item1 == "Left.MoveInFlag");
|
|
}
|
|
|
|
/// <summary>Verifies that previewing an unknown template fails.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Preview_unknown_template_fails()
|
|
{
|
|
var r = await _sut.PreviewAsync(999, SiteId);
|
|
Assert.True(r.IsFailure);
|
|
}
|
|
|
|
/// <summary>Verifies that previewing never touches the instance repository.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Preview_never_touches_instance_repository()
|
|
{
|
|
await _sut.PreviewAsync(TemplateId, SiteId);
|
|
await _templateRepo.DidNotReceive().GetInstanceByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// review 2026-10-21 04/U5: the composition walk reads compositions off the already-loaded
|
|
/// template (GetTemplateWithChildrenAsync Includes them) and issues no separate
|
|
/// per-template composition query — while still resolving the composed module.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Flatten_reads_compositions_from_loaded_template_without_extra_query()
|
|
{
|
|
var result = await _sut.FlattenAndValidateAsync(InstanceId, validateScripts: false);
|
|
|
|
Assert.True(result.IsSuccess, result.IsSuccess ? "" : result.Error);
|
|
Assert.Contains(result.Value.Configuration.Attributes, a => a.CanonicalName == "Left.MoveInFlag");
|
|
await _templateRepo.DidNotReceive().GetCompositionsByTemplateIdAsync(
|
|
Arg.Any<int>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// review 2026-10-21 04/P2: the hash-only entry point returns exactly the hash the
|
|
/// validating entry point does (so staleness verdicts cannot drift from the deploy and
|
|
/// comparison paths), without the validation-only shared-script and schema-library loads.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ComputeRevisionHash_matches_validating_flatten_without_validation_loads()
|
|
{
|
|
var hashOnly = await _sut.ComputeRevisionHashAsync(InstanceId);
|
|
|
|
await _templateRepo.DidNotReceive().GetAllSharedScriptsAsync(Arg.Any<CancellationToken>());
|
|
await _sharedSchemaRepo.DidNotReceive().ListAsync(Arg.Any<CancellationToken>());
|
|
|
|
var full = await _sut.FlattenAndValidateAsync(InstanceId, validateScripts: false);
|
|
|
|
Assert.True(hashOnly.IsSuccess, hashOnly.IsSuccess ? "" : hashOnly.Error);
|
|
Assert.True(full.IsSuccess, full.IsSuccess ? "" : full.Error);
|
|
Assert.Equal(full.Value.RevisionHash, hashOnly.Value);
|
|
}
|
|
|
|
/// <summary>The hash-only entry point fails like the validating one for an unknown instance.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ComputeRevisionHash_unknown_instance_fails()
|
|
{
|
|
var r = await _sut.ComputeRevisionHashAsync(999_999);
|
|
Assert.True(r.IsFailure);
|
|
}
|
|
}
|