1616
1717package org .springframework .boot .actuate .autoconfigure ;
1818
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-
3019import javax .servlet .Filter ;
3120import javax .servlet .FilterChain ;
3221
4938import org .springframework .web .bind .annotation .ResponseStatus ;
5039import org .springframework .web .bind .annotation .RestController ;
5140
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+
5252/**
5353 * Tests for {@link MetricFilterAutoConfiguration}.
5454 *
5555 * @author Phillip Webb
5656 */
5757public class MetricFilterAutoConfigurationTests {
58-
5958
6059 @ Test
6160 public void recordsHttpInteractions () throws Exception {
@@ -79,52 +78,55 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
7978 anyDouble ());
8079 context .close ();
8180 }
82-
81+
8382 @ Test
8483 public void recordsHttpInteractionsWithTemplateVariable () throws Exception {
8584 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
8685 Config .class , MetricFilterAutoConfiguration .class );
8786 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 ());
9595 context .close ();
9696 }
97-
97+
9898 @ Test
99- public void recordsKnown404HttpInteractionsAsSingleMetricWithPathAndTemplateVariable () throws Exception {
99+ public void recordsKnown404HttpInteractionsAsSingleMetricWithPathAndTemplateVariable ()
100+ throws Exception {
100101 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
101102 Config .class , MetricFilterAutoConfiguration .class );
102103 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 ());
110112 context .close ();
111113 }
112-
114+
113115 @ Test
114116 public void records404HttpInteractionsAsSingleMetric () throws Exception {
115117 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (
116118 Config .class , MetricFilterAutoConfiguration .class );
117119 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 ());
128130 context .close ();
129131 }
130132
@@ -148,27 +150,23 @@ public CounterService counterService() {
148150 public GaugeService gaugeService () {
149151 return mock (GaugeService .class );
150152 }
151-
153+
152154 }
153155
154156}
155157
156-
157158@ RestController
158- class MetricFilterTestController
159- {
160-
159+ class MetricFilterTestController {
160+
161161 @ RequestMapping ("templateVarTest/{someVariable}" )
162- public String testTemplateVariableResolution (@ PathVariable String someVariable )
163- {
162+ public String testTemplateVariableResolution (@ PathVariable String someVariable ) {
164163 return someVariable ;
165164 }
166-
165+
167166 @ RequestMapping ("knownPath/{someVariable}" )
168167 @ ResponseStatus (HttpStatus .NOT_FOUND )
169168 @ ResponseBody
170- public String testKnownPathWith404Response (@ PathVariable String someVariable )
171- {
169+ public String testKnownPathWith404Response (@ PathVariable String someVariable ) {
172170 return someVariable ;
173171 }
174172}
0 commit comments