61c6278bd0
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.
216 lines
9.3 KiB
C#
216 lines
9.3 KiB
C#
using System.Text.Json;
|
|
using Akka.Actor;
|
|
using Akka.TestKit.Xunit2;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.ManagementService.Tests;
|
|
|
|
/// <summary>
|
|
/// CLI-2 (WP2 task 2.8): the site, data-connection and api-method update handlers are
|
|
/// preserve-if-null. A null command field keeps the stored value; an empty string
|
|
/// clears an optional field. Before the fix every field was assigned unconditionally,
|
|
/// so <c>site update --id 3 --name X</c> nulled both gRPC addresses and
|
|
/// <c>api-method update --script …</c> reset the timeout to 30 and dropped both
|
|
/// definitions.
|
|
/// </summary>
|
|
public class PartialUpdateHandlerTests : TestKit, IDisposable
|
|
{
|
|
private readonly ServiceCollection _services = new ServiceCollection().AddTestConfigurationTransaction();
|
|
private readonly ISiteRepository _siteRepo = Substitute.For<ISiteRepository>();
|
|
private readonly IInboundApiRepository _apiRepo = Substitute.For<IInboundApiRepository>();
|
|
|
|
/// <summary>Initializes the harness with stubbed repositories.</summary>
|
|
public PartialUpdateHandlerTests()
|
|
{
|
|
_services.AddScoped(_ => _siteRepo);
|
|
_services.AddScoped(_ => _apiRepo);
|
|
_services.AddScoped(_ => Substitute.For<IAuditService>());
|
|
_apiRepo.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(1);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
void IDisposable.Dispose() => Shutdown();
|
|
|
|
private IActorRef CreateActor() =>
|
|
Sys.ActorOf(Props.Create(() => new ManagementActor(
|
|
_services.BuildServiceProvider(), NullLogger<ManagementActor>.Instance)));
|
|
|
|
private static ManagementEnvelope Admin(object command) =>
|
|
new(new AuthenticatedUser("admin", "Admin", new[] { "Administrator", "Designer" }, Array.Empty<string>()),
|
|
command, Guid.NewGuid().ToString("N"));
|
|
|
|
private static Site StoredSite() => new("Plant", "site-3")
|
|
{
|
|
Id = 3,
|
|
Description = "desc",
|
|
NodeAAddress = "akka.tcp://scadabridge@a:8081",
|
|
NodeBAddress = "akka.tcp://scadabridge@b:8081",
|
|
GrpcNodeAAddress = "http://a:8083",
|
|
GrpcNodeBAddress = "http://b:8083",
|
|
};
|
|
|
|
/// <summary>A name-only site update keeps the description and all four addresses.</summary>
|
|
[Fact]
|
|
public void UpdateSite_NameOnly_PreservesEveryOtherField()
|
|
{
|
|
var site = StoredSite();
|
|
_siteRepo.GetSiteByIdAsync(3, Arg.Any<CancellationToken>()).Returns(site);
|
|
|
|
CreateActor().Tell(Admin(new UpdateSiteCommand(3, "Plant 3", null)));
|
|
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(10));
|
|
|
|
Assert.Equal("Plant 3", site.Name);
|
|
Assert.Equal("desc", site.Description);
|
|
Assert.Equal("akka.tcp://scadabridge@a:8081", site.NodeAAddress);
|
|
Assert.Equal("akka.tcp://scadabridge@b:8081", site.NodeBAddress);
|
|
Assert.Equal("http://a:8083", site.GrpcNodeAAddress);
|
|
Assert.Equal("http://b:8083", site.GrpcNodeBAddress);
|
|
}
|
|
|
|
/// <summary>An address-only site update keeps the name; an empty string clears a field.</summary>
|
|
[Fact]
|
|
public void UpdateSite_EmptyString_ClearsField_NullNameKeepsName()
|
|
{
|
|
var site = StoredSite();
|
|
_siteRepo.GetSiteByIdAsync(3, Arg.Any<CancellationToken>()).Returns(site);
|
|
|
|
CreateActor().Tell(Admin(new UpdateSiteCommand(3, null, "", GrpcNodeBAddress: "")));
|
|
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(10));
|
|
|
|
Assert.Equal("Plant", site.Name);
|
|
Assert.Null(site.Description);
|
|
Assert.Null(site.GrpcNodeBAddress);
|
|
Assert.Equal("http://a:8083", site.GrpcNodeAAddress);
|
|
}
|
|
|
|
/// <summary>A preserved address is still validated; a malformed incoming one is refused.</summary>
|
|
[Fact]
|
|
public void UpdateSite_MalformedIncomingAddress_IsRefused()
|
|
{
|
|
var site = StoredSite();
|
|
_siteRepo.GetSiteByIdAsync(3, Arg.Any<CancellationToken>()).Returns(site);
|
|
|
|
CreateActor().Tell(Admin(new UpdateSiteCommand(3, null, null, GrpcNodeAAddress: "a:8083")));
|
|
var error = ExpectMsg<ManagementError>(TimeSpan.FromSeconds(10));
|
|
|
|
Assert.Contains("GrpcNodeAAddress", error.Error);
|
|
Assert.Equal("http://a:8083", site.GrpcNodeAAddress);
|
|
}
|
|
|
|
/// <summary>A name-only data-connection update keeps protocol, both configs and the retry count.</summary>
|
|
[Fact]
|
|
public void UpdateDataConnection_NameOnly_PreservesConfigsAndRetryCount()
|
|
{
|
|
var conn = new DataConnection("c1", "OpcUa", 1)
|
|
{
|
|
Id = 7,
|
|
PrimaryConfiguration = """{"Endpoint":"opc.tcp://p","UserIdentity":{"Password":"pw"}}""",
|
|
BackupConfiguration = """{"Endpoint":"opc.tcp://b"}""",
|
|
FailoverRetryCount = 9,
|
|
};
|
|
_siteRepo.GetDataConnectionByIdAsync(7, Arg.Any<CancellationToken>()).Returns(conn);
|
|
|
|
CreateActor().Tell(Admin(new UpdateDataConnectionCommand(7, "renamed", null, null)));
|
|
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(10));
|
|
|
|
Assert.Equal("renamed", conn.Name);
|
|
Assert.Equal("OpcUa", conn.Protocol);
|
|
Assert.Contains("\"pw\"", conn.PrimaryConfiguration);
|
|
Assert.Equal("""{"Endpoint":"opc.tcp://b"}""", conn.BackupConfiguration);
|
|
Assert.Equal(9, conn.FailoverRetryCount);
|
|
}
|
|
|
|
/// <summary>An empty backup configuration removes the backup endpoint.</summary>
|
|
[Fact]
|
|
public void UpdateDataConnection_EmptyBackup_RemovesBackup()
|
|
{
|
|
var conn = new DataConnection("c1", "OpcUa", 1)
|
|
{
|
|
Id = 7,
|
|
PrimaryConfiguration = """{"Endpoint":"opc.tcp://p"}""",
|
|
BackupConfiguration = """{"Endpoint":"opc.tcp://b"}""",
|
|
};
|
|
_siteRepo.GetDataConnectionByIdAsync(7, Arg.Any<CancellationToken>()).Returns(conn);
|
|
|
|
CreateActor().Tell(Admin(new UpdateDataConnectionCommand(7, null, null, null, "", 3)));
|
|
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(10));
|
|
|
|
Assert.Null(conn.BackupConfiguration);
|
|
Assert.Equal("""{"Endpoint":"opc.tcp://p"}""", conn.PrimaryConfiguration);
|
|
Assert.Equal(3, conn.FailoverRetryCount);
|
|
}
|
|
|
|
/// <summary>A script-only api-method update keeps the timeout and both definitions.</summary>
|
|
[Fact]
|
|
public void UpdateApiMethod_ScriptOnly_PreservesTimeoutAndDefinitions()
|
|
{
|
|
var method = new ApiMethod("M", "return 1;")
|
|
{
|
|
Id = 5,
|
|
TimeoutSeconds = 120,
|
|
ParameterDefinitions = """[{"name":"x","type":"Integer"}]""",
|
|
ReturnDefinition = """[{"name":"y","type":"Integer"}]""",
|
|
};
|
|
_apiRepo.GetApiMethodByIdAsync(5, Arg.Any<CancellationToken>()).Returns(method);
|
|
|
|
CreateActor().Tell(Admin(new UpdateApiMethodCommand(5, "return 2;", null, null, null)));
|
|
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(10));
|
|
|
|
Assert.Equal("return 2;", method.Script);
|
|
Assert.Equal(120, method.TimeoutSeconds);
|
|
Assert.Equal("""[{"name":"x","type":"Integer"}]""", method.ParameterDefinitions);
|
|
Assert.Equal("""[{"name":"y","type":"Integer"}]""", method.ReturnDefinition);
|
|
}
|
|
|
|
/// <summary>A timeout-only api-method update keeps the script; "" clears a definition.</summary>
|
|
[Fact]
|
|
public void UpdateApiMethod_TimeoutOnly_KeepsScript_EmptyClearsDefinition()
|
|
{
|
|
var method = new ApiMethod("M", "return 1;")
|
|
{
|
|
Id = 5,
|
|
TimeoutSeconds = 30,
|
|
ParameterDefinitions = """[{"name":"x","type":"Integer"}]""",
|
|
};
|
|
_apiRepo.GetApiMethodByIdAsync(5, Arg.Any<CancellationToken>()).Returns(method);
|
|
|
|
CreateActor().Tell(Admin(new UpdateApiMethodCommand(5, null, 90, "", null)));
|
|
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(10));
|
|
|
|
Assert.Equal("return 1;", method.Script);
|
|
Assert.Equal(90, method.TimeoutSeconds);
|
|
Assert.Null(method.ParameterDefinitions);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wire compatibility: an older CLI always sends every field (non-null ints included);
|
|
/// that payload must still deserialize into the now-nullable records with its values.
|
|
/// </summary>
|
|
[Fact]
|
|
public void OlderClientPayloads_StillDeserialize()
|
|
{
|
|
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
|
|
|
|
var dc = JsonSerializer.Deserialize<UpdateDataConnectionCommand>(
|
|
"""{"DataConnectionId":7,"Name":"X","Protocol":"OpcUa","PrimaryConfiguration":"{}","BackupConfiguration":null,"FailoverRetryCount":3}""",
|
|
options)!;
|
|
Assert.Equal(new UpdateDataConnectionCommand(7, "X", "OpcUa", "{}", null, 3), dc);
|
|
|
|
var api = JsonSerializer.Deserialize<UpdateApiMethodCommand>(
|
|
"""{"ApiMethodId":5,"Script":"return 1;","TimeoutSeconds":30,"ParameterDefinitions":null,"ReturnDefinition":null}""",
|
|
options)!;
|
|
Assert.Equal(new UpdateApiMethodCommand(5, "return 1;", 30, null, null), api);
|
|
|
|
var site = JsonSerializer.Deserialize<UpdateSiteCommand>(
|
|
"""{"SiteId":3,"Name":"S","Description":null}""", options)!;
|
|
Assert.Equal(new UpdateSiteCommand(3, "S", null), site);
|
|
}
|
|
}
|