Skip to content

Commit

Permalink
Upgraded Sample to Latest MassTransit, rewrote all tests to match the…
Browse files Browse the repository at this point in the history
… supported test harness configuration
  • Loading branch information
phatboyg committed Jan 26, 2023
1 parent c53429b commit c182cbc
Show file tree
Hide file tree
Showing 20 changed files with 635 additions and 861 deletions.
8 changes: 8 additions & 0 deletions Sample-Library.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library.Contracts", "src\Li
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library.Integration.Tests", "tests\Library.Integration.Tests\Library.Integration.Tests.csproj", "{7679FD7A-1584-4579-ABCF-664DA5FC0133}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{597F4D76-F469-408D-9E1E-1863D508D1C6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{685E5157-F445-4E99-9BF6-59CB72BDD96E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -74,5 +78,9 @@ Global
{7679FD7A-1584-4579-ABCF-664DA5FC0133}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E94A9FE2-1455-4BA1-AEE8-8F7AA1941ED3} = {685E5157-F445-4E99-9BF6-59CB72BDD96E}
{1E9CCBB9-EF1C-4993-BF00-B90DCBF932DA} = {597F4D76-F469-408D-9E1E-1863D508D1C6}
{C5B5F038-995D-4466-8F8F-2D51751D133F} = {597F4D76-F469-408D-9E1E-1863D508D1C6}
{7679FD7A-1584-4579-ABCF-664DA5FC0133} = {685E5157-F445-4E99-9BF6-59CB72BDD96E}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions src/Library.Components/Library.Components.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit" Version="8.0.0"/>
<PackageReference Include="MassTransit" Version="8.0.10"/>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Library.Contracts/Library.Contracts.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit" Version="8.0.0"/>
<PackageReference Include="MassTransit" Version="8.0.10"/>
</ItemGroup>

</Project>
66 changes: 43 additions & 23 deletions tests/Library.Components.Tests/BookReturnStateMachine_Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,36 @@ namespace Library.Components.Tests
using Consumers;
using Contracts;
using MassTransit;
using MassTransit.Testing;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using StateMachines;


public class When_a_book_is_returned :
StateMachineTestFixture<BookReturnStateMachine, BookReturn>
public class When_a_book_is_returned
{
[Test]
public async Task Should_request_the_fine_be_charged()
{
await using var provider = new ServiceCollection()
.ConfigureMassTransit(x =>
{
x.AddConsumer<ChargeFineConsumer>();
x.AddSagaStateMachine<BookReturnStateMachine, BookReturn>();
})
.BuildServiceProvider(true);

var harness = provider.GetTestHarness();

await harness.Start();

var checkOutId = NewId.NextGuid();
var bookId = NewId.NextGuid();
var memberId = NewId.NextGuid();

var now = DateTime.UtcNow;

await TestHarness.Bus.Publish<BookReturned>(new
await harness.Bus.Publish<BookReturned>(new
{
CheckOutId = checkOutId,
InVar.Timestamp,
Expand All @@ -32,34 +45,43 @@ await TestHarness.Bus.Publish<BookReturned>(new
ReturnDate = now,
});

Guid? existsId = await SagaHarness.Exists(checkOutId, x => x.ChargingFine);
Assert.IsTrue(existsId.HasValue, "Saga did not exist");
var sagaHarness = harness.GetSagaStateMachineHarness<BookReturnStateMachine, BookReturn>();

Assert.IsTrue(await TestHarness.Consumed.Any<ChargeMemberFine>(), "Fine not consumed");
Guid? existsId = await sagaHarness.Exists(checkOutId, x => x.ChargingFine);
Assert.IsTrue(existsId.HasValue, "Saga did not exist");

Assert.IsTrue(await TestHarness.Consumed.Any<FineCharged>(), "Fine charged");
}
Assert.IsTrue(await harness.Consumed.Any<ChargeMemberFine>(), "Fine not consumed");

protected override void ConfigureMassTransit(IBusRegistrationConfigurator configurator)
{
configurator.AddConsumer<ChargeFineConsumer>();
Assert.IsTrue(await harness.Consumed.Any<FineCharged>(), "Fine charged");
}
}


public class When_a_book_is_returned_and_fails_to_charge_fine :
StateMachineTestFixture<BookReturnStateMachine, BookReturn>
public class When_a_book_is_returned_and_fails_to_charge_fine
{
[Test]
public async Task Should_handle_the_request_fault()
{
await using var provider = new ServiceCollection()
.ConfigureMassTransit(x =>
{
x.AddConsumer<BadChargeFineConsumer>()
.Endpoint(e => e.Name = KebabCaseEndpointNameFormatter.Instance.Consumer<ChargeFineConsumer>());
x.AddSagaStateMachine<BookReturnStateMachine, BookReturn>();
})
.BuildServiceProvider(true);

var harness = provider.GetTestHarness();

await harness.Start();

var checkOutId = NewId.NextGuid();
var bookId = NewId.NextGuid();
var memberId = NewId.NextGuid();

var now = DateTime.UtcNow;

await TestHarness.Bus.Publish<BookReturned>(new
await harness.Bus.Publish<BookReturned>(new
{
CheckOutId = checkOutId,
InVar.Timestamp,
Expand All @@ -70,22 +92,20 @@ await TestHarness.Bus.Publish<BookReturned>(new
ReturnDate = now,
});

Guid? existsId = await SagaHarness.Exists(checkOutId, x => x.ChargingFine);
var sagaHarness = harness.GetSagaStateMachineHarness<BookReturnStateMachine, BookReturn>();

Guid? existsId = await sagaHarness.Exists(checkOutId, x => x.ChargingFine);
Assert.IsTrue(existsId.HasValue, "Saga did not exist");

Assert.IsTrue(await TestHarness.Consumed.Any<ChargeMemberFine>(), "Fine not consumed");
Assert.IsTrue(await harness.Consumed.Any<ChargeMemberFine>(), "Fine not consumed");

Assert.IsTrue(await TestHarness.Consumed.Any<Fault<ChargeMemberFine>>(), "Fine charged");
}
Assert.IsTrue(await harness.Consumed.Any<Fault<ChargeMemberFine>>(), "Fine charged");

protected override void ConfigureMassTransit(IBusRegistrationConfigurator configurator)
{
configurator.AddConsumer<BadChargeFineConsumer>()
.Endpoint(x => x.Name = KebabCaseEndpointNameFormatter.Instance.Consumer<ChargeFineConsumer>());
await harness.Stop();
}


public class BadChargeFineConsumer :
class BadChargeFineConsumer :
IConsumer<ChargeMemberFine>
{
public async Task Consume(ConsumeContext<ChargeMemberFine> context)
Expand Down
53 changes: 39 additions & 14 deletions tests/Library.Components.Tests/BookStateMachine_Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,90 @@ namespace Library.Components.Tests
using Contracts;
using MassTransit;
using MassTransit.Testing;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using StateMachines;


public class When_a_book_is_added :
StateMachineTestFixture<BookStateMachine, Book>
public class When_a_book_is_added
{
[Test]
public async Task Should_create_a_saga_instance()
{
await using var provider = new ServiceCollection()
.ConfigureMassTransit(x =>
{
x.AddSagaStateMachine<BookStateMachine, Book>();
})
.BuildServiceProvider(true);

var harness = provider.GetTestHarness();

await harness.Start();

var bookId = NewId.NextGuid();

await TestHarness.Bus.Publish<BookAdded>(new
await harness.Bus.Publish<BookAdded>(new
{
BookId = bookId,
Isbn = "0307969959",
Title = "Neuromancer"
});

Assert.IsTrue(await TestHarness.Consumed.Any<BookAdded>(), "Message not consumed");
Assert.IsTrue(await harness.Consumed.Any<BookAdded>(), "Message not consumed");

var sagaHarness = harness.GetSagaStateMachineHarness<BookStateMachine, Book>();

Assert.IsTrue(await SagaHarness.Consumed.Any<BookAdded>(), "Message not consumed by saga");
Assert.IsTrue(await sagaHarness.Consumed.Any<BookAdded>(), "Message not consumed by saga");

Assert.That(await SagaHarness.Created.Any(x => x.CorrelationId == bookId));
Assert.That(await sagaHarness.Created.Any(x => x.CorrelationId == bookId));

var instance = SagaHarness.Created.ContainsInState(bookId, Machine, Machine.Available);
var instance = sagaHarness.Created.ContainsInState(bookId, sagaHarness.StateMachine, sagaHarness.StateMachine.Available);
Assert.IsNotNull(instance, "Saga instance not found");

Guid? existsId = await SagaHarness.Exists(bookId, x => x.Available);
Guid? existsId = await sagaHarness.Exists(bookId, x => x.Available);
Assert.IsTrue(existsId.HasValue, "Saga did not exist");
}
}


public class When_a_book_is_checked_out :
StateMachineTestFixture<BookStateMachine, Book>
public class When_a_book_is_checked_out
{
[Test]
public async Task Should_change_state_to_checked_out()
{
await using var provider = new ServiceCollection()
.ConfigureMassTransit(x =>
{
x.AddSagaStateMachine<BookStateMachine, Book>();
})
.BuildServiceProvider(true);

var harness = provider.GetTestHarness();

await harness.Start();

var bookId = NewId.NextGuid();

await TestHarness.Bus.Publish<BookAdded>(new
await harness.Bus.Publish<BookAdded>(new
{
BookId = bookId,
Isbn = "0307969959",
Title = "Neuromancer"
});

Guid? existsId = await SagaHarness.Exists(bookId, x => x.Available);
var sagaHarness = harness.GetSagaStateMachineHarness<BookStateMachine, Book>();

Guid? existsId = await sagaHarness.Exists(bookId, x => x.Available);
Assert.IsTrue(existsId.HasValue, "Saga did not exist");

await TestHarness.Bus.Publish<BookCheckedOut>(new
await harness.Bus.Publish<BookCheckedOut>(new
{
BookId = bookId,
InVar.Timestamp
});

existsId = await SagaHarness.Exists(bookId, x => x.CheckedOut);
existsId = await sagaHarness.Exists(bookId, x => x.CheckedOut);
Assert.IsTrue(existsId.HasValue, "Saga was not checked out");
}
}
Expand Down
Loading

0 comments on commit c182cbc

Please sign in to comment.