Skip to content

Commit 98e2c52

Browse files
committed
Merge branch 'develop'
2 parents 48803aa + c44c5c8 commit 98e2c52

2 files changed

Lines changed: 75 additions & 29 deletions

File tree

src/protagonist/API.Tests/Integration/CustomerQueueTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,7 @@ public async Task Post_TestBatch_MarksBatchAsSuperseded_IfNoImagesFound()
14021402

14031403
var dbBatch = await dbContext.Batches.SingleAsync(b => b.Id == 201);
14041404
dbBatch.Superseded.Should().BeTrue();
1405+
dbBatch.Finished.Should().NotBeNull("Batch was marked as finished");
14051406

14061407
A.CallTo(() =>
14071408
NotificationSender.SendBatchCompletedMessage(
@@ -1410,6 +1411,33 @@ public async Task Post_TestBatch_MarksBatchAsSuperseded_IfNoImagesFound()
14101411
.MustHaveHappened();
14111412
}
14121413

1414+
[Fact]
1415+
public async Task Post_TestBatch_MarksBatchAsSuperseded_IfNoImagesFound_NoNotificationRaisedIfAlreadyFinished()
1416+
{
1417+
// Arrange
1418+
const int batchId = 2011;
1419+
await dbContext.Batches.AddTestBatch(batchId, finished: DateTime.UtcNow);
1420+
await dbContext.SaveChangesAsync();
1421+
var path = $"customers/99/queue/batches/{batchId}/test";
1422+
1423+
// Act
1424+
var response = await httpClient.AsCustomer().PostAsync(path, null!);
1425+
1426+
// Assert
1427+
response.StatusCode.Should().Be(HttpStatusCode.OK);
1428+
var jsonDoc = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
1429+
jsonDoc.RootElement.GetProperty("success").GetBoolean().Should().BeTrue();
1430+
1431+
var dbBatch = await dbContext.Batches.SingleAsync(b => b.Id == batchId);
1432+
dbBatch.Superseded.Should().BeTrue();
1433+
1434+
A.CallTo(() =>
1435+
NotificationSender.SendBatchCompletedMessage(
1436+
A<Batch>.That.Matches(b => b.Id == dbBatch.Id),
1437+
A<CancellationToken>._))
1438+
.MustNotHaveHappened();
1439+
}
1440+
14131441
[Fact]
14141442
public async Task Post_TestBatch_MarksBatchAsComplete_IfImagesFoundAndAllFinished_AndBatchNotFinished()
14151443
{
@@ -1511,6 +1539,40 @@ public async Task Post_TestBatch_DoesNotChangeBatchFinished_IfImagesFoundAndAllF
15111539
.MustNotHaveHappened();
15121540
}
15131541

1542+
[Fact]
1543+
public async Task Post_TestBatch_DoesNotFinishBatch_IfImagesFoundAndAllFinished_ButFinishedBeforeBatchSubmitted()
1544+
{
1545+
// This test covers the case where existing Assets were reprocessed, they have been finished in the past but this
1546+
// was prior to batch creation
1547+
const int batch = 206;
1548+
var submitted = DateTime.UtcNow.AddDays(3);
1549+
await dbContext.Batches.AddTestBatch(batch, count: 3, submitted: submitted);
1550+
await dbContext.Images.AddTestAsset(AssetId.FromString("2/1/june"), batch: batch, finished: DateTime.UtcNow);
1551+
await dbContext.Images.AddTestAsset(AssetId.FromString("2/1/hunger"), batch: batch, finished: DateTime.UtcNow);
1552+
await dbContext.Images.AddTestAsset(AssetId.FromString("2/1/grace"), batch: batch, finished: DateTime.UtcNow);
1553+
await dbContext.SaveChangesAsync();
1554+
var path = $"customers/99/queue/batches/{batch}/test";
1555+
1556+
// Act
1557+
var response = await httpClient.AsCustomer().PostAsync(path, null!);
1558+
1559+
// Assert
1560+
response.StatusCode.Should().Be(HttpStatusCode.OK);
1561+
var jsonDoc = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
1562+
jsonDoc.RootElement.GetProperty("success").GetBoolean().Should().BeFalse();
1563+
1564+
var dbBatch = await dbContext.Batches.SingleAsync(b => b.Id == batch);
1565+
dbBatch.Superseded.Should().BeFalse();
1566+
dbBatch.Finished.Should().BeNull("All images are completed but they completed before submission");
1567+
dbBatch.Count.Should().Be(3);
1568+
1569+
A.CallTo(() =>
1570+
NotificationSender.SendBatchCompletedMessage(
1571+
A<Batch>.That.Matches(b => b.Id == dbBatch.Id),
1572+
A<CancellationToken>._))
1573+
.MustNotHaveHappened();
1574+
}
1575+
15141576
[Fact]
15151577
public async Task Post_CreateBatch_201_IfImageOptimisationPolicySetForImage_AndLegacyEnabled()
15161578
{

src/protagonist/API/Features/Queues/Requests/TestBatch.cs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,20 @@
1010
namespace API.Features.Queues.Requests;
1111

1212
/// <summary>
13-
/// Tests batch to check if it has been superseded or is complete
13+
/// Tests batch to check if it has been superseded, is complete or has incorrect counts
1414
/// </summary>
15-
public class TestBatch : IRequest<bool?>
15+
public class TestBatch(int customerId, int batchId) : IRequest<bool?>
1616
{
17-
public int CustomerId { get; }
18-
public int BatchId { get; }
19-
20-
public TestBatch(int customerId, int batchId)
21-
{
22-
CustomerId = customerId;
23-
BatchId = batchId;
24-
}
17+
public int CustomerId { get; } = customerId;
18+
public int BatchId { get; } = batchId;
2519
}
2620

27-
public class TestBatchHandler : IRequestHandler<TestBatch, bool?>
21+
public class TestBatchHandler(
22+
DlcsContext dlcsContext,
23+
IBatchCompletedNotificationSender batchCompletedNotificationSender,
24+
ILogger<TestBatchHandler> logger)
25+
: IRequestHandler<TestBatch, bool?>
2826
{
29-
private readonly DlcsContext dlcsContext;
30-
private readonly ILogger<TestBatchHandler> logger;
31-
private readonly IBatchCompletedNotificationSender batchCompletedNotificationSender;
32-
33-
public TestBatchHandler(
34-
DlcsContext dlcsContext,
35-
IBatchCompletedNotificationSender batchCompletedNotificationSender,
36-
ILogger<TestBatchHandler> logger)
37-
{
38-
this.dlcsContext = dlcsContext;
39-
this.batchCompletedNotificationSender = batchCompletedNotificationSender;
40-
this.logger = logger;
41-
}
42-
4327
public async Task<bool?> Handle(TestBatch request, CancellationToken cancellationToken)
4428
{
4529
var batch = await GetBatchToTest(request, cancellationToken);
@@ -54,12 +38,12 @@ public TestBatchHandler(
5438
bool changesMade = false;
5539
if (!batch.Superseded && IsBatchSuperseded(batchImages))
5640
{
57-
logger.LogDebug("Batch {BatchId} for superseded", request.BatchId);
41+
logger.LogDebug("Batch {BatchId} superseded", request.BatchId);
5842
batch.Superseded = true;
5943
changesMade = true;
6044
}
6145

62-
if (IsBatchComplete(batchImages))
46+
if (IsBatchComplete(batch, batchImages))
6347
{
6448
logger.LogDebug("Batch {BatchId} complete", request.BatchId);
6549
if (batch.Finished == null)
@@ -91,8 +75,8 @@ public TestBatchHandler(
9175
return false;
9276
}
9377

94-
private static bool IsBatchComplete(IEnumerable<Asset> batchImages)
95-
=> batchImages.All(i => i.Finished != null);
78+
private static bool IsBatchComplete(Batch batch, IEnumerable<Asset> batchImages)
79+
=> batchImages.All(i => i.Finished != null && i.Finished >= batch.Submitted);
9680

9781
private Task<Batch?> GetBatchToTest(TestBatch request, CancellationToken cancellationToken)
9882
{

0 commit comments

Comments
 (0)