Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Configuration.Tests/ConfigurationGapsSchemaTests.cs
T
Joseph Doherty 486628cf0a fix(config): #998 deploy gate rejects duplicate ZTag/SAPID, duplicate owned historian tagnames and dangling parent links; NULL-keyed unique indexes enforced; node/cluster edits carry a RowVersion
C-3 ZTag/SAPID uniqueness enforced from the draft (reservation ledger has no writer); UI/doc claim corrected.
C-5 HistorianTagnameDuplicate (owned tags, per cluster, case-insensitive; readOnly exempt).
C-8 UX_NodeAcl_PrincipalScope / UX_LdapGroupRoleMapping_Group_Cluster unfiltered (migration with duplicate pre-check) + friendly service/page checks.
C-9 RowVersion on ClusterNode/ServerCluster, edit pages use it; node delete removes its NodeDeploymentState rows.
C-10 DanglingReference rule; dead PathTooLong/EquipmentUuidImmutable rules + PriorEquipment/Enterprise/Site inputs deleted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-03 10:49:57 -05:00

175 lines
7.3 KiB
C#

using Microsoft.EntityFrameworkCore;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Configuration;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
/// <summary>
/// lmxopcua#998 (review 2026-12-02 03-configuration C-8, C-9) against the real SQL schema: the two unique
/// indexes over nullable columns must cover the NULL-key rows, and ClusterNode / ServerCluster carry a
/// concurrency token.
/// </summary>
[Trait("Category", "SchemaCompliance")]
[Collection(nameof(SchemaComplianceCollection))]
public sealed class ConfigurationGapsSchemaTests
{
private readonly SchemaComplianceFixture _fixture;
/// <summary>Initializes a new instance of the <see cref="ConfigurationGapsSchemaTests"/> class.</summary>
/// <param name="fixture">The schema compliance fixture (real SQL DB).</param>
public ConfigurationGapsSchemaTests(SchemaComplianceFixture fixture) => _fixture = fixture;
private OtOpcUaConfigDbContext NewContext() =>
new(new DbContextOptionsBuilder<OtOpcUaConfigDbContext>().UseSqlServer(_fixture.ConnectionString).Options);
private static string Id(string prefix) => $"{prefix}-{Guid.NewGuid():N}";
private string SeedCluster()
{
var clusterId = Id("cl");
using var db = NewContext();
db.ServerClusters.Add(new ServerCluster
{
ClusterId = clusterId, Name = Id("Cluster"), Enterprise = "zb", Site = "dev", NodeCount = 1,
RedundancyMode = RedundancyMode.None, CreatedBy = "t998",
});
db.SaveChanges();
return clusterId;
}
private static NodeAcl ClusterAcl(string clusterId, string principal) => new()
{
NodeAclId = Id("acl"), ClusterId = clusterId, Principal = principal,
ScopeKind = NodeAclScopeKind.Cluster, ScopeId = null, PermissionFlags = NodePermissions.Browse,
};
/// <summary>The recorded index definitions are unfiltered (HasFilter(null)) — the model comment's "one row per key" holds for NULLs too.</summary>
[Fact]
public void The_two_nullable_unique_indexes_are_not_filtered()
{
using var db = NewContext();
var filtered = db.Database.SqlQueryRaw<string>(
"SELECT name AS Value FROM sys.indexes WHERE is_unique = 1 AND has_filter = 1 " +
"AND name IN ('UX_NodeAcl_PrincipalScope', 'UX_LdapGroupRoleMapping_Group_Cluster')").ToList();
filtered.ShouldBeEmpty();
}
/// <summary>Two system-wide mappings for one group (NULL ClusterId) now collide.</summary>
[Fact]
public void Two_system_wide_mappings_for_one_group_are_rejected()
{
var group = Id("cn=grp");
using var db = NewContext();
db.LdapGroupRoleMappings.Add(new LdapGroupRoleMapping { LdapGroup = group, Role = AdminRole.Administrator, IsSystemWide = true });
db.LdapGroupRoleMappings.Add(new LdapGroupRoleMapping { LdapGroup = group, Role = AdminRole.Designer, IsSystemWide = true });
Should.Throw<DbUpdateException>(() => db.SaveChanges());
}
/// <summary>Positive control: a system-wide row plus a cluster row, and one row for another group, are all fine.</summary>
[Fact]
public void System_wide_plus_cluster_scoped_and_other_groups_are_accepted()
{
var group = Id("cn=grp");
var cluster = SeedCluster();
using var db = NewContext();
db.LdapGroupRoleMappings.Add(new LdapGroupRoleMapping { LdapGroup = group, Role = AdminRole.Administrator, IsSystemWide = true });
db.LdapGroupRoleMappings.Add(new LdapGroupRoleMapping { LdapGroup = group, Role = AdminRole.Designer, ClusterId = cluster, IsSystemWide = false });
db.LdapGroupRoleMappings.Add(new LdapGroupRoleMapping { LdapGroup = Id("cn=other"), Role = AdminRole.Administrator, IsSystemWide = true });
Should.NotThrow(() => db.SaveChanges());
}
/// <summary>Two Cluster-scope ACL rows (NULL ScopeId) for one principal in one cluster now collide.</summary>
[Fact]
public void Two_cluster_scope_acls_for_one_principal_are_rejected()
{
var cluster = SeedCluster();
using var db = NewContext();
db.NodeAcls.Add(ClusterAcl(cluster, "grp-a"));
db.NodeAcls.Add(ClusterAcl(cluster, "grp-a"));
Should.Throw<DbUpdateException>(() => db.SaveChanges());
}
/// <summary>Positive control: other principal, and the same principal in another cluster, are fine.</summary>
[Fact]
public void Cluster_scope_acls_for_other_principals_or_clusters_are_accepted()
{
var a = SeedCluster();
var b = SeedCluster();
using var db = NewContext();
db.NodeAcls.Add(ClusterAcl(a, "grp-a"));
db.NodeAcls.Add(ClusterAcl(a, "grp-b"));
db.NodeAcls.Add(ClusterAcl(b, "grp-a"));
Should.NotThrow(() => db.SaveChanges());
}
/// <summary>A stale ClusterNode write (token loaded before a concurrent edit) is refused, not last-writer-wins.</summary>
[Fact]
public void A_stale_cluster_node_edit_is_refused()
{
var cluster = SeedCluster();
var nodeId = Id("node") + ":4053";
using (var seed = NewContext())
{
seed.ClusterNodes.Add(new ClusterNode
{
NodeId = nodeId, ClusterId = cluster, Host = "h", ApplicationUri = "urn:" + Id("n"), CreatedBy = "t998",
});
seed.SaveChanges();
}
byte[] staleToken;
using (var form = NewContext()) staleToken = form.ClusterNodes.AsNoTracking().Single(n => n.NodeId == nodeId).RowVersion;
staleToken.ShouldNotBeEmpty();
using (var other = NewContext())
{
var n = other.ClusterNodes.Single(x => x.NodeId == nodeId);
n.Enabled = false;
other.SaveChanges();
}
using var stale = NewContext();
var entity = stale.ClusterNodes.Single(x => x.NodeId == nodeId);
stale.Entry(entity).Property(e => e.RowVersion).OriginalValue = staleToken;
entity.Host = "changed";
Should.Throw<DbUpdateConcurrencyException>(() => stale.SaveChanges());
}
/// <summary>The same for ServerCluster; positive control: a write with the CURRENT token succeeds.</summary>
[Fact]
public void A_stale_server_cluster_edit_is_refused_but_a_current_one_succeeds()
{
var cluster = SeedCluster();
byte[] staleToken;
using (var form = NewContext()) staleToken = form.ServerClusters.AsNoTracking().Single(c => c.ClusterId == cluster).RowVersion;
using (var other = NewContext())
{
other.ServerClusters.Single(c => c.ClusterId == cluster).Notes = "concurrent";
other.SaveChanges();
}
using (var stale = NewContext())
{
var entity = stale.ServerClusters.Single(c => c.ClusterId == cluster);
stale.Entry(entity).Property(e => e.RowVersion).OriginalValue = staleToken;
entity.Notes = "stale";
Should.Throw<DbUpdateConcurrencyException>(() => stale.SaveChanges());
}
using var current = NewContext();
var fresh = current.ServerClusters.Single(c => c.ClusterId == cluster);
current.Entry(fresh).Property(e => e.RowVersion).OriginalValue = fresh.RowVersion;
fresh.Notes = "current";
Should.NotThrow(() => current.SaveChanges());
}
}