7b0b9c7365
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.
373 lines
14 KiB
C#
373 lines
14 KiB
C#
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);
|
|
}
|
|
}
|