Skip to content

Commit

Permalink
Add NServiceBus.Testing v9 snippets (#6294)
Browse files Browse the repository at this point in the history
* Update project to net8.0

* Update references

* Add pre-release marker

* Remove 7to8 upgrade guide from v9

* Tweaks

---------

Co-authored-by: Brandon Ording <[email protected]>
  • Loading branch information
kentdr and bording authored Nov 3, 2023
1 parent b8fa62b commit 7fd07a0
Show file tree
Hide file tree
Showing 31 changed files with 813 additions and 9 deletions.
8 changes: 7 additions & 1 deletion Snippets/Testing/Testing.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32228.430
MinimumVisualStudioVersion = 15.0.26730.12
Expand All @@ -8,6 +8,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Testing_8", "Testing_8\Test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing_7.4", "Testing_7.4\Testing_7.4.csproj", "{3F227313-831D-48C4-8688-A7F32A4489E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing_9", "Testing_9\Testing_9.csproj", "{F9DC5144-34D4-4A83-BF7E-E86D8C6AA6EA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,6 +28,10 @@ Global
{3F227313-831D-48C4-8688-A7F32A4489E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F227313-831D-48C4-8688-A7F32A4489E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F227313-831D-48C4-8688-A7F32A4489E8}.Release|Any CPU.Build.0 = Release|Any CPU
{F9DC5144-34D4-4A83-BF7E-E86D8C6AA6EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9DC5144-34D4-4A83-BF7E-E86D8C6AA6EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9DC5144-34D4-4A83-BF7E-E86D8C6AA6EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9DC5144-34D4-4A83-BF7E-E86D8C6AA6EA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 3 additions & 3 deletions Snippets/Testing/Testing_7.4/Testing_7.4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" />
<PackageReference Include="NServiceBus.Testing" Version="7.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.*" />
<PackageReference Include="NServiceBus.Testing" Version="7.4.*" />
<PackageReference Include="NUnit" Version="3.*" />
<PackageReference Include="NUnit3TestAdapter" Version="3.*" />
<PackageReference Include="NUnit3TestAdapter" Version="4.*" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions Snippets/Testing/Testing_7/Testing_7.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<NoWarn>0618</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.*" />
<PackageReference Include="NServiceBus.Gateway" Version="3.*" />
<PackageReference Include="NServiceBus.Testing" Version="7.*" />
<PackageReference Include="NServiceBus.UniformSession.Testing" Version="2.*" />
<PackageReference Include="NUnit" Version="3.*" />
<PackageReference Include="NUnit3TestAdapter" Version="3.*" />
<PackageReference Include="NUnit3TestAdapter" Version="4.*" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions Snippets/Testing/Testing_8/Testing_8.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.*" />
<PackageReference Include="NServiceBus.Gateway" Version="4.*" />
<PackageReference Include="NServiceBus.Testing" Version="8.*" />
<PackageReference Include="NServiceBus.UniformSession.Testing" Version="3.*" />
<PackageReference Include="NUnit" Version="3.*" />
<PackageReference Include="NUnit3TestAdapter" Version="3.*" />
<PackageReference Include="NUnit3TestAdapter" Version="4.*" />
</ItemGroup>
</Project>
26 changes: 26 additions & 0 deletions Snippets/Testing/Testing_9/Behavior/BehaviorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Threading.Tasks;
using NServiceBus.Pipeline;
using NServiceBus.Testing;
using NUnit.Framework;

[Explicit]
[TestFixture]
public class BehaviorTests
{
#region BehaviorTest
[Test]
public async Task ShouldAddCustomHeaderToMyResponse()
{
var behavior = new CustomBehavior();
var context = new TestableOutgoingLogicalMessageContext
{
Message = new OutgoingLogicalMessage(typeof(MyResponse), new MyResponse())
};

await behavior.Invoke(context, () => Task.CompletedTask)
.ConfigureAwait(false);

Assert.AreEqual("custom header value", context.Headers["custom-header"]);
}
#endregion
}
19 changes: 19 additions & 0 deletions Snippets/Testing/Testing_9/Data/CustomBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;
using NServiceBus.Pipeline;

#region SampleBehavior
public class CustomBehavior :
Behavior<IOutgoingLogicalMessageContext>
{
public override Task Invoke(IOutgoingLogicalMessageContext context, Func<Task> next)
{
if (context.Message.MessageType == typeof(MyResponse))
{
context.Headers.Add("custom-header", "custom header value");
}

return next();
}
}
#endregion
49 changes: 49 additions & 0 deletions Snippets/Testing/Testing_9/Data/DiscountPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NServiceBus;
using System.Threading.Tasks;

#region SampleSaga
public class DiscountPolicy :
Saga<DiscountPolicyData>,
IAmStartedByMessages<SubmitOrder>
{
public Task Handle(SubmitOrder message, IMessageHandlerContext context)
{
Data.CustomerId = message.CustomerId;
Data.TotalAmount += message.TotalAmount;

if (Data.TotalAmount >= 1000)
{
return ProcessWithDiscount(message, context);
}
return ProcessOrder(message, context);
}

Task ProcessWithDiscount(SubmitOrder message, IMessageHandlerContext context)
{
var processOrder = new ProcessOrder
{
CustomerId = Data.CustomerId,
OrderId = message.OrderId,
TotalAmount = message.TotalAmount * (decimal)0.9
};
return context.Send(processOrder);
}

Task ProcessOrder(SubmitOrder message, IMessageHandlerContext context)
{
var processOrder = new ProcessOrder
{
CustomerId = Data.CustomerId,
OrderId = message.OrderId,
TotalAmount = message.TotalAmount
};
return context.Send(processOrder);
}

protected override void ConfigureHowToFindSaga(SagaPropertyMapper<DiscountPolicyData> mapper)
{
mapper.MapSaga(saga => saga.CustomerId)
.ToMessage<SubmitOrder>(msg => msg.CustomerId);
}
}
#endregion
9 changes: 9 additions & 0 deletions Snippets/Testing/Testing_9/Data/DiscountPolicyData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using NServiceBus;

public class DiscountPolicyData :
ContainSagaData
{
public Guid CustomerId { get; set; }
public decimal TotalAmount { get; set; }
}
16 changes: 16 additions & 0 deletions Snippets/Testing/Testing_9/Data/MyHandlerWithLogging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Logging;

public class MyHandlerWithLogging :
IHandleMessages<MyRequest>
{
public Task Handle(MyRequest message, IMessageHandlerContext context)
{
logger.Debug("Some log message");

return Task.CompletedTask;
}

static ILog logger = LogManager.GetLogger<MyHandlerWithLogging>();
}
13 changes: 13 additions & 0 deletions Snippets/Testing/Testing_9/Data/MyReplyingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;
using NServiceBus;

#region SimpleHandler
public class MyReplyingHandler :
IHandleMessages<MyRequest>
{
public Task Handle(MyRequest message, IMessageHandlerContext context)
{
return context.Reply(new MyResponse());
}
}
#endregion
3 changes: 3 additions & 0 deletions Snippets/Testing/Testing_9/Data/MyRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class MyRequest
{
}
3 changes: 3 additions & 0 deletions Snippets/Testing/Testing_9/Data/MyResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class MyResponse
{
}
10 changes: 10 additions & 0 deletions Snippets/Testing/Testing_9/Data/ProcessOrder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using NServiceBus;

public class ProcessOrder :
IMessage
{
public Guid CustomerId { get; set; }
public Guid OrderId { get; set; }
public decimal TotalAmount { get; set; }
}
10 changes: 10 additions & 0 deletions Snippets/Testing/Testing_9/Data/SubmitOrder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using NServiceBus;

public class SubmitOrder :
IMessage
{
public Guid CustomerId { get; set; }
public Guid OrderId { get; set; }
public decimal TotalAmount { get; set; }
}
33 changes: 33 additions & 0 deletions Snippets/Testing/Testing_9/Handlers/InterfaceMessageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Threading.Tasks;

using NServiceBus;
using NServiceBus.MessageInterfaces.MessageMapper.Reflection;
using NServiceBus.Testing;

public class InterfaceMessageTests
{
public interface IMyMessage { }

public async Task TestingInterfaceMessages()
{
var handler = new MyMessageHandler();
var context = new TestableMessageHandlerContext();

#region InterfaceMessageCreation
var messageMapper = new MessageMapper();
var myMessage = messageMapper.CreateInstance<IMyMessage>(message => { /* ... */ });

await handler.Handle(myMessage, context)
.ConfigureAwait(false);
#endregion

}

public class MyMessageHandler : IHandleMessages<IMyMessage>
{
public Task Handle(IMyMessage message, IMessageHandlerContext context)
{
return Task.CompletedTask;
}
}
}
23 changes: 23 additions & 0 deletions Snippets/Testing/Testing_9/Handlers/MessageHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Threading.Tasks;
using NServiceBus.Testing;
using NUnit.Framework;

[Explicit]
[TestFixture]
public class MessageHandlerTests
{
#region HandlerTest
[Test]
public async Task ShouldReplyWithResponseMessage()
{
var handler = new MyReplyingHandler();
var context = new TestableMessageHandlerContext();

await handler.Handle(new MyRequest(), context)
.ConfigureAwait(false);

Assert.AreEqual(1, context.RepliedMessages.Length);
Assert.IsInstanceOf<MyResponse>(context.RepliedMessages[0].Message);
}
#endregion
}
54 changes: 54 additions & 0 deletions Snippets/Testing/Testing_9/Logging/LoggingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;
using NServiceBus.Logging;
using NServiceBus.Testing;
using NUnit.Framework;

#region LoggerTestingSetup
[SetUpFixture]
public class LoggingSetupFixture
{
static StringBuilder logStatements = new StringBuilder();

[OneTimeSetUp]
public void SetUp()
{
LogManager.Use<TestingLoggerFactory>()
.WriteTo(new StringWriter(logStatements));
}

public static string LogStatements => logStatements.ToString();

public static void Clear()
{
logStatements.Clear();
}
}
#endregion

[Explicit]
[TestFixture]
public class LoggingTests
{
#region LoggerTesting

[SetUp]
public void SetUp()
{
LoggingSetupFixture.Clear();
}

[Test]
public async Task ShouldLogCorrectly()
{
var handler = new MyHandlerWithLogging();

await handler.Handle(new MyRequest(), new TestableMessageHandlerContext())
.ConfigureAwait(false);

StringAssert.Contains("Some log message", LoggingSetupFixture.LogStatements);
}

#endregion
}
Loading

0 comments on commit 7fd07a0

Please sign in to comment.