22
33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .assertj .core .api .Assertions .assertThatThrownBy ;
5+ import static org .mockito .Mockito .mock ;
56
67import com .retailsvc .http .JsonMapper ;
78import com .retailsvc .http .MethodNotAllowedException ;
3738class RequestPreparationFilterTest {
3839
3940 private HttpExchange exchange (String method , String path , byte [] body ) {
40- HttpExchange ex = Mockito . mock (HttpExchange .class );
41+ HttpExchange ex = mock (HttpExchange .class );
4142 Mockito .when (ex .getRequestMethod ()).thenReturn (method );
4243 Mockito .when (ex .getRequestURI ()).thenReturn (URI .create (path ));
4344 Mockito .when (ex .getRequestHeaders ()).thenReturn (new Headers ());
@@ -81,7 +82,7 @@ void successPathBindsRequestContextDuringChain() throws Exception {
8182 AtomicReference <String > seenOpId = new AtomicReference <>();
8283 AtomicReference <Map <String , String >> seenPathParams = new AtomicReference <>();
8384
84- Filter .Chain chain = Mockito . mock (Filter .Chain .class );
85+ Filter .Chain chain = mock (Filter .Chain .class );
8586 Mockito .doAnswer (
8687 inv -> {
8788 seenOpId .set (Request .operationId ());
@@ -112,7 +113,7 @@ void unknownPathThrowsNotFound() {
112113 Filter f = newFilter (spec );
113114
114115 HttpExchange ex = exchange ("GET" , "/missing" , new byte [0 ]);
115- assertThatThrownBy (() -> f .doFilter (ex , Mockito . mock (Filter .Chain .class )))
116+ assertThatThrownBy (() -> f .doFilter (ex , mock (Filter .Chain .class )))
116117 .isInstanceOf (NotFoundException .class );
117118 }
118119
@@ -130,7 +131,7 @@ void wrongMethodThrowsMethodNotAllowed() {
130131 Filter f = newFilter (spec );
131132
132133 HttpExchange ex = exchange ("POST" , "/x" , new byte [0 ]);
133- assertThatThrownBy (() -> f .doFilter (ex , Mockito . mock (Filter .Chain .class )))
134+ assertThatThrownBy (() -> f .doFilter (ex , mock (Filter .Chain .class )))
134135 .isInstanceOf (MethodNotAllowedException .class );
135136 }
136137
@@ -149,7 +150,7 @@ void invalidQueryParamThrowsValidation() {
149150 Filter f = newFilter (spec );
150151
151152 HttpExchange ex = exchange ("GET" , "/x?q=ab" , new byte [0 ]);
152- assertThatThrownBy (() -> f .doFilter (ex , Mockito . mock (Filter .Chain .class )))
153+ assertThatThrownBy (() -> f .doFilter (ex , mock (Filter .Chain .class )))
153154 .isInstanceOf (ValidationException .class )
154155 .extracting (t -> ((ValidationException ) t ).error ().pointer ())
155156 .isEqualTo ("/query/q" );
@@ -170,7 +171,9 @@ void integerQueryParamIsCoercedFromStringBeforeValidation() throws Exception {
170171 Filter f = newFilter (spec );
171172
172173 HttpExchange ex = exchange ("GET" , "/x?n=42" , new byte [0 ]);
173- f .doFilter (ex , Mockito .mock (Filter .Chain .class ));
174+ Filter .Chain chain = mock (Filter .Chain .class );
175+ f .doFilter (ex , chain );
176+ Mockito .verify (chain ).doFilter (ex );
174177 }
175178
176179 @ Test
@@ -188,7 +191,7 @@ void integerQueryParamRejectsNonNumericString() {
188191 Filter f = newFilter (spec );
189192
190193 HttpExchange ex = exchange ("GET" , "/x?n=abc" , new byte [0 ]);
191- assertThatThrownBy (() -> f .doFilter (ex , Mockito . mock (Filter .Chain .class )))
194+ assertThatThrownBy (() -> f .doFilter (ex , mock (Filter .Chain .class )))
192195 .isInstanceOf (ValidationException .class )
193196 .extracting (t -> ((ValidationException ) t ).error ().keyword ())
194197 .isEqualTo ("type" );
@@ -209,7 +212,9 @@ void numberQueryParamIsCoercedFromStringBeforeValidation() throws Exception {
209212 Filter f = newFilter (spec );
210213
211214 HttpExchange ex = exchange ("GET" , "/x?n=1.5" , new byte [0 ]);
212- f .doFilter (ex , Mockito .mock (Filter .Chain .class ));
215+ Filter .Chain chain = mock (Filter .Chain .class );
216+ f .doFilter (ex , chain );
217+ Mockito .verify (chain ).doFilter (ex );
213218 }
214219
215220 @ Test
@@ -227,7 +232,7 @@ void numberQueryParamRejectsNonNumericString() {
227232 Filter f = newFilter (spec );
228233
229234 HttpExchange ex = exchange ("GET" , "/x?n=abc" , new byte [0 ]);
230- assertThatThrownBy (() -> f .doFilter (ex , Mockito . mock (Filter .Chain .class )))
235+ assertThatThrownBy (() -> f .doFilter (ex , mock (Filter .Chain .class )))
231236 .isInstanceOf (ValidationException .class )
232237 .extracting (t -> ((ValidationException ) t ).error ().keyword ())
233238 .isEqualTo ("type" );
@@ -247,8 +252,14 @@ void booleanQueryParamCoercesTrueAndFalse() throws Exception {
247252 Spec spec = specWith (op );
248253 Filter f = newFilter (spec );
249254
250- f .doFilter (exchange ("GET" , "/x?b=true" , new byte [0 ]), Mockito .mock (Filter .Chain .class ));
251- f .doFilter (exchange ("GET" , "/x?b=false" , new byte [0 ]), Mockito .mock (Filter .Chain .class ));
255+ Filter .Chain trueChain = mock (Filter .Chain .class );
256+ Filter .Chain falseChain = mock (Filter .Chain .class );
257+ HttpExchange trueEx = exchange ("GET" , "/x?b=true" , new byte [0 ]);
258+ HttpExchange falseEx = exchange ("GET" , "/x?b=false" , new byte [0 ]);
259+ f .doFilter (trueEx , trueChain );
260+ f .doFilter (falseEx , falseChain );
261+ Mockito .verify (trueChain ).doFilter (trueEx );
262+ Mockito .verify (falseChain ).doFilter (falseEx );
252263 }
253264
254265 @ Test
@@ -266,7 +277,7 @@ void booleanQueryParamRejectsNonBooleanString() {
266277 Filter f = newFilter (spec );
267278
268279 HttpExchange ex = exchange ("GET" , "/x?b=yes" , new byte [0 ]);
269- assertThatThrownBy (() -> f .doFilter (ex , Mockito . mock (Filter .Chain .class )))
280+ assertThatThrownBy (() -> f .doFilter (ex , mock (Filter .Chain .class )))
270281 .isInstanceOf (ValidationException .class )
271282 .extracting (t -> ((ValidationException ) t ).error ().keyword ())
272283 .isEqualTo ("type" );
0 commit comments