Skip to content
This repository was archived by the owner on Apr 29, 2022. It is now read-only.

Commit 9e3e0f4

Browse files
author
Anton Vorontsov
committed
Created WildcardExtensionsTests. Changed license to MIT.
1 parent 3fcf7a8 commit 9e3e0f4

File tree

6 files changed

+136
-694
lines changed

6 files changed

+136
-694
lines changed

LICENSE.txt

Lines changed: 21 additions & 674 deletions
Large diffs are not rendered by default.

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,6 @@ All notable changes being tracked in the [changelog](./docs/changelog.md) file.
175175

176176
## License
177177

178-
This library licenced under GNU General Public License v3. That means you are free to use it anywhere you want, but if you modify library by yourself you have to provide all notable changes to the community.
178+
This library licenced under MIT license.
179179

180180
Feel free to contribute!

src/RabbitMQ.Client.Core.DependencyInjection/TreeNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace RabbitMQ.Client.Core.DependencyInjection
44
{
5-
internal class TreeNode
5+
public class TreeNode
66
{
77
public string KeyPartition { get; set; }
88

99
public TreeNode Parent { get; set; }
1010

11-
public List<TreeNode> Nodes { get; set; } = new List<TreeNode>();
11+
public List<TreeNode> Nodes { get; } = new List<TreeNode>();
1212

1313
public bool IsLastNode { get; set; }
1414
}

src/RabbitMQ.Client.Core.DependencyInjection/WildcardExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
namespace RabbitMQ.Client.Core.DependencyInjection
55
{
6-
internal static class WildcardExtensions
6+
public static class WildcardExtensions
77
{
88
private const string Separator = ".";
99
private const string SingleWordPattern = "*";
1010
private const string MultipleWordsPattern = "#";
1111

12-
internal static IEnumerable<TreeNode> ConstructTree(IEnumerable<string> routingKeyBindings)
12+
public static IEnumerable<TreeNode> ConstructTree(IEnumerable<string> routingKeyBindings)
1313
{
1414
var tree = new List<TreeNode>();
1515

@@ -49,7 +49,7 @@ internal static IEnumerable<TreeNode> ConstructTree(IEnumerable<string> routingK
4949
return tree;
5050
}
5151

52-
internal static IEnumerable<string> GetMatchingRoutingKeys(IEnumerable<TreeNode> bindingsTree, string[] routingKeyParts, int depth = 0)
52+
public static IEnumerable<string> GetMatchingRoutePatterns(IEnumerable<TreeNode> bindingsTree, string[] routingKeyParts, int depth = 0)
5353
{
5454
foreach (var node in bindingsTree)
5555
{
@@ -65,7 +65,7 @@ internal static IEnumerable<string> GetMatchingRoutingKeys(IEnumerable<TreeNode>
6565
var tails = CollectRoutingKeyTails(routingKeyParts, depth);
6666
foreach (var tail in tails)
6767
{
68-
var routes = GetMatchingRoutingKeys(node.Nodes, tail, depth: 0);
68+
var routes = GetMatchingRoutePatterns(node.Nodes, tail, depth: 0);
6969
foreach (var route in routes)
7070
{
7171
yield return route;
@@ -81,7 +81,7 @@ internal static IEnumerable<string> GetMatchingRoutingKeys(IEnumerable<TreeNode>
8181
}
8282
else if (routingKeyParts.Length != depth + 1 && node.Nodes.Any())
8383
{
84-
var routes = GetMatchingRoutingKeys(node.Nodes, routingKeyParts, depth + 1).ToList();
84+
var routes = GetMatchingRoutePatterns(node.Nodes, routingKeyParts, depth + 1).ToList();
8585
foreach (var route in routes)
8686
{
8787
yield return route;
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Xunit;
4+
5+
namespace RabbitMQ.Client.Core.DependencyInjection.Tests.UnitTests
6+
{
7+
public class WildcardExtensionsTests
8+
{
9+
readonly string[] _routes;
10+
11+
public WildcardExtensionsTests()
12+
{
13+
_routes = new[] {
14+
"#",
15+
"#.delete",
16+
"#.create",
17+
"#.update",
18+
"create.*",
19+
"create.#",
20+
"*.update",
21+
"*.create.*",
22+
"*.*.*",
23+
"*.*.create",
24+
};
25+
}
26+
27+
[Fact]
28+
public void ShouldProperlyConstructTree()
29+
{
30+
var tree = WildcardExtensions.ConstructTree(_routes);
31+
32+
var countNodes = CountNodes(tree);
33+
Assert.Equal(15, countNodes);
34+
35+
Assert.Equal(4, tree.Count());
36+
Assert.Contains(tree, x => x.IsLastNode && x.KeyPartition == "#");
37+
Assert.Contains(tree, x => !x.IsLastNode && x.KeyPartition == "#");
38+
Assert.Contains(tree, x => x.KeyPartition == "*");
39+
Assert.Contains(tree, x => x.KeyPartition == "create");
40+
41+
var sharpNodes = tree.FirstOrDefault(x => !x.IsLastNode && x.KeyPartition == "#").Nodes;
42+
Assert.Equal(3, sharpNodes.Count);
43+
Assert.Contains(sharpNodes, x => x.KeyPartition == "delete");
44+
Assert.Contains(sharpNodes, x => x.KeyPartition == "create");
45+
Assert.Contains(sharpNodes, x => x.KeyPartition == "update");
46+
47+
var createNodes = tree.FirstOrDefault(x => x.KeyPartition == "create").Nodes;
48+
Assert.Equal(2, createNodes.Count);
49+
Assert.Contains(createNodes, x => x.KeyPartition == "*");
50+
Assert.Contains(createNodes, x => x.KeyPartition == "#");
51+
52+
var asteriskNodes = tree.FirstOrDefault(x => x.KeyPartition == "*").Nodes;
53+
Assert.Equal(3, asteriskNodes.Count);
54+
Assert.Contains(asteriskNodes, x => x.KeyPartition == "*");
55+
Assert.Contains(asteriskNodes, x => x.KeyPartition == "create");
56+
Assert.Contains(asteriskNodes, x => x.KeyPartition == "update");
57+
58+
var doubleAsteriskNodes = asteriskNodes.FirstOrDefault(x => x.KeyPartition == "*").Nodes;
59+
Assert.Equal(2, doubleAsteriskNodes.Count);
60+
Assert.Contains(doubleAsteriskNodes, x => x.KeyPartition == "*");
61+
Assert.Contains(doubleAsteriskNodes, x => x.KeyPartition == "create");
62+
63+
var asteriskCreateNodes = asteriskNodes.FirstOrDefault(x => x.KeyPartition == "create").Nodes;
64+
Assert.Single(asteriskCreateNodes);
65+
Assert.Contains(asteriskCreateNodes, x => x.KeyPartition == "*");
66+
}
67+
68+
[Theory]
69+
[InlineData("create.connection", new[] { "#", "create.*", "create.#" })]
70+
[InlineData("create.stable.connection", new[] { "#", "create.#", "*.*.*" })]
71+
[InlineData("connection.create.stable", new[] { "#", "*.create.*", "*.*.*" })]
72+
[InlineData("file.delete", new[] { "#", "#.delete" })]
73+
[InlineData("file.info.delete", new[] { "#", "#.delete", "*.*.*" })]
74+
[InlineData("file.update", new[] { "#", "#.update", "*.update" })]
75+
[InlineData("file.update.author", new[] { "#", "*.*.*" })]
76+
[InlineData("file.update.author.credentials", new[] { "#" })]
77+
[InlineData("report.create", new[] { "#", "#.create" })]
78+
[InlineData("final.report.create", new[] { "#", "#.create", "*.*.*", "*.*.create" })]
79+
public void ShouldProperlyGetMatchingRoutes(string routingKey, IEnumerable<string> routes)
80+
{
81+
var tree = WildcardExtensions.ConstructTree(_routes);
82+
83+
var routingKeyParts = routingKey.Split(".");
84+
var matchingRoutes = WildcardExtensions.GetMatchingRoutePatterns(tree, routingKeyParts).ToList();
85+
86+
Assert.Equal(routes.Count(), matchingRoutes.Count);
87+
foreach (var route in routes)
88+
{
89+
Assert.Contains(matchingRoutes, x => x == route);
90+
}
91+
}
92+
93+
int CountNodes(IEnumerable<TreeNode> nodes)
94+
{
95+
var count = nodes.Count();
96+
foreach (var node in nodes)
97+
{
98+
if (node.Nodes.Any())
99+
{
100+
count += CountNodes(node.Nodes);
101+
}
102+
}
103+
104+
return count;
105+
}
106+
}
107+
}

tests/RabbitMQ.Client.Core.DependencyInjection.Tests/WildcardExtensionsTests.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)