-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NServiceBus.Testing v9 snippets (#6294)
* 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
Showing
31 changed files
with
813 additions
and
9 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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> |
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,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 | ||
} |
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,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 |
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,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 |
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,9 @@ | ||
using System; | ||
using NServiceBus; | ||
|
||
public class DiscountPolicyData : | ||
ContainSagaData | ||
{ | ||
public Guid CustomerId { get; set; } | ||
public decimal TotalAmount { get; set; } | ||
} |
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,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>(); | ||
} |
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,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 |
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,3 @@ | ||
public class MyRequest | ||
{ | ||
} |
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,3 @@ | ||
public class MyResponse | ||
{ | ||
} |
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,10 @@ | ||
using System; | ||
using NServiceBus; | ||
|
||
public class ProcessOrder : | ||
IMessage | ||
{ | ||
public Guid CustomerId { get; set; } | ||
public Guid OrderId { get; set; } | ||
public decimal TotalAmount { get; set; } | ||
} |
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,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
33
Snippets/Testing/Testing_9/Handlers/InterfaceMessageTests.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,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
23
Snippets/Testing/Testing_9/Handlers/MessageHandlerTests.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,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 | ||
} |
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,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 | ||
} |
Oops, something went wrong.