feat(#23): elevate connection-binding completeness to a deploy-gating Error (M2.8)
Pre-deployment validation only WARNED when a data-sourced attribute had no
connection binding, so an instance with unresolved bindings still passed IsValid
and could deploy. There was also no check that a binding resolves to a connection
that actually exists at the target site.
- ValidationService.Validate gains an opt-in `enforceConnectionBindings` flag
(default false) plus a `siteConnectionNames` set. Default-false keeps the
template DESIGN-TIME path (ManagementActor.HandleValidateTemplate) non-blocking,
since bindings are legitimately set later at instance/deploy time. The DEPLOY
path (FlatteningPipeline) opts in (true) so:
* a data-sourced attribute with no binding is now a deploy-gating Error;
* a binding to a connection that does not exist on the target site is an Error.
Static (non-data-sourced) attributes are never flagged.
- FlatteningPipeline computes the site-connection-names set from the loaded site
data connections (mirroring M2.1's alarmCapableConnectionNames) and threads it in.
- Tests: TemplateEngine.Tests covers design-time warning / deploy-time error /
static-ok / exists-at-site / non-existent-connection. New
FlatteningPipelineConnectionBindingTests proves the deploy path enforces it.
Mark M2.7 + M2.8 completed in the plan task tracker.
This commit is contained in:
+99
@@ -0,0 +1,99 @@
|
||||
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.Flattening;
|
||||
using ZB.MOM.WW.ScadaBridge.DeploymentManager;
|
||||
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
|
||||
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Validation;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.DeploymentManager.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// M2.8 (#23): proves the deploy path (FlatteningPipeline.FlattenAndValidateAsync)
|
||||
/// opts into connection-binding enforcement, so a data-sourced attribute with no
|
||||
/// binding gates the deployment as an ERROR (not just a warning), and that a binding
|
||||
/// resolving to a connection that actually exists at the target site passes.
|
||||
/// </summary>
|
||||
public class FlatteningPipelineConnectionBindingTests
|
||||
{
|
||||
private const int InstanceId = 1;
|
||||
private const int TemplateId = 10;
|
||||
private const int SiteId = 100;
|
||||
private const int ConnectionId = 7;
|
||||
|
||||
private readonly ITemplateEngineRepository _templateRepo = Substitute.For<ITemplateEngineRepository>();
|
||||
private readonly ISiteRepository _siteRepo = Substitute.For<ISiteRepository>();
|
||||
private readonly FlatteningPipeline _sut;
|
||||
|
||||
public FlatteningPipelineConnectionBindingTests()
|
||||
{
|
||||
_sut = new FlatteningPipeline(
|
||||
_templateRepo,
|
||||
_siteRepo,
|
||||
new FlatteningService(),
|
||||
new ValidationService(),
|
||||
new RevisionHashService());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a single-template chain with one data-sourced attribute ("Temp") and a
|
||||
/// site that owns a single "PlantBus" data connection. The instance optionally
|
||||
/// binds "Temp" to <paramref name="boundConnectionId"/>.
|
||||
/// </summary>
|
||||
private void Arrange(int? boundConnectionId)
|
||||
{
|
||||
var template = new Template("Tank") { Id = TemplateId };
|
||||
template.Attributes.Add(new TemplateAttribute("Temp")
|
||||
{
|
||||
DataType = DataType.Double,
|
||||
DataSourceReference = "ns=2;s=Temp"
|
||||
});
|
||||
|
||||
var instance = new Instance("Tank-01") { Id = InstanceId, TemplateId = TemplateId, SiteId = SiteId };
|
||||
if (boundConnectionId.HasValue)
|
||||
{
|
||||
instance.ConnectionBindings.Add(new InstanceConnectionBinding("Temp")
|
||||
{
|
||||
InstanceId = InstanceId,
|
||||
DataConnectionId = boundConnectionId.Value
|
||||
});
|
||||
}
|
||||
|
||||
_templateRepo.GetInstanceByIdAsync(InstanceId, Arg.Any<CancellationToken>()).Returns(instance);
|
||||
_templateRepo.GetTemplateWithChildrenAsync(TemplateId, Arg.Any<CancellationToken>()).Returns(template);
|
||||
_templateRepo.GetCompositionsByTemplateIdAsync(TemplateId, Arg.Any<CancellationToken>()).Returns([]);
|
||||
_templateRepo.GetAllSharedScriptsAsync(Arg.Any<CancellationToken>()).Returns([]);
|
||||
|
||||
var connection = new DataConnection("PlantBus", "OpcUa", SiteId) { Id = ConnectionId };
|
||||
_siteRepo.GetDataConnectionsBySiteIdAsync(SiteId, Arg.Any<CancellationToken>())
|
||||
.Returns([connection]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FlattenAndValidate_DataSourcedAttributeWithNoBinding_ReportsBindingError()
|
||||
{
|
||||
Arrange(boundConnectionId: null);
|
||||
|
||||
var result = await _sut.FlattenAndValidateAsync(InstanceId);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.False(result.Value.Validation.IsValid);
|
||||
Assert.Contains(result.Value.Validation.Errors,
|
||||
e => e.Category == ValidationCategory.ConnectionBinding);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FlattenAndValidate_BindingToExistingSiteConnection_NoBindingError()
|
||||
{
|
||||
Arrange(boundConnectionId: ConnectionId);
|
||||
|
||||
var result = await _sut.FlattenAndValidateAsync(InstanceId);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.DoesNotContain(result.Value.Validation.Errors,
|
||||
e => e.Category == ValidationCategory.ConnectionBinding);
|
||||
}
|
||||
}
|
||||
+117
-1
@@ -161,8 +161,11 @@ public class ValidationServiceTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_UnboundDataSourceAttribute_ReturnsWarning()
|
||||
public void Validate_UnboundDataSourceAttribute_DesignTime_ReturnsWarningNotError()
|
||||
{
|
||||
// M2.8 (#23): at template design time (the default, enforceConnectionBindings:false)
|
||||
// a data-sourced attribute is legitimately unbound — bindings are set later at
|
||||
// instance/deploy time. So this must stay a non-blocking WARNING and IsValid true.
|
||||
var config = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
@@ -180,6 +183,119 @@ public class ValidationServiceTests
|
||||
|
||||
var result = _sut.Validate(config);
|
||||
Assert.Contains(result.Warnings, w => w.Category == ValidationCategory.ConnectionBinding);
|
||||
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ConnectionBinding);
|
||||
Assert.True(result.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_UnboundDataSourceAttribute_DeployTime_ReturnsErrorAndBlocks()
|
||||
{
|
||||
// M2.8 (#23): the deploy path opts in (enforceConnectionBindings:true). A data-sourced
|
||||
// attribute with no binding now gates the deployment as an ERROR (IsValid false).
|
||||
var config = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute
|
||||
{
|
||||
CanonicalName = "Temp",
|
||||
DataType = "Double",
|
||||
DataSourceReference = "ns=2;s=Temp",
|
||||
BoundDataConnectionId = null // No binding!
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var result = _sut.Validate(config, enforceConnectionBindings: true);
|
||||
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ConnectionBinding);
|
||||
Assert.False(result.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_StaticAttributeWithoutBinding_DeployTime_NoBindingError()
|
||||
{
|
||||
// M2.8 (#23): only DATA-SOURCED attributes require a binding. A static attribute
|
||||
// (DataSourceReference == null) must remain OK even under deploy-time enforcement.
|
||||
var config = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute
|
||||
{
|
||||
CanonicalName = "Setpoint",
|
||||
DataType = "Double",
|
||||
Value = "42",
|
||||
DataSourceReference = null,
|
||||
BoundDataConnectionId = null
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var result = _sut.Validate(config, enforceConnectionBindings: true);
|
||||
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ConnectionBinding);
|
||||
Assert.True(result.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_BoundToExistingSiteConnection_DeployTime_NoBindingError()
|
||||
{
|
||||
// M2.8 (#23): a data-sourced attribute bound to a connection that exists at the
|
||||
// target site passes the binding gate.
|
||||
var config = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute
|
||||
{
|
||||
CanonicalName = "Temp",
|
||||
DataType = "Double",
|
||||
DataSourceReference = "ns=2;s=Temp",
|
||||
BoundDataConnectionId = 7,
|
||||
BoundDataConnectionName = "PlantBus"
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var result = _sut.Validate(
|
||||
config,
|
||||
enforceConnectionBindings: true,
|
||||
siteConnectionNames: new HashSet<string>(StringComparer.Ordinal) { "PlantBus" });
|
||||
|
||||
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ConnectionBinding);
|
||||
Assert.True(result.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_BoundToNonExistentSiteConnection_DeployTime_ReturnsError()
|
||||
{
|
||||
// M2.8 (#23): a binding pointing at a connection that does NOT exist on the
|
||||
// target site is an ERROR that blocks deployment.
|
||||
var config = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute
|
||||
{
|
||||
CanonicalName = "Temp",
|
||||
DataType = "Double",
|
||||
DataSourceReference = "ns=2;s=Temp",
|
||||
BoundDataConnectionId = 99,
|
||||
BoundDataConnectionName = "GhostBus"
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var result = _sut.Validate(
|
||||
config,
|
||||
enforceConnectionBindings: true,
|
||||
siteConnectionNames: new HashSet<string>(StringComparer.Ordinal) { "PlantBus" });
|
||||
|
||||
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ConnectionBinding);
|
||||
Assert.False(result.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user