8
8
import java .util .HashMap ;
9
9
import java .util .List ;
10
10
import java .util .Map ;
11
+ import java .util .Map .Entry ;
11
12
12
13
public class GraphiteNamePatternTest {
13
14
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
-
45
15
@ Test
46
16
public void createNew_WHEN_ValidPattern_THEN_ShouldCreateThePatternSuccessfully () {
47
17
final List <String > validPatterns = Arrays .asList (
@@ -58,6 +28,7 @@ public void createNew_WHEN_ValidPattern_THEN_ShouldCreateThePatternSuccessfully(
58
28
}
59
29
}
60
30
31
+
61
32
@ Test
62
33
public void createNew_WHEN_ValidPattern_THEN_ShouldInitInternalPatternSuccessfully () {
63
34
final Map <String , String > validPatterns = new HashMap <String , String >();
@@ -72,6 +43,7 @@ public void createNew_WHEN_ValidPattern_THEN_ShouldInitInternalPatternSuccessful
72
43
}
73
44
}
74
45
46
+
75
47
@ Test
76
48
public void match_WHEN_NotMatchingMetricNameProvided_THEN_ShouldNotMatch () {
77
49
final GraphiteNamePattern pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
@@ -86,6 +58,7 @@ public void match_WHEN_NotMatchingMetricNameProvided_THEN_ShouldNotMatch() {
86
58
}
87
59
}
88
60
61
+
89
62
@ Test
90
63
public void match_WHEN_MatchingMetricNameProvided_THEN_ShouldMatch () {
91
64
final GraphiteNamePattern pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
@@ -102,6 +75,28 @@ public void match_WHEN_MatchingMetricNameProvided_THEN_ShouldMatch() {
102
75
}
103
76
}
104
77
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
+
105
100
@ Test
106
101
public void extractParameters () {
107
102
GraphiteNamePattern pattern ;
@@ -110,17 +105,18 @@ public void extractParameters() {
110
105
expected .put ("${1}" , "400" );
111
106
pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
112
107
Assertions .assertThat (pattern .extractParameters ("org.test.controller.gather.status.400" ))
113
- .isEqualTo (expected );
108
+ .isEqualTo (expected );
114
109
115
110
expected = new HashMap <String , String >();
116
111
expected .put ("${0}" , "org" );
117
112
expected .put ("${1}" , "gather" );
118
113
expected .put ("${2}" , "400" );
119
114
pattern = new GraphiteNamePattern ("*.test.controller.*.status.*" );
120
115
Assertions .assertThat (pattern .extractParameters ("org.test.controller.gather.status.400" ))
121
- .isEqualTo (expected );
116
+ .isEqualTo (expected );
122
117
}
123
118
119
+
124
120
@ Test
125
121
public void extractParameters_WHEN_emptyStringInDottedMetricsName_THEN_ShouldReturnEmptyString () {
126
122
GraphiteNamePattern pattern ;
@@ -129,16 +125,42 @@ public void extractParameters_WHEN_emptyStringInDottedMetricsName_THEN_ShouldRet
129
125
expected .put ("${1}" , "400" );
130
126
pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
131
127
Assertions .assertThat (pattern .extractParameters ("org.test.controller..status.400" ))
132
- .isEqualTo (expected );
128
+ .isEqualTo (expected );
133
129
134
130
}
135
131
132
+
136
133
@ Test
137
134
public void extractParameters_WHEN_moreDots_THEN_ShouldReturnNoMatches () {
138
135
GraphiteNamePattern pattern ;
139
136
pattern = new GraphiteNamePattern ("org.test.controller.*.status.*" );
140
137
Assertions .assertThat (pattern .extractParameters ("org.test.controller...status.400" ))
141
- .isEqualTo (Collections .emptyMap ());
138
+ .isEqualTo (Collections .emptyMap ());
139
+
140
+ }
142
141
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
+ }
143
165
}
144
166
}
0 commit comments