513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
105 lines
5.2 KiB
C#
105 lines
5.2 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);
|
|
_templateRepo.GetCompositionsByTemplateIdAsync(TemplateId, Arg.Any<CancellationToken>()).Returns([composition]);
|
|
_templateRepo.GetCompositionsByTemplateIdAsync(ModuleTemplateId, Arg.Any<CancellationToken>()).Returns([]);
|
|
_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>());
|
|
}
|
|
}
|