using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.AdminUI.Uns; using ZB.MOM.WW.OtOpcUa.Configuration.Entities; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns; /// /// lmxopcua#1049 / #1040: the authoring mirrors of the new database constraints. A ZTag / SAPID another equipment /// holds (case-insensitive, trimmed) is refused on create, update and CSV import with the owner named — not left /// to UX_Equipment_ZTag / UX_Equipment_SAPID to fail the save (or a whole import batch) with no row /// named; a VirtualTag / scripted alarm naming a script that does not exist is refused before /// FK_VirtualTag_Script / FK_ScriptedAlarm_Script would. /// [Trait("Category", "Unit")] public sealed class UnsTreeServiceExternalIdAndScriptReferenceTests { private static (UnsTreeService Service, string DbName) Fresh() { var dbName = $"uns-extid-{Guid.NewGuid():N}"; using var db = UnsTreeTestDb.CreateNamed(dbName); db.UnsAreas.Add(new UnsArea { UnsAreaId = "AREA-1", ClusterId = "MAIN", Name = "a" }); db.UnsLines.Add(new UnsLine { UnsLineId = "LINE-1", UnsAreaId = "AREA-1", Name = "l" }); db.SaveChanges(); return (new UnsTreeService(UnsTreeTestDb.Factory(dbName)), dbName); } private static EquipmentInput Input(string name, string machineCode, string? zTag = null, string? sapId = null) => new(name, machineCode, "LINE-1", ZTag: zTag, SAPID: sapId, Manufacturer: null, Model: null, SerialNumber: null, HardwareRevision: null, SoftwareRevision: null, YearOfConstruction: null, AssetLocation: null, ManufacturerUri: null, DeviceManualUri: null, Enabled: true); /// Create refuses a ZTag another equipment holds and names it; a trimmed copy counts as the same value. [Fact] public async Task Create_refuses_a_ZTag_another_equipment_holds() { var (svc, dbName) = Fresh(); var ct = TestContext.Current.CancellationToken; (await svc.CreateEquipmentAsync(Input("m1", "MC1", zTag: "ZT-1"), ct)).Ok.ShouldBeTrue(); using var db = UnsTreeTestDb.CreateNamed(dbName); var owner = db.Equipment.Single().EquipmentId; var dup = await svc.CreateEquipmentAsync(Input("m2", "MC2", zTag: " ZT-1 "), ct); dup.Ok.ShouldBeFalse(); dup.Error.ShouldNotBeNull().ShouldContain("ZTag 'ZT-1'"); dup.Error.ShouldContain(owner); } /// Update refuses a SAPID another equipment holds, but keeping its OWN value is fine. [Fact] public async Task Update_refuses_another_equipments_SAPID_but_keeps_its_own() { var (svc, dbName) = Fresh(); var ct = TestContext.Current.CancellationToken; (await svc.CreateEquipmentAsync(Input("m1", "MC1", sapId: "SAP-1"), ct)).Ok.ShouldBeTrue(); (await svc.CreateEquipmentAsync(Input("m2", "MC2", sapId: "SAP-2"), ct)).Ok.ShouldBeTrue(); using var db = UnsTreeTestDb.CreateNamed(dbName); var second = db.Equipment.Single(e => e.MachineCode == "MC2"); var clash = await svc.UpdateEquipmentAsync(second.EquipmentId, Input("m2", "MC2", sapId: "SAP-1"), second.RowVersion, ct); clash.Ok.ShouldBeFalse(); clash.Error.ShouldNotBeNull().ShouldContain("SAPID 'SAP-1'"); var own = await svc.UpdateEquipmentAsync(second.EquipmentId, Input("m2-renamed", "MC2", sapId: "SAP-2"), second.RowVersion, ct); own.Ok.ShouldBeTrue(); } /// Blank identifiers are stored as NULL and never clash; values are stored trimmed. [Fact] public async Task Blank_identifiers_are_none_and_values_are_stored_trimmed() { var (svc, dbName) = Fresh(); var ct = TestContext.Current.CancellationToken; (await svc.CreateEquipmentAsync(Input("m1", "MC1", zTag: " ", sapId: ""), ct)).Ok.ShouldBeTrue(); (await svc.CreateEquipmentAsync(Input("m2", "MC2", zTag: " ", sapId: ""), ct)).Ok.ShouldBeTrue(); (await svc.CreateEquipmentAsync(Input("m3", "MC3", zTag: " ZT-3 "), ct)).Ok.ShouldBeTrue(); using var db = UnsTreeTestDb.CreateNamed(dbName); db.Equipment.Count(e => e.ZTag == null && e.SAPID == null).ShouldBe(2); db.Equipment.Single(e => e.MachineCode == "MC3").ZTag.ShouldBe("ZT-3"); } /// /// Import names each clashing row — against the DB and against an earlier row of the same file — and still /// imports the clean rows (the clash no longer fails the whole batch at SaveChanges). /// [Fact] public async Task Import_names_clashing_rows_and_imports_the_rest() { var (svc, dbName) = Fresh(); var ct = TestContext.Current.CancellationToken; (await svc.CreateEquipmentAsync(Input("m1", "MC1", zTag: "ZT-1"), ct)).Ok.ShouldBeTrue(); var result = await svc.ImportEquipmentAsync( [ Input("m2", "MC2", zTag: "zt-1"), Input("m3", "MC3", sapId: "SAP-3"), Input("m4", "MC4", sapId: "SAP-3"), Input("m5", "MC5", zTag: "ZT-5", sapId: "SAP-5"), ], ct); result.Inserted.ShouldBe(2); result.Errors.Count.ShouldBe(2); result.Errors.ShouldContain(e => e.Contains("'MC2'") && e.Contains("ZTag")); result.Errors.ShouldContain(e => e.Contains("'MC4'") && e.Contains("SAPID 'SAP-3'")); using var db = UnsTreeTestDb.CreateNamed(dbName); db.Equipment.Select(e => e.MachineCode).ToList().ShouldBe(["MC1", "MC3", "MC5"], ignoreOrder: true); } /// A VirtualTag naming a missing script is refused with the script named; positive control binds SCRIPT-1. [Fact] public async Task A_virtual_tag_naming_a_missing_script_is_refused() { var dbName = $"uns-vtscript-{Guid.NewGuid():N}"; UnsTreeTestDb.SeedNamed(dbName); var svc = new UnsTreeService(UnsTreeTestDb.Factory(dbName)); var ct = TestContext.Current.CancellationToken; var missing = await svc.CreateVirtualTagAsync(UnsTreeTestDb.SeededEquipmentId, new VirtualTagInput("VT-X", "x", DataType: "Double", ScriptId: "SCRIPT-GONE", ChangeTriggered: true, TimerIntervalMs: null, Historize: false, Enabled: true), ct); missing.Ok.ShouldBeFalse(); missing.Error.ShouldNotBeNull().ShouldContain("SCRIPT-GONE"); var ok = await svc.CreateVirtualTagAsync(UnsTreeTestDb.SeededEquipmentId, new VirtualTagInput("VT-Y", "y", DataType: "Double", ScriptId: "SCRIPT-1", ChangeTriggered: true, TimerIntervalMs: null, Historize: false, Enabled: true), ct); ok.Ok.ShouldBeTrue(); } /// A scripted alarm naming a missing predicate script is refused. [Fact] public async Task A_scripted_alarm_naming_a_missing_predicate_script_is_refused() { var dbName = $"uns-sascript-{Guid.NewGuid():N}"; UnsTreeTestDb.SeedNamed(dbName); var svc = new UnsTreeService(UnsTreeTestDb.Factory(dbName)); var result = await svc.CreateScriptedAlarmAsync(UnsTreeTestDb.SeededEquipmentId, new ScriptedAlarmInput("SA-X", "x", "LimitAlarm", 700, "m", "SCRIPT-GONE", HistorizeToAveva: true, Retain: true, Enabled: true), TestContext.Current.CancellationToken); result.Ok.ShouldBeFalse(); result.Error.ShouldNotBeNull().ShouldContain("SCRIPT-GONE"); } }