138 lines
6.5 KiB
C#
138 lines
6.5 KiB
C#
using Akka.TestKit.Xunit2;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using ZB.MOM.WW.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Integration.Infrastructure;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Integration;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Entities;
|
|
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Migrations;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// SCA-1 end to end: <c>Parked</c> is re-openable. An operator Retry of a parked cached call
|
|
/// re-opens the site tracking row and the central <c>SiteCalls</c> row, so the retried delivery's
|
|
/// outcome lands on both; an operator Discard closes both as <c>Discarded</c>. Before the fix both
|
|
/// stayed <c>Parked</c> forever (site: later writes were no-ops on a terminal row; central: equal
|
|
/// terminal rank was immutable).
|
|
/// </summary>
|
|
public class ParkedReopenCombinedTelemetryTests : TestKit, IClassFixture<MsSqlMigrationFixture>
|
|
{
|
|
private readonly MsSqlMigrationFixture _fixture;
|
|
|
|
/// <summary>Initializes the test with the shared SQL Server migration fixture.</summary>
|
|
/// <param name="fixture">The shared MS SQL migration fixture.</param>
|
|
public ParkedReopenCombinedTelemetryTests(MsSqlMigrationFixture fixture) => _fixture = fixture;
|
|
|
|
private static string NewSiteId() => "test-sca1-" + Guid.NewGuid().ToString("N")[..8];
|
|
|
|
private static CachedCallTelemetry SubmitPacket(TrackedOperationId id, string siteId, DateTime nowUtc) =>
|
|
new(
|
|
Audit: ScadaBridgeAuditEventFactory.Create(
|
|
eventId: Guid.NewGuid(),
|
|
occurredAtUtc: nowUtc,
|
|
channel: AuditChannel.ApiOutbound,
|
|
kind: AuditKind.CachedSubmit,
|
|
correlationId: id.Value,
|
|
sourceSiteId: siteId,
|
|
target: "ERP.GetOrder",
|
|
status: AuditStatus.Submitted),
|
|
Operational: new SiteCallOperational(
|
|
id, "ApiOutbound", "ERP.GetOrder", siteId, null, "Submitted", 0, null, null,
|
|
nowUtc, nowUtc, null));
|
|
|
|
private static CachedCallAttemptContext Ctx(
|
|
TrackedOperationId id, string siteId, CachedCallAttemptOutcome outcome, int retryCount,
|
|
string? lastError, DateTime createdUtc, DateTime occurredUtc) =>
|
|
new(id, "ApiOutbound", "ERP.GetOrder", siteId, outcome, retryCount, lastError, null,
|
|
createdUtc, occurredUtc, null, "Plant.Pump42");
|
|
|
|
private static async Task ParkAsync(CombinedTelemetryHarness harness, TrackedOperationId id, string siteId, DateTime t0)
|
|
{
|
|
await harness.EmitSubmitAsync(SubmitPacket(id, siteId, t0));
|
|
await harness.EmitAttemptAsync(Ctx(id, siteId, CachedCallAttemptOutcome.ParkedMaxRetries, 3, "HTTP 500", t0, t0.AddSeconds(30)));
|
|
Assert.Equal("Parked", (await harness.TrackingStore.GetStatusAsync(id))!.Status);
|
|
}
|
|
|
|
/// <summary>Retry of a parked call, then a successful delivery: site and central both end Delivered.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[SkippableFact]
|
|
public async Task ParkedThenOperatorRetryThenDelivered_SiteAndCentral_EndDelivered()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
|
|
var siteId = NewSiteId();
|
|
var id = TrackedOperationId.New();
|
|
var t0 = new DateTime(2026, 12, 1, 9, 0, 0, DateTimeKind.Utc);
|
|
await using var harness = new CombinedTelemetryHarness(_fixture, this);
|
|
await ParkAsync(harness, id, siteId, t0);
|
|
|
|
await harness.EmitAttemptAsync(Ctx(id, siteId, CachedCallAttemptOutcome.OperatorRetried, 0, null, t0, t0.AddMinutes(5)));
|
|
await harness.TrackingStore.FlushAsync();
|
|
|
|
var reopened = await harness.TrackingStore.GetStatusAsync(id);
|
|
Assert.Equal("Attempted", reopened!.Status);
|
|
Assert.Null(reopened.TerminalAtUtc);
|
|
|
|
await using (var read = harness.CreateReadContext())
|
|
{
|
|
var row = await read.Set<SiteCall>().SingleAsync(s => s.TrackedOperationId == id);
|
|
Assert.Equal("Attempted", row.Status);
|
|
Assert.Null(row.TerminalAtUtc);
|
|
}
|
|
|
|
await harness.EmitAttemptAsync(Ctx(id, siteId, CachedCallAttemptOutcome.Delivered, 1, null, t0, t0.AddMinutes(6)));
|
|
|
|
var delivered = await harness.TrackingStore.GetStatusAsync(id);
|
|
Assert.Equal("Delivered", delivered!.Status);
|
|
Assert.NotNull(delivered.TerminalAtUtc);
|
|
|
|
await using (var read = harness.CreateReadContext())
|
|
{
|
|
var row = await read.Set<SiteCall>().SingleAsync(s => s.TrackedOperationId == id);
|
|
Assert.Equal("Delivered", row.Status);
|
|
Assert.NotNull(row.TerminalAtUtc);
|
|
|
|
// The re-queue is recorded as a second CachedSubmit (Submitted) row.
|
|
var submits = await read.Set<AuditLogRow>()
|
|
.Where(e => e.SourceSiteId == siteId && e.Kind == AuditKind.CachedSubmit)
|
|
.CountAsync();
|
|
Assert.Equal(2, submits);
|
|
}
|
|
}
|
|
|
|
/// <summary>Discard of a parked call: site and central both end Discarded.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[SkippableFact]
|
|
public async Task ParkedThenOperatorDiscard_SiteAndCentral_EndDiscarded()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
|
|
var siteId = NewSiteId();
|
|
var id = TrackedOperationId.New();
|
|
var t0 = new DateTime(2026, 12, 1, 10, 0, 0, DateTimeKind.Utc);
|
|
await using var harness = new CombinedTelemetryHarness(_fixture, this);
|
|
await ParkAsync(harness, id, siteId, t0);
|
|
|
|
await harness.EmitAttemptAsync(Ctx(id, siteId, CachedCallAttemptOutcome.OperatorDiscarded, 3, "Discarded by operator", t0, t0.AddMinutes(5)));
|
|
|
|
var site = await harness.TrackingStore.GetStatusAsync(id);
|
|
Assert.Equal("Discarded", site!.Status);
|
|
Assert.NotNull(site.TerminalAtUtc);
|
|
|
|
await using var read = harness.CreateReadContext();
|
|
var row = await read.Set<SiteCall>().SingleAsync(s => s.TrackedOperationId == id);
|
|
Assert.Equal("Discarded", row.Status);
|
|
|
|
var resolves = await read.Set<AuditLogRow>()
|
|
.Where(e => e.SourceSiteId == siteId && e.Kind == AuditKind.CachedResolve)
|
|
.Select(e => e.Status)
|
|
.ToListAsync();
|
|
Assert.Contains(AuditStatus.Discarded, resolves);
|
|
}
|
|
}
|