a26d6ba317
The Native Alarm Source Overrides card had no bulk affordance — the CSV path shipped CLI-only (instance native-alarm-source import --file), so the UI could only retarget one source at a time inline. Adds a second InputFile on that card, mirroring the attribute importer's UX (hidden input behind a button-styled label, 512 KB cap, success/error alert with the per-line error list, toast). Parsing reuses the SHARED NativeAlarmSourceOverrideCsvParser — the exact parser the CLI uses — and the new pure InstanceConfigure.BuildNativeAlarmSourceCsvImport applies the same batch rules the server enforces in ManagementActor.HandleSetInstanceNativeAlarmSourceOverrides: the source must resolve, must not be template-locked, and may appear at most once; any error rejects the whole file and applies nothing. Semantics match the CLI: merge, not full replace — sources absent from the file keep their existing override, a blank field keeps the inherited value, and an all-blank row clears that source's override (equivalent to the CLI's all-null override row, without leaving a dead row behind). Persistence reuses the inline editor's path: SaveNativeOverride's upsert body is extracted to UpsertNativeOverrideCore (no SaveChangesAsync inside), so the import commits the validated batch in one SaveChangesAsync — no new server method, no duplicated parsing. Tests: InstanceConfigureNativeAlarmCsvImportTests (9) — happy path, blank-field inheritance, all-blank clear, merge semantics, unknown/locked/duplicate source and parser-error rejection, plus structural pins on the InputFile wiring. Docs: Component-CentralUI.md native-alarm-source card gains the import bullet.
200 lines
8.3 KiB
C#
200 lines
8.3 KiB
C#
using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Deployment;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Components;
|
|
|
|
/// <summary>
|
|
/// The Instance Configure page's Native Alarm Source Overrides card accepts a CSV of
|
|
/// per-instance retargets via an <c><InputFile></c> — the UI half of the CLI's
|
|
/// <c>instance native-alarm-source import --file</c>. The upload is parsed with the
|
|
/// SHARED <see cref="NativeAlarmSourceOverrideCsvParser"/> (no duplicated parsing),
|
|
/// validated against the template's source bindings with the same batch rules the
|
|
/// server enforces (name resolves, not template-locked, no duplicate source), and —
|
|
/// all-or-nothing — either upserted through the SAME repository path the inline Save
|
|
/// uses or rejected with the per-line error list and nothing applied.
|
|
///
|
|
/// <para>
|
|
/// <c>InstanceConfigure</c> is a heavyweight page (≈7 injected services incl. the
|
|
/// flattening pipeline), so — consistent with the attribute-importer and native-alarm
|
|
/// card coverage — the parse→validate→build-rows core is extracted to an
|
|
/// <c>internal static</c> helper exercised directly here, plus structural assertions
|
|
/// over the component source that pin the InputFile + reuse-the-existing-save-path
|
|
/// wiring.
|
|
/// </para>
|
|
/// </summary>
|
|
public class InstanceConfigureNativeAlarmCsvImportTests
|
|
{
|
|
private static string InstanceConfigureMarkup
|
|
{
|
|
get
|
|
{
|
|
var dir = AppContext.BaseDirectory;
|
|
for (var i = 0; i < 6 && dir is not null; i++)
|
|
dir = Directory.GetParent(dir)?.FullName;
|
|
return File.ReadAllText(Path.Combine(dir!, "src", "ZB.MOM.WW.ScadaBridge.CentralUI",
|
|
"Components", "Pages", "Deployment", "InstanceConfigure.razor"));
|
|
}
|
|
}
|
|
|
|
private static List<TemplateNativeAlarmSource> Sources() => new()
|
|
{
|
|
new TemplateNativeAlarmSource("BoilerAlarms")
|
|
{
|
|
ConnectionName = "opc-a", SourceReference = "ns=2;s=Boiler", IsLocked = false
|
|
},
|
|
new TemplateNativeAlarmSource("PumpAlarms")
|
|
{
|
|
ConnectionName = "opc-a", SourceReference = "ns=2;s=Pump", IsLocked = false
|
|
},
|
|
new TemplateNativeAlarmSource("LockedAlarms")
|
|
{
|
|
ConnectionName = "opc-a", SourceReference = "ns=2;s=Locked", IsLocked = true
|
|
},
|
|
};
|
|
|
|
// ── Core: valid CSV → rows, no errors ───────────────────────────────────
|
|
|
|
[Fact]
|
|
public void ValidCsv_BuildsRetargetRows_WithNoErrors()
|
|
{
|
|
var csv = "SourceName,Connection,SourceReference,Filter\n"
|
|
+ "BoilerAlarms,opc-b,ns=2;s=Boiler2,HighHigh\n"
|
|
+ "PumpAlarms,,ns=2;s=Pump2,\n";
|
|
var parsed = NativeAlarmSourceOverrideCsvParser.Parse(csv);
|
|
|
|
var outcome = InstanceConfigure.BuildNativeAlarmSourceCsvImport(parsed, Sources());
|
|
|
|
Assert.False(outcome.HasErrors);
|
|
Assert.Empty(outcome.Errors);
|
|
Assert.Equal(2, outcome.Rows.Count);
|
|
|
|
var boiler = outcome.Rows[0];
|
|
Assert.Equal("BoilerAlarms", boiler.SourceName);
|
|
Assert.Equal("opc-b", boiler.Connection);
|
|
Assert.Equal("ns=2;s=Boiler2", boiler.SourceReference);
|
|
Assert.Equal("HighHigh", boiler.Filter);
|
|
Assert.False(boiler.IsClear);
|
|
|
|
// Blank field = keep the inherited value (null), same as the inline editor.
|
|
var pump = outcome.Rows[1];
|
|
Assert.Null(pump.Connection);
|
|
Assert.Equal("ns=2;s=Pump2", pump.SourceReference);
|
|
Assert.Null(pump.Filter);
|
|
}
|
|
|
|
[Fact]
|
|
public void AllBlankRow_IsMarkedAsClear()
|
|
{
|
|
// Every field blank = "inherit everything" — the caller clears any existing
|
|
// override row, exactly as the inline Save does when all fields are blank.
|
|
var csv = "SourceName,Connection,SourceReference,Filter\nBoilerAlarms,,,\n";
|
|
var parsed = NativeAlarmSourceOverrideCsvParser.Parse(csv);
|
|
|
|
var outcome = InstanceConfigure.BuildNativeAlarmSourceCsvImport(parsed, Sources());
|
|
|
|
Assert.False(outcome.HasErrors);
|
|
var row = Assert.Single(outcome.Rows);
|
|
Assert.True(row.IsClear);
|
|
}
|
|
|
|
[Fact]
|
|
public void SourcesAbsentFromTheFile_AreUntouched_MergeNotReplace()
|
|
{
|
|
// Merge semantics (matching the CLI): only the named source is in the batch;
|
|
// PumpAlarms/LockedAlarms keep whatever override they already have.
|
|
var csv = "SourceName,Connection,SourceReference,Filter\nBoilerAlarms,opc-b,,\n";
|
|
var parsed = NativeAlarmSourceOverrideCsvParser.Parse(csv);
|
|
|
|
var outcome = InstanceConfigure.BuildNativeAlarmSourceCsvImport(parsed, Sources());
|
|
|
|
Assert.False(outcome.HasErrors);
|
|
Assert.Equal("BoilerAlarms", Assert.Single(outcome.Rows).SourceName);
|
|
}
|
|
|
|
// ── Core: bad rows → errors, NO rows (all-or-nothing) ───────────────────
|
|
|
|
[Fact]
|
|
public void UnknownSource_ProducesError_AndAppliesNothing()
|
|
{
|
|
var csv = "SourceName,Connection,SourceReference,Filter\n"
|
|
+ "DoesNotExist,opc-b,,\n"
|
|
+ "BoilerAlarms,opc-b,,\n";
|
|
var parsed = NativeAlarmSourceOverrideCsvParser.Parse(csv);
|
|
|
|
var outcome = InstanceConfigure.BuildNativeAlarmSourceCsvImport(parsed, Sources());
|
|
|
|
Assert.True(outcome.HasErrors);
|
|
Assert.Empty(outcome.Rows); // all-or-nothing: nothing applied
|
|
Assert.Contains(outcome.Errors, e => e.Contains("DoesNotExist") && e.Contains("Line 2"));
|
|
}
|
|
|
|
[Fact]
|
|
public void TemplateLockedSource_IsRejected_LikeTheServer()
|
|
{
|
|
var csv = "SourceName,Connection,SourceReference,Filter\nLockedAlarms,opc-b,,\n";
|
|
var parsed = NativeAlarmSourceOverrideCsvParser.Parse(csv);
|
|
|
|
var outcome = InstanceConfigure.BuildNativeAlarmSourceCsvImport(parsed, Sources());
|
|
|
|
Assert.True(outcome.HasErrors);
|
|
Assert.Empty(outcome.Rows);
|
|
Assert.Contains(outcome.Errors, e => e.Contains("LockedAlarms") && e.Contains("locked"));
|
|
}
|
|
|
|
[Fact]
|
|
public void DuplicateSource_IsRejected_AndAppliesNothing()
|
|
{
|
|
var csv = "SourceName,Connection,SourceReference,Filter\n"
|
|
+ "BoilerAlarms,opc-b,,\n"
|
|
+ "BoilerAlarms,opc-c,,\n";
|
|
var parsed = NativeAlarmSourceOverrideCsvParser.Parse(csv);
|
|
|
|
var outcome = InstanceConfigure.BuildNativeAlarmSourceCsvImport(parsed, Sources());
|
|
|
|
Assert.True(outcome.HasErrors);
|
|
Assert.Empty(outcome.Rows);
|
|
Assert.Contains(outcome.Errors, e => e.Contains("BoilerAlarms") && e.Contains("more than once"));
|
|
}
|
|
|
|
[Fact]
|
|
public void ParserErrors_PropagateThrough_AndApplyNothing()
|
|
{
|
|
// A bad header makes the parser emit an error and zero rows; the import must
|
|
// surface that error and apply nothing.
|
|
var csv = "Wrong,Header\nBoilerAlarms,opc-b,,\n";
|
|
var parsed = NativeAlarmSourceOverrideCsvParser.Parse(csv);
|
|
|
|
var outcome = InstanceConfigure.BuildNativeAlarmSourceCsvImport(parsed, Sources());
|
|
|
|
Assert.True(outcome.HasErrors);
|
|
Assert.Empty(outcome.Rows);
|
|
Assert.NotEmpty(outcome.Errors);
|
|
}
|
|
|
|
// ── Structural: InputFile + reuse-the-existing-save-path wiring ─────────
|
|
|
|
[Fact]
|
|
public void Page_WiresNativeAlarmSourceCsvInputFile_WithTestHooks()
|
|
{
|
|
var markup = InstanceConfigureMarkup;
|
|
Assert.Contains("data-test=\"nas-csv-import-input\"", markup);
|
|
Assert.Contains("data-test=\"nas-csv-import-result\"", markup);
|
|
Assert.Contains("OnNativeAlarmSourceCsvImportSelectedAsync", markup);
|
|
// Reuses the SHARED parser — no duplicated CSV parsing in the UI.
|
|
Assert.Contains("NativeAlarmSourceOverrideCsvParser.Parse", markup);
|
|
Assert.Contains("BuildNativeAlarmSourceCsvImport", markup);
|
|
}
|
|
|
|
[Fact]
|
|
public void Import_AppliesViaTheExistingRepositoryUpsertPath()
|
|
{
|
|
var markup = InstanceConfigureMarkup;
|
|
// The inline Save and the CSV import share one persistence helper.
|
|
Assert.Contains("UpsertNativeOverrideCore", markup);
|
|
Assert.Contains("ClearNativeOverrideCore", markup);
|
|
// Same size cap as the attribute importer.
|
|
Assert.Contains("MaxCsvImportBytes", markup);
|
|
}
|
|
}
|