refactor: rename ScadaLink → ZB.MOM.WW.ScadaBridge (code + projects + namespaces)
Solution + 23 src projects + 26 test projects renamed; folders, csproj, namespaces, and ScadaLinkDbContext/ScadaBridgeDbContext class updated. ActorSystem "scadalink" → "scadabridge", Akka seed-node URLs migrated. SQL roles/logins, LDAP domains, CLI command name, and CLI config dir (~/.scadalink → ~/.scadabridge) also renamed. Build green; 5 Host.Tests fail awaiting SQL login rename in next commit. Pre-existing StaleTagMonitor timing flakes unchanged. Rename script committed at tools/rename-to-scadabridge.sh.
This commit is contained in:
+118
@@ -0,0 +1,118 @@
|
||||
using System.Text.Json;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Flattening;
|
||||
|
||||
public class DeploymentPackageTests
|
||||
{
|
||||
[Fact]
|
||||
public void DeploymentPackage_JsonSerializable()
|
||||
{
|
||||
var package = new DeploymentPackage
|
||||
{
|
||||
InstanceUniqueName = "PumpStation1",
|
||||
DeploymentId = "dep-abc123",
|
||||
RevisionHash = "sha256:abcdef1234567890",
|
||||
DeployedBy = "admin@company.com",
|
||||
DeployedAtUtc = new DateTimeOffset(2026, 3, 16, 12, 0, 0, TimeSpan.Zero),
|
||||
Configuration = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "PumpStation1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute
|
||||
{
|
||||
CanonicalName = "Temperature",
|
||||
Value = "25.0",
|
||||
DataType = "Double",
|
||||
BoundDataConnectionId = 100,
|
||||
BoundDataConnectionName = "OPC-Server1",
|
||||
BoundDataConnectionProtocol = "OpcUa",
|
||||
DataSourceReference = "ns=2;s=Temp"
|
||||
}
|
||||
],
|
||||
Alarms =
|
||||
[
|
||||
new ResolvedAlarm
|
||||
{
|
||||
CanonicalName = "HighTemp",
|
||||
TriggerType = "RangeViolation",
|
||||
PriorityLevel = 1
|
||||
}
|
||||
],
|
||||
Scripts =
|
||||
[
|
||||
new ResolvedScript
|
||||
{
|
||||
CanonicalName = "Monitor",
|
||||
Code = "var x = Attributes[\"Temperature\"].Value;"
|
||||
}
|
||||
]
|
||||
},
|
||||
PreviousRevisionHash = null
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(package);
|
||||
Assert.NotNull(json);
|
||||
Assert.Contains("PumpStation1", json);
|
||||
Assert.Contains("sha256:abcdef1234567890", json);
|
||||
|
||||
var deserialized = JsonSerializer.Deserialize<DeploymentPackage>(json);
|
||||
Assert.NotNull(deserialized);
|
||||
Assert.Equal("PumpStation1", deserialized.InstanceUniqueName);
|
||||
Assert.Equal("dep-abc123", deserialized.DeploymentId);
|
||||
Assert.Single(deserialized.Configuration.Attributes);
|
||||
Assert.Equal("Temperature", deserialized.Configuration.Attributes[0].CanonicalName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeploymentPackage_WithDiff_Serializable()
|
||||
{
|
||||
var package = new DeploymentPackage
|
||||
{
|
||||
InstanceUniqueName = "Inst1",
|
||||
DeploymentId = "dep-1",
|
||||
RevisionHash = "sha256:new",
|
||||
DeployedBy = "admin",
|
||||
DeployedAtUtc = DateTimeOffset.UtcNow,
|
||||
Configuration = new FlattenedConfiguration { InstanceUniqueName = "Inst1" },
|
||||
Diff = new ConfigurationDiff
|
||||
{
|
||||
InstanceUniqueName = "Inst1",
|
||||
OldRevisionHash = "sha256:old",
|
||||
NewRevisionHash = "sha256:new",
|
||||
AttributeChanges =
|
||||
[
|
||||
new DiffEntry<ResolvedAttribute>
|
||||
{
|
||||
CanonicalName = "Temp",
|
||||
ChangeType = DiffChangeType.Changed,
|
||||
OldValue = new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double" },
|
||||
NewValue = new ResolvedAttribute { CanonicalName = "Temp", Value = "50", DataType = "Double" }
|
||||
}
|
||||
]
|
||||
},
|
||||
PreviousRevisionHash = "sha256:old"
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(package);
|
||||
var deserialized = JsonSerializer.Deserialize<DeploymentPackage>(json);
|
||||
|
||||
Assert.NotNull(deserialized?.Diff);
|
||||
Assert.True(deserialized.Diff.HasChanges);
|
||||
Assert.Equal("sha256:old", deserialized.PreviousRevisionHash);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FlattenedConfiguration_DefaultValues()
|
||||
{
|
||||
var config = new FlattenedConfiguration();
|
||||
|
||||
Assert.Equal(string.Empty, config.InstanceUniqueName);
|
||||
Assert.Empty(config.Attributes);
|
||||
Assert.Empty(config.Alarms);
|
||||
Assert.Empty(config.Scripts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
||||
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Flattening;
|
||||
|
||||
public class DiffServiceTests
|
||||
{
|
||||
private readonly DiffService _sut = new();
|
||||
|
||||
[Fact]
|
||||
public void ComputeDiff_NullOldConfig_AllAdded()
|
||||
{
|
||||
var newConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double" },
|
||||
new ResolvedAttribute { CanonicalName = "Status", Value = "OK", DataType = "String" }
|
||||
],
|
||||
Alarms =
|
||||
[
|
||||
new ResolvedAlarm { CanonicalName = "HighTemp", TriggerType = "RangeViolation" }
|
||||
],
|
||||
Scripts =
|
||||
[
|
||||
new ResolvedScript { CanonicalName = "Monitor", Code = "// code" }
|
||||
]
|
||||
};
|
||||
|
||||
var diff = _sut.ComputeDiff(null, newConfig);
|
||||
|
||||
Assert.True(diff.HasChanges);
|
||||
Assert.Equal(2, diff.AttributeChanges.Count);
|
||||
Assert.All(diff.AttributeChanges, c => Assert.Equal(DiffChangeType.Added, c.ChangeType));
|
||||
Assert.Single(diff.AlarmChanges);
|
||||
Assert.Single(diff.ScriptChanges);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeDiff_IdenticalConfigs_NoChanges()
|
||||
{
|
||||
var config = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double" }],
|
||||
Alarms = [],
|
||||
Scripts = []
|
||||
};
|
||||
|
||||
var diff = _sut.ComputeDiff(config, config);
|
||||
|
||||
Assert.False(diff.HasChanges);
|
||||
Assert.Empty(diff.AttributeChanges);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeDiff_AttributeRemoved_DetectedAsRemoved()
|
||||
{
|
||||
var oldConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double" },
|
||||
new ResolvedAttribute { CanonicalName = "Removed", Value = "x", DataType = "String" }
|
||||
]
|
||||
};
|
||||
var newConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double" }]
|
||||
};
|
||||
|
||||
var diff = _sut.ComputeDiff(oldConfig, newConfig);
|
||||
|
||||
Assert.True(diff.HasChanges);
|
||||
Assert.Single(diff.AttributeChanges);
|
||||
Assert.Equal(DiffChangeType.Removed, diff.AttributeChanges[0].ChangeType);
|
||||
Assert.Equal("Removed", diff.AttributeChanges[0].CanonicalName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeDiff_AttributeChanged_DetectedAsChanged()
|
||||
{
|
||||
var oldConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double" }]
|
||||
};
|
||||
var newConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes = [new ResolvedAttribute { CanonicalName = "Temp", Value = "50", DataType = "Double" }]
|
||||
};
|
||||
|
||||
var diff = _sut.ComputeDiff(oldConfig, newConfig);
|
||||
|
||||
Assert.True(diff.HasChanges);
|
||||
Assert.Single(diff.AttributeChanges);
|
||||
Assert.Equal(DiffChangeType.Changed, diff.AttributeChanges[0].ChangeType);
|
||||
Assert.Equal("25", diff.AttributeChanges[0].OldValue?.Value);
|
||||
Assert.Equal("50", diff.AttributeChanges[0].NewValue?.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeDiff_RevisionHashes_Included()
|
||||
{
|
||||
var config = new FlattenedConfiguration { InstanceUniqueName = "Instance1" };
|
||||
var diff = _sut.ComputeDiff(config, config, "sha256:old", "sha256:new");
|
||||
|
||||
Assert.Equal("sha256:old", diff.OldRevisionHash);
|
||||
Assert.Equal("sha256:new", diff.NewRevisionHash);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeDiff_ScriptCodeChange_Detected()
|
||||
{
|
||||
var oldConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Scripts = [new ResolvedScript { CanonicalName = "Script1", Code = "// v1" }]
|
||||
};
|
||||
var newConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Scripts = [new ResolvedScript { CanonicalName = "Script1", Code = "// v2" }]
|
||||
};
|
||||
|
||||
var diff = _sut.ComputeDiff(oldConfig, newConfig);
|
||||
|
||||
Assert.True(diff.HasChanges);
|
||||
Assert.Single(diff.ScriptChanges);
|
||||
Assert.Equal(DiffChangeType.Changed, diff.ScriptChanges[0].ChangeType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeDiff_AttributeDescriptionChange_DetectedAsChanged()
|
||||
{
|
||||
// TemplateEngine-017: AttributesEqual must compare Description.
|
||||
var oldConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double", Description = "Original" }
|
||||
]
|
||||
};
|
||||
var newConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute { CanonicalName = "Temp", Value = "25", DataType = "Double", Description = "Updated" }
|
||||
]
|
||||
};
|
||||
|
||||
var diff = _sut.ComputeDiff(oldConfig, newConfig);
|
||||
|
||||
Assert.True(diff.HasChanges);
|
||||
Assert.Single(diff.AttributeChanges);
|
||||
Assert.Equal(DiffChangeType.Changed, diff.AttributeChanges[0].ChangeType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeDiff_AlarmDescriptionChange_DetectedAsChanged()
|
||||
{
|
||||
// TemplateEngine-017: AlarmsEqual must compare Description.
|
||||
var oldConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Alarms =
|
||||
[
|
||||
new ResolvedAlarm { CanonicalName = "HighTemp", TriggerType = "RangeViolation", Description = "Original" }
|
||||
]
|
||||
};
|
||||
var newConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Alarms =
|
||||
[
|
||||
new ResolvedAlarm { CanonicalName = "HighTemp", TriggerType = "RangeViolation", Description = "Updated" }
|
||||
]
|
||||
};
|
||||
|
||||
var diff = _sut.ComputeDiff(oldConfig, newConfig);
|
||||
|
||||
Assert.True(diff.HasChanges);
|
||||
Assert.Single(diff.AlarmChanges);
|
||||
Assert.Equal(DiffChangeType.Changed, diff.AlarmChanges[0].ChangeType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConnectionsEqual_IdenticalConfigs_ReturnsTrue()
|
||||
{
|
||||
// TemplateEngine-017: ConnectionsEqual is the comparator callers use
|
||||
// to detect connection-endpoint drift (the diff-view extension that
|
||||
// surfaces this in the UI is tracked under TemplateEngine-018).
|
||||
var a = new ConnectionConfig
|
||||
{
|
||||
Protocol = "OpcUa",
|
||||
ConfigurationJson = "{\"endpoint\":\"opc.tcp://host-a\"}",
|
||||
BackupConfigurationJson = "{\"endpoint\":\"opc.tcp://host-b\"}",
|
||||
FailoverRetryCount = 3
|
||||
};
|
||||
var b = a with { };
|
||||
|
||||
Assert.True(DiffService.ConnectionsEqual(a, b));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConnectionsEqual_EndpointEdit_ReturnsFalse()
|
||||
{
|
||||
// TemplateEngine-017: primary endpoint JSON edit must surface as a
|
||||
// change. Without this, deployment redeploys ship a different
|
||||
// ConnectionConfig with no visible drift signal.
|
||||
var a = new ConnectionConfig
|
||||
{
|
||||
Protocol = "OpcUa",
|
||||
ConfigurationJson = "{\"endpoint\":\"opc.tcp://host-a:4840\"}",
|
||||
FailoverRetryCount = 3
|
||||
};
|
||||
var b = a with { ConfigurationJson = "{\"endpoint\":\"opc.tcp://host-b:4840\"}" };
|
||||
|
||||
Assert.False(DiffService.ConnectionsEqual(a, b));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConnectionsEqual_BackupConfigurationEdit_ReturnsFalse()
|
||||
{
|
||||
var a = new ConnectionConfig { Protocol = "OpcUa", ConfigurationJson = "{}", BackupConfigurationJson = null, FailoverRetryCount = 3 };
|
||||
var b = a with { BackupConfigurationJson = "{\"endpoint\":\"opc.tcp://backup\"}" };
|
||||
|
||||
Assert.False(DiffService.ConnectionsEqual(a, b));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConnectionsEqual_FailoverRetryCountEdit_ReturnsFalse()
|
||||
{
|
||||
var a = new ConnectionConfig { Protocol = "OpcUa", ConfigurationJson = "{}", FailoverRetryCount = 3 };
|
||||
var b = a with { FailoverRetryCount = 5 };
|
||||
|
||||
Assert.False(DiffService.ConnectionsEqual(a, b));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConnectionsEqual_ProtocolEdit_ReturnsFalse()
|
||||
{
|
||||
var a = new ConnectionConfig { Protocol = "OpcUa", ConfigurationJson = "{}", FailoverRetryCount = 3 };
|
||||
var b = a with { Protocol = "Modbus" };
|
||||
|
||||
Assert.False(DiffService.ConnectionsEqual(a, b));
|
||||
}
|
||||
|
||||
// ── TemplateEngine-018: ComputeConnectionsDiff produces Added/Removed/Changed entries ──
|
||||
|
||||
[Fact]
|
||||
public void ComputeConnectionsDiff_NewBindingAdded_ReportedAsAdded()
|
||||
{
|
||||
// First-time binding (or instance gains its first data-sourced
|
||||
// attribute) — old config has no Connections map, new config does.
|
||||
// The pre-018 diff shape silently dropped this so operators saw
|
||||
// "no changes" when the deployment package was structurally larger.
|
||||
var oldConfig = new FlattenedConfiguration { InstanceUniqueName = "Instance1" };
|
||||
var newConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Connections = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["plc1"] = new ConnectionConfig
|
||||
{
|
||||
Protocol = "OpcUa",
|
||||
ConfigurationJson = "{\"endpoint\":\"opc.tcp://host\"}",
|
||||
FailoverRetryCount = 3,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var diff = _sut.ComputeConnectionsDiff(oldConfig, newConfig);
|
||||
|
||||
Assert.Single(diff);
|
||||
Assert.Equal("plc1", diff[0].CanonicalName);
|
||||
Assert.Equal(DiffChangeType.Added, diff[0].ChangeType);
|
||||
Assert.Null(diff[0].OldValue);
|
||||
Assert.NotNull(diff[0].NewValue);
|
||||
Assert.Equal("OpcUa", diff[0].NewValue!.Protocol);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeConnectionsDiff_BindingCleared_ReportedAsRemoved()
|
||||
{
|
||||
// Last data-sourced attribute removed — old config carried a
|
||||
// connection, new config does not.
|
||||
var oldConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Connections = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["plc1"] = new ConnectionConfig { Protocol = "OpcUa", ConfigurationJson = "{}" }
|
||||
}
|
||||
};
|
||||
var newConfig = new FlattenedConfiguration { InstanceUniqueName = "Instance1" };
|
||||
|
||||
var diff = _sut.ComputeConnectionsDiff(oldConfig, newConfig);
|
||||
|
||||
Assert.Single(diff);
|
||||
Assert.Equal("plc1", diff[0].CanonicalName);
|
||||
Assert.Equal(DiffChangeType.Removed, diff[0].ChangeType);
|
||||
Assert.NotNull(diff[0].OldValue);
|
||||
Assert.Null(diff[0].NewValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeConnectionsDiff_EndpointEdit_ReportedAsChanged()
|
||||
{
|
||||
// A connection-endpoint edit must surface as a Changed diff entry —
|
||||
// the deployment package will ship a different ConnectionConfig and
|
||||
// the operator-facing diff view must say so.
|
||||
var oldConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Connections = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["plc1"] = new ConnectionConfig
|
||||
{
|
||||
Protocol = "OpcUa",
|
||||
ConfigurationJson = "{\"endpoint\":\"opc.tcp://host-a:4840\"}",
|
||||
FailoverRetryCount = 3,
|
||||
}
|
||||
}
|
||||
};
|
||||
var newConfig = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
Connections = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["plc1"] = new ConnectionConfig
|
||||
{
|
||||
Protocol = "OpcUa",
|
||||
ConfigurationJson = "{\"endpoint\":\"opc.tcp://host-b:4840\"}",
|
||||
FailoverRetryCount = 3,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var diff = _sut.ComputeConnectionsDiff(oldConfig, newConfig);
|
||||
|
||||
Assert.Single(diff);
|
||||
Assert.Equal("plc1", diff[0].CanonicalName);
|
||||
Assert.Equal(DiffChangeType.Changed, diff[0].ChangeType);
|
||||
Assert.Contains("host-a", diff[0].OldValue!.ConfigurationJson);
|
||||
Assert.Contains("host-b", diff[0].NewValue!.ConfigurationJson);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeConnectionsDiff_IdenticalConnections_NoEntries()
|
||||
{
|
||||
// Sanity check: an unchanged connection produces no diff entry, so
|
||||
// ComputeConnectionsDiff stays quiet when nothing relevant has
|
||||
// changed.
|
||||
var connections = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["plc1"] = new ConnectionConfig { Protocol = "OpcUa", ConfigurationJson = "{}" }
|
||||
};
|
||||
var oldConfig = new FlattenedConfiguration { InstanceUniqueName = "Instance1", Connections = connections };
|
||||
var newConfig = new FlattenedConfiguration { InstanceUniqueName = "Instance1", Connections = connections };
|
||||
|
||||
var diff = _sut.ComputeConnectionsDiff(oldConfig, newConfig);
|
||||
|
||||
Assert.Empty(diff);
|
||||
}
|
||||
}
|
||||
+349
@@ -0,0 +1,349 @@
|
||||
using System.Text.Json;
|
||||
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.Types.Enums;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
||||
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Flattening;
|
||||
|
||||
public class FlatteningServiceMergeTests
|
||||
{
|
||||
// ── MergeHiLoConfig ────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void MergeHiLoConfig_DerivedKeysWin_InheritedKeysSurvive()
|
||||
{
|
||||
const string inherited = @"{""attributeName"":""Temp"",""loLo"":0,""lo"":10,""hi"":80,""hiHi"":100}";
|
||||
const string derived = @"{""hi"":90}"; // derived only overrides Hi
|
||||
|
||||
var result = FlatteningService.MergeHiLoConfig(inherited, derived);
|
||||
|
||||
Assert.NotNull(result);
|
||||
using var doc = JsonDocument.Parse(result!);
|
||||
Assert.Equal("Temp", doc.RootElement.GetProperty("attributeName").GetString());
|
||||
Assert.Equal(0, doc.RootElement.GetProperty("loLo").GetDouble());
|
||||
Assert.Equal(10, doc.RootElement.GetProperty("lo").GetDouble());
|
||||
Assert.Equal(90, doc.RootElement.GetProperty("hi").GetDouble()); // overridden
|
||||
Assert.Equal(100, doc.RootElement.GetProperty("hiHi").GetDouble()); // inherited
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MergeHiLoConfig_DerivedCanOverrideAttribute()
|
||||
{
|
||||
const string inherited = @"{""attributeName"":""Temp"",""hi"":80}";
|
||||
const string derived = @"{""attributeName"":""Pressure"",""hi"":50}";
|
||||
|
||||
var result = FlatteningService.MergeHiLoConfig(inherited, derived);
|
||||
|
||||
using var doc = JsonDocument.Parse(result!);
|
||||
Assert.Equal("Pressure", doc.RootElement.GetProperty("attributeName").GetString());
|
||||
Assert.Equal(50, doc.RootElement.GetProperty("hi").GetDouble());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MergeHiLoConfig_DerivedNull_ReturnsInherited()
|
||||
{
|
||||
const string inherited = @"{""hi"":80}";
|
||||
|
||||
var result = FlatteningService.MergeHiLoConfig(inherited, null);
|
||||
|
||||
Assert.Equal(inherited, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MergeHiLoConfig_InheritedNull_ReturnsDerived()
|
||||
{
|
||||
const string derived = @"{""hi"":80}";
|
||||
|
||||
var result = FlatteningService.MergeHiLoConfig(null, derived);
|
||||
|
||||
Assert.Equal(derived, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MergeHiLoConfig_BothNull_ReturnsNull()
|
||||
{
|
||||
Assert.Null(FlatteningService.MergeHiLoConfig(null, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MergeHiLoConfig_MalformedInherited_FallsBackToDerived()
|
||||
{
|
||||
// Safe fallback — never throw on malformed input.
|
||||
const string derived = @"{""hi"":80}";
|
||||
|
||||
var result = FlatteningService.MergeHiLoConfig("{not valid", derived);
|
||||
|
||||
Assert.Equal(derived, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MergeHiLoConfig_DerivedAddsNewKey_PreservesInheritedRest()
|
||||
{
|
||||
// Derived adds a deadband that the base didn't have.
|
||||
const string inherited = @"{""hi"":80,""hiHi"":100}";
|
||||
const string derived = @"{""hiDeadband"":3}";
|
||||
|
||||
var result = FlatteningService.MergeHiLoConfig(inherited, derived);
|
||||
|
||||
using var doc = JsonDocument.Parse(result!);
|
||||
Assert.Equal(80, doc.RootElement.GetProperty("hi").GetDouble());
|
||||
Assert.Equal(100, doc.RootElement.GetProperty("hiHi").GetDouble());
|
||||
Assert.Equal(3, doc.RootElement.GetProperty("hiDeadband").GetDouble());
|
||||
}
|
||||
|
||||
// ── DiffHiLoConfig ─────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void DiffHiLoConfig_NoChanges_ReturnsNull()
|
||||
{
|
||||
const string both = @"{""attributeName"":""Temp"",""hi"":80}";
|
||||
Assert.Null(FlatteningService.DiffHiLoConfig(both, both));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiffHiLoConfig_ChangedKey_ReturnsOnlyChangedKey()
|
||||
{
|
||||
const string inherited = @"{""attributeName"":""Temp"",""loLo"":0,""lo"":10,""hi"":80,""hiHi"":100}";
|
||||
const string edited = @"{""attributeName"":""Temp"",""loLo"":0,""lo"":10,""hi"":90,""hiHi"":100}";
|
||||
|
||||
var diff = FlatteningService.DiffHiLoConfig(inherited, edited);
|
||||
|
||||
Assert.NotNull(diff);
|
||||
using var doc = JsonDocument.Parse(diff!);
|
||||
var prop = Assert.Single(doc.RootElement.EnumerateObject());
|
||||
Assert.Equal("hi", prop.Name);
|
||||
Assert.Equal(90, prop.Value.GetDouble());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiffHiLoConfig_NewKey_AddedToDiff()
|
||||
{
|
||||
const string inherited = @"{""attributeName"":""Temp"",""hi"":80}";
|
||||
const string edited = @"{""attributeName"":""Temp"",""hi"":80,""hiDeadband"":3}";
|
||||
|
||||
var diff = FlatteningService.DiffHiLoConfig(inherited, edited);
|
||||
|
||||
Assert.NotNull(diff);
|
||||
using var doc = JsonDocument.Parse(diff!);
|
||||
Assert.Equal(3, doc.RootElement.GetProperty("hiDeadband").GetDouble());
|
||||
Assert.False(doc.RootElement.TryGetProperty("hi", out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiffHiLoConfig_NullInherited_ReturnsEditedVerbatim()
|
||||
{
|
||||
const string edited = @"{""attributeName"":""Temp"",""hi"":80}";
|
||||
Assert.Equal(edited, FlatteningService.DiffHiLoConfig(null, edited));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiffHiLoConfig_NullEdited_ReturnsNull()
|
||||
{
|
||||
Assert.Null(FlatteningService.DiffHiLoConfig(@"{""hi"":80}", null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiffHiLoConfig_IgnoresStringEscapeDifferences()
|
||||
{
|
||||
// Inherited has literal em-dash; edited has the unicode-escaped form.
|
||||
// Decoded values are identical, so the key should NOT be in the diff.
|
||||
const string inherited = @"{""attributeName"":""Temp"",""hi"":80,""hiMessage"":""High — investigate""}";
|
||||
const string edited = @"{""attributeName"":""Temp"",""hi"":80,""hiMessage"":""High — investigate""}";
|
||||
|
||||
var diff = FlatteningService.DiffHiLoConfig(inherited, edited);
|
||||
|
||||
Assert.Null(diff); // no real change once values are decoded
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiffHiLoConfig_IgnoresNumericFormatDifferences()
|
||||
{
|
||||
// 85 vs 85.0 are the same number — should not produce a diff.
|
||||
const string inherited = @"{""hi"":85}";
|
||||
const string edited = @"{""hi"":85.0}";
|
||||
Assert.Null(FlatteningService.DiffHiLoConfig(inherited, edited));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiffHiLoConfig_RoundTripsThroughMerge()
|
||||
{
|
||||
// Merge(inherited, Diff(inherited, edited)) ≡ edited — when the
|
||||
// edited config is itself a superset/equivalent of inherited.
|
||||
const string inherited = @"{""attributeName"":""Temp"",""hi"":80,""hiHi"":100}";
|
||||
const string edited = @"{""attributeName"":""Temp"",""hi"":90,""hiHi"":100,""hiDeadband"":5}";
|
||||
|
||||
var diff = FlatteningService.DiffHiLoConfig(inherited, edited);
|
||||
var merged = FlatteningService.MergeHiLoConfig(inherited, diff);
|
||||
|
||||
using var origDoc = JsonDocument.Parse(edited);
|
||||
using var mergedDoc = JsonDocument.Parse(merged!);
|
||||
Assert.Equal(origDoc.RootElement.GetProperty("hi").GetDouble(),
|
||||
mergedDoc.RootElement.GetProperty("hi").GetDouble());
|
||||
Assert.Equal(origDoc.RootElement.GetProperty("hiHi").GetDouble(),
|
||||
mergedDoc.RootElement.GetProperty("hiHi").GetDouble());
|
||||
Assert.Equal(origDoc.RootElement.GetProperty("hiDeadband").GetDouble(),
|
||||
mergedDoc.RootElement.GetProperty("hiDeadband").GetDouble());
|
||||
}
|
||||
|
||||
// ── Instance-level alarm override (end-to-end Flatten) ─────────────────
|
||||
|
||||
private static (Template, Instance) BuildHiLoFixture(string inheritedJson, InstanceAlarmOverride? ovr = null, bool locked = false)
|
||||
{
|
||||
var template = new Template("PumpTpl")
|
||||
{
|
||||
Id = 1,
|
||||
Alarms = new List<TemplateAlarm>
|
||||
{
|
||||
new("Temp")
|
||||
{
|
||||
Id = 10,
|
||||
TemplateId = 1,
|
||||
TriggerType = AlarmTriggerType.HiLo,
|
||||
TriggerConfiguration = inheritedJson,
|
||||
PriorityLevel = 500,
|
||||
IsLocked = locked
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var instance = new Instance("Pump-1") { Id = 100, TemplateId = 1, SiteId = 1 };
|
||||
if (ovr != null) instance.AlarmOverrides.Add(ovr);
|
||||
return (template, instance);
|
||||
}
|
||||
|
||||
private static FlattenedConfiguration Flatten(Template template, Instance instance)
|
||||
{
|
||||
var sut = new FlatteningService();
|
||||
var result = sut.Flatten(
|
||||
instance,
|
||||
new[] { template },
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
if (!result.IsSuccess) Assert.Fail(result.Error);
|
||||
return result.Value!;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InstanceAlarmOverride_HiLo_MergesSetpoints()
|
||||
{
|
||||
// Template has {hi=80, hiHi=100, lo=10, loLo=0}. Instance overrides hi=90 only.
|
||||
var (tpl, inst) = BuildHiLoFixture(
|
||||
@"{""attributeName"":""Temp"",""loLo"":0,""lo"":10,""hi"":80,""hiHi"":100}",
|
||||
new InstanceAlarmOverride("Temp")
|
||||
{
|
||||
InstanceId = 100,
|
||||
TriggerConfigurationOverride = @"{""hi"":90}"
|
||||
});
|
||||
|
||||
var flat = Flatten(tpl, inst);
|
||||
var alarm = flat.Alarms.Single();
|
||||
|
||||
using var doc = JsonDocument.Parse(alarm.TriggerConfiguration!);
|
||||
Assert.Equal(0, doc.RootElement.GetProperty("loLo").GetDouble());
|
||||
Assert.Equal(10, doc.RootElement.GetProperty("lo").GetDouble());
|
||||
Assert.Equal(90, doc.RootElement.GetProperty("hi").GetDouble()); // overridden
|
||||
Assert.Equal(100, doc.RootElement.GetProperty("hiHi").GetDouble()); // inherited
|
||||
Assert.Equal("Override", alarm.Source);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InstanceAlarmOverride_OverridesPriority()
|
||||
{
|
||||
var (tpl, inst) = BuildHiLoFixture(
|
||||
@"{""attributeName"":""Temp"",""hi"":80}",
|
||||
new InstanceAlarmOverride("Temp")
|
||||
{
|
||||
InstanceId = 100,
|
||||
PriorityLevelOverride = 950
|
||||
});
|
||||
|
||||
var flat = Flatten(tpl, inst);
|
||||
var alarm = flat.Alarms.Single();
|
||||
|
||||
Assert.Equal(950, alarm.PriorityLevel);
|
||||
Assert.Equal("Override", alarm.Source);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InstanceAlarmOverride_LockedAlarm_OverrideSilentlyIgnored()
|
||||
{
|
||||
// Locked alarm — override should be a no-op at flatten time. (The
|
||||
// InstanceService.SetAlarmOverrideAsync write-time check is what
|
||||
// prevents the override from being persisted in the first place;
|
||||
// this test covers the runtime safety net.)
|
||||
var (tpl, inst) = BuildHiLoFixture(
|
||||
@"{""attributeName"":""Temp"",""hi"":80}",
|
||||
new InstanceAlarmOverride("Temp")
|
||||
{
|
||||
InstanceId = 100,
|
||||
TriggerConfigurationOverride = @"{""hi"":999}"
|
||||
},
|
||||
locked: true);
|
||||
|
||||
var flat = Flatten(tpl, inst);
|
||||
var alarm = flat.Alarms.Single();
|
||||
|
||||
using var doc = JsonDocument.Parse(alarm.TriggerConfiguration!);
|
||||
Assert.Equal(80, doc.RootElement.GetProperty("hi").GetDouble()); // not overridden
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InstanceAlarmOverride_UnknownName_DoesNothing()
|
||||
{
|
||||
// Override targets an alarm name that doesn't exist on the template —
|
||||
// silently ignored (same behavior as attribute overrides).
|
||||
var (tpl, inst) = BuildHiLoFixture(
|
||||
@"{""attributeName"":""Temp"",""hi"":80}",
|
||||
new InstanceAlarmOverride("DoesNotExist")
|
||||
{
|
||||
InstanceId = 100,
|
||||
TriggerConfigurationOverride = @"{""hi"":999}"
|
||||
});
|
||||
|
||||
var flat = Flatten(tpl, inst);
|
||||
var alarm = flat.Alarms.Single();
|
||||
|
||||
using var doc = JsonDocument.Parse(alarm.TriggerConfiguration!);
|
||||
Assert.Equal(80, doc.RootElement.GetProperty("hi").GetDouble());
|
||||
Assert.NotEqual("Override", alarm.Source);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InstanceAlarmOverride_BinaryTrigger_ReplacesWholeConfig()
|
||||
{
|
||||
// For non-HiLo trigger types, an instance override replaces the whole
|
||||
// TriggerConfiguration (no per-key merge).
|
||||
var template = new Template("PumpTpl")
|
||||
{
|
||||
Id = 1,
|
||||
Alarms = new List<TemplateAlarm>
|
||||
{
|
||||
new("Temp")
|
||||
{
|
||||
Id = 10,
|
||||
TemplateId = 1,
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = @"{""attributeName"":""T"",""min"":0,""max"":100}",
|
||||
PriorityLevel = 500
|
||||
}
|
||||
}
|
||||
};
|
||||
var instance = new Instance("Pump-1") { Id = 100, TemplateId = 1, SiteId = 1 };
|
||||
instance.AlarmOverrides.Add(new InstanceAlarmOverride("Temp")
|
||||
{
|
||||
InstanceId = 100,
|
||||
TriggerConfigurationOverride = @"{""attributeName"":""T"",""min"":-50,""max"":50}"
|
||||
});
|
||||
|
||||
var flat = Flatten(template, instance);
|
||||
var alarm = flat.Alarms.Single();
|
||||
|
||||
using var doc = JsonDocument.Parse(alarm.TriggerConfiguration!);
|
||||
Assert.Equal(-50, doc.RootElement.GetProperty("min").GetDouble());
|
||||
Assert.Equal(50, doc.RootElement.GetProperty("max").GetDouble());
|
||||
}
|
||||
}
|
||||
+669
@@ -0,0 +1,669 @@
|
||||
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.Types.Enums;
|
||||
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Flattening;
|
||||
|
||||
public class FlatteningServiceTests
|
||||
{
|
||||
private readonly FlatteningService _sut = new();
|
||||
|
||||
private static Instance CreateInstance(string name = "TestInstance", int templateId = 1, int siteId = 1) =>
|
||||
new(name) { Id = 1, TemplateId = templateId, SiteId = siteId };
|
||||
|
||||
private static Template CreateTemplate(int id, string name, int? parentId = null)
|
||||
{
|
||||
var t = new Template(name) { Id = id, ParentTemplateId = parentId };
|
||||
return t;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_EmptyTemplateChain_ReturnsFailure()
|
||||
{
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("empty", result.Error, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_SingleTemplate_ResolvesAttributes()
|
||||
{
|
||||
var template = CreateTemplate(1, "Base");
|
||||
template.Attributes.Add(new TemplateAttribute("Temperature") { DataType = DataType.Double, Value = "25.0" });
|
||||
template.Attributes.Add(new TemplateAttribute("Status") { DataType = DataType.String, Value = "OK" });
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[template],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(2, result.Value.Attributes.Count);
|
||||
Assert.Equal("Temperature", result.Value.Attributes[1].CanonicalName); // Sorted
|
||||
Assert.Equal("25.0", result.Value.Attributes[1].Value);
|
||||
Assert.Equal("Status", result.Value.Attributes[0].CanonicalName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InheritanceChain_DerivedOverridesBase()
|
||||
{
|
||||
var baseTemplate = CreateTemplate(2, "Base");
|
||||
baseTemplate.Attributes.Add(new TemplateAttribute("Speed") { DataType = DataType.Double, Value = "100.0" });
|
||||
baseTemplate.Attributes.Add(new TemplateAttribute("BaseOnly") { DataType = DataType.String, Value = "base" });
|
||||
|
||||
var childTemplate = CreateTemplate(1, "Child", parentId: 2);
|
||||
childTemplate.Attributes.Add(new TemplateAttribute("Speed") { DataType = DataType.Double, Value = "200.0" });
|
||||
childTemplate.Attributes.Add(new TemplateAttribute("ChildOnly") { DataType = DataType.Int32, Value = "42" });
|
||||
|
||||
// Chain: [child, base] — most-derived first
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[childTemplate, baseTemplate],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(3, result.Value.Attributes.Count);
|
||||
|
||||
var speed = result.Value.Attributes.First(a => a.CanonicalName == "Speed");
|
||||
Assert.Equal("200.0", speed.Value); // Child's value wins
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_LockedAttribute_NotOverriddenByDerived()
|
||||
{
|
||||
var baseTemplate = CreateTemplate(2, "Base");
|
||||
baseTemplate.Attributes.Add(new TemplateAttribute("Locked") { DataType = DataType.String, Value = "locked", IsLocked = true });
|
||||
|
||||
var childTemplate = CreateTemplate(1, "Child", parentId: 2);
|
||||
childTemplate.Attributes.Add(new TemplateAttribute("Locked") { DataType = DataType.String, Value = "overridden" });
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[childTemplate, baseTemplate],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var locked = result.Value.Attributes.First(a => a.CanonicalName == "Locked");
|
||||
Assert.Equal("locked", locked.Value); // Base locked value preserved
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InstanceOverride_AppliedToUnlockedAttribute()
|
||||
{
|
||||
var template = CreateTemplate(1, "Base");
|
||||
template.Attributes.Add(new TemplateAttribute("Threshold") { DataType = DataType.Double, Value = "50.0" });
|
||||
|
||||
var instance = CreateInstance();
|
||||
instance.AttributeOverrides.Add(new InstanceAttributeOverride("Threshold") { OverrideValue = "75.0" });
|
||||
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[template],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var attr = result.Value.Attributes.First(a => a.CanonicalName == "Threshold");
|
||||
Assert.Equal("75.0", attr.Value);
|
||||
Assert.Equal("Override", attr.Source);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InstanceOverride_SkippedForLockedAttribute()
|
||||
{
|
||||
var template = CreateTemplate(1, "Base");
|
||||
template.Attributes.Add(new TemplateAttribute("Locked") { DataType = DataType.String, Value = "original", IsLocked = true });
|
||||
|
||||
var instance = CreateInstance();
|
||||
instance.AttributeOverrides.Add(new InstanceAttributeOverride("Locked") { OverrideValue = "changed" });
|
||||
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[template],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var attr = result.Value.Attributes.First(a => a.CanonicalName == "Locked");
|
||||
Assert.Equal("original", attr.Value); // Lock honored
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_ComposedModule_PathQualifiedNames()
|
||||
{
|
||||
var composedTemplate = CreateTemplate(2, "Pump");
|
||||
composedTemplate.Attributes.Add(new TemplateAttribute("RPM") { DataType = DataType.Double, Value = "1500" });
|
||||
composedTemplate.Scripts.Add(new TemplateScript("Start", "// start") { Id = 10 });
|
||||
|
||||
var parentTemplate = CreateTemplate(1, "Station");
|
||||
parentTemplate.Attributes.Add(new TemplateAttribute("StationName") { DataType = DataType.String, Value = "S1" });
|
||||
|
||||
var compositions = new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||
{
|
||||
[1] = new List<TemplateComposition>
|
||||
{
|
||||
new("MainPump") { ComposedTemplateId = 2 }
|
||||
}
|
||||
};
|
||||
|
||||
var composedChains = new Dictionary<int, IReadOnlyList<Template>>
|
||||
{
|
||||
[2] = [composedTemplate]
|
||||
};
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[parentTemplate],
|
||||
compositions,
|
||||
composedChains,
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Contains(result.Value.Attributes, a => a.CanonicalName == "MainPump.RPM");
|
||||
Assert.Contains(result.Value.Attributes, a => a.CanonicalName == "StationName");
|
||||
Assert.Contains(result.Value.Scripts, s => s.CanonicalName == "MainPump.Start");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_ConnectionBindings_ResolvedCorrectly()
|
||||
{
|
||||
var template = CreateTemplate(1, "Base");
|
||||
template.Attributes.Add(new TemplateAttribute("Temp")
|
||||
{
|
||||
DataType = DataType.Double,
|
||||
DataSourceReference = "ns=2;s=Temperature"
|
||||
});
|
||||
|
||||
var instance = CreateInstance();
|
||||
instance.ConnectionBindings.Add(new InstanceConnectionBinding("Temp") { DataConnectionId = 100 });
|
||||
|
||||
var connections = new Dictionary<int, DataConnection>
|
||||
{
|
||||
[100] = new("OPC-Server1", "OpcUa", 1) { Id = 100, PrimaryConfiguration = "opc.tcp://localhost:4840" }
|
||||
};
|
||||
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[template],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
connections);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var attr = result.Value.Attributes.First(a => a.CanonicalName == "Temp");
|
||||
Assert.Equal(100, attr.BoundDataConnectionId);
|
||||
Assert.Equal("OPC-Server1", attr.BoundDataConnectionName);
|
||||
Assert.Equal("OpcUa", attr.BoundDataConnectionProtocol);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_Alarms_ResolvedFromChain()
|
||||
{
|
||||
var template = CreateTemplate(1, "Base");
|
||||
template.Alarms.Add(new TemplateAlarm("HighTemp")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Temp\",\"high\":100}",
|
||||
PriorityLevel = 1
|
||||
});
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[template],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Single(result.Value.Alarms);
|
||||
Assert.Equal("HighTemp", result.Value.Alarms[0].CanonicalName);
|
||||
Assert.Equal("RangeViolation", result.Value.Alarms[0].TriggerType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InstanceMetadata_SetCorrectly()
|
||||
{
|
||||
var template = CreateTemplate(1, "Base");
|
||||
var instance = CreateInstance("MyInstance", templateId: 1, siteId: 5);
|
||||
instance.AreaId = 3;
|
||||
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[template],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal("MyInstance", result.Value.InstanceUniqueName);
|
||||
Assert.Equal(1, result.Value.TemplateId);
|
||||
Assert.Equal(5, result.Value.SiteId);
|
||||
Assert.Equal(3, result.Value.AreaId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InheritedAttributeOnDerived_BaseValueWins()
|
||||
{
|
||||
var baseTemplate = CreateTemplate(2, "Sensor");
|
||||
baseTemplate.Attributes.Add(new TemplateAttribute("SetPoint") { DataType = DataType.Double, Value = "100.0" });
|
||||
|
||||
var derived = CreateTemplate(1, "Pump.TempSensor", parentId: 2);
|
||||
derived.Attributes.Add(new TemplateAttribute("SetPoint")
|
||||
{
|
||||
DataType = DataType.Double,
|
||||
Value = "STALE",
|
||||
IsInherited = true
|
||||
});
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[derived, baseTemplate],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var setPoint = result.Value.Attributes.First(a => a.CanonicalName == "SetPoint");
|
||||
Assert.Equal("100.0", setPoint.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_OverriddenAttributeOnDerived_DerivedValueWins()
|
||||
{
|
||||
var baseTemplate = CreateTemplate(2, "Sensor");
|
||||
baseTemplate.Attributes.Add(new TemplateAttribute("SetPoint") { DataType = DataType.Double, Value = "100.0" });
|
||||
|
||||
var derived = CreateTemplate(1, "Pump.TempSensor", parentId: 2);
|
||||
derived.Attributes.Add(new TemplateAttribute("SetPoint")
|
||||
{
|
||||
DataType = DataType.Double,
|
||||
Value = "150.0",
|
||||
IsInherited = false
|
||||
});
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[derived, baseTemplate],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var setPoint = result.Value.Attributes.First(a => a.CanonicalName == "SetPoint");
|
||||
Assert.Equal("150.0", setPoint.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_LockedInDerivedOverride_Fails()
|
||||
{
|
||||
var baseTemplate = CreateTemplate(2, "Sensor");
|
||||
baseTemplate.Attributes.Add(new TemplateAttribute("SetPoint")
|
||||
{
|
||||
DataType = DataType.Double,
|
||||
Value = "100.0",
|
||||
LockedInDerived = true
|
||||
});
|
||||
|
||||
var derived = CreateTemplate(1, "Pump.TempSensor", parentId: 2);
|
||||
derived.Attributes.Add(new TemplateAttribute("SetPoint")
|
||||
{
|
||||
DataType = DataType.Double,
|
||||
Value = "150.0",
|
||||
IsInherited = false
|
||||
});
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[derived, baseTemplate],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("LockedInDerived", result.Error);
|
||||
Assert.Contains("SetPoint", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InheritedScriptOnDerived_BaseCodeWins()
|
||||
{
|
||||
var baseTemplate = CreateTemplate(2, "Sensor");
|
||||
baseTemplate.Scripts.Add(new TemplateScript("Sample", "return base;"));
|
||||
|
||||
var derived = CreateTemplate(1, "Pump.TempSensor", parentId: 2);
|
||||
derived.Scripts.Add(new TemplateScript("Sample", "stale code") { IsInherited = true });
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[derived, baseTemplate],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var script = result.Value.Scripts.First(s => s.CanonicalName == "Sample");
|
||||
Assert.Equal("return base;", script.Code);
|
||||
}
|
||||
|
||||
// ── TemplateEngine-002: per-slot alarm override ────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Flatten_InheritedAlarmOnDerived_BaseValueWins()
|
||||
{
|
||||
var baseTemplate = CreateTemplate(2, "Sensor");
|
||||
baseTemplate.Alarms.Add(new TemplateAlarm("HighTemp")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Temp\",\"high\":100}",
|
||||
PriorityLevel = 5
|
||||
});
|
||||
|
||||
var derived = CreateTemplate(1, "Pump.TempSensor", parentId: 2);
|
||||
derived.Alarms.Add(new TemplateAlarm("HighTemp")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Temp\",\"high\":999}",
|
||||
PriorityLevel = 99,
|
||||
IsInherited = true
|
||||
});
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[derived, baseTemplate],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var alarm = result.Value.Alarms.First(a => a.CanonicalName == "HighTemp");
|
||||
Assert.Equal(5, alarm.PriorityLevel);
|
||||
Assert.Equal("{\"attributeName\":\"Temp\",\"high\":100}", alarm.TriggerConfiguration);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_OverriddenAlarmOnDerived_DerivedValueWins()
|
||||
{
|
||||
var baseTemplate = CreateTemplate(2, "Sensor");
|
||||
baseTemplate.Alarms.Add(new TemplateAlarm("HighTemp")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Temp\",\"high\":100}",
|
||||
PriorityLevel = 5
|
||||
});
|
||||
|
||||
var derived = CreateTemplate(1, "Pump.TempSensor", parentId: 2);
|
||||
derived.Alarms.Add(new TemplateAlarm("HighTemp")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Temp\",\"high\":120}",
|
||||
PriorityLevel = 42,
|
||||
IsInherited = false
|
||||
});
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[derived, baseTemplate],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var alarm = result.Value.Alarms.First(a => a.CanonicalName == "HighTemp");
|
||||
Assert.Equal(42, alarm.PriorityLevel);
|
||||
Assert.Equal("{\"attributeName\":\"Temp\",\"high\":120}", alarm.TriggerConfiguration);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_LockedInDerivedAlarmOverride_Fails()
|
||||
{
|
||||
var baseTemplate = CreateTemplate(2, "Sensor");
|
||||
baseTemplate.Alarms.Add(new TemplateAlarm("HighTemp")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Temp\",\"high\":100}",
|
||||
PriorityLevel = 5,
|
||||
LockedInDerived = true
|
||||
});
|
||||
|
||||
var derived = CreateTemplate(1, "Pump.TempSensor", parentId: 2);
|
||||
derived.Alarms.Add(new TemplateAlarm("HighTemp")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Temp\",\"high\":120}",
|
||||
PriorityLevel = 42,
|
||||
IsInherited = false
|
||||
});
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(
|
||||
instance,
|
||||
[derived, baseTemplate],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("LockedInDerived", result.Error);
|
||||
Assert.Contains("HighTemp", result.Error);
|
||||
}
|
||||
|
||||
// ── TemplateEngine-001: deep composition nesting ───────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Flatten_ThreeLevelComposition_AttributesAlarmsScriptsAllResolved()
|
||||
{
|
||||
// Station composes Pump (level 1); Pump composes Motor (level 2);
|
||||
// Motor composes Bearing (level 3).
|
||||
var bearing = CreateTemplate(4, "Bearing");
|
||||
bearing.Attributes.Add(new TemplateAttribute("Vibration") { DataType = DataType.Double, Value = "0.1" });
|
||||
bearing.Alarms.Add(new TemplateAlarm("HighVibration")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Vibration\",\"high\":5}",
|
||||
PriorityLevel = 1
|
||||
});
|
||||
bearing.Scripts.Add(new TemplateScript("MonitorBearing", "// monitor") { Id = 40 });
|
||||
|
||||
var motor = CreateTemplate(3, "Motor");
|
||||
motor.Attributes.Add(new TemplateAttribute("Current") { DataType = DataType.Double, Value = "10" });
|
||||
|
||||
var pump = CreateTemplate(2, "Pump");
|
||||
pump.Attributes.Add(new TemplateAttribute("RPM") { DataType = DataType.Double, Value = "1500" });
|
||||
|
||||
var station = CreateTemplate(1, "Station");
|
||||
|
||||
var compositions = new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||
{
|
||||
[1] = new List<TemplateComposition> { new("MainPump") { ComposedTemplateId = 2 } },
|
||||
[2] = new List<TemplateComposition> { new("DriveMotor") { ComposedTemplateId = 3 } },
|
||||
[3] = new List<TemplateComposition> { new("FrontBearing") { ComposedTemplateId = 4 } },
|
||||
};
|
||||
var composedChains = new Dictionary<int, IReadOnlyList<Template>>
|
||||
{
|
||||
[2] = [pump],
|
||||
[3] = [motor],
|
||||
[4] = [bearing],
|
||||
};
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(instance, [station], compositions, composedChains,
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
// Level 3 attribute must be present with the full path-qualified name.
|
||||
Assert.Contains(result.Value.Attributes,
|
||||
a => a.CanonicalName == "MainPump.DriveMotor.FrontBearing.Vibration");
|
||||
// Level 3 alarm must be present (was dropped entirely before).
|
||||
Assert.Contains(result.Value.Alarms,
|
||||
a => a.CanonicalName == "MainPump.DriveMotor.FrontBearing.HighVibration");
|
||||
// Level 3 script must be present (was dropped entirely before).
|
||||
Assert.Contains(result.Value.Scripts,
|
||||
s => s.CanonicalName == "MainPump.DriveMotor.FrontBearing.MonitorBearing");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_NestedComposedAlarm_TriggerAttributePrefixed()
|
||||
{
|
||||
var bearing = CreateTemplate(4, "Bearing");
|
||||
bearing.Attributes.Add(new TemplateAttribute("Vibration") { DataType = DataType.Double, Value = "0.1" });
|
||||
bearing.Alarms.Add(new TemplateAlarm("HighVibration")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Vibration\",\"high\":5}",
|
||||
PriorityLevel = 1
|
||||
});
|
||||
|
||||
var motor = CreateTemplate(3, "Motor");
|
||||
var pump = CreateTemplate(2, "Pump");
|
||||
var station = CreateTemplate(1, "Station");
|
||||
|
||||
var compositions = new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||
{
|
||||
[1] = new List<TemplateComposition> { new("MainPump") { ComposedTemplateId = 2 } },
|
||||
[2] = new List<TemplateComposition> { new("DriveMotor") { ComposedTemplateId = 3 } },
|
||||
[3] = new List<TemplateComposition> { new("FrontBearing") { ComposedTemplateId = 4 } },
|
||||
};
|
||||
var composedChains = new Dictionary<int, IReadOnlyList<Template>>
|
||||
{
|
||||
[2] = [pump], [3] = [motor], [4] = [bearing],
|
||||
};
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(instance, [station], compositions, composedChains,
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var alarm = result.Value.Alarms.First(a => a.CanonicalName.EndsWith("HighVibration"));
|
||||
// The trigger's attribute reference must carry the full nested prefix.
|
||||
Assert.Contains("MainPump.DriveMotor.FrontBearing.Vibration", alarm.TriggerConfiguration);
|
||||
}
|
||||
|
||||
// ── TemplateEngine-004: alarm on-trigger script resolution ─────────────
|
||||
|
||||
[Fact]
|
||||
public void Flatten_AlarmOnTriggerScript_ResolvedToCanonicalName()
|
||||
{
|
||||
var template = CreateTemplate(1, "Base");
|
||||
template.Scripts.Add(new TemplateScript("HandleAlarm", "// handle") { Id = 50 });
|
||||
template.Alarms.Add(new TemplateAlarm("HighTemp")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.RangeViolation,
|
||||
TriggerConfiguration = "{\"attributeName\":\"Temp\",\"high\":100}",
|
||||
PriorityLevel = 1,
|
||||
OnTriggerScriptId = 50
|
||||
});
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(instance, [template],
|
||||
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var alarm = result.Value.Alarms.First(a => a.CanonicalName == "HighTemp");
|
||||
Assert.Equal("HandleAlarm", alarm.OnTriggerScriptCanonicalName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Flatten_ComposedAlarmOnTriggerScript_ResolvedWithPrefix()
|
||||
{
|
||||
var composedTemplate = CreateTemplate(2, "Pump");
|
||||
composedTemplate.Scripts.Add(new TemplateScript("PumpAlarmHandler", "// h") { Id = 60 });
|
||||
composedTemplate.Alarms.Add(new TemplateAlarm("PumpFault")
|
||||
{
|
||||
TriggerType = AlarmTriggerType.ValueMatch,
|
||||
TriggerConfiguration = "{\"attributeName\":\"State\",\"value\":\"FAULT\"}",
|
||||
PriorityLevel = 5,
|
||||
OnTriggerScriptId = 60
|
||||
});
|
||||
|
||||
var station = CreateTemplate(1, "Station");
|
||||
|
||||
var compositions = new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||
{
|
||||
[1] = new List<TemplateComposition> { new("MainPump") { ComposedTemplateId = 2 } }
|
||||
};
|
||||
var composedChains = new Dictionary<int, IReadOnlyList<Template>>
|
||||
{
|
||||
[2] = [composedTemplate]
|
||||
};
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(instance, [station], compositions, composedChains,
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
var alarm = result.Value.Alarms.First(a => a.CanonicalName == "MainPump.PumpFault");
|
||||
Assert.Equal("MainPump.PumpAlarmHandler", alarm.OnTriggerScriptCanonicalName);
|
||||
}
|
||||
|
||||
// ── TemplateEngine-016: composed-script ScriptScope.ParentPath ─────────
|
||||
|
||||
[Fact]
|
||||
public void Flatten_NestedComposedScript_ScopeCarriesCorrectParentPath()
|
||||
{
|
||||
// Station composes Pump (level 1); Pump composes Motor (level 2).
|
||||
// The depth-1 script's parent is the root template (ParentPath "");
|
||||
// the depth-2 script's parent is the Pump module (ParentPath "MainPump").
|
||||
var motor = CreateTemplate(3, "Motor");
|
||||
motor.Scripts.Add(new TemplateScript("MonitorMotor", "// m") { Id = 70 });
|
||||
|
||||
var pump = CreateTemplate(2, "Pump");
|
||||
pump.Scripts.Add(new TemplateScript("MonitorPump", "// p") { Id = 71 });
|
||||
|
||||
var station = CreateTemplate(1, "Station");
|
||||
|
||||
var compositions = new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||
{
|
||||
[1] = new List<TemplateComposition> { new("MainPump") { ComposedTemplateId = 2 } },
|
||||
[2] = new List<TemplateComposition> { new("DriveMotor") { ComposedTemplateId = 3 } },
|
||||
};
|
||||
var composedChains = new Dictionary<int, IReadOnlyList<Template>>
|
||||
{
|
||||
[2] = [pump], [3] = [motor],
|
||||
};
|
||||
|
||||
var instance = CreateInstance();
|
||||
var result = _sut.Flatten(instance, [station], compositions, composedChains,
|
||||
new Dictionary<int, DataConnection>());
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
|
||||
var depth1 = result.Value.Scripts.First(s => s.CanonicalName == "MainPump.MonitorPump");
|
||||
Assert.Equal("MainPump", depth1.Scope.SelfPath);
|
||||
Assert.Equal("", depth1.Scope.ParentPath);
|
||||
|
||||
var depth2 = result.Value.Scripts.First(s => s.CanonicalName == "MainPump.DriveMotor.MonitorMotor");
|
||||
Assert.Equal("MainPump.DriveMotor", depth2.Scope.SelfPath);
|
||||
// Parent module of a depth-2 script is the enclosing Pump module.
|
||||
Assert.Equal("MainPump", depth2.Scope.ParentPath);
|
||||
}
|
||||
}
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
||||
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Flattening;
|
||||
|
||||
public class RevisionHashServiceTests
|
||||
{
|
||||
private readonly RevisionHashService _sut = new();
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_SameContent_SameHash()
|
||||
{
|
||||
var config1 = CreateConfig("Instance1", "25.0");
|
||||
var config2 = CreateConfig("Instance1", "25.0");
|
||||
|
||||
var hash1 = _sut.ComputeHash(config1);
|
||||
var hash2 = _sut.ComputeHash(config2);
|
||||
|
||||
Assert.Equal(hash1, hash2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_DifferentContent_DifferentHash()
|
||||
{
|
||||
var config1 = CreateConfig("Instance1", "25.0");
|
||||
var config2 = CreateConfig("Instance1", "50.0");
|
||||
|
||||
var hash1 = _sut.ComputeHash(config1);
|
||||
var hash2 = _sut.ComputeHash(config2);
|
||||
|
||||
Assert.NotEqual(hash1, hash2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_StartsWithSha256Prefix()
|
||||
{
|
||||
var config = CreateConfig("Instance1", "25.0");
|
||||
var hash = _sut.ComputeHash(config);
|
||||
|
||||
Assert.StartsWith("sha256:", hash);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_DeterministicAcrossRuns()
|
||||
{
|
||||
// Different GeneratedAtUtc should NOT affect the hash (volatile field excluded)
|
||||
var config1 = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Attributes = [new ResolvedAttribute { CanonicalName = "A", Value = "1", DataType = "Int32" }],
|
||||
GeneratedAtUtc = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero)
|
||||
};
|
||||
var config2 = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Attributes = [new ResolvedAttribute { CanonicalName = "A", Value = "1", DataType = "Int32" }],
|
||||
GeneratedAtUtc = new DateTimeOffset(2026, 6, 15, 12, 0, 0, TimeSpan.Zero)
|
||||
};
|
||||
|
||||
Assert.Equal(_sut.ComputeHash(config1), _sut.ComputeHash(config2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_NullConfig_ThrowsArgumentNull()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _sut.ComputeHash(null!));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_AttributeOrder_DoesNotAffectHash()
|
||||
{
|
||||
var config1 = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute { CanonicalName = "A", Value = "1", DataType = "Int32" },
|
||||
new ResolvedAttribute { CanonicalName = "B", Value = "2", DataType = "Int32" }
|
||||
]
|
||||
};
|
||||
var config2 = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute { CanonicalName = "B", Value = "2", DataType = "Int32" },
|
||||
new ResolvedAttribute { CanonicalName = "A", Value = "1", DataType = "Int32" }
|
||||
]
|
||||
};
|
||||
|
||||
Assert.Equal(_sut.ComputeHash(config1), _sut.ComputeHash(config2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HashableRecords_PropertiesDeclaredAlphabetically()
|
||||
{
|
||||
// TemplateEngine-011: revision-hash determinism depends entirely on the
|
||||
// private Hashable* records declaring their properties in alphabetical
|
||||
// order (System.Text.Json emits properties in CLR declaration order and
|
||||
// does not sort). This guards against a contributor silently changing
|
||||
// every revision hash by adding a property out of order.
|
||||
var nested = typeof(RevisionHashService)
|
||||
.GetNestedTypes(System.Reflection.BindingFlags.NonPublic)
|
||||
.Where(t => t.Name.StartsWith("Hashable"))
|
||||
.ToList();
|
||||
|
||||
Assert.NotEmpty(nested);
|
||||
|
||||
foreach (var type in nested)
|
||||
{
|
||||
var propNames = type
|
||||
.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
|
||||
.Where(p => p.Name != "EqualityContract")
|
||||
.Select(p => p.Name)
|
||||
.ToList();
|
||||
|
||||
var sorted = propNames.OrderBy(n => n, StringComparer.Ordinal).ToList();
|
||||
|
||||
Assert.True(
|
||||
propNames.SequenceEqual(sorted),
|
||||
$"{type.Name} properties must be declared alphabetically. " +
|
||||
$"Declared: [{string.Join(", ", propNames)}] Expected: [{string.Join(", ", sorted)}]");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_AttributeDescriptionEdit_ChangesHash()
|
||||
{
|
||||
// TemplateEngine-017: Description must be folded into the hash so that
|
||||
// edits to authoring-time documentation (which still travels in the
|
||||
// deployed payload) flow through the staleness indicator.
|
||||
var baseAttr = new ResolvedAttribute
|
||||
{
|
||||
CanonicalName = "Temperature",
|
||||
Value = "25",
|
||||
DataType = "Double",
|
||||
Description = "Original description"
|
||||
};
|
||||
var editedAttr = baseAttr with { Description = "Updated description" };
|
||||
|
||||
var configBefore = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Attributes = [baseAttr]
|
||||
};
|
||||
var configAfter = configBefore with { Attributes = [editedAttr] };
|
||||
|
||||
Assert.NotEqual(_sut.ComputeHash(configBefore), _sut.ComputeHash(configAfter));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_AlarmDescriptionEdit_ChangesHash()
|
||||
{
|
||||
// TemplateEngine-017: same Description contract applies to alarms.
|
||||
var baseAlarm = new ResolvedAlarm
|
||||
{
|
||||
CanonicalName = "HighTemp",
|
||||
TriggerType = "RangeViolation",
|
||||
Description = "Original"
|
||||
};
|
||||
var editedAlarm = baseAlarm with { Description = "Updated" };
|
||||
|
||||
var configBefore = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Alarms = [baseAlarm]
|
||||
};
|
||||
var configAfter = configBefore with { Alarms = [editedAlarm] };
|
||||
|
||||
Assert.NotEqual(_sut.ComputeHash(configBefore), _sut.ComputeHash(configAfter));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_ConnectionEndpointEdit_ChangesHash()
|
||||
{
|
||||
// TemplateEngine-017: a Deployment user editing the primary endpoint
|
||||
// JSON of a data connection bound to an instance must produce a
|
||||
// different revision hash. The connection's protocol, primary/backup
|
||||
// configuration JSON, and failover retry count are all part of the
|
||||
// deployment package and therefore part of the hash input.
|
||||
var connectionsBefore = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["plc1"] = new ConnectionConfig
|
||||
{
|
||||
Protocol = "OpcUa",
|
||||
ConfigurationJson = "{\"endpoint\":\"opc.tcp://host-a:4840\"}",
|
||||
BackupConfigurationJson = null,
|
||||
FailoverRetryCount = 3
|
||||
}
|
||||
};
|
||||
var connectionsAfter = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["plc1"] = connectionsBefore["plc1"] with
|
||||
{
|
||||
ConfigurationJson = "{\"endpoint\":\"opc.tcp://host-b:4840\"}"
|
||||
}
|
||||
};
|
||||
|
||||
var configBefore = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Connections = connectionsBefore
|
||||
};
|
||||
var configAfter = configBefore with { Connections = connectionsAfter };
|
||||
|
||||
Assert.NotEqual(_sut.ComputeHash(configBefore), _sut.ComputeHash(configAfter));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_ConnectionProtocolEdit_ChangesHash()
|
||||
{
|
||||
// TemplateEngine-017: changing protocol must change the hash.
|
||||
var connectionsBefore = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["plc1"] = new ConnectionConfig
|
||||
{
|
||||
Protocol = "OpcUa",
|
||||
ConfigurationJson = "{}",
|
||||
FailoverRetryCount = 3
|
||||
}
|
||||
};
|
||||
var connectionsAfter = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["plc1"] = connectionsBefore["plc1"] with { Protocol = "Modbus" }
|
||||
};
|
||||
|
||||
var configBefore = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Connections = connectionsBefore
|
||||
};
|
||||
var configAfter = configBefore with { Connections = connectionsAfter };
|
||||
|
||||
Assert.NotEqual(_sut.ComputeHash(configBefore), _sut.ComputeHash(configAfter));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ComputeHash_ConnectionsSameContent_SameHash()
|
||||
{
|
||||
// TemplateEngine-017: equal Connections maps must yield the same hash,
|
||||
// regardless of dictionary iteration order (the SortedDictionary
|
||||
// projection guards this).
|
||||
var connections1 = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["b"] = new ConnectionConfig { Protocol = "OpcUa", ConfigurationJson = "{\"k\":2}", FailoverRetryCount = 3 },
|
||||
["a"] = new ConnectionConfig { Protocol = "OpcUa", ConfigurationJson = "{\"k\":1}", FailoverRetryCount = 3 }
|
||||
};
|
||||
var connections2 = new Dictionary<string, ConnectionConfig>
|
||||
{
|
||||
["a"] = new ConnectionConfig { Protocol = "OpcUa", ConfigurationJson = "{\"k\":1}", FailoverRetryCount = 3 },
|
||||
["b"] = new ConnectionConfig { Protocol = "OpcUa", ConfigurationJson = "{\"k\":2}", FailoverRetryCount = 3 }
|
||||
};
|
||||
|
||||
var config1 = new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = "Instance1",
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Connections = connections1
|
||||
};
|
||||
var config2 = config1 with { Connections = connections2 };
|
||||
|
||||
Assert.Equal(_sut.ComputeHash(config1), _sut.ComputeHash(config2));
|
||||
}
|
||||
|
||||
private static FlattenedConfiguration CreateConfig(string instanceName, string tempValue)
|
||||
{
|
||||
return new FlattenedConfiguration
|
||||
{
|
||||
InstanceUniqueName = instanceName,
|
||||
TemplateId = 1,
|
||||
SiteId = 1,
|
||||
Attributes =
|
||||
[
|
||||
new ResolvedAttribute { CanonicalName = "Temperature", Value = tempValue, DataType = "Double" }
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user