Unit test Azure Service Bus routing

Clement OlivierDyn365CE4 years ago12 Views

MockServiceBus

Unit test Azure Service Bus routing

Supporting & maintaining Azure Service Bus routing rules could be tricky to design without regressions and unexpected behaviors.

MockServiceBus with help of TSQL.Parser provides solution to unit test your subscriptions routing.

Unit test Azure Service Bus routing

Install-Package MockServiceBus

Basic example

A brokered message with custom property flag=1 to be correctly transfered for subscription1 and ignored for subscription2.

NameSpace.AssertRouting(String topicname, Message message, Dictionary expectedResults)

will return green test result

Unit test Azure Service Bus routing

  • changing the flag value without changing expected outcome
            var message = new Message
            {
                CustomProperties = new Dictionary<string, object>
                {
                    { "flag", "2" },
                }
            };
Assert.AreEqual failed. Expected:<Transfered>. Actual:<Ignored>. subscription1

Unit test Azure Service Bus routing

  • not declaring expected output for a specific subscription
            servicebus.AssertRouting("topic", message, new Dictionary<string, MessageState> {
                { "subscription1", MessageState.Transfered }
            });

Assert.Fail failed. unexpected subscribers subscription2

Unit test Azure Service Bus routing

Casting numbers

MockServiceBus needs a litle help for number types with prefixing with {Int32} fieldname. space is important

Switching from flag='1' to flag=1

            var subscription1 = new Subscription { Name = "subscription1", Rules = new List<Rule> { new Rule { Filter = "{Int32} flag=1" } } };

            var message = new Message
            {
                CustomProperties = new Dictionary<string, object>
                {
                    { "flag", 1 },
                }
            };

Supported features

Full list not available now, but complex queries are supported

NOT EXISTS([forcedestination]) AND NOT EXISTS([subitem]) AND [fromsystem] != ‘{system}’ AND [reference.crmguid] != ‘null’ AND {Int32} [contract.active] = 1 AND (NOT EXISTS([startdelta]) OR {Int32} [startdelta] >= 0) AND ([fromsystem] != ‘sap’

Next ?

  • whole structure of topics and subscriptions (+ rules) can be loaded from json file
  • re-using same structure files & unit test for automatically updating subscriptions rules in Azure Service Bus

Original Post https://stuffandtacos.azurewebsites.net/2020/03/21/unit-test-azure-service-bus-routing/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
March 2025
MTWTFSS
      1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       
« Feb   Apr »
Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...