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.
526 lines
21 KiB
C#
526 lines
21 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();
|
|
|
|
/// <summary>Verifies that a valid configuration returns success.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that an empty instance name returns an error.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that a naming collision returns an error.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that a forbidden API returns a compilation error.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that mismatched braces return a compilation error.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that an alarm referencing a missing attribute returns an error.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that an alarm referencing an existing attribute produces no error.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that a script trigger referencing a missing attribute returns an error.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that an unbound data-sourced attribute at design time returns a warning rather than an error.</summary>
|
|
[Fact]
|
|
public void Validate_UnboundDataSourceAttribute_DesignTime_ReturnsWarningNotError()
|
|
{
|
|
// 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);
|
|
}
|
|
|
|
/// <summary>Verifies that an unbound data-sourced attribute at deploy time returns an error and blocks.</summary>
|
|
[Fact]
|
|
public void Validate_UnboundDataSourceAttribute_DeployTime_ReturnsErrorAndBlocks()
|
|
{
|
|
// 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);
|
|
}
|
|
|
|
/// <summary>Verifies that a static attribute without a binding produces no binding error at deploy time.</summary>
|
|
[Fact]
|
|
public void Validate_StaticAttributeWithoutBinding_DeployTime_NoBindingError()
|
|
{
|
|
// 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);
|
|
}
|
|
|
|
/// <summary>Verifies that an attribute bound to an existing site connection produces no binding error at deploy time.</summary>
|
|
[Fact]
|
|
public void Validate_BoundToExistingSiteConnection_DeployTime_NoBindingError()
|
|
{
|
|
// 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);
|
|
}
|
|
|
|
/// <summary>Verifies that an attribute bound to a non-existent site connection returns an error at deploy time.</summary>
|
|
[Fact]
|
|
public void Validate_BoundToNonExistentSiteConnection_DeployTime_ReturnsError()
|
|
{
|
|
// 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);
|
|
}
|
|
|
|
/// <summary>Verifies that the exists-at-site check is inert for a bound attribute when no site is set at deploy time.</summary>
|
|
[Fact]
|
|
public void Validate_BoundAttributeWithNoSiteSet_DeployTime_ExistsAtSiteCheckIsInert()
|
|
{
|
|
// 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);
|
|
}
|
|
|
|
/// <summary>Verifies that an empty configuration returns a warning.</summary>
|
|
[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);
|
|
}
|
|
|
|
// --- Per-trigger AnalysisKind (Advisory default | Strict escalates) ---
|
|
// The only currently-advisory finding in CheckExpressionTrigger is the blank/empty
|
|
// expression (which "will never fire"). Advisory keeps it a non-blocking warning;
|
|
// Strict promotes it to a deploy-blocking error. The kind rides the existing
|
|
// trigger-config JSON ({"expression":"...","analysisKind":"Strict"}) — no migration.
|
|
|
|
/// <summary>Verifies that a blank expression trigger under the default advisory kind warns but passes.</summary>
|
|
[Fact]
|
|
public void Validate_ExpressionTrigger_BlankExpression_AdvisoryDefault_WarnsButPasses()
|
|
{
|
|
// No analysisKind in the config → Advisory (today's behavior): a blank expression
|
|
// is a non-blocking warning, validation still passes.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "BlankAlarm",
|
|
TriggerType = "Expression",
|
|
TriggerConfiguration = "{\"expression\":\"\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Warnings, w => w.Category == ValidationCategory.AlarmTriggerReference);
|
|
Assert.DoesNotContain(result.Errors, e => e.Category == ValidationCategory.AlarmTriggerReference);
|
|
Assert.True(result.IsValid);
|
|
}
|
|
|
|
/// <summary>Verifies that a blank expression trigger under the strict kind fails with an error.</summary>
|
|
[Fact]
|
|
public void Validate_ExpressionTrigger_BlankExpression_StrictKind_FailsWithError()
|
|
{
|
|
// analysisKind:"Strict" promotes the blank-expression advisory to a deploy-blocking error.
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "BlankAlarm",
|
|
TriggerType = "Expression",
|
|
TriggerConfiguration = "{\"expression\":\"\",\"analysisKind\":\"Strict\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.AlarmTriggerReference);
|
|
Assert.DoesNotContain(result.Warnings, w => w.Category == ValidationCategory.AlarmTriggerReference);
|
|
Assert.False(result.IsValid);
|
|
}
|
|
|
|
/// <summary>Verifies that a blank script expression trigger under the strict kind fails with an error.</summary>
|
|
[Fact]
|
|
public void Validate_ScriptExpressionTrigger_BlankExpression_StrictKind_FailsWithError()
|
|
{
|
|
// Strict escalation also applies to script expression triggers (mirrors the alarm path).
|
|
var config = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "BlankScript",
|
|
Code = "var x = 1;",
|
|
TriggerType = "Expression",
|
|
TriggerConfiguration = "{\"expression\":\" \",\"analysisKind\":\"Strict\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var result = _sut.Validate(config);
|
|
Assert.Contains(result.Errors, e => e.Category == ValidationCategory.ScriptTriggerReference);
|
|
Assert.False(result.IsValid);
|
|
}
|
|
|
|
/// <summary>Verifies that a valid expression trigger passes under both kinds.</summary>
|
|
[Fact]
|
|
public void Validate_ExpressionTrigger_ValidExpression_PassesUnderBothKinds()
|
|
{
|
|
// A genuinely valid expression must pass clean under Advisory AND Strict —
|
|
// Strict only escalates the currently-advisory findings, it does not invent new ones.
|
|
var attributes = new[]
|
|
{
|
|
new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double" }
|
|
};
|
|
|
|
var advisory = new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes = attributes,
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "HighTemp",
|
|
TriggerType = "Expression",
|
|
TriggerConfiguration = "{\"expression\":\"(double)Attributes[\\\"Temp\\\"] > 50\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var strict = advisory with
|
|
{
|
|
Alarms =
|
|
[
|
|
new ResolvedAlarm
|
|
{
|
|
CanonicalName = "HighTemp",
|
|
TriggerType = "Expression",
|
|
TriggerConfiguration = "{\"expression\":\"(double)Attributes[\\\"Temp\\\"] > 50\",\"analysisKind\":\"Strict\"}"
|
|
}
|
|
]
|
|
};
|
|
|
|
var advisoryResult = _sut.Validate(advisory);
|
|
var strictResult = _sut.Validate(strict);
|
|
|
|
Assert.DoesNotContain(advisoryResult.Errors, e => e.Category == ValidationCategory.AlarmTriggerReference);
|
|
Assert.DoesNotContain(advisoryResult.Warnings, w => w.Category == ValidationCategory.AlarmTriggerReference);
|
|
Assert.DoesNotContain(strictResult.Errors, e => e.Category == ValidationCategory.AlarmTriggerReference);
|
|
Assert.DoesNotContain(strictResult.Warnings, w => w.Category == ValidationCategory.AlarmTriggerReference);
|
|
}
|
|
|
|
/// <summary>Verifies that skipping compilation skips the expression trigger syntax check.</summary>
|
|
[Fact]
|
|
public void Validate_SkipCompilation_SkipsExpressionTriggerSyntaxCheck()
|
|
{
|
|
var config = ConfigWithExpressionTriggerScript("this is not C# ((");
|
|
var gated = new ValidationService().Validate(config, validateScriptCompilation: false);
|
|
Assert.DoesNotContain(gated.Errors, e => e.Message.Contains("failed validation")); // FAILS today
|
|
var full = new ValidationService().Validate(config);
|
|
Assert.Contains(full.Errors, e => e.Message.Contains("failed validation")); // deploy gate unchanged
|
|
}
|
|
|
|
/// <summary>Verifies that skipping compilation still checks the expression attribute references.</summary>
|
|
[Fact]
|
|
public void Validate_SkipCompilation_StillChecksExpressionAttributeReferences()
|
|
{
|
|
// expression "Attributes[\"Ghost\"] != null" referencing a missing attribute:
|
|
// the reference scan is cheap string work and MUST survive the gate.
|
|
var config = ConfigWithExpressionTriggerScript("Attributes[\"Ghost\"] != null");
|
|
var gated = new ValidationService().Validate(config, validateScriptCompilation: false);
|
|
Assert.Contains(gated.Errors, e => e.Message.Contains("Ghost"));
|
|
}
|
|
|
|
private static FlattenedConfiguration ConfigWithExpressionTriggerScript(string expression)
|
|
{
|
|
var json = System.Text.Json.JsonSerializer.Serialize(new { expression });
|
|
return new FlattenedConfiguration
|
|
{
|
|
InstanceUniqueName = "Instance1",
|
|
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double" }],
|
|
Scripts =
|
|
[
|
|
new ResolvedScript
|
|
{
|
|
CanonicalName = "OnExpr",
|
|
TriggerType = "Expression",
|
|
TriggerConfiguration = json
|
|
}
|
|
]
|
|
};
|
|
}
|
|
}
|