Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDMS-215 adds tests for alvs decisions arriving before btms decisions, and fixed an issue with the pairing not working in this case #104

Merged
merged 4 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Btms.Analytics.Tests/FinalisedOnlyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace Btms.Analytics.Tests;
public class FinalisedOnlyTests(ITestOutputHelper output) : MultipleScenarioGeneratorBaseTest(output)
{
[Theory]
[InlineData(typeof(Mrn24GBDDJER3ZFRMZAR9ScenarioGenerator), false, true)]
[InlineData(typeof(Mrn24GBDDJER3ZFRMZAR9ScenarioGenerator), true, false)]
[InlineData(typeof(Mrn24Gbddjer3Zfrmzar9ScenarioGenerator), false, true)]
[InlineData(typeof(Mrn24Gbddjer3Zfrmzar9ScenarioGenerator), true, false)]
public async Task ShouldReturnCorrectAggregation(Type generatorType, bool finalisedOnly, bool returnsResults)
{
EnsureEnvironmentInitialised(generatorType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Btms.Analytics.Tests;

public class MovementsByUniqueDocumentReferenceTests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24GBDDJER3ZFRMZAR9ScenarioGenerator>(output)
: ScenarioGeneratorBaseTest<Mrn24Gbddjer3Zfrmzar9ScenarioGenerator>(output)
{
[Fact]
public async Task ShouldReturnExpectedAggregation()
Expand Down
94 changes: 94 additions & 0 deletions Btms.Backend.IntegrationTests/DecisionFinalisationOrderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using FluentAssertions;
using TestDataGenerator.Scenarios.SpecificFiles;
using TestGenerator.IntegrationTesting.Backend;
using TestGenerator.IntegrationTesting.Backend.Extensions;
using Xunit;
using Xunit.Abstractions;

namespace Btms.Backend.IntegrationTests;

/// <summary>
/// At the moment the integration tests mostly process everything sequentially in the expected order
/// however in reality, sometimes we process decisions and finalisations before the import notifications,
/// so we want to ensure we cover these with tests
/// </summary>
/// <param name="output"></param>
[Trait("Category", "Integration")]
public class DecisionFinalisationOrderTests(ITestOutputHelper output) : MultipleScenarioGeneratorBaseTest(output)
{

static readonly List<object[]> scenarios = [[typeof(Mrn24Gbde8Olvkzxsyar1ScenarioGenerator), "C03"],
[typeof(Mrn24Gbde8Olvkzxsyar1ImportNotificationsAtEndScenarioGenerator), "C03"]
];

public static IEnumerable<object[]> Scenarios()
{
foreach (var scenario in scenarios)
{
yield return [scenario[0]];
}
}

public static IEnumerable<object[]> ScenariosWithExpectedDecisionCode()
{
foreach (var scenario in scenarios)
{
yield return [scenario[0], scenario[1]];
}
}

[Theory]
[MemberData(nameof(ScenariosWithExpectedDecisionCode))]
public void ShouldHaveCorrectDecisionCode(Type generatorType, string expectedDecisionCode)
{
base.TestOutputHelper.WriteLine("Generator : {0}, Decision Code : {1}", generatorType!.FullName, expectedDecisionCode);
EnsureEnvironmentInitialised(generatorType);
CheckDecisionCode(expectedDecisionCode);
}

private void CheckDecisionCode(string expectedDecisionCode)
{
var movement =
Client
.GetSingleMovement();

TestOutputHelper.WriteLine("MRN {0}, expectedDecisionCode {1}", movement.EntryReference, expectedDecisionCode);

movement
.Decisions!.MaxBy(d => d.ServiceHeader!.ServiceCalled)?
.Items!.First()
.Checks!.First()
.DecisionCode.Should().Be(expectedDecisionCode);
}

[Theory]
[MemberData(nameof(Scenarios))]
public void ShouldHaveMaximumBtmsDecisionCode(Type generatorType)
{
EnsureEnvironmentInitialised(generatorType);

var movement = Client
.GetSingleMovement();

movement
.AlvsDecisionStatus.Context.DecisionComparison!.BtmsDecisionNumber
.Should()
.Be(movement.Decisions.Max(d => d.Header.DecisionNumber));
}


[Theory]
[MemberData(nameof(Scenarios))]
public void ShouldHaveMaximumAlvsDecisionCode(Type generatorType)
{
EnsureEnvironmentInitialised(generatorType);

var movement = Client
.GetSingleMovement();

movement.AlvsDecisionStatus.Context.AlvsDecisionNumber
.Should()
.Be(movement.AlvsDecisionStatus.Decisions.Max(d => d.Decision.Header.DecisionNumber));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ public void ShouldHaveCorrectAuditEntries()
(CreatedBySystem.Btms, "Decision", 1, null),
(CreatedBySystem.Btms, "Linked", null, null), //TODO : can we get context in here including the notification info
(CreatedBySystem.Btms, "Decision", 2, 1),
(CreatedBySystem.Btms, "Decision", 3, 2),
(CreatedBySystem.Alvs, "Decision", 1, null), //TODO : we should be able to use the IBM provided file to get some context

(CreatedBySystem.Btms, "Decision", 3, 2),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Btms.Backend.IntegrationTests.DecisionTests;

[Trait("Category", "Integration"), Trait("Segment", "CDMS-205-Ac3")]
public class ChedPpPhsiPartiallyRejected(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24GBDPN81VSULAGAR9ScenarioGenerator>(output)
: ScenarioGeneratorBaseTest<Mrn24Gbdpn81Vsulagar9ScenarioGenerator>(output)
{

/// <summary>
Expand Down Expand Up @@ -105,9 +105,9 @@ public void ShouldHaveCorrectDecisionAuditEntries()
decisionWithLinkAndContext.Context.As<DecisionContext>()!.ImportNotifications!
.Select(n => (n.Id, n.Version))
.Should()
.Equal([
.Equal(
( notification.ReferenceNumber!, 1 )
]);
);
}

// [Fact]
Expand All @@ -124,15 +124,15 @@ public void ShouldHave2AlvsDecisions()
[FailingFact(jiraTicket:"CDMS-205", "Has Ched PP Checks"), Trait("JiraTicket", "CDMS-205")]
public void ShouldHaveDecisionMatched()
{
var movement = Client
Client
.GetSingleMovement()
.AlvsDecisionStatus.Context!.DecisionComparison!.DecisionMatched
.Should().BeTrue();
}

// [Fact]
[FailingFact(jiraTicket:"CDMS-205", "Has Ched PP Checks"), Trait("JiraTicket", "CDMS-205")]
public void ShouldHaveChedPPDecisionStatus()
public void ShouldHaveChedPpDecisionStatus()
{
Client
.GetSingleMovement()
Expand Down Expand Up @@ -169,8 +169,6 @@ public void ShouldHaveCorrectBtmsStatus()
[Fact]
public async Task ShouldNotHaveExceptions()
{
// TestOutputHelper.WriteLine("Querying for aggregated data");

var result = await Client
.GetExceptions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Btms.Backend.IntegrationTests.DecisionTests;

[Trait("Category", "Integration"), Trait("Segment", "CDMS-205-Ac5")]
public class Mrn24GBDDJER3ZFRMZAR9Tests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24GBDDJER3ZFRMZAR9ScenarioGenerator>(output)
public class Mrn24Gbddjer3Zfrmzar9Tests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24Gbddjer3Zfrmzar9ScenarioGenerator>(output)
{

[FailingFact(jiraTicket:"CDMS-235"), Trait("JiraTicket", "CDMS-235")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Btms.Backend.IntegrationTests.DecisionTests;

[Trait("Category", "Integration")]
public class Mrn24Gbde3Cf94H96Tar0ModifiedTests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24GBDE3CF94H96TAR0ModifiedScenarioGenerator>(output)
: ScenarioGeneratorBaseTest<Mrn24Gbde3Cf94H96Tar0ModifiedScenarioGenerator>(output)
{

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Btms.Backend.IntegrationTests.DecisionTests;

[Trait("Category", "Integration")]
public class Mrn24Gbde3Cf94H96Tar0Tests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24GBDE3CF94H96TAR0ScenarioGenerator>(output)
: ScenarioGeneratorBaseTest<Mrn24Gbde3Cf94H96Tar0ScenarioGenerator>(output)
{
[FailingFact(jiraTicket:"CDMS-234"), Trait("JiraTicket", "CDMS-234")]
// [Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
namespace Btms.Backend.IntegrationTests.DecisionTests;

[Trait("Category", "Integration")]
public class Mrn24GBDEHMFC4WGXVAR7Tests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24GBDEHMFC4WGXVAR7ScenarioGenerator>(output)
public class Mrn24Gbdehmfc4Wgxvar7Tests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24Gbdehmfc4Wgxvar7ScenarioGenerator>(output)
{
[FailingFact(jiraTicket:"CDMS-229"), Trait("JiraTicket", "CDMS-229")]
public void ShouldHaveCorrectAlvsDecisionMatchedStatusOnDecison()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using Btms.Common.Extensions;
using Btms.Model.Auditing;
using Btms.Model.Cds;
using Btms.Types.Ipaffs;
using FluentAssertions;
using TestDataGenerator.Scenarios.SpecificFiles;
using TestGenerator.IntegrationTesting.Backend;
using TestGenerator.IntegrationTesting.Backend.Extensions;
using Xunit;
using Xunit.Abstractions;
using ImportNotificationTypeEnum = Btms.Model.Ipaffs.ImportNotificationTypeEnum;

namespace Btms.Backend.IntegrationTests.DecisionTests;

[Trait("Category", "Integration"), Trait("Segment", "CDMS-249")]
public class Mrn24GBDYHI8LMFLDQAR6Tests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24GBDYHI8LMFLDQAR6ScenarioGenerator>(output)
public class Mrn24Gbdyhi8Lmfldqar6Tests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<Mrn24Gbdyhi8Lmfldqar6ScenarioGenerator>(output)
{
[FailingFact(jiraTicket:"CDMS-242"), Trait("JiraTicket", "CDMS-242")]
// [Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Btms.Backend.IntegrationTests.PreprocessingTests;

[Trait("Category", "Integration")]
public class EnsureDuplicateItemsAreNotCreatedTests(ITestOutputHelper output)
: ScenarioGeneratorBaseTest<DuplicateMovementItems_CDMS_211>(output)
: ScenarioGeneratorBaseTest<DuplicateMovementItemsCdms211>(output)
{
[Fact]
public void ShouldNotCreateDuplicateItems()
Expand Down
36 changes: 20 additions & 16 deletions Btms.Business/Builders/MovementBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using Btms.Common.Extensions;
using Btms.Model;
using Btms.Model.Auditing;
using Btms.Model.Cds;
using Btms.Model.ChangeLog;
using Microsoft.Extensions.Logging;
using Btms.Business.Extensions;
using Btms.Model;
using Btms.Model.Ipaffs;
using Microsoft.Extensions.Logging;

namespace Btms.Business.Builders;

Expand Down Expand Up @@ -99,14 +98,14 @@ public MovementBuilder MergeFinalisation(string path, CdsFinalisation finalisati
return this;
}

public MovementBuilder MergeDecision(string path, Model.Cds.CdsDecision decision, List<DecisionImportNotifications>? notificationContext)
public MovementBuilder MergeDecision(string path, CdsDecision decision, List<DecisionImportNotifications>? notificationContext)
{
GuardNullMovement();

HasChanges = true;

var sourceSystem = decision.ServiceHeader?.SourceSystem;
var isAlvs = sourceSystem != "BTMS";
var isAlvs = sourceSystem == "ALVS";
var isBtms = sourceSystem == "BTMS";
DecisionContext context;

Expand Down Expand Up @@ -202,32 +201,37 @@ private static string BuildNormalizedDecisionPath(string fullPath)
return fullPath.Replace("RAW/DECISIONS/", "");
}

private DecisionContext FindAlvsPairAndUpdate(CdsDecision decision)
private DecisionContext FindAlvsPairAndUpdate(CdsDecision btmsDecision)
{
GuardNullMovement();

var alvsDecision = _movement.AlvsDecisionStatus?.Decisions
.SingleOrDefault(d => d.Context.DecisionComparison.HasValue());
var alvsDecision = _movement.AlvsDecisionStatus.Decisions
.MaxBy(d => d.Context.DecisionComparison.HasValue());

logger.LogInformation("FindAlvsPairAndUpdate btmsDecision number {BtmsDecisionNumber}, alvs paired decision number {PairedDecisionNumber}", btmsDecision.Header!.DecisionNumber, alvsDecision?.Context.DecisionComparison!.BtmsDecisionNumber);

if (alvsDecision != null)
{
var shouldPair = decision.Header!.DecisionNumber > alvsDecision.Context.DecisionComparison!.BtmsDecisionNumber;
var shouldPair = btmsDecision.Header!.DecisionNumber > alvsDecision.Context.DecisionComparison!.BtmsDecisionNumber;

logger.LogInformation("FindAlvsPairAndUpdate ShouldPair {ShouldPair}", shouldPair);

// Updates the pair status if we've received a newer BTMS decision than that already paired.
SetDecisionComparison(alvsDecision, shouldPair, decision.Header!.DecisionNumber!.Value);
SetDecisionComparison(alvsDecision, shouldPair, btmsDecision.Header!.DecisionNumber!.Value);

if (shouldPair)
{
CompareDecisions(alvsDecision, decision);
CompareDecisions(alvsDecision, btmsDecision);
}


_movement.AlvsDecisionStatus.Context = alvsDecision.Context;

return alvsDecision.Context;
}

// I'm Sure there's a better way to do this!
return new DecisionContext()
{
EntryVersionNumber = decision.Header!.EntryVersionNumber!.Value,
EntryVersionNumber = btmsDecision.Header!.EntryVersionNumber!.Value,
};
}

Expand Down Expand Up @@ -291,7 +295,7 @@ private AlvsDecision CreateAlvsDecisionWithContext(CdsDecision alvsDecision)

private void SetDecisionComparison(AlvsDecision decision, bool paired, int? btmsDecisionNumber)
{
logger.LogInformation("SetPairStatus {decision}, {paired} {btmsDecisionNumber}",
logger.LogInformation("SetDecisionComparison SetPairStatus AlvsDecision {AlvsDecision}, Paired {Paired} BtmsDecisionNumber {BtmsDecisionNumber}",
decision.Context.AlvsDecisionNumber, paired, btmsDecisionNumber);

decision.Context.DecisionComparison = !paired ? null : new DecisionComparison()
Expand Down
17 changes: 14 additions & 3 deletions Btms.Consumers/DecisionsConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Btms.Backend.Data;
using Btms.Business.Builders;
using Btms.Consumers.Extensions;
using Btms.Model;
using Btms.Model.Cds;
using Btms.Types.Alvs;
using Btms.Types.Alvs.Mapping;
using Microsoft.Extensions.Logging;
using SlimMessageBus;

namespace Btms.Consumers;

public class DecisionsConsumer(IMongoDbContext dbContext, MovementBuilderFactory movementBuilderFactory)
public class DecisionsConsumer(IMongoDbContext dbContext, MovementBuilderFactory movementBuilderFactory, ILogger<DecisionsConsumer> logger)
: IConsumer<Decision>, IConsumerWithContext
{
public async Task OnHandle(Decision message)
Expand All @@ -28,7 +28,18 @@ public async Task OnHandle(Decision message)

if (existingMovementBuilder.HasChanges)
{
await dbContext.Movements.Update(existingMovementBuilder.Build(), existingMovement._Etag);
var movement = existingMovementBuilder.Build();
logger.LogInformation("OnHandle HasChanges max btmsDecision number {MaxBtmsDecisionNumber}, max alvs decision number {MaxAlvsDecisionNumber} paired btms decision number {PairedBtmsDecisionNumber}, paired alvs decision number {PairedAlvsDecisionNumber}",
movement.Decisions.Max(d => d.Header.DecisionNumber),
movement.AlvsDecisionStatus.Decisions.Max(d => d.Decision.Header.DecisionNumber),
movement.AlvsDecisionStatus.Context.DecisionComparison?.BtmsDecisionNumber,
movement.AlvsDecisionStatus.Context.AlvsDecisionNumber);

await dbContext.Movements.Update(movement, existingMovement._Etag);
}
else
{
logger.LogInformation("OnHandle no changes to store");
}
}
}
Expand Down
Loading