Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/PartialUpdateCommandTests.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

99 lines
4.6 KiB
C#

using System.CommandLine;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
/// <summary>
/// CLI-2 (WP2 task 2.8): <c>site update</c>, <c>data-connection update</c> and
/// <c>api-method update</c> send only the options actually passed. Before the fix an
/// omitted option was sent as null (or as a default: <c>--failover-retry-count</c> 3,
/// <c>--timeout</c> 30) and the server overwrote the stored value — e.g.
/// <c>site update --id 3 --name X</c> nulled both gRPC addresses. The server now treats
/// null as "keep"; these tests pin that the CLI sends null for every omitted option.
/// </summary>
public class PartialUpdateCommandTests
{
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 async Task<T> ParseAsync<T>(
Func<Option<string>, Option<string>, Option<string>, Option<string>, Func<object, int>, Command> build,
params string[] args)
{
var captured = new List<object>();
var root = new RootCommand { Url, Username, Password, Format };
root.Add(build(Url, Format, Username, Password, cmd => { captured.Add(cmd); return 0; }));
var exit = await root.Parse(args).InvokeAsync();
Assert.Equal(0, exit);
return Assert.IsType<T>(Assert.Single(captured));
}
/// <summary>A name-only site update leaves every other field null (= unchanged).</summary>
[Fact]
public async Task SiteUpdate_NameOnly_SendsNullForEveryOtherField()
{
var cmd = await ParseAsync<UpdateSiteCommand>(SiteCommands.BuildUpdate, "update", "--id", "3", "--name", "Plant 3");
Assert.Equal(new UpdateSiteCommand(3, "Plant 3", null, null, null, null, null), cmd);
}
/// <summary>A site update can change one address without supplying the name.</summary>
[Fact]
public async Task SiteUpdate_AddressOnly_SendsNullName()
{
var cmd = await ParseAsync<UpdateSiteCommand>(SiteCommands.BuildUpdate,
"update", "--id", "3", "--grpc-node-a-address", "http://a:8083");
Assert.Null(cmd.Name);
Assert.Equal("http://a:8083", cmd.GrpcNodeAAddress);
Assert.Null(cmd.GrpcNodeBAddress);
}
/// <summary>An explicitly empty option is sent as "" (the server clears the field).</summary>
[Fact]
public async Task SiteUpdate_EmptyDescription_SendsEmptyStringToClear()
{
var cmd = await ParseAsync<UpdateSiteCommand>(SiteCommands.BuildUpdate, "update", "--id", "3", "--description", "");
Assert.Equal("", cmd.Description);
Assert.Null(cmd.Name);
}
/// <summary>A name-only data-connection update sends no protocol, configs or retry count.</summary>
[Fact]
public async Task DataConnectionUpdate_NameOnly_SendsNullForEveryOtherField()
{
var cmd = await ParseAsync<UpdateDataConnectionCommand>(DataConnectionCommands.BuildUpdate,
"update", "--id", "7", "--name", "X");
Assert.Equal(new UpdateDataConnectionCommand(7, "X", null, null, null, null), cmd);
}
/// <summary>A passed retry count and protocol are forwarded.</summary>
[Fact]
public async Task DataConnectionUpdate_PassedOptions_AreForwarded()
{
var cmd = await ParseAsync<UpdateDataConnectionCommand>(DataConnectionCommands.BuildUpdate,
"update", "--id", "7", "--protocol", "OpcUa", "--failover-retry-count", "5", "--backup-config", "");
Assert.Equal(new UpdateDataConnectionCommand(7, null, "OpcUa", null, "", 5), cmd);
}
/// <summary>A script-only api-method update keeps the timeout and both definitions.</summary>
[Fact]
public async Task ApiMethodUpdate_ScriptOnly_SendsNullTimeoutAndDefinitions()
{
var cmd = await ParseAsync<UpdateApiMethodCommand>(ApiMethodCommands.BuildUpdate,
"update", "--id", "5", "--script", "return 1;");
Assert.Equal(new UpdateApiMethodCommand(5, "return 1;", null, null, null, null), cmd);
}
/// <summary>A timeout-only api-method update sends no script.</summary>
[Fact]
public async Task ApiMethodUpdate_TimeoutOnly_SendsNullScript()
{
var cmd = await ParseAsync<UpdateApiMethodCommand>(ApiMethodCommands.BuildUpdate,
"update", "--id", "5", "--timeout", "120");
Assert.Null(cmd.Script);
Assert.Equal(120, cmd.TimeoutSeconds);
}
}