-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove the deep cloning and update linking to use the changeset
- Loading branch information
Thomas Anderson
committed
Dec 9, 2024
1 parent
c497790
commit 955a6d0
Showing
34 changed files
with
757 additions
and
268 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Btms.Business.Tests/PreProcessing/MovementPreProcessingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Btms.Backend.Data.InMemory; | ||
using Btms.Business.Pipelines.PreProcessing; | ||
using Btms.Types.Alvs; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using TestDataGenerator; | ||
using Xunit; | ||
|
||
namespace Btms.Business.Tests.PreProcessing; | ||
|
||
public class MovementPreProcessingTests | ||
{ | ||
[Fact] | ||
public async Task WhenNotificationNotExists_ThenShouldBeCreated() | ||
{ | ||
// ARRANGE | ||
var clearanceRequest = CreateAlvsClearanceRequest(); | ||
var dbContext = new MemoryMongoDbContext(); | ||
var preProcessor = new MovementPreProcessor(dbContext, NullLogger<MovementPreProcessor>.Instance); | ||
|
||
|
||
// ACT | ||
var preProcessingResult = await preProcessor.Process( | ||
new PreProcessingContext<AlvsClearanceRequest>(clearanceRequest, "TestMessageId")); | ||
|
||
// ASSERT | ||
preProcessingResult.Outcome.Should().Be(PreProcessingOutcome.New); | ||
var savedMovement = await dbContext.Movements.Find(clearanceRequest!.Header!.EntryReference!); | ||
savedMovement.Should().NotBeNull(); | ||
savedMovement.AuditEntries.Count.Should().Be(1); | ||
savedMovement.AuditEntries[0].Status.Should().Be("Created"); | ||
} | ||
|
||
private static AlvsClearanceRequest CreateAlvsClearanceRequest() | ||
{ | ||
return ClearanceRequestBuilder.Default() | ||
.WithValidDocumentReferenceNumbers().Build(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
Btms.Business.Tests/PreProcessing/NotificationsPreProcessingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Btms.Backend.Data.InMemory; | ||
using Btms.Business.Pipelines.PreProcessing; | ||
using Btms.Types.Ipaffs; | ||
using Btms.Types.Ipaffs.Mapping; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using TestDataGenerator; | ||
using Xunit; | ||
|
||
namespace Btms.Business.Tests.PreProcessing | ||
{ | ||
public class NotificationsPreProcessingTests | ||
{ | ||
[Fact] | ||
public async Task WhenNotificationNotExists_ThenShouldBeCreated() | ||
{ | ||
// ARRANGE | ||
var notification = CreateImportNotification(); | ||
var dbContext = new MemoryMongoDbContext(); | ||
var preProcessor = new ImportNotificationPreProcessor(dbContext, NullLogger<ImportNotificationPreProcessor>.Instance); | ||
|
||
// ACT | ||
var preProcessingResult = await preProcessor.Process( | ||
new PreProcessingContext<ImportNotification>(notification, "TestMessageId")); | ||
|
||
// ASSERT | ||
preProcessingResult.Outcome.Should().Be(PreProcessingOutcome.New); | ||
var savedNotification = await dbContext.Notifications.Find(notification!.ReferenceNumber!); | ||
savedNotification.Should().NotBeNull(); | ||
savedNotification.AuditEntries.Count.Should().Be(1); | ||
savedNotification.AuditEntries[0].Status.Should().Be("Created"); | ||
} | ||
|
||
[Fact] | ||
public async Task WhenNotificationExists_AndLastUpdatedIsNewer_ThenShouldBeUpdated() | ||
{ | ||
// ARRANGE | ||
var notification = CreateImportNotification(); | ||
var dbContext = new MemoryMongoDbContext(); | ||
await dbContext.Notifications.Insert(notification.MapWithTransform()); | ||
notification.LastUpdated = notification.LastUpdated?.AddHours(1); | ||
var preProcessor = new ImportNotificationPreProcessor(dbContext, NullLogger<ImportNotificationPreProcessor>.Instance); | ||
|
||
|
||
// ACT | ||
var preProcessingResult = await preProcessor.Process( | ||
new PreProcessingContext<ImportNotification>(notification, "TestMessageId")); | ||
|
||
// ASSERT | ||
preProcessingResult.Outcome.Should().Be(PreProcessingOutcome.Changed); | ||
var savedNotification = await dbContext.Notifications.Find(notification!.ReferenceNumber!); | ||
savedNotification.Should().NotBeNull(); | ||
savedNotification.AuditEntries.Count.Should().Be(1); | ||
savedNotification.AuditEntries[0].Status.Should().Be("Updated"); | ||
} | ||
|
||
private static ImportNotification CreateImportNotification() | ||
{ | ||
return ImportNotificationBuilder.Default() | ||
.WithReferenceNumber(ImportNotificationTypeEnum.Chedpp, 1, DateTime.UtcNow, 1) | ||
.WithRandomCommodities(1, 2) | ||
.Do(x => | ||
{ | ||
foreach (var parameterSet in x.PartOne?.Commodities?.ComplementParameterSets!) | ||
{ | ||
parameterSet.KeyDataPairs = null; | ||
} | ||
}).Build(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Btms.Model.Data; | ||
|
||
namespace Btms.Business.Pipelines.PreProcessing; | ||
|
||
public interface IPreProcessor<TInput, TOutput> where TOutput : IAuditable | ||
{ | ||
Task<PreProcessingResult<TOutput>> Process(PreProcessingContext<TInput> preProcessingContext); | ||
} |
50 changes: 50 additions & 0 deletions
50
Btms.Business/Pipelines/PreProcessing/ImportNotificationPreProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Btms.Backend.Data; | ||
using Btms.Common.Extensions; | ||
using Btms.Model.ChangeLog; | ||
using Btms.Types.Ipaffs; | ||
using Btms.Types.Ipaffs.Mapping; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Btms.Business.Pipelines.PreProcessing; | ||
|
||
public class ImportNotificationPreProcessor(IMongoDbContext dbContext, ILogger<ImportNotificationPreProcessor> logger) : IPreProcessor<ImportNotification, Model.Ipaffs.ImportNotification> | ||
{ | ||
public async Task<PreProcessingResult<Model.Ipaffs.ImportNotification>> Process(PreProcessingContext<ImportNotification> preProcessingContext) | ||
{ | ||
var internalNotification = preProcessingContext.Message.MapWithTransform(); | ||
var existingNotification = | ||
await dbContext.Notifications.Find(preProcessingContext.Message.ReferenceNumber!); | ||
|
||
if (existingNotification is null) | ||
{ | ||
internalNotification.Create(preProcessingContext.MessageId); | ||
await dbContext.Notifications.Insert(internalNotification); | ||
return PreProcessResult.New(internalNotification); | ||
} | ||
|
||
|
||
if (internalNotification.UpdatedSource.TrimMicroseconds() > | ||
existingNotification.UpdatedSource.TrimMicroseconds()) | ||
{ | ||
internalNotification.AuditEntries = existingNotification.AuditEntries; | ||
internalNotification.CreatedSource = existingNotification.CreatedSource; | ||
|
||
var changeSet = internalNotification.GenerateChangeSet(existingNotification); | ||
|
||
internalNotification.Update(preProcessingContext.MessageId, changeSet); | ||
await dbContext.Notifications.Update(internalNotification, existingNotification._Etag); | ||
|
||
return PreProcessResult.Changed(internalNotification, changeSet); | ||
} | ||
|
||
if (internalNotification.UpdatedSource.TrimMicroseconds() == | ||
existingNotification.UpdatedSource.TrimMicroseconds()) | ||
{ | ||
return PreProcessResult.AlreadyProcessed(existingNotification); | ||
} | ||
|
||
logger.MessageSkipped(preProcessingContext.MessageId!, preProcessingContext.Message.ReferenceNumber!); | ||
return PreProcessResult.Skipped(existingNotification); | ||
|
||
} | ||
} |
Oops, something went wrong.