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+ }
0 commit comments