16
16
17
17
package org .springframework .boot .actuate .autoconfigure ;
18
18
19
- import static org .hamcrest .Matchers .equalTo ;
20
- import static org .junit .Assert .assertThat ;
21
- import static org .mockito .BDDMockito .willAnswer ;
22
- import static org .mockito .Matchers .anyDouble ;
23
- import static org .mockito .Matchers .eq ;
24
- import static org .mockito .Mockito .mock ;
25
- import static org .mockito .Mockito .times ;
26
- import static org .mockito .Mockito .verify ;
27
- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
28
- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
29
-
30
19
import javax .servlet .Filter ;
31
20
import javax .servlet .FilterChain ;
32
21
49
38
import org .springframework .web .bind .annotation .ResponseStatus ;
50
39
import org .springframework .web .bind .annotation .RestController ;
51
40
41
+ import static org .hamcrest .Matchers .equalTo ;
42
+ import static org .junit .Assert .assertThat ;
43
+ import static org .mockito .BDDMockito .willAnswer ;
44
+ import static org .mockito .Matchers .anyDouble ;
45
+ import static org .mockito .Matchers .eq ;
46
+ import static org .mockito .Mockito .mock ;
47
+ import static org .mockito .Mockito .times ;
48
+ import static org .mockito .Mockito .verify ;
49
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
50
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
51
+
52
52
/**
53
53
* Tests for {@link MetricFilterAutoConfiguration}.
54
54
*
55
55
* @author Phillip Webb
56
56
*/
57
57
public class MetricFilterAutoConfigurationTests {
58
-
59
58
60
59
@ Test
61
60
public void recordsHttpInteractions () throws Exception {
@@ -79,52 +78,55 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
79
78
anyDouble ());
80
79
context .close ();
81
80
}
82
-
81
+
83
82
@ Test
84
83
public void recordsHttpInteractionsWithTemplateVariable () throws Exception {
85
84
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
86
85
Config .class , MetricFilterAutoConfiguration .class );
87
86
Filter filter = context .getBean (Filter .class );
88
- MockMvc mvc = MockMvcBuilders .standaloneSetup (new MetricFilterTestController ()).addFilter (filter ).build ();
89
- mvc .perform (get ("/templateVarTest/foo" ))
90
- .andExpect (status ().isOk ());
91
-
92
- verify (context .getBean (CounterService .class )).increment ("status.200.templateVarTest.-someVariable-" );
93
- verify (context .getBean (GaugeService .class )).submit (eq ("response.templateVarTest.-someVariable-" ),
94
- anyDouble ());
87
+ MockMvc mvc = MockMvcBuilders .standaloneSetup (new MetricFilterTestController ())
88
+ .addFilter (filter ).build ();
89
+ mvc .perform (get ("/templateVarTest/foo" )).andExpect (status ().isOk ());
90
+
91
+ verify (context .getBean (CounterService .class )).increment (
92
+ "status.200.templateVarTest.-someVariable-" );
93
+ verify (context .getBean (GaugeService .class )).submit (
94
+ eq ("response.templateVarTest.-someVariable-" ), anyDouble ());
95
95
context .close ();
96
96
}
97
-
97
+
98
98
@ Test
99
- public void recordsKnown404HttpInteractionsAsSingleMetricWithPathAndTemplateVariable () throws Exception {
99
+ public void recordsKnown404HttpInteractionsAsSingleMetricWithPathAndTemplateVariable ()
100
+ throws Exception {
100
101
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
101
102
Config .class , MetricFilterAutoConfiguration .class );
102
103
Filter filter = context .getBean (Filter .class );
103
- MockMvc mvc = MockMvcBuilders .standaloneSetup (new MetricFilterTestController ()).addFilter (filter ).build ();
104
- mvc .perform (get ("/knownPath/foo" ))
105
- .andExpect (status ().isNotFound ());
106
-
107
- verify (context .getBean (CounterService .class )).increment ("status.404.knownPath.-someVariable-" );
108
- verify (context .getBean (GaugeService .class )).submit (eq ("response.knownPath.-someVariable-" ),
109
- anyDouble ());
104
+ MockMvc mvc = MockMvcBuilders .standaloneSetup (new MetricFilterTestController ())
105
+ .addFilter (filter ).build ();
106
+ mvc .perform (get ("/knownPath/foo" )).andExpect (status ().isNotFound ());
107
+
108
+ verify (context .getBean (CounterService .class )).increment (
109
+ "status.404.knownPath.-someVariable-" );
110
+ verify (context .getBean (GaugeService .class )).submit (
111
+ eq ("response.knownPath.-someVariable-" ), anyDouble ());
110
112
context .close ();
111
113
}
112
-
114
+
113
115
@ Test
114
116
public void records404HttpInteractionsAsSingleMetric () throws Exception {
115
117
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
116
118
Config .class , MetricFilterAutoConfiguration .class );
117
119
Filter filter = context .getBean (Filter .class );
118
- MockMvc mvc = MockMvcBuilders .standaloneSetup (new MetricFilterTestController ()). addFilter ( filter ). build ();
119
- mvc . perform ( get ( "/unknownPath/1" ))
120
- .andExpect (status ().isNotFound ());
121
-
122
- mvc .perform (get ("/unknownPath/2" ))
123
- . andExpect ( status (). isNotFound ());
124
-
125
- verify ( context . getBean ( CounterService . class ), times ( 2 )). increment ( "status.404.unknownPath " );
126
- verify (context .getBean (GaugeService .class ), times (2 )).submit (eq ( "response.unknownPath" ),
127
- anyDouble ());
120
+ MockMvc mvc = MockMvcBuilders .standaloneSetup (new MetricFilterTestController ())
121
+ . addFilter ( filter ). build ();
122
+ mvc . perform ( get ( "/unknownPath/1" )) .andExpect (status ().isNotFound ());
123
+
124
+ mvc .perform (get ("/unknownPath/2" )). andExpect ( status (). isNotFound ());
125
+
126
+ verify ( context . getBean ( CounterService . class ), times ( 2 )). increment (
127
+ "status.404.unmapped " );
128
+ verify (context .getBean (GaugeService .class ), times (2 )).submit (
129
+ eq ( "response.unmapped" ), anyDouble ());
128
130
context .close ();
129
131
}
130
132
@@ -148,27 +150,23 @@ public CounterService counterService() {
148
150
public GaugeService gaugeService () {
149
151
return mock (GaugeService .class );
150
152
}
151
-
153
+
152
154
}
153
155
154
156
}
155
157
156
-
157
158
@ RestController
158
- class MetricFilterTestController
159
- {
160
-
159
+ class MetricFilterTestController {
160
+
161
161
@ RequestMapping ("templateVarTest/{someVariable}" )
162
- public String testTemplateVariableResolution (@ PathVariable String someVariable )
163
- {
162
+ public String testTemplateVariableResolution (@ PathVariable String someVariable ) {
164
163
return someVariable ;
165
164
}
166
-
165
+
167
166
@ RequestMapping ("knownPath/{someVariable}" )
168
167
@ ResponseStatus (HttpStatus .NOT_FOUND )
169
168
@ ResponseBody
170
- public String testKnownPathWith404Response (@ PathVariable String someVariable )
171
- {
169
+ public String testKnownPathWith404Response (@ PathVariable String someVariable ) {
172
170
return someVariable ;
173
171
}
174
172
}
0 commit comments