Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/UpdateCommandContractTests.cs
T
Joseph Doherty 61c6278bd0 fix(cli): site/data-connection/api-method update are preserve-if-null partial updates (CLI-2, WP2 task 2.8)
Decision 2.8: preserve-if-null on the server. UpdateSiteCommand,
UpdateDataConnectionCommand and UpdateApiMethodCommand fields (except the id)
are now nullable; the ManagementActor handlers apply only non-null values and
treat an empty string as 'clear this optional field'. The CLI update commands
no longer require --name/--protocol/--script and drop the --failover-retry-count
(3) and --timeout (30) defaults, so an omitted option travels as null instead of
overwriting the stored value (site update --name used to null both gRPC
addresses). The site gRPC-address validation now runs on the merged values.

Additive/compatible: the fields only became nullable, so an older CLI that
always sends every field still deserializes (pinned by a test) and now simply
updates every field as before. The Central UI full-entity forms send "" for a
field the operator cleared (description/addresses, removed backup endpoint,
cleared parameter/return definitions) so clearing keeps working.
2026-12-03 09:48:00 -05:00

160 lines
8.7 KiB
C#

using System.CommandLine;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
/// <summary>
/// Regression tests for the update commands. Site, data-connection and api-method updates are
/// preserve-if-null partial updates (CLI-2, see <see cref="PartialUpdateCommandTests"/>); the
/// remaining <c>Update*Command</c> records in Commons carry
/// non-nullable "core" fields (e.g. <c>string Name</c>, <c>string Protocol</c>,
/// <c>string Script</c>) — an update is a <em>whole-entity replace</em>, not a sparse
/// patch. The CLI must therefore mark those core flags as <c>Required</c>: making them
/// optional would let an omitted flag send <c>null</c>/empty and silently blank the
/// field server-side. These tests pin that contract so the documented surface and the
/// implemented surface stay aligned.
/// </summary>
public class UpdateCommandContractTests
{
private static readonly Option<string> Url = new("--url") { Recursive = true };
private static readonly Option<string> Username = new("--username") { Recursive = true };
private static readonly Option<string> Password = new("--password") { Recursive = true };
private static readonly Option<string> Format = CliOptions.CreateFormatOption();
private static Command UpdateCommand(Command group, params string[] path)
{
var current = group;
foreach (var segment in path)
current = current.Subcommands.Single(c => c.Name == segment);
return current;
}
private static void AssertRequired(Command command, params string[] requiredOptionNames)
{
foreach (var name in requiredOptionNames)
{
var option = command.Options.SingleOrDefault(o => o.Name == name);
Assert.True(option != null, $"'{command.Name}' is missing expected option '{name}'.");
Assert.True(option!.Required, $"'{command.Name}' option '{name}' must be Required (whole-replace contract).");
}
}
/// <summary>Verifies that the template update command marks its core fields as required.</summary>
[Fact]
public void TemplateUpdate_CoreFieldsRequired()
=> AssertRequired(UpdateCommand(TemplateCommands.Build(Url, Format, Username, Password), "update"), "--name");
/// <summary>Verifies that the template attribute update command marks its core fields as required.</summary>
[Fact]
public void TemplateAttributeUpdate_CoreFieldsRequired()
=> AssertRequired(UpdateCommand(TemplateCommands.Build(Url, Format, Username, Password), "attribute", "update"), "--name", "--data-type");
/// <summary>Verifies that the template alarm update command marks its core fields as required.</summary>
[Fact]
public void TemplateAlarmUpdate_CoreFieldsRequired()
=> AssertRequired(UpdateCommand(TemplateCommands.Build(Url, Format, Username, Password), "alarm", "update"), "--name", "--trigger-type", "--priority");
/// <summary>Verifies that the template script update command marks its core fields as required.</summary>
[Fact]
public void TemplateScriptUpdate_CoreFieldsRequired()
=> AssertRequired(UpdateCommand(TemplateCommands.Build(Url, Format, Username, Password), "script", "update"), "--name", "--code");
/// <summary>CLI-2: site update is a preserve-if-null partial update, so only <c>--id</c> is required.</summary>
[Fact]
public void SiteUpdate_IsPartial_OnlyIdRequired()
{
var update = UpdateCommand(SiteCommands.Build(Url, Format, Username, Password), "update");
AssertRequired(update, "--id");
AssertOptional(update, "--name", "--description", "--node-a-address", "--node-b-address",
"--grpc-node-a-address", "--grpc-node-b-address");
}
/// <summary>CLI-2: data-connection update is a preserve-if-null partial update, so only <c>--id</c> is required.</summary>
[Fact]
public void DataConnectionUpdate_IsPartial_OnlyIdRequired()
{
var update = UpdateCommand(DataConnectionCommands.Build(Url, Format, Username, Password), "update");
AssertRequired(update, "--id");
AssertOptional(update, "--name", "--protocol", "--primary-config", "--backup-config", "--failover-retry-count");
}
/// <summary>Verifies that the external system update command marks its core fields as required.</summary>
[Fact]
public void ExternalSystemUpdate_CoreFieldsRequired()
=> AssertRequired(UpdateCommand(ExternalSystemCommands.Build(Url, Format, Username, Password), "update"), "--name", "--endpoint-url", "--auth-type");
/// <summary>Verifies that the notification update command requires only the name and leaves the recipient options conditionally validated.</summary>
[Fact]
public void NotificationUpdate_CoreFieldsRequired()
{
// Only --name is unconditionally required. The recipient flags are channel-
// conditional (SMS Notifications S6): --emails is required for --type email and
// --phones for --type sms, so neither can be flagged Required at the option level
// — the create/update builders validate the channel/recipient pairing instead.
var update = UpdateCommand(NotificationCommands.Build(Url, Format, Username, Password), "update");
AssertRequired(update, "--name");
foreach (var name in new[] { "--emails", "--phones" })
{
var option = update.Options.SingleOrDefault(o => o.Name == name);
Assert.True(option != null, $"notification update is missing option '{name}'.");
Assert.False(option!.Required, $"notification update option '{name}' must be conditionally validated, not Required.");
}
}
/// <summary>Verifies that the SMS update command requires only the id and account SID and leaves the sender and token options optional.</summary>
[Fact]
public void SmsUpdate_CoreFieldsRequired()
{
// --id and --account-sid are unconditionally Required. --from-number is NOT Required:
// it is either-or with --messaging-service-sid (Twilio Messaging-Service-only configs),
// so the builder validates "at least one sender identity" instead of the option layer.
// --auth-token stays optional (preserve-if-omitted; empty == omitted, never "clear").
var update = UpdateCommand(NotificationCommands.Build(Url, Format, Username, Password), "sms", "update");
AssertRequired(update, "--id", "--account-sid");
foreach (var name in new[] { "--from-number", "--auth-token" })
{
var option = update.Options.SingleOrDefault(o => o.Name == name);
Assert.True(option != null, $"sms update is missing option '{name}'.");
Assert.False(option!.Required, $"sms update '{name}' must be conditionally validated / optional, not Required.");
}
}
/// <summary>CLI-2: api-method update is a preserve-if-null partial update, so only <c>--id</c> is required.</summary>
[Fact]
public void ApiMethodUpdate_IsPartial_OnlyIdRequired()
{
var update = UpdateCommand(ApiMethodCommands.Build(Url, Format, Username, Password), "update");
AssertRequired(update, "--id");
AssertOptional(update, "--script", "--timeout", "--parameters", "--return-def", "--trace-all-calls");
}
private static void AssertOptional(Command command, params string[] optionNames)
{
foreach (var name in optionNames)
{
var option = command.Options.SingleOrDefault(o => o.Name == name);
Assert.True(option != null, $"'{command.Name}' is missing expected option '{name}'.");
Assert.False(option!.Required, $"'{command.Name}' option '{name}' must be optional (partial update).");
Assert.False(option.HasDefaultValue, $"'{command.Name}' option '{name}' must have no default — an omitted option must travel as null.");
}
}
/// <summary>Verifies that the external system method update command keeps all of its fields optional because it is a sparse update.</summary>
[Fact]
public void ExternalSystemMethodUpdate_IsGenuinelySparse_CoreFieldsOptional()
{
// UpdateExternalSystemMethodCommand is the one update record whose fields are
// genuinely all-nullable, so its flags are correctly optional. Pin that too so
// it is not mistakenly forced to Required.
var update = UpdateCommand(ExternalSystemCommands.Build(Url, Format, Username, Password), "method", "update");
foreach (var name in new[] { "--name", "--http-method", "--path" })
{
var option = update.Options.SingleOrDefault(o => o.Name == name);
Assert.True(option != null, $"method update is missing option '{name}'.");
Assert.False(option!.Required, $"method update option '{name}' should be optional (sparse-patch record).");
}
}
}