3773509888
A-3: UnsTreeService pre-checks sibling (cluster,area,line) names on create/update/import, catches DbUpdateException, validates column lengths. A-4: blank ack password keeps the stored one; first-time blank refused. A-6: BrowseSessionReaper drains sessions in a finally on shutdown. A-7: node flushes response headers; central treats them as established; deadline warning throttled. A-9: key delete always reloads and names leftover ack secrets when cleanup fails. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
259 lines
11 KiB
C#
259 lines
11 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
|
|
|
|
/// <summary>
|
|
/// #997 A-3 — the database enforces unique (ClusterId,Name) areas, (UnsAreaId,Name) lines and
|
|
/// (UnsLineId,Name) equipment, but the service only pre-checked logical ids, so a duplicate sibling name
|
|
/// escaped as an unhandled <c>DbUpdateException</c> that <c>PageErrorBoundary</c> turned into a blank page.
|
|
/// EF InMemory does not enforce unique indexes, so these tests drive the service's own pre-checks, and an
|
|
/// interceptor that throws <see cref="DbUpdateException"/> stands in for the SQL Server backstop (the
|
|
/// concurrent-insert race the pre-check cannot close).
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class UnsTreeServiceSiblingNameTests
|
|
{
|
|
private static (UnsTreeService Service, string DbName) Seeded()
|
|
{
|
|
var dbName = $"uns-sib-{Guid.NewGuid():N}";
|
|
UnsTreeTestDb.SeedNamed(dbName);
|
|
return (new UnsTreeService(UnsTreeTestDb.Factory(dbName)), dbName);
|
|
}
|
|
|
|
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
|
|
|
private static EquipmentInput Eq(string name, string machineCode, string line = "LINE-1", string? manufacturer = null) =>
|
|
new(name, machineCode, line,
|
|
ZTag: null, SAPID: null, Manufacturer: manufacturer, Model: null, SerialNumber: null,
|
|
HardwareRevision: null, SoftwareRevision: null, YearOfConstruction: null,
|
|
AssetLocation: null, ManufacturerUri: null, DeviceManualUri: null, Enabled: true);
|
|
|
|
[Fact]
|
|
public async Task CreateArea_with_a_sibling_name_is_refused_with_a_message()
|
|
{
|
|
var (service, dbName) = Seeded();
|
|
|
|
var result = await service.CreateAreaAsync("MAIN", "AREA-NEW", "assembly", null, Ct);
|
|
|
|
result.Ok.ShouldBeFalse();
|
|
result.Error.ShouldNotBeNull().ShouldContain("assembly");
|
|
using var db = UnsTreeTestDb.CreateNamed(dbName);
|
|
db.UnsAreas.Count(a => a.ClusterId == "MAIN").ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateArea_with_the_same_name_in_ANOTHER_cluster_is_allowed()
|
|
{
|
|
var (service, _) = Seeded();
|
|
|
|
(await service.CreateAreaAsync(UnsTreeTestDb.EmptyClusterId, "AREA-NEW", "assembly", null, Ct)).Ok.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateArea_onto_a_sibling_name_is_refused_but_keeping_its_own_name_is_fine()
|
|
{
|
|
var (service, dbName) = Seeded();
|
|
(await service.CreateAreaAsync("MAIN", "AREA-2", "packaging", null, Ct)).Ok.ShouldBeTrue();
|
|
byte[] rv;
|
|
using (var db = UnsTreeTestDb.CreateNamed(dbName)) rv = db.UnsAreas.Single(a => a.UnsAreaId == "AREA-2").RowVersion;
|
|
|
|
var clash = await service.UpdateAreaAsync("AREA-2", "assembly", null, "MAIN", rv, Ct);
|
|
var self = await service.UpdateAreaAsync("AREA-2", "packaging", "n", "MAIN", rv, Ct);
|
|
|
|
clash.Ok.ShouldBeFalse();
|
|
clash.Error.ShouldNotBeNull().ShouldContain("assembly");
|
|
self.Ok.ShouldBeTrue(); // positive control: the self-exclusion must not block an ordinary save
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateArea_moved_into_a_cluster_that_has_that_name_is_refused()
|
|
{
|
|
var (service, dbName) = Seeded();
|
|
(await service.CreateAreaAsync(UnsTreeTestDb.EmptyClusterId, "AREA-2", "assembly", null, Ct)).Ok.ShouldBeTrue();
|
|
byte[] rv;
|
|
using (var db = UnsTreeTestDb.CreateNamed(dbName)) rv = db.UnsAreas.Single(a => a.UnsAreaId == "AREA-2").RowVersion;
|
|
|
|
var result = await service.UpdateAreaAsync("AREA-2", "assembly", null, "MAIN", rv, Ct);
|
|
|
|
result.Ok.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateLine_with_a_sibling_name_is_refused()
|
|
{
|
|
var (service, _) = Seeded();
|
|
|
|
var result = await service.CreateLineAsync("AREA-1", "LINE-NEW", "line-a", null, Ct);
|
|
|
|
result.Ok.ShouldBeFalse();
|
|
result.Error.ShouldNotBeNull().ShouldContain("line-a");
|
|
(await service.CreateLineAsync("AREA-1", "LINE-NEW", "line-b", null, Ct)).Ok.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateLine_onto_a_sibling_name_or_into_an_area_holding_it_is_refused()
|
|
{
|
|
var (service, dbName) = Seeded();
|
|
(await service.CreateLineAsync("AREA-1", "LINE-2", "line-b", null, Ct)).Ok.ShouldBeTrue();
|
|
(await service.CreateAreaAsync("MAIN", "AREA-2", "packaging", null, Ct)).Ok.ShouldBeTrue();
|
|
byte[] rv;
|
|
using (var db = UnsTreeTestDb.CreateNamed(dbName)) rv = db.UnsLines.Single(l => l.UnsLineId == "LINE-2").RowVersion;
|
|
|
|
(await service.UpdateLineAsync("LINE-2", "line-a", null, "AREA-1", rv, Ct)).Ok.ShouldBeFalse();
|
|
// Re-homing to an area that has no 'line-b' is fine; keeping its own name is not a clash.
|
|
(await service.UpdateLineAsync("LINE-2", "line-b", null, "AREA-1", rv, Ct)).Ok.ShouldBeTrue();
|
|
(await service.UpdateLineAsync("LINE-2", "line-a", null, "AREA-2", rv, Ct)).Ok.ShouldBeTrue();
|
|
// ...but moving a 'line-a' back under AREA-1 collides with the seeded line-a.
|
|
(await service.UpdateLineAsync("LINE-2", "line-a", null, "AREA-1", rv, Ct)).Ok.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateEquipment_with_a_sibling_name_is_refused()
|
|
{
|
|
var (service, dbName) = Seeded();
|
|
|
|
var clash = await service.CreateEquipmentAsync(Eq("machine-1", "mc_new"), Ct);
|
|
var ok = await service.CreateEquipmentAsync(Eq("machine-2", "mc_new"), Ct);
|
|
|
|
clash.Ok.ShouldBeFalse();
|
|
clash.Error.ShouldNotBeNull().ShouldContain("machine-1");
|
|
ok.Ok.ShouldBeTrue();
|
|
using var db = UnsTreeTestDb.CreateNamed(dbName);
|
|
db.Equipment.Count().ShouldBe(2);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateEquipment_onto_a_sibling_name_is_refused()
|
|
{
|
|
var (service, dbName) = Seeded();
|
|
var created = await service.CreateEquipmentAsync(Eq("machine-2", "mc_2"), Ct);
|
|
byte[] rv;
|
|
using (var db = UnsTreeTestDb.CreateNamed(dbName)) rv = db.Equipment.Single(e => e.EquipmentId == created.CreatedId).RowVersion;
|
|
|
|
var clash = await service.UpdateEquipmentAsync(created.CreatedId!, Eq("machine-1", "mc_2"), rv, Ct);
|
|
var self = await service.UpdateEquipmentAsync(created.CreatedId!, Eq("machine-2", "mc_2"), rv, Ct);
|
|
|
|
clash.Ok.ShouldBeFalse();
|
|
clash.Error.ShouldNotBeNull().ShouldContain("machine-1");
|
|
self.Ok.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateEquipment_moved_to_a_line_that_has_that_name_is_refused()
|
|
{
|
|
var (service, dbName) = Seeded();
|
|
(await service.CreateLineAsync("AREA-1", "LINE-2", "line-b", null, Ct)).Ok.ShouldBeTrue();
|
|
var created = await service.CreateEquipmentAsync(Eq("machine-1", "mc_2", "LINE-2"), Ct); // same name, other line: allowed
|
|
created.Ok.ShouldBeTrue();
|
|
byte[] rv;
|
|
using (var db = UnsTreeTestDb.CreateNamed(dbName)) rv = db.Equipment.Single(e => e.EquipmentId == created.CreatedId).RowVersion;
|
|
|
|
var result = await service.UpdateEquipmentAsync(created.CreatedId!, Eq("machine-1", "mc_2", "LINE-1"), rv, Ct);
|
|
|
|
result.Ok.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Import_reports_a_duplicate_name_as_a_row_error_instead_of_failing_the_batch()
|
|
{
|
|
var (service, dbName) = Seeded();
|
|
|
|
var result = await service.ImportEquipmentAsync(
|
|
[
|
|
Eq("machine-1", "mc_a"), // clashes with the seeded row on the same line
|
|
Eq("fresh", "mc_b"), // good
|
|
Eq("fresh", "mc_c"), // duplicates the previous row inside the batch
|
|
Eq("Fresh", "mc_d"), // case-insensitive, as the SQL index is
|
|
], Ct);
|
|
|
|
result.Inserted.ShouldBe(1);
|
|
result.Errors.Count.ShouldBe(3);
|
|
result.Errors.ShouldContain(e => e.Contains("mc_a") && e.Contains("machine-1"));
|
|
result.Errors.ShouldContain(e => e.Contains("mc_c"));
|
|
result.Errors.ShouldContain(e => e.Contains("mc_d"));
|
|
using var db = UnsTreeTestDb.CreateNamed(dbName);
|
|
db.Equipment.Count().ShouldBe(2);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Import_and_create_reject_an_over_long_column_with_a_message()
|
|
{
|
|
var (service, _) = Seeded();
|
|
var tooLong = new string('x', 65);
|
|
|
|
var import = await service.ImportEquipmentAsync([Eq("e1", "mc_a", manufacturer: tooLong), Eq("e2", "mc_b")], Ct);
|
|
var create = await service.CreateEquipmentAsync(Eq("e3", "mc_c", manufacturer: tooLong), Ct);
|
|
|
|
import.Inserted.ShouldBe(1);
|
|
import.Errors.ShouldHaveSingleItem().ShouldContain("Manufacturer");
|
|
create.Ok.ShouldBeFalse();
|
|
create.Error.ShouldNotBeNull().ShouldContain("Manufacturer");
|
|
}
|
|
|
|
// --- the concurrent-insert backstop -------------------------------------------------------------------
|
|
|
|
private static UnsTreeService ServiceWhoseSaveThrows(string dbName) =>
|
|
new(new ThrowingFactory(dbName));
|
|
|
|
[Fact]
|
|
public async Task A_DbUpdateException_from_the_unique_index_race_becomes_a_friendly_result_everywhere()
|
|
{
|
|
var dbName = $"uns-sib-{Guid.NewGuid():N}";
|
|
UnsTreeTestDb.SeedNamed(dbName);
|
|
var service = ServiceWhoseSaveThrows(dbName);
|
|
byte[] areaRv, lineRv, eqRv;
|
|
using (var db = UnsTreeTestDb.CreateNamed(dbName))
|
|
{
|
|
areaRv = db.UnsAreas.Single().RowVersion;
|
|
lineRv = db.UnsLines.Single().RowVersion;
|
|
eqRv = db.Equipment.Single().RowVersion;
|
|
}
|
|
|
|
var results = new[]
|
|
{
|
|
await service.CreateAreaAsync("MAIN", "AREA-X", "x", null, Ct),
|
|
await service.UpdateAreaAsync("AREA-1", "y", null, "MAIN", areaRv, Ct),
|
|
await service.CreateLineAsync("AREA-1", "LINE-X", "x", null, Ct),
|
|
await service.UpdateLineAsync("LINE-1", "y", null, "AREA-1", lineRv, Ct),
|
|
await service.CreateEquipmentAsync(Eq("x", "mc_x"), Ct),
|
|
await service.UpdateEquipmentAsync(UnsTreeTestDb.SeededEquipmentId, Eq("y", "mc_y"), eqRv, Ct),
|
|
};
|
|
|
|
foreach (var r in results)
|
|
{
|
|
r.Ok.ShouldBeFalse();
|
|
r.Error.ShouldNotBeNull();
|
|
r.Error.ShouldNotContain("SaveChanges"); // never the raw EF text
|
|
}
|
|
|
|
var import = await service.ImportEquipmentAsync([Eq("z", "mc_z")], Ct);
|
|
import.Inserted.ShouldBe(0);
|
|
import.Errors.ShouldNotBeEmpty();
|
|
}
|
|
|
|
private sealed class ThrowOnSave : SaveChangesInterceptor
|
|
{
|
|
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(
|
|
DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default) =>
|
|
throw new DbUpdateException("SaveChanges failed: simulated unique-index violation");
|
|
}
|
|
|
|
private sealed class ThrowingFactory(string name) : IDbContextFactory<OtOpcUaConfigDbContext>
|
|
{
|
|
public OtOpcUaConfigDbContext CreateDbContext() =>
|
|
new(new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
|
|
.UseInMemoryDatabase(name)
|
|
.AddInterceptors(new ThrowOnSave())
|
|
.Options);
|
|
|
|
public Task<OtOpcUaConfigDbContext> CreateDbContextAsync(CancellationToken ct = default) =>
|
|
Task.FromResult(CreateDbContext());
|
|
}
|
|
}
|