4242import org .springframework .boot .actuate .endpoint .web .EndpointMediaTypes ;
4343import org .springframework .boot .actuate .endpoint .web .ExposableWebEndpoint ;
4444import org .springframework .boot .actuate .endpoint .web .annotation .WebEndpointDiscoverer ;
45+ import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
4546import org .springframework .boot .web .embedded .tomcat .TomcatServletWebServerFactory ;
4647import org .springframework .boot .web .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
4748import org .springframework .context .ApplicationContext ;
7071 */
7172class CloudFoundryMvcWebEndpointIntegrationTests {
7273
73- private static TokenValidator tokenValidator = mock (TokenValidator .class );
74+ private final TokenValidator tokenValidator = mock (TokenValidator .class );
7475
75- private static CloudFoundrySecurityService securityService = mock (CloudFoundrySecurityService .class );
76+ private final CloudFoundrySecurityService securityService = mock (CloudFoundrySecurityService .class );
7677
7778 @ Test
7879 void operationWithSecurityInterceptorForbidden () {
79- given (securityService .getAccessLevel (any (), eq ("app-id" ))).willReturn (AccessLevel .RESTRICTED );
80+ given (this . securityService .getAccessLevel (any (), eq ("app-id" ))).willReturn (AccessLevel .RESTRICTED );
8081 load (TestEndpointConfiguration .class ,
8182 (client ) -> client .get ()
8283 .uri ("/cfApplication/test" )
@@ -89,7 +90,7 @@ void operationWithSecurityInterceptorForbidden() {
8990
9091 @ Test
9192 void operationWithSecurityInterceptorSuccess () {
92- given (securityService .getAccessLevel (any (), eq ("app-id" ))).willReturn (AccessLevel .FULL );
93+ given (this . securityService .getAccessLevel (any (), eq ("app-id" ))).willReturn (AccessLevel .FULL );
9394 load (TestEndpointConfiguration .class ,
9495 (client ) -> client .get ()
9596 .uri ("/cfApplication/test" )
@@ -119,7 +120,7 @@ void responseToOptionsRequestIncludesCorsHeaders() {
119120
120121 @ Test
121122 void linksToOtherEndpointsWithFullAccess () {
122- given (securityService .getAccessLevel (any (), eq ("app-id" ))).willReturn (AccessLevel .FULL );
123+ given (this . securityService .getAccessLevel (any (), eq ("app-id" ))).willReturn (AccessLevel .FULL );
123124 load (TestEndpointConfiguration .class ,
124125 (client ) -> client .get ()
125126 .uri ("/cfApplication" )
@@ -157,7 +158,7 @@ void linksToOtherEndpointsWithFullAccess() {
157158 void linksToOtherEndpointsForbidden () {
158159 CloudFoundryAuthorizationException exception = new CloudFoundryAuthorizationException (Reason .INVALID_TOKEN ,
159160 "invalid-token" );
160- willThrow (exception ).given (tokenValidator ).validate (any ());
161+ willThrow (exception ).given (this . tokenValidator ).validate (any ());
161162 load (TestEndpointConfiguration .class ,
162163 (client ) -> client .get ()
163164 .uri ("/cfApplication" )
@@ -170,7 +171,7 @@ void linksToOtherEndpointsForbidden() {
170171
171172 @ Test
172173 void linksToOtherEndpointsWithRestrictedAccess () {
173- given (securityService .getAccessLevel (any (), eq ("app-id" ))).willReturn (AccessLevel .RESTRICTED );
174+ given (this . securityService .getAccessLevel (any (), eq ("app-id" ))).willReturn (AccessLevel .RESTRICTED );
174175 load (TestEndpointConfiguration .class ,
175176 (client ) -> client .get ()
176177 .uri ("/cfApplication" )
@@ -198,26 +199,23 @@ void linksToOtherEndpointsWithRestrictedAccess() {
198199 .doesNotExist ());
199200 }
200201
201- private AnnotationConfigServletWebServerApplicationContext createApplicationContext (Class <?>... config ) {
202- return new AnnotationConfigServletWebServerApplicationContext (config );
202+ private void load (Class <?> configuration , Consumer <WebTestClient > clientConsumer ) {
203+ BiConsumer <ApplicationContext , WebTestClient > consumer = (context , client ) -> clientConsumer .accept (client );
204+ new WebApplicationContextRunner (AnnotationConfigServletWebServerApplicationContext ::new )
205+ .withUserConfiguration (configuration , CloudFoundryMvcConfiguration .class )
206+ .withBean (TokenValidator .class , () -> this .tokenValidator )
207+ .withBean (CloudFoundrySecurityService .class , () -> this .securityService )
208+ .run ((context ) -> consumer .accept (context , WebTestClient .bindToServer ()
209+ .baseUrl ("http://localhost:" + getPort (
210+ (AnnotationConfigServletWebServerApplicationContext ) context .getSourceApplicationContext ()))
211+ .responseTimeout (Duration .ofMinutes (5 ))
212+ .build ()));
203213 }
204214
205215 private int getPort (AnnotationConfigServletWebServerApplicationContext context ) {
206216 return context .getWebServer ().getPort ();
207217 }
208218
209- private void load (Class <?> configuration , Consumer <WebTestClient > clientConsumer ) {
210- BiConsumer <ApplicationContext , WebTestClient > consumer = (context , client ) -> clientConsumer .accept (client );
211- try (AnnotationConfigServletWebServerApplicationContext context = createApplicationContext (configuration ,
212- CloudFoundryMvcConfiguration .class )) {
213- consumer .accept (context ,
214- WebTestClient .bindToServer ()
215- .baseUrl ("http://localhost:" + getPort (context ))
216- .responseTimeout (Duration .ofMinutes (5 ))
217- .build ());
218- }
219- }
220-
221219 private String mockAccessToken () {
222220 return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu"
223221 + "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ."
@@ -229,7 +227,8 @@ private String mockAccessToken() {
229227 static class CloudFoundryMvcConfiguration {
230228
231229 @ Bean
232- CloudFoundrySecurityInterceptor interceptor () {
230+ CloudFoundrySecurityInterceptor interceptor (TokenValidator tokenValidator ,
231+ CloudFoundrySecurityService securityService ) {
233232 return new CloudFoundrySecurityInterceptor (tokenValidator , securityService , "app-id" );
234233 }
235234
0 commit comments