203 lines
9.2 KiB
C#
203 lines
9.2 KiB
C#
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
|
|
using ZB.MOM.WW.ScadaBridge.ManagementService;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.ManagementService.Tests;
|
|
|
|
/// <summary>
|
|
/// Tests for <see cref="ManagementEndpoints"/> request-body parsing
|
|
/// parsing and Ask-timeout resolution.
|
|
/// </summary>
|
|
public class ManagementEndpointsTests
|
|
{
|
|
/// <summary>Verifies that parsing a request with an explicit payload deserializes into the command type.</summary>
|
|
[Fact]
|
|
public void ParseCommand_WithExplicitPayload_DeserializesIntoCommandType()
|
|
{
|
|
var json = """{ "command": "CreateSite", "payload": { "name": "Site1", "siteIdentifier": "SITE1", "description": "Desc" } }""";
|
|
|
|
var result = ManagementEndpoints.ParseCommand(json);
|
|
|
|
Assert.True(result.Success);
|
|
var command = Assert.IsType<CreateSiteCommand>(result.Command);
|
|
Assert.Equal("Site1", command.Name);
|
|
Assert.Equal("SITE1", command.SiteIdentifier);
|
|
Assert.Equal("Desc", command.Description);
|
|
}
|
|
|
|
/// <summary>
|
|
/// <c>MgmtDeploySiteCommand.Force</c> is an ADDITIVE field, so a client
|
|
/// that predates it — every CLI older than this change, and every scripted
|
|
/// HTTP caller — must keep working and must land on the SAFE default. Absent
|
|
/// means <c>false</c> means "skip instances whose config is unchanged", which
|
|
/// is the non-destructive behaviour; only an explicit <c>true</c> restores the
|
|
/// unconditional redeploy.
|
|
/// </summary>
|
|
/// <param name="json">The management request body to parse.</param>
|
|
/// <param name="expectedForce">The expected value of the command's <c>Force</c> flag.</param>
|
|
[Theory]
|
|
[InlineData("""{ "command": "MgmtDeploySite", "payload": { "siteId": 7 } }""", false)]
|
|
[InlineData("""{ "command": "MgmtDeploySite", "payload": { "siteId": 7, "force": false } }""", false)]
|
|
[InlineData("""{ "command": "MgmtDeploySite", "payload": { "siteId": 7, "force": true } }""", true)]
|
|
public void ParseCommand_DeploySite_ForceDefaultsToTheNonDestructiveBehaviour(string json, bool expectedForce)
|
|
{
|
|
var result = ManagementEndpoints.ParseCommand(json);
|
|
|
|
Assert.True(result.Success);
|
|
var command = Assert.IsType<MgmtDeploySiteCommand>(result.Command);
|
|
Assert.Equal(7, command.SiteId);
|
|
Assert.Equal(expectedForce, command.Force);
|
|
}
|
|
|
|
/// <summary>
|
|
/// <c>MgmtDeployArea</c> (Gitea #40) parses with the same safe <c>Force</c>
|
|
/// default as the site deploy: absent means skip unchanged instances.
|
|
/// </summary>
|
|
/// <param name="json">The management request body to parse.</param>
|
|
/// <param name="expectedForce">The expected value of the command's <c>Force</c> flag.</param>
|
|
[Theory]
|
|
[InlineData("""{ "command": "MgmtDeployArea", "payload": { "areaId": 12 } }""", false)]
|
|
[InlineData("""{ "command": "MgmtDeployArea", "payload": { "areaId": 12, "force": true } }""", true)]
|
|
public void ParseCommand_DeployArea_ForceDefaultsToTheNonDestructiveBehaviour(string json, bool expectedForce)
|
|
{
|
|
var result = ManagementEndpoints.ParseCommand(json);
|
|
|
|
Assert.True(result.Success);
|
|
var command = Assert.IsType<MgmtDeployAreaCommand>(result.Command);
|
|
Assert.Equal(12, command.AreaId);
|
|
Assert.Equal(expectedForce, command.Force);
|
|
}
|
|
|
|
/// <summary>Verifies that parsing a request with no payload deserializes a parameterless command.</summary>
|
|
[Fact]
|
|
public void ParseCommand_WithMissingPayload_DeserializesParameterlessCommand()
|
|
{
|
|
// No "payload" field at all -- the fallback must not allocate a throwaway
|
|
// JsonDocument and must still produce a valid parameterless command.
|
|
var json = """{ "command": "ListTemplates" }""";
|
|
|
|
var result = ManagementEndpoints.ParseCommand(json);
|
|
|
|
Assert.True(result.Success);
|
|
Assert.IsType<ListTemplatesCommand>(result.Command);
|
|
}
|
|
|
|
/// <summary>Verifies that parsing invalid JSON returns a failure.</summary>
|
|
[Fact]
|
|
public void ParseCommand_WithInvalidJson_ReturnsFailure()
|
|
{
|
|
var result = ManagementEndpoints.ParseCommand("{ not json");
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("BAD_REQUEST", result.ErrorCode);
|
|
}
|
|
|
|
/// <summary>Verifies that parsing a request without a command field returns a failure.</summary>
|
|
[Fact]
|
|
public void ParseCommand_WithMissingCommandField_ReturnsFailure()
|
|
{
|
|
var result = ManagementEndpoints.ParseCommand("""{ "payload": {} }""");
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("BAD_REQUEST", result.ErrorCode);
|
|
}
|
|
|
|
/// <summary>Verifies that parsing an unknown command returns a failure.</summary>
|
|
[Fact]
|
|
public void ParseCommand_WithUnknownCommand_ReturnsFailure()
|
|
{
|
|
var result = ManagementEndpoints.ParseCommand("""{ "command": "NoSuchCommand" }""");
|
|
|
|
Assert.False(result.Success);
|
|
Assert.Equal("BAD_REQUEST", result.ErrorCode);
|
|
}
|
|
|
|
// ========================================================================
|
|
// Command-timeout configuration
|
|
//
|
|
// ManagementServiceOptions.CommandTimeout must actually drive the Ask
|
|
// timeout instead of a hard-coded 30s constant.
|
|
// ========================================================================
|
|
|
|
/// <summary>Verifies that the Ask timeout uses the configured command timeout.</summary>
|
|
[Fact]
|
|
public void ResolveAskTimeout_UsesConfiguredCommandTimeout()
|
|
{
|
|
var options = new ManagementServiceOptions { CommandTimeout = TimeSpan.FromSeconds(75) };
|
|
|
|
var timeout = ManagementEndpoints.ResolveAskTimeout(options);
|
|
|
|
Assert.Equal(TimeSpan.FromSeconds(75), timeout);
|
|
}
|
|
|
|
/// <summary>Verifies that null options fall back to the default Ask timeout.</summary>
|
|
[Fact]
|
|
public void ResolveAskTimeout_WithNullOptions_FallsBackToDefault()
|
|
{
|
|
var timeout = ManagementEndpoints.ResolveAskTimeout(null);
|
|
|
|
Assert.Equal(TimeSpan.FromSeconds(30), timeout);
|
|
}
|
|
|
|
/// <summary>Verifies that a non-positive configured timeout falls back to the default Ask timeout.</summary>
|
|
[Fact]
|
|
public void ResolveAskTimeout_WithNonPositiveTimeout_FallsBackToDefault()
|
|
{
|
|
// A misconfigured zero/negative timeout would make every Ask fail
|
|
// immediately; fall back to the safe default instead.
|
|
var options = new ManagementServiceOptions { CommandTimeout = TimeSpan.Zero };
|
|
|
|
var timeout = ManagementEndpoints.ResolveAskTimeout(options);
|
|
|
|
Assert.Equal(TimeSpan.FromSeconds(30), timeout);
|
|
}
|
|
|
|
// ========================================================================
|
|
// Per-command long-running Ask timeout (arch-review S1)
|
|
//
|
|
// Transport/deploy commands legitimately run for minutes; they must use the
|
|
// LongRunningCommandTimeout (default 5 min) instead of the 30 s default.
|
|
// ========================================================================
|
|
|
|
/// <summary>Verifies that long-running commands use the long Ask timeout.</summary>
|
|
/// <param name="cmd">The long-running command type under test.</param>
|
|
[Theory]
|
|
[InlineData(typeof(ImportBundleCommand))]
|
|
[InlineData(typeof(ExportBundleCommand))]
|
|
[InlineData(typeof(PreviewBundleCommand))]
|
|
[InlineData(typeof(MgmtDeployArtifactsCommand))]
|
|
[InlineData(typeof(MgmtDeployInstanceCommand))]
|
|
public void ResolveAskTimeout_LongRunningCommand_UsesLongTimeout(Type cmd)
|
|
=> Assert.Equal(TimeSpan.FromMinutes(5), ManagementEndpoints.ResolveAskTimeout(null, cmd));
|
|
|
|
/// <summary>Verifies that an ordinary command uses the default Ask timeout.</summary>
|
|
[Fact]
|
|
public void ResolveAskTimeout_OrdinaryCommand_UsesDefault()
|
|
=> Assert.Equal(TimeSpan.FromSeconds(30), ManagementEndpoints.ResolveAskTimeout(null, typeof(ListTemplatesCommand)));
|
|
|
|
/// <summary>Verifies that a configured long-running timeout is honored.</summary>
|
|
[Fact]
|
|
public void ResolveAskTimeout_ConfiguredLongTimeout_Honored()
|
|
=> Assert.Equal(TimeSpan.FromMinutes(10), ManagementEndpoints.ResolveAskTimeout(
|
|
new ManagementServiceOptions { LongRunningCommandTimeout = TimeSpan.FromMinutes(10) },
|
|
typeof(ImportBundleCommand)));
|
|
/// <summary>Verifies that a bulk Ask timeout leaves a grace period beyond the overall scheduling deadline.</summary>
|
|
[Fact]
|
|
public void BulkAsk_LeavesGraceBeyondOverallSchedulingDeadline()
|
|
{
|
|
var options = new ManagementServiceOptions { LongRunningCommandTimeout = TimeSpan.FromMinutes(2) };
|
|
Assert.Equal(TimeSpan.FromMinutes(3), ManagementEndpoints.ResolveAskTimeout(options, typeof(MgmtDeploySiteCommand)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// The area-subtree bulk deploy (Gitea #40) is a bulk deploy too: same
|
|
/// long-running deadline plus the same cleanup grace as the site deploy.
|
|
/// </summary>
|
|
[Fact]
|
|
public void BulkAsk_AreaDeploy_GetsTheSameGraceAsSiteDeploy()
|
|
{
|
|
var options = new ManagementServiceOptions { LongRunningCommandTimeout = TimeSpan.FromMinutes(2) };
|
|
Assert.Equal(TimeSpan.FromMinutes(3), ManagementEndpoints.ResolveAskTimeout(options, typeof(MgmtDeployAreaCommand)));
|
|
Assert.Equal(TimeSpan.FromMinutes(6), ManagementEndpoints.ResolveAskTimeout(null, typeof(MgmtDeployAreaCommand)));
|
|
}
|
|
}
|