513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
267 lines
12 KiB
C#
267 lines
12 KiB
C#
using System.CommandLine;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using ZB.MOM.WW.ScadaBridge.CLI;
|
|
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests.Commands;
|
|
|
|
/// <summary>
|
|
/// Tests for the <c>scadabridge audit backfill-source-node</c> subcommand:
|
|
/// argument parsing, request-body construction, HTTP wiring, and CLI scaffold.
|
|
/// </summary>
|
|
[Collection("Console")]
|
|
public class AuditBackfillCommandTests
|
|
{
|
|
// ─────────────────────────────────────────────────────────────────────
|
|
// BuildRequestBody
|
|
// ─────────────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>Verifies that building a request body from default arguments contains the expected sentinel, before and batch size fields.</summary>
|
|
[Fact]
|
|
public void BuildRequestBody_DefaultArgs_ContainsExpectedFields()
|
|
{
|
|
var args = new AuditBackfillSourceNodeArgs
|
|
{
|
|
Sentinel = "unknown",
|
|
Before = "2026-01-01T00:00:00Z",
|
|
BatchSize = 5000,
|
|
};
|
|
|
|
var body = AuditBackfillHelpers.BuildRequestBody(args);
|
|
using var doc = JsonDocument.Parse(body);
|
|
var root = doc.RootElement;
|
|
|
|
Assert.Equal("unknown", root.GetProperty("sentinel").GetString());
|
|
Assert.Equal("2026-01-01T00:00:00Z", root.GetProperty("before").GetString());
|
|
Assert.Equal(5000, root.GetProperty("batchSize").GetInt32());
|
|
}
|
|
|
|
/// <summary>Verifies that a custom sentinel and batch size are reflected in the request body JSON.</summary>
|
|
[Fact]
|
|
public void BuildRequestBody_CustomSentinelAndBatch_ReflectedInJson()
|
|
{
|
|
var args = new AuditBackfillSourceNodeArgs
|
|
{
|
|
Sentinel = "pre-feature",
|
|
Before = "2026-06-01T00:00:00Z",
|
|
BatchSize = 1000,
|
|
};
|
|
|
|
var body = AuditBackfillHelpers.BuildRequestBody(args);
|
|
using var doc = JsonDocument.Parse(body);
|
|
var root = doc.RootElement;
|
|
|
|
Assert.Equal("pre-feature", root.GetProperty("sentinel").GetString());
|
|
Assert.Equal("2026-06-01T00:00:00Z", root.GetProperty("before").GetString());
|
|
Assert.Equal(1000, root.GetProperty("batchSize").GetInt32());
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────────────────────
|
|
// RunBackfillAsync — HTTP execution
|
|
// ─────────────────────────────────────────────────────────────────────
|
|
|
|
private sealed class CapturingHandler : HttpMessageHandler
|
|
{
|
|
private readonly HttpStatusCode _status;
|
|
private readonly string _responseBody;
|
|
|
|
/// <summary>Initializes a new handler that returns the given status and body for every request.</summary>
|
|
/// <param name="status">The HTTP status code to return.</param>
|
|
/// <param name="responseBody">The response body to return.</param>
|
|
public CapturingHandler(HttpStatusCode status, string responseBody)
|
|
{
|
|
_status = status;
|
|
_responseBody = responseBody;
|
|
}
|
|
|
|
/// <summary>Gets the path and query of the last request received.</summary>
|
|
public string? LastRequestUri { get; private set; }
|
|
/// <summary>Gets the body of the last request received.</summary>
|
|
public string? LastRequestBody { get; private set; }
|
|
/// <summary>Gets the HTTP method of the last request received.</summary>
|
|
public string? LastMethod { get; private set; }
|
|
|
|
/// <inheritdoc />
|
|
protected override async Task<HttpResponseMessage> SendAsync(
|
|
HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
LastRequestUri = request.RequestUri!.PathAndQuery;
|
|
LastMethod = request.Method.Method;
|
|
if (request.Content != null)
|
|
{
|
|
LastRequestBody = await request.Content.ReadAsStringAsync(cancellationToken);
|
|
}
|
|
return new HttpResponseMessage(_status)
|
|
{
|
|
Content = new StringContent(_responseBody, Encoding.UTF8, "application/json"),
|
|
};
|
|
}
|
|
}
|
|
|
|
private static string SuccessBody(long rowsUpdated = 42, string sentinel = "unknown", string before = "2026-01-01T00:00:00.0000000Z")
|
|
=> JsonSerializer.Serialize(new { rowsUpdated, sentinel, before });
|
|
|
|
/// <summary>Verifies that a successful backfill returns exit code zero and writes the response to the output.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task RunBackfill_Success_ReturnsZeroAndWritesOutput()
|
|
{
|
|
var handler = new CapturingHandler(HttpStatusCode.OK, SuccessBody(rowsUpdated: 42));
|
|
var client = new ManagementHttpClient(new HttpClient(handler), "http://localhost:9001", "u", "p");
|
|
var output = new StringWriter();
|
|
|
|
var args = new AuditBackfillSourceNodeArgs
|
|
{
|
|
Sentinel = "unknown",
|
|
Before = "2026-01-01T00:00:00Z",
|
|
BatchSize = 5000,
|
|
};
|
|
|
|
var exit = await AuditBackfillHelpers.RunBackfillAsync(client, args, output);
|
|
|
|
Assert.Equal(0, exit);
|
|
var text = output.ToString();
|
|
Assert.Contains("42", text);
|
|
Assert.Contains("backfill complete", text, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
/// <summary>Verifies that the backfill request is sent to the backfill endpoint path.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task RunBackfill_RequestUri_ContainsBackfillPath()
|
|
{
|
|
var handler = new CapturingHandler(HttpStatusCode.OK, SuccessBody());
|
|
var client = new ManagementHttpClient(new HttpClient(handler), "http://localhost:9001", "u", "p");
|
|
var output = new StringWriter();
|
|
|
|
await AuditBackfillHelpers.RunBackfillAsync(
|
|
client,
|
|
new AuditBackfillSourceNodeArgs { Sentinel = "unknown", Before = "2026-01-01T00:00:00Z" },
|
|
output);
|
|
|
|
Assert.Contains("backfill-source-node", handler.LastRequestUri);
|
|
Assert.Equal("POST", handler.LastMethod);
|
|
}
|
|
|
|
/// <summary>Verifies that the backfill request body contains the sentinel and the before timestamp.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task RunBackfill_RequestBody_ContainsSentinelAndBefore()
|
|
{
|
|
var handler = new CapturingHandler(HttpStatusCode.OK, SuccessBody());
|
|
var client = new ManagementHttpClient(new HttpClient(handler), "http://localhost:9001", "u", "p");
|
|
var output = new StringWriter();
|
|
|
|
await AuditBackfillHelpers.RunBackfillAsync(
|
|
client,
|
|
new AuditBackfillSourceNodeArgs
|
|
{
|
|
Sentinel = "pre-feature",
|
|
Before = "2026-01-01T00:00:00Z",
|
|
BatchSize = 2000,
|
|
},
|
|
output);
|
|
|
|
Assert.NotNull(handler.LastRequestBody);
|
|
using var doc = JsonDocument.Parse(handler.LastRequestBody!);
|
|
Assert.Equal("pre-feature", doc.RootElement.GetProperty("sentinel").GetString());
|
|
Assert.Equal("2026-01-01T00:00:00Z", doc.RootElement.GetProperty("before").GetString());
|
|
Assert.Equal(2000, doc.RootElement.GetProperty("batchSize").GetInt32());
|
|
}
|
|
|
|
/// <summary>Verifies that an HTTP 403 response yields exit code 2.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task RunBackfill_Http403_ReturnsExitCode2()
|
|
{
|
|
var handler = new CapturingHandler(HttpStatusCode.Forbidden,
|
|
"{\"error\":\"Permission required.\",\"code\":\"UNAUTHORIZED\"}");
|
|
var client = new ManagementHttpClient(new HttpClient(handler), "http://localhost:9001", "u", "p");
|
|
var output = new StringWriter();
|
|
|
|
var exit = await AuditBackfillHelpers.RunBackfillAsync(
|
|
client,
|
|
new AuditBackfillSourceNodeArgs { Sentinel = "unknown", Before = "2026-01-01T00:00:00Z" },
|
|
output);
|
|
|
|
Assert.Equal(2, exit);
|
|
}
|
|
|
|
/// <summary>Verifies that an HTTP 500 response yields exit code 1.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task RunBackfill_Http500_ReturnsExitCode1()
|
|
{
|
|
var handler = new CapturingHandler(HttpStatusCode.InternalServerError,
|
|
"{\"error\":\"boom\",\"code\":\"INTERNAL\"}");
|
|
var client = new ManagementHttpClient(new HttpClient(handler), "http://localhost:9001", "u", "p");
|
|
var output = new StringWriter();
|
|
|
|
var exit = await AuditBackfillHelpers.RunBackfillAsync(
|
|
client,
|
|
new AuditBackfillSourceNodeArgs { Sentinel = "unknown", Before = "2026-01-01T00:00:00Z" },
|
|
output);
|
|
|
|
Assert.Equal(1, exit);
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────────────────────
|
|
// CLI parsing
|
|
// ─────────────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>Verifies that the backfill-source-node subcommand exists in the audit command group.</summary>
|
|
[Fact]
|
|
public void BackfillSourceNode_Subcommand_ExistsInAuditCommandGroup()
|
|
{
|
|
var root = AuditCommandTestHarness.BuildRoot();
|
|
var parse = root.Parse(new[] { "audit", "backfill-source-node", "--help" });
|
|
Assert.Empty(parse.Errors);
|
|
}
|
|
|
|
/// <summary>Verifies that the before option is required on the backfill-source-node subcommand.</summary>
|
|
[Fact]
|
|
public void BackfillSourceNode_BeforeOption_IsRequired()
|
|
{
|
|
var root = AuditCommandTestHarness.BuildRoot();
|
|
var (exit, _, err) = AuditCommandTestHarness.Invoke(root, "audit", "backfill-source-node");
|
|
Assert.NotEqual(0, exit);
|
|
}
|
|
|
|
/// <summary>Verifies that the help text describes the sentinel and before options.</summary>
|
|
[Fact]
|
|
public void BackfillSourceNode_HelpText_DescribesSentinelAndBefore()
|
|
{
|
|
var root = AuditCommandTestHarness.BuildRoot();
|
|
var output = new StringWriter();
|
|
var exit = root.Parse(new[] { "audit", "backfill-source-node", "--help" })
|
|
.Invoke(new InvocationConfiguration { Output = output });
|
|
|
|
Assert.Equal(0, exit);
|
|
var text = output.ToString();
|
|
Assert.Contains("sentinel", text, StringComparison.OrdinalIgnoreCase);
|
|
Assert.Contains("before", text, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
/// <summary>Verifies that the default sentinel value is unknown.</summary>
|
|
[Fact]
|
|
public void BackfillSourceNode_DefaultSentinel_IsUnknown()
|
|
{
|
|
// Verify the default sentinel value is "unknown" as documented.
|
|
var url = new Option<string>("--url") { Recursive = true };
|
|
var username = new Option<string>("--username") { Recursive = true };
|
|
var password = new Option<string>("--password") { Recursive = true };
|
|
var format = CliOptions.CreateFormatOption();
|
|
|
|
var auditGroup = AuditCommands.Build(url, format, username, password);
|
|
var backfillCmd = auditGroup.Subcommands
|
|
.FirstOrDefault(c => c.Name == "backfill-source-node");
|
|
|
|
Assert.NotNull(backfillCmd);
|
|
|
|
// The subcommand exists and its description mentions maintenance/sentinel.
|
|
Assert.False(string.IsNullOrWhiteSpace(backfillCmd!.Description));
|
|
}
|
|
}
|