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; /// /// 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. /// [Trait("Category", "SchemaCompliance")] [Collection(nameof(SchemaComplianceCollection))] public sealed class ConfigurationGapsSchemaTests { private readonly SchemaComplianceFixture _fixture; /// Initializes a new instance of the class. /// The schema compliance fixture (real SQL DB). public ConfigurationGapsSchemaTests(SchemaComplianceFixture fixture) => _fixture = fixture; private OtOpcUaConfigDbContext NewContext() => new(new DbContextOptionsBuilder().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, }; /// The recorded index definitions are unfiltered (HasFilter(null)) — the model comment's "one row per key" holds for NULLs too. [Fact] public void The_two_nullable_unique_indexes_are_not_filtered() { using var db = NewContext(); var filtered = db.Database.SqlQueryRaw( "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(); } /// Two system-wide mappings for one group (NULL ClusterId) now collide. [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(() => db.SaveChanges()); } /// Positive control: a system-wide row plus a cluster row, and one row for another group, are all fine. [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()); } /// Two Cluster-scope ACL rows (NULL ScopeId) for one principal in one cluster now collide. [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(() => db.SaveChanges()); } /// Positive control: other principal, and the same principal in another cluster, are fine. [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()); } /// A stale ClusterNode write (token loaded before a concurrent edit) is refused, not last-writer-wins. [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(() => stale.SaveChanges()); } /// The same for ServerCluster; positive control: a write with the CURRENT token succeeds. [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(() => 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()); } }