21b801b71f
Add code comments in ValidateConnectionBindingCompleteness explaining that the unbound-attribute branch also covers the silently-dropped stale-binding case (cross-reference FlatteningService.ApplyConnectionBindings), and that the `continue` skips the exists-at-site check for unbound attrs. Add two new tests: - FlatteningPipelineConnectionBindingTests: stale DataConnectionId (999) not present in site connections → flattener drops it silently → validator reports ConnectionBinding Error, IsValid false. - ValidationServiceTests: enforce:true + siteConnectionNames:null on a properly-bound attribute → no ConnectionBinding error (exists-at-site check stays inert when site set is not supplied).
340 lines
12 KiB
C#
340 lines
12 KiB
C#
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
|
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Validation;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Validation;
|
|
|
|
public class ValidationServiceTests
|
|
{
|
|
private readonly ValidationService _sut = new();
|
|
|
|
[Fact]
|
|
public void Validate_ValidConfig_ReturnsSuccess()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double" }],
|
|
Scripts = [new ResolvedScript { CanonicalName = "Monitor", Code = "var x = 1;" }]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.True(result.IsValid);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_EmptyInstanceName_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "",
|
|
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", DataType = "Double" }]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.False(result.IsValid);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.FlatteningFailure);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_NamingCollision_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute { CanonicalName = "Temp", DataType = "Double" },
|
|
new ResolvedAttribute { CanonicalName = "Temp", DataType = "Int32" } // Duplicate!
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.False(result.IsValid);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.NamingCollision);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_ForbiddenApi_ReturnsCompilationError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "BadScript",
|
|
Code = "System.IO.File.ReadAllText(\"secret.txt\");"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.False(result.IsValid);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ScriptCompilation);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_MismatchedBraces_ReturnsCompilationError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript { CanonicalName = "Bad", Code = "if (true) {" }
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.False(result.IsValid);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ScriptCompilation);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_AlarmReferencesMissingAttribute_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", DataType = "Double" }],
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "HighPressure",
|
|
TriggerType = "RangeViolation",
|
|
TriggerConfiguration = "{\"attributeName\":\"Pressure\"}" // Pressure doesn't exist
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.False(result.IsValid);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.AlarmTriggerReference);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_AlarmReferencesExistingAttribute_NoError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", DataType = "Double" }],
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "HighTemp",
|
|
TriggerType = "RangeViolation",
|
|
TriggerConfiguration = "{\"attributeName\":\"Temp\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
// Should not have alarm trigger reference errors
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.AlarmTriggerReference);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_ScriptTriggerReferencesMissingAttribute_ReturnsError()
|
|
{
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", DataType = "Double" }],
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "OnChange",
|
|
Code = "var x = 1;",
|
|
TriggerConfiguration = "{\"attributeName\":\"Missing\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.False(result.IsValid);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ScriptTriggerReference);
|
|
}
|
|
|
|
[Fact]
|
|
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",
|
|
Attributes =
|
|
[
|
|
new ResolvedAttribute
|
|
{
|
|
CanonicalName = "Temp",
|
|
DataType = "Double",
|
|
DataSourceReference = "ns=2;s=Temp",
|
|
BoundDataConnectionId = null // No binding!
|
|
}
|
|
]
|
|
};
|
|
|
|
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]
|
|
public void Validate_BoundAttributeWithNoSiteSet_DeployTime_ExistsAtSiteCheckIsInert()
|
|
{
|
|
// M2.8 (#23): when siteConnectionNames is null the "exists at site" half of the
|
|
// binding check stays inert — a properly-bound data-sourced attribute must NOT
|
|
// produce a ConnectionBinding error, even under deploy-time enforcement.
|
|
// This pins the contract: passing enforce:true + siteConnectionNames:null is safe
|
|
// (e.g. when the caller doesn't have a site connection set available yet).
|
|
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: null);
|
|
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.ConnectionBinding);
|
|
Assert.True(result.IsValid);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_EmptyConfig_ReturnsWarning()
|
|
{
|
|
var config = new FlattenedConfiguration { InstanceUniqueName = "Instance1" };
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Warnings, w => w.Category == ValidationCategory.FlatteningFailure);
|
|
}
|
|
}
|