From 7ff4396bc941a08ed238904fa9402b462e36a394 Mon Sep 17 00:00:00 2001 From: Kyle McMaster Date: Thu, 26 Sep 2024 21:56:26 -0400 Subject: [PATCH 1/4] Update editorconfig --- .editorconfig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.editorconfig b/.editorconfig index 7ea9b64..cf4d021 100644 --- a/.editorconfig +++ b/.editorconfig @@ -146,6 +146,10 @@ csharp_style_expression_bodied_local_functions = false:silent ############################### # VB Coding Conventions # ############################### + +# IDE0005: Using directive is unnecessary. +dotnet_diagnostic.IDE0005.severity = warning + [*.vb] # Modifier preferences visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion \ No newline at end of file From 5e5de548e4e2d97d4464cce04de99e3ac9bca58d Mon Sep 17 00:00:00 2001 From: Kyle McMaster Date: Thu, 26 Sep 2024 21:57:21 -0400 Subject: [PATCH 2/4] Update ContributorVerifiedEventTests for upcoming Scenario tests, Add StartContributorVerificationCommand tests for saga --- .../ContributorVerifiedEventTests.cs | 28 ------------ ...tartContributorVerificationCommandTests.cs | 44 +++++++++++++++++++ 2 files changed, 44 insertions(+), 28 deletions(-) create mode 100644 tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/StartContributorVerificationCommandTests.cs diff --git a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/ContributorVerifiedEventTests.cs b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/ContributorVerifiedEventTests.cs index 4839ac0..bb3ec2d 100644 --- a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/ContributorVerifiedEventTests.cs +++ b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/ContributorVerifiedEventTests.cs @@ -1,8 +1,5 @@ using FluentAssertions; -using FluentAssertions.Execution; using NServiceBus.Testing; -using NServiceBusTutorial.Core.ContributorAggregate; -using NServiceBusTutorial.Core.ContributorAggregate.Commands; using NServiceBusTutorial.Core.ContributorAggregate.Events; using NServiceBusTutorial.Saga; using Xunit; @@ -21,29 +18,4 @@ public async Task ShouldMarkSagaAsCompleted() result.Completed.Should().BeTrue(); } - - [Fact] - public async Task ShouldInitializeSagaAndMarkAsCompleted() - { - int expectedContributorId = 1; - var startMessage = new StartContributorVerificationCommand() - { - ContributorId = expectedContributorId - }; - var message = new ContributorVerifiedEvent() - { - ContributorId = expectedContributorId - }; - var saga = new TestableSaga(); - var context = new TestableMessageHandlerContext(); - - var result = await saga.Handle(startMessage, context); - using var assertionScope = new AssertionScope(); - result.SagaDataSnapshot.ContributorId.Should().Be(expectedContributorId); - - result = await saga.Handle(message, context); - result.Completed.Should().BeTrue(); - var timeoutMessage = result.FindTimeoutMessage(); - timeoutMessage.Should().NotBeNull(); - } } diff --git a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/StartContributorVerificationCommandTests.cs b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/StartContributorVerificationCommandTests.cs new file mode 100644 index 0000000..b344278 --- /dev/null +++ b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/StartContributorVerificationCommandTests.cs @@ -0,0 +1,44 @@ +using FluentAssertions; +using NServiceBus.Testing; +using NServiceBusTutorial.Core.ContributorAggregate.Commands; +using NServiceBusTutorial.Saga; +using Xunit; + +namespace NServiceBusTutorial.UnitTests.ContributorVerificationSagaTests; + +public class StartContributorVerificationCommandTests +{ + [Fact] + public async Task ShouldSendVerifyContributorCommand() + { + var message = new StartContributorVerificationCommand(); + var saga = new ContributorVerificationSaga + { + Data = new() + }; + var context = new TestableMessageHandlerContext(); + + await saga.Handle(message, context); + + var sentMessage = context.FindSentMessage(); + sentMessage.Should().NotBeNull(); + } + + [Fact] + public async Task ShouldSetContributorIdOnSagaState() + { + var message = new StartContributorVerificationCommand() + { + ContributorId = 4680 + }; + var saga = new ContributorVerificationSaga + { + Data = new() + }; + var context = new TestableMessageHandlerContext(); + + await saga.Handle(message, context); + + saga.Data.ContributorId.Should().Be(message.ContributorId); + } +} From fba7e5707493e46248a9ab8b90e43a8af535ffc8 Mon Sep 17 00:00:00 2001 From: Kyle McMaster Date: Thu, 26 Sep 2024 22:22:54 -0400 Subject: [PATCH 3/4] Add scenario tests and cleanup invalid test --- .../SagaScenarioTests.cs | 38 +++++++++++++++++++ ...tartContributorVerificationCommandTests.cs | 18 --------- 2 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/SagaScenarioTests.cs diff --git a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/SagaScenarioTests.cs b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/SagaScenarioTests.cs new file mode 100644 index 0000000..f869510 --- /dev/null +++ b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/SagaScenarioTests.cs @@ -0,0 +1,38 @@ +using FluentAssertions; +using FluentAssertions.Execution; +using NServiceBus.Testing; +using NServiceBusTutorial.Core.ContributorAggregate.Commands; +using NServiceBusTutorial.Core.ContributorAggregate.Events; +using NServiceBusTutorial.Saga; +using Xunit; + +namespace NServiceBusTutorial.UnitTests.ContributorVerificationSagaTests; + +public class SagaScenarioTests +{ + [Fact] + public async Task ShouldInitializeSagaAndMarkAsCompleted() + { + int expectedContributorId = 1; + var startMessage = new StartContributorVerificationCommand() + { + ContributorId = expectedContributorId + }; + var message = new ContributorVerifiedEvent() + { + ContributorId = expectedContributorId + }; + var saga = new TestableSaga(); + var context = new TestableMessageHandlerContext(); + + var result = await saga.Handle(startMessage, context); + + using var assertionScope = new AssertionScope(); + result.SagaDataSnapshot.ContributorId.Should().Be(expectedContributorId); + + result = await saga.Handle(message, context); + result.Completed.Should().BeTrue(); + var timeoutMessage = result.FindTimeoutMessage(); + timeoutMessage.Should().NotBeNull(); + } +} diff --git a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/StartContributorVerificationCommandTests.cs b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/StartContributorVerificationCommandTests.cs index b344278..366401f 100644 --- a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/StartContributorVerificationCommandTests.cs +++ b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/StartContributorVerificationCommandTests.cs @@ -23,22 +23,4 @@ public async Task ShouldSendVerifyContributorCommand() var sentMessage = context.FindSentMessage(); sentMessage.Should().NotBeNull(); } - - [Fact] - public async Task ShouldSetContributorIdOnSagaState() - { - var message = new StartContributorVerificationCommand() - { - ContributorId = 4680 - }; - var saga = new ContributorVerificationSaga - { - Data = new() - }; - var context = new TestableMessageHandlerContext(); - - await saga.Handle(message, context); - - saga.Data.ContributorId.Should().Be(message.ContributorId); - } } From 714ff9671d1aada5c80157cdc56f4ed7d20f7713 Mon Sep 17 00:00:00 2001 From: Kyle McMaster Date: Fri, 27 Sep 2024 11:32:46 -0400 Subject: [PATCH 4/4] Add additional scenario tests --- .../ContributorVerifiedEventTests.cs | 6 ++-- .../SagaScenarioTests.cs | 36 ++++++++++++++----- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/ContributorVerifiedEventTests.cs b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/ContributorVerifiedEventTests.cs index bb3ec2d..d582056 100644 --- a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/ContributorVerifiedEventTests.cs +++ b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/ContributorVerifiedEventTests.cs @@ -11,11 +11,11 @@ public class ContributorVerifiedEventTests public async Task ShouldMarkSagaAsCompleted() { var message = new ContributorVerifiedEvent(); - var saga = new TestableSaga(); + var saga = new ContributorVerificationSaga(); var context = new TestableMessageHandlerContext(); - var result = await saga.Handle(message, context); + await saga.Handle(message, context); - result.Completed.Should().BeTrue(); + saga.Completed.Should().BeTrue(); } } diff --git a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/SagaScenarioTests.cs b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/SagaScenarioTests.cs index f869510..0a7aa02 100644 --- a/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/SagaScenarioTests.cs +++ b/tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/SagaScenarioTests.cs @@ -13,26 +13,44 @@ public class SagaScenarioTests [Fact] public async Task ShouldInitializeSagaAndMarkAsCompleted() { - int expectedContributorId = 1; - var startMessage = new StartContributorVerificationCommand() + int expectedContributorId = 4680; + var startCommand = new StartContributorVerificationCommand { ContributorId = expectedContributorId }; - var message = new ContributorVerifiedEvent() + var verifiedEvent = new ContributorVerifiedEvent { ContributorId = expectedContributorId }; var saga = new TestableSaga(); var context = new TestableMessageHandlerContext(); - var result = await saga.Handle(startMessage, context); + var startResult = await saga.Handle(startCommand, context); + var completeResult = await saga.Handle(verifiedEvent, context); using var assertionScope = new AssertionScope(); - result.SagaDataSnapshot.ContributorId.Should().Be(expectedContributorId); + startResult.SagaDataSnapshot.ContributorId.Should().Be(expectedContributorId); + completeResult.Completed.Should().BeTrue(); + } - result = await saga.Handle(message, context); - result.Completed.Should().BeTrue(); - var timeoutMessage = result.FindTimeoutMessage(); - timeoutMessage.Should().NotBeNull(); + [Fact] + public async Task ShouldTimeoutWhenTimeAdvancesOverLimit() + { + int expectedContributorId = 4680; + var startCommand = new StartContributorVerificationCommand + { + ContributorId = expectedContributorId + }; + var saga = new TestableSaga(); + var context = new TestableMessageHandlerContext(); + + var startResult = await saga.Handle(startCommand, context); + var timeouts = await saga.AdvanceTime(TimeSpan.FromHours(25)); + + using var assertionScope = new AssertionScope(); + startResult.SagaDataSnapshot.ContributorId.Should().Be(expectedContributorId); + var timeoutResult = timeouts.Single(); + timeoutResult.FindSentMessage().Should().NotBeNull(); + timeoutResult.Completed.Should().BeTrue(); } }