88import java .util .HashMap ;
99import java .util .List ;
1010import java .util .Map ;
11+ import java .util .Map .Entry ;
1112
1213public class GraphiteNamePatternTest {
1314
14- @ Test (expected = IllegalArgumentException .class )
15- public void createNew_WHEN_InvalidPattern_THEN_ShouldThrowException () {
16- final List <String > invalidPatterns = Arrays .asList (
17- "" ,
18- "a" ,
19- "1org" ,
20- "1org." ,
21- "org." ,
22- "org.**" ,
23- "org.**" ,
24- "org.company-" ,
25- "org.company-." ,
26- "org.company-*" ,
27- "org.company.**" ,
28- "org.company.**-" ,
29- "org.com*pany.*" ,
30- "org.test.contr.oller.gather.status..400" ,
31- "org.test.controller.gather.status..400"
32- );
33- final GraphiteNamePattern graphiteNamePattern = new GraphiteNamePattern ("" );
34- for (String pattern : invalidPatterns ) {
35- try {
36- new GraphiteNamePattern (pattern );
37-
38- Assertions .failBecauseExceptionWasNotThrown (IllegalArgumentException .class );
39- } catch (IllegalArgumentException e ) {
40- Assertions .assertThat (e ).hasMessageContaining (pattern );
41- }
42- }
43- }
44-
4515 @ Test
4616 public void createNew_WHEN_ValidPattern_THEN_ShouldCreateThePatternSuccessfully () {
4717 final List <String > validPatterns = Arrays .asList (
@@ -58,6 +28,7 @@ public void createNew_WHEN_ValidPattern_THEN_ShouldCreateThePatternSuccessfully(
5828 }
5929 }
6030
31+
6132 @ Test
6233 public void createNew_WHEN_ValidPattern_THEN_ShouldInitInternalPatternSuccessfully () {
6334 final Map <String , String > validPatterns = new HashMap <String , String >();
@@ -72,6 +43,7 @@ public void createNew_WHEN_ValidPattern_THEN_ShouldInitInternalPatternSuccessful
7243 }
7344 }
7445
46+
7547 @ Test
7648 public void match_WHEN_NotMatchingMetricNameProvided_THEN_ShouldNotMatch () {
7749 final GraphiteNamePattern pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
@@ -86,6 +58,7 @@ public void match_WHEN_NotMatchingMetricNameProvided_THEN_ShouldNotMatch() {
8658 }
8759 }
8860
61+
8962 @ Test
9063 public void match_WHEN_MatchingMetricNameProvided_THEN_ShouldMatch () {
9164 final GraphiteNamePattern pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
@@ -102,6 +75,28 @@ public void match_WHEN_MatchingMetricNameProvided_THEN_ShouldMatch() {
10275 }
10376 }
10477
78+
79+ @ Test
80+ public void match_WHEN_onlyGlob_THEN_ShouldMatchAny () {
81+ GraphiteNamePattern pattern = new GraphiteNamePattern ("*" );
82+ Assertions .assertThat (pattern .matches ("foo" )).isTrue ();
83+ Assertions .assertThat (pattern .matches ("bar" )).isTrue ();
84+ }
85+
86+
87+ @ Test
88+ public void match_WHEN_varyingFormats_THEN_ShouldMatchRegardless () {
89+ Map <String , String > metricsToPatterns = new HashMap <String , String >();
90+ metricsToPatterns .put ("snake_case_example_metric" , "snake_case_*_metric" );
91+ metricsToPatterns .put ("CamelCasedExampleMetric" , "CamelCased*Metric" );
92+ metricsToPatterns .put ("Weird.Mixture_Of_Formats.Example_metric" , "Weird.Mixture_Of_Formats.*_metric" );
93+
94+ for (Entry <String , String > metricToPattern : metricsToPatterns .entrySet ()) {
95+ Assertions .assertThat (new GraphiteNamePattern (metricToPattern .getValue ()).matches (metricToPattern .getKey ())).isTrue ();
96+ }
97+ }
98+
99+
105100 @ Test
106101 public void extractParameters () {
107102 GraphiteNamePattern pattern ;
@@ -110,17 +105,18 @@ public void extractParameters() {
110105 expected .put ("${1}" , "400" );
111106 pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
112107 Assertions .assertThat (pattern .extractParameters ("org.test.controller.gather.status.400" ))
113- .isEqualTo (expected );
108+ .isEqualTo (expected );
114109
115110 expected = new HashMap <String , String >();
116111 expected .put ("${0}" , "org" );
117112 expected .put ("${1}" , "gather" );
118113 expected .put ("${2}" , "400" );
119114 pattern = new GraphiteNamePattern ("*.test.controller.*.status.*" );
120115 Assertions .assertThat (pattern .extractParameters ("org.test.controller.gather.status.400" ))
121- .isEqualTo (expected );
116+ .isEqualTo (expected );
122117 }
123118
119+
124120 @ Test
125121 public void extractParameters_WHEN_emptyStringInDottedMetricsName_THEN_ShouldReturnEmptyString () {
126122 GraphiteNamePattern pattern ;
@@ -129,16 +125,42 @@ public void extractParameters_WHEN_emptyStringInDottedMetricsName_THEN_ShouldRet
129125 expected .put ("${1}" , "400" );
130126 pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
131127 Assertions .assertThat (pattern .extractParameters ("org.test.controller..status.400" ))
132- .isEqualTo (expected );
128+ .isEqualTo (expected );
133129
134130 }
135131
132+
136133 @ Test
137134 public void extractParameters_WHEN_moreDots_THEN_ShouldReturnNoMatches () {
138135 GraphiteNamePattern pattern ;
139136 pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
140137 Assertions .assertThat (pattern .extractParameters ("org.test.controller...status.400" ))
141- .isEqualTo (Collections .emptyMap ());
138+ .isEqualTo (Collections .emptyMap ());
139+
140+ }
142141
142+ @ Test
143+ public void extractParameters_WHEN_onlyGlob_THEN_ShouldExtractEntireMetric () {
144+ String metric = "http_requests" ;
145+ GraphiteNamePattern pattern = new GraphiteNamePattern ("*" );
146+ Map <String , String > expected = new HashMap <String , String >();
147+ expected .put ("${0}" , metric );
148+ Assertions .assertThat (pattern .extractParameters (metric )).isEqualTo (expected );
149+ }
150+
151+
152+ @ Test
153+ public void extractParameters_WHEN_varyingFormats_THEN_ShouldExtractRegardless () {
154+ Map <String , String > metricsToPatterns = new HashMap <String , String >();
155+ metricsToPatterns .put ("snake_case_example_metric" , "snake_case_*_metric" );
156+ metricsToPatterns .put ("CamelCasedExampleMetric" , "CamelCased*Metric" );
157+ metricsToPatterns .put ("Weird.Mixture_Of_Formats.Example_metric" , "Weird.Mixture_Of_Formats.*_metric" );
158+
159+ for (Entry <String , String > metricToPattern : metricsToPatterns .entrySet ()) {
160+ GraphiteNamePattern graphiteNamePattern = new GraphiteNamePattern (metricToPattern .getValue ());
161+ Entry <String , String > actual = graphiteNamePattern .extractParameters (metricToPattern .getKey ()).entrySet ().iterator ().next ();
162+ Assertions .assertThat (actual .getKey ()).isEqualTo ("${0}" );
163+ Assertions .assertThat (actual .getValue ()).isEqualToIgnoringCase ("example" );
164+ }
143165 }
144166}
0 commit comments