Skip to content

Commit c9d98dc

Browse files
authored
Add NServiceBus 10 version of the Testing snippets (#7757)
* Add NServiceBus 10 version of the Testing snippets * Change the message type so the test won't fail
1 parent 73ba1d7 commit c9d98dc

30 files changed

+794
-4
lines changed

Snippets/Testing/Testing.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing_7.4", "Testing_7.4\
1010
EndProject
1111
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing_9", "Testing_9\Testing_9.csproj", "{F9DC5144-34D4-4A83-BF7E-E86D8C6AA6EA}"
1212
EndProject
13+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing_10", "Testing_10\Testing_10.csproj", "{DE104B13-79E8-427A-85B3-608190E2BB67}"
14+
EndProject
1315
Global
1416
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1517
Debug|Any CPU = Debug|Any CPU
@@ -32,6 +34,10 @@ Global
3234
{F9DC5144-34D4-4A83-BF7E-E86D8C6AA6EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
3335
{F9DC5144-34D4-4A83-BF7E-E86D8C6AA6EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
3436
{F9DC5144-34D4-4A83-BF7E-E86D8C6AA6EA}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{DE104B13-79E8-427A-85B3-608190E2BB67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{DE104B13-79E8-427A-85B3-608190E2BB67}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{DE104B13-79E8-427A-85B3-608190E2BB67}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{DE104B13-79E8-427A-85B3-608190E2BB67}.Release|Any CPU.Build.0 = Release|Any CPU
3541
EndGlobalSection
3642
GlobalSection(SolutionProperties) = preSolution
3743
HideSolutionNode = FALSE
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus.Pipeline;
3+
using NServiceBus.Testing;
4+
using NUnit.Framework;
5+
6+
[Explicit]
7+
[TestFixture]
8+
public class BehaviorTests
9+
{
10+
#region BehaviorTest
11+
[Test]
12+
public async Task ShouldAddCustomHeaderToMyResponse()
13+
{
14+
var behavior = new CustomBehavior();
15+
var context = new TestableOutgoingLogicalMessageContext
16+
{
17+
Message = new OutgoingLogicalMessage(typeof(MyResponse), new MyResponse())
18+
};
19+
20+
await behavior.Invoke(context, () => Task.CompletedTask);
21+
22+
Assert.That(context.Headers["custom-header"], Is.EqualTo("custom header value"));
23+
}
24+
#endregion
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NServiceBus.Pipeline;
4+
5+
#region SampleBehavior
6+
7+
public class CustomBehavior :
8+
Behavior<IOutgoingLogicalMessageContext>
9+
{
10+
public override Task Invoke(IOutgoingLogicalMessageContext context, Func<Task> next)
11+
{
12+
if (context.Message.MessageType == typeof(MyResponse))
13+
{
14+
context.Headers.Add("custom-header", "custom header value");
15+
}
16+
17+
return next();
18+
}
19+
}
20+
21+
#endregion
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
4+
#region SampleSaga
5+
6+
public class DiscountPolicy :
7+
Saga<DiscountPolicyData>,
8+
IAmStartedByMessages<SubmitOrder>
9+
{
10+
public Task Handle(SubmitOrder message, IMessageHandlerContext context)
11+
{
12+
Data.CustomerId = message.CustomerId;
13+
Data.TotalAmount += message.TotalAmount;
14+
15+
if (Data.TotalAmount >= 1000)
16+
{
17+
return ProcessWithDiscount(message, context);
18+
}
19+
return ProcessOrder(message, context);
20+
}
21+
22+
Task ProcessWithDiscount(SubmitOrder message, IMessageHandlerContext context)
23+
{
24+
var processOrder = new ProcessOrder
25+
{
26+
CustomerId = Data.CustomerId,
27+
OrderId = message.OrderId,
28+
TotalAmount = message.TotalAmount * (decimal)0.9
29+
};
30+
return context.Send(processOrder);
31+
}
32+
33+
Task ProcessOrder(SubmitOrder message, IMessageHandlerContext context)
34+
{
35+
var processOrder = new ProcessOrder
36+
{
37+
CustomerId = Data.CustomerId,
38+
OrderId = message.OrderId,
39+
TotalAmount = message.TotalAmount
40+
};
41+
return context.Send(processOrder);
42+
}
43+
44+
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<DiscountPolicyData> mapper)
45+
{
46+
mapper.MapSaga(saga => saga.CustomerId)
47+
.ToMessage<SubmitOrder>(msg => msg.CustomerId);
48+
}
49+
}
50+
51+
#endregion
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
using NServiceBus;
3+
4+
public class DiscountPolicyData :
5+
ContainSagaData
6+
{
7+
public Guid CustomerId { get; set; }
8+
public decimal TotalAmount { get; set; }
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
using NServiceBus.Logging;
4+
5+
public class MyHandlerWithLogging :
6+
IHandleMessages<MyRequest>
7+
{
8+
public Task Handle(MyRequest message, IMessageHandlerContext context)
9+
{
10+
logger.Debug("Some log message");
11+
12+
return Task.CompletedTask;
13+
}
14+
15+
static ILog logger = LogManager.GetLogger<MyHandlerWithLogging>();
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
4+
#region SimpleHandler
5+
6+
public class MyReplyingHandler :
7+
IHandleMessages<MyRequest>
8+
{
9+
public Task Handle(MyRequest message, IMessageHandlerContext context)
10+
{
11+
return context.Reply(new MyResponse());
12+
}
13+
}
14+
15+
#endregion
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class MyRequest
2+
{
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class MyResponse
2+
{
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using NServiceBus;
3+
4+
public class ProcessOrder :
5+
IMessage
6+
{
7+
public Guid CustomerId { get; set; }
8+
public Guid OrderId { get; set; }
9+
public decimal TotalAmount { get; set; }
10+
}

0 commit comments

Comments
 (0)