4
4
import com .codahale .metrics .health .HealthCheck ;
5
5
import com .codahale .metrics .health .HealthCheckFilter ;
6
6
import com .codahale .metrics .health .HealthCheckRegistry ;
7
+ import com .fasterxml .jackson .databind .ObjectMapper ;
7
8
import jakarta .servlet .ServletConfig ;
8
9
import jakarta .servlet .ServletContext ;
9
10
import jakarta .servlet .ServletException ;
15
16
16
17
import java .time .ZonedDateTime ;
17
18
import java .time .format .DateTimeFormatter ;
19
+ import java .util .concurrent .Callable ;
18
20
import java .util .concurrent .ExecutorService ;
19
21
import java .util .concurrent .Executors ;
20
22
@@ -75,136 +77,79 @@ public void tearDown() {
75
77
public void returns501IfNoHealthChecksAreRegistered () throws Exception {
76
78
processRequest ();
77
79
78
- assertThat (response .getStatus ())
79
- .isEqualTo (501 );
80
- assertThat (response .getContent ())
81
- .isEqualTo ("{}" );
82
- assertThat (response .get (HttpHeader .CONTENT_TYPE ))
83
- .isEqualTo ("application/json" );
80
+ assertThat (response .getStatus ()).isEqualTo (501 );
81
+ assertThat (response .get (HttpHeader .CONTENT_TYPE )).isEqualTo ("application/json" );
82
+ assertThat (response .getContent ()).isEqualTo ("{}" );
84
83
}
85
84
86
85
@ Test
87
86
public void returnsA200IfAllHealthChecksAreHealthy () throws Exception {
88
- registry .register ("fun" , new HealthCheck () {
89
- @ Override
90
- protected Result check () {
91
- return healthyResultUsingFixedClockWithMessage ("whee" );
92
- }
93
-
94
- @ Override
95
- protected Clock clock () {
96
- return FIXED_CLOCK ;
97
- }
98
- });
87
+ registry .register ("fun" , new TestHealthCheck (() -> healthyResultWithMessage ("whee" )));
99
88
100
89
processRequest ();
101
90
102
- assertThat (response .getStatus ())
103
- . isEqualTo (200 );
91
+ assertThat (response .getStatus ()). isEqualTo ( 200 );
92
+ assertThat ( response . get ( HttpHeader . CONTENT_TYPE )). isEqualTo ("application/json" );
104
93
assertThat (response .getContent ())
105
94
.isEqualTo ("{\" fun\" :{\" healthy\" :true,\" message\" :\" whee\" ,\" duration\" :0,\" timestamp\" :\" " +
106
95
EXPECTED_TIMESTAMP +
107
96
"\" }}" );
108
- assertThat (response .get (HttpHeader .CONTENT_TYPE ))
109
- .isEqualTo ("application/json" );
110
97
}
111
98
112
99
@ Test
113
100
public void returnsASubsetOfHealthChecksIfFiltered () throws Exception {
114
- registry .register ("fun" , new HealthCheck () {
115
- @ Override
116
- protected Result check () {
117
- return healthyResultUsingFixedClockWithMessage ("whee" );
118
- }
119
-
120
- @ Override
121
- protected Clock clock () {
122
- return FIXED_CLOCK ;
123
- }
124
- });
125
-
126
- registry .register ("filtered" , new HealthCheck () {
127
- @ Override
128
- protected Result check () {
129
- return Result .unhealthy ("whee" );
130
- }
131
-
132
- @ Override
133
- protected Clock clock () {
134
- return FIXED_CLOCK ;
135
- }
136
- });
101
+ registry .register ("fun" , new TestHealthCheck (() -> healthyResultWithMessage ("whee" )));
102
+ registry .register ("filtered" , new TestHealthCheck (() -> unhealthyResultWithMessage ("whee" )));
137
103
138
104
processRequest ();
139
105
140
- assertThat (response .getStatus ())
141
- . isEqualTo (200 );
106
+ assertThat (response .getStatus ()). isEqualTo ( 200 );
107
+ assertThat ( response . get ( HttpHeader . CONTENT_TYPE )). isEqualTo ("application/json" );
142
108
assertThat (response .getContent ())
143
109
.isEqualTo ("{\" fun\" :{\" healthy\" :true,\" message\" :\" whee\" ,\" duration\" :0,\" timestamp\" :\" " +
144
110
EXPECTED_TIMESTAMP +
145
111
"\" }}" );
146
- assertThat (response .get (HttpHeader .CONTENT_TYPE ))
147
- .isEqualTo ("application/json" );
148
112
}
149
113
150
114
@ Test
151
115
public void returnsA500IfAnyHealthChecksAreUnhealthy () throws Exception {
152
- registry .register ("fun" , new HealthCheck () {
153
- @ Override
154
- protected Result check () {
155
- return healthyResultUsingFixedClockWithMessage ("whee" );
156
- }
157
-
158
- @ Override
159
- protected Clock clock () {
160
- return FIXED_CLOCK ;
161
- }
162
- });
163
-
164
- registry .register ("notFun" , new HealthCheck () {
165
- @ Override
166
- protected Result check () {
167
- return Result .builder ().usingClock (FIXED_CLOCK ).unhealthy ().withMessage ("whee" ).build ();
168
- }
169
-
170
- @ Override
171
- protected Clock clock () {
172
- return FIXED_CLOCK ;
173
- }
174
- });
116
+ registry .register ("fun" , new TestHealthCheck (() -> healthyResultWithMessage ("whee" )));
117
+ registry .register ("notFun" , new TestHealthCheck (() -> unhealthyResultWithMessage ("whee" )));
175
118
176
119
processRequest ();
177
120
178
- assertThat (response .getStatus ())
179
- .isEqualTo (500 );
180
- assertThat (response .getContent ())
181
- .contains (
121
+ assertThat (response .getStatus ()).isEqualTo (500 );
122
+ assertThat (response .get (HttpHeader .CONTENT_TYPE )).isEqualTo ("application/json" );
123
+ assertThat (response .getContent ()).contains (
182
124
"{\" fun\" :{\" healthy\" :true,\" message\" :\" whee\" ,\" duration\" :0,\" timestamp\" :\" " + EXPECTED_TIMESTAMP + "\" }" ,
183
125
",\" notFun\" :{\" healthy\" :false,\" message\" :\" whee\" ,\" duration\" :0,\" timestamp\" :\" " + EXPECTED_TIMESTAMP + "\" }}" );
184
- assertThat (response .get (HttpHeader .CONTENT_TYPE ))
185
- .isEqualTo ("application/json" );
126
+ }
127
+
128
+ @ Test
129
+ public void returnsA200IfAnyHealthChecksAreUnhealthyAndHttpStatusIndicatorIsDisabled () throws Exception {
130
+ registry .register ("fun" , new TestHealthCheck (() -> healthyResultWithMessage ("whee" )));
131
+ registry .register ("notFun" , new TestHealthCheck (() -> unhealthyResultWithMessage ("whee" )));
132
+ request .setURI ("/healthchecks?httpStatusIndicator=false" );
133
+
134
+ processRequest ();
135
+
136
+ assertThat (response .getStatus ()).isEqualTo (200 );
137
+ assertThat (response .get (HttpHeader .CONTENT_TYPE )).isEqualTo ("application/json" );
138
+ assertThat (response .getContent ()).contains (
139
+ "{\" fun\" :{\" healthy\" :true,\" message\" :\" whee\" ,\" duration\" :0,\" timestamp\" :\" " + EXPECTED_TIMESTAMP + "\" }" ,
140
+ ",\" notFun\" :{\" healthy\" :false,\" message\" :\" whee\" ,\" duration\" :0,\" timestamp\" :\" " + EXPECTED_TIMESTAMP + "\" }}" );
186
141
}
187
142
188
143
@ Test
189
144
public void optionallyPrettyPrintsTheJson () throws Exception {
190
- registry .register ("fun" , new HealthCheck () {
191
- @ Override
192
- protected Result check () {
193
- return healthyResultUsingFixedClockWithMessage ("foo bar 123" );
194
- }
195
-
196
- @ Override
197
- protected Clock clock () {
198
- return FIXED_CLOCK ;
199
- }
200
- });
145
+ registry .register ("fun" , new TestHealthCheck (() -> healthyResultWithMessage ("foo bar 123" )));
201
146
202
147
request .setURI ("/healthchecks?pretty=true" );
203
148
204
149
processRequest ();
205
150
206
- assertThat (response .getStatus ())
207
- . isEqualTo (200 );
151
+ assertThat (response .getStatus ()). isEqualTo ( 200 );
152
+ assertThat ( response . get ( HttpHeader . CONTENT_TYPE )). isEqualTo ("application/json" );
208
153
assertThat (response .getContent ())
209
154
.isEqualTo (String .format ("{%n" +
210
155
" \" fun\" : {%n" +
@@ -213,18 +158,24 @@ protected Clock clock() {
213
158
" \" duration\" : 0,%n" +
214
159
" \" timestamp\" : \" " + EXPECTED_TIMESTAMP + "\" " +
215
160
"%n }%n}" ));
216
- assertThat (response .get (HttpHeader .CONTENT_TYPE ))
217
- .isEqualTo ("application/json" );
218
161
}
219
162
220
- private static HealthCheck .Result healthyResultUsingFixedClockWithMessage (String message ) {
163
+ private static HealthCheck .Result healthyResultWithMessage (String message ) {
221
164
return HealthCheck .Result .builder ()
222
165
.healthy ()
223
166
.withMessage (message )
224
167
.usingClock (FIXED_CLOCK )
225
168
.build ();
226
169
}
227
170
171
+ private static HealthCheck .Result unhealthyResultWithMessage (String message ) {
172
+ return HealthCheck .Result .builder ()
173
+ .unhealthy ()
174
+ .withMessage (message )
175
+ .usingClock (FIXED_CLOCK )
176
+ .build ();
177
+ }
178
+
228
179
@ Test
229
180
public void constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig () throws Exception {
230
181
final HealthCheckRegistry healthCheckRegistry = mock (HealthCheckRegistry .class );
@@ -266,4 +217,39 @@ public void constructorWithRegistryAsArgumentUsesServletConfigWhenNullButWrongTy
266
217
final io .dropwizard .metrics .servlets .HealthCheckServlet healthCheckServlet = new HealthCheckServlet (null );
267
218
healthCheckServlet .init (servletConfig );
268
219
}
220
+
221
+ @ Test
222
+ public void constructorWithObjectMapperAsArgumentUsesServletConfigWhenNullButWrongTypeInContext () throws Exception {
223
+ final ServletContext servletContext = mock (ServletContext .class );
224
+ final ServletConfig servletConfig = mock (ServletConfig .class );
225
+ when (servletConfig .getServletContext ()).thenReturn (servletContext );
226
+ when (servletContext .getAttribute (HealthCheckServlet .HEALTH_CHECK_REGISTRY )).thenReturn (registry );
227
+ when (servletContext .getAttribute (HealthCheckServlet .HEALTH_CHECK_MAPPER )).thenReturn ("IRELLEVANT_STRING" );
228
+
229
+ final HealthCheckServlet healthCheckServlet = new HealthCheckServlet (null );
230
+ healthCheckServlet .init (servletConfig );
231
+
232
+ assertThat (healthCheckServlet .getMapper ())
233
+ .isNotNull ()
234
+ .isInstanceOf (ObjectMapper .class );
235
+ }
236
+
237
+ static class TestHealthCheck extends HealthCheck {
238
+ private final Callable <Result > check ;
239
+
240
+ public TestHealthCheck (Callable <Result > check ) {
241
+ this .check = check ;
242
+ }
243
+
244
+ @ Override
245
+ protected Result check () throws Exception {
246
+ return check .call ();
247
+ }
248
+
249
+ @ Override
250
+ protected Clock clock () {
251
+ return FIXED_CLOCK ;
252
+ }
253
+ }
254
+
269
255
}
0 commit comments