62fe1e685d
CLI import no longer has to reset MES overrides to inherit; a legacy 4-column file still parses and the Central UI import keeps the stored flag for it. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
227 lines
9.9 KiB
C#
227 lines
9.9 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 ───────────────────────────────────
|
|
|
|
/// <summary>Verifies that a valid CSV builds retarget rows with no errors.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Gitea #102: the MesReportable column flows through the import rows.</summary>
|
|
[Fact]
|
|
public void MesColumn_FlowsThroughToImportRows()
|
|
{
|
|
var csv = "SourceName,Connection,SourceReference,Filter,MesReportable\n"
|
|
+ "BoilerAlarms,,,,true\n"
|
|
+ "PumpAlarms,,,,\n";
|
|
var parsed = NativeAlarmSourceOverrideCsvParser.Parse(csv);
|
|
|
|
var outcome = InstanceConfigure.BuildNativeAlarmSourceCsvImport(parsed, Sources());
|
|
|
|
Assert.False(outcome.HasErrors);
|
|
Assert.True(outcome.Rows[0].HasMesColumn);
|
|
Assert.True(outcome.Rows[0].MesReportable);
|
|
Assert.True(outcome.Rows[1].HasMesColumn);
|
|
Assert.Null(outcome.Rows[1].MesReportable);
|
|
}
|
|
|
|
/// <summary>Verifies that a row with all fields blank is marked as a clear.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that sources absent from the file are left untouched because the import merges rather than replaces.</summary>
|
|
[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) ───────────────────
|
|
|
|
/// <summary>Verifies that an unknown source produces an error and nothing is applied.</summary>
|
|
[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"));
|
|
}
|
|
|
|
/// <summary>Verifies that a template-locked source is rejected, matching the server's behavior.</summary>
|
|
[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"));
|
|
}
|
|
|
|
/// <summary>Verifies that a duplicate source is rejected and nothing is applied.</summary>
|
|
[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"));
|
|
}
|
|
|
|
/// <summary>Verifies that parser errors propagate through and nothing is applied.</summary>
|
|
[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 ─────────
|
|
|
|
/// <summary>Verifies that the page wires the native-alarm-source CSV input file with its test hooks.</summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>Verifies that the import applies changes through the existing repository upsert path.</summary>
|
|
[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);
|
|
}
|
|
}
|