42
42
import org .springframework .boot .actuate .endpoint .web .EndpointMediaTypes ;
43
43
import org .springframework .boot .actuate .endpoint .web .ExposableWebEndpoint ;
44
44
import org .springframework .boot .actuate .endpoint .web .annotation .WebEndpointDiscoverer ;
45
+ import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
45
46
import org .springframework .boot .web .embedded .tomcat .TomcatServletWebServerFactory ;
46
47
import org .springframework .boot .web .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
47
48
import org .springframework .context .ApplicationContext ;
70
71
*/
71
72
class CloudFoundryMvcWebEndpointIntegrationTests {
72
73
73
- private static TokenValidator tokenValidator = mock (TokenValidator .class );
74
+ private final TokenValidator tokenValidator = mock (TokenValidator .class );
74
75
75
- private static CloudFoundrySecurityService securityService = mock (CloudFoundrySecurityService .class );
76
+ private final CloudFoundrySecurityService securityService = mock (CloudFoundrySecurityService .class );
76
77
77
78
@ Test
78
79
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 );
80
81
load (TestEndpointConfiguration .class ,
81
82
(client ) -> client .get ()
82
83
.uri ("/cfApplication/test" )
@@ -89,7 +90,7 @@ void operationWithSecurityInterceptorForbidden() {
89
90
90
91
@ Test
91
92
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 );
93
94
load (TestEndpointConfiguration .class ,
94
95
(client ) -> client .get ()
95
96
.uri ("/cfApplication/test" )
@@ -119,7 +120,7 @@ void responseToOptionsRequestIncludesCorsHeaders() {
119
120
120
121
@ Test
121
122
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 );
123
124
load (TestEndpointConfiguration .class ,
124
125
(client ) -> client .get ()
125
126
.uri ("/cfApplication" )
@@ -157,7 +158,7 @@ void linksToOtherEndpointsWithFullAccess() {
157
158
void linksToOtherEndpointsForbidden () {
158
159
CloudFoundryAuthorizationException exception = new CloudFoundryAuthorizationException (Reason .INVALID_TOKEN ,
159
160
"invalid-token" );
160
- willThrow (exception ).given (tokenValidator ).validate (any ());
161
+ willThrow (exception ).given (this . tokenValidator ).validate (any ());
161
162
load (TestEndpointConfiguration .class ,
162
163
(client ) -> client .get ()
163
164
.uri ("/cfApplication" )
@@ -170,7 +171,7 @@ void linksToOtherEndpointsForbidden() {
170
171
171
172
@ Test
172
173
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 );
174
175
load (TestEndpointConfiguration .class ,
175
176
(client ) -> client .get ()
176
177
.uri ("/cfApplication" )
@@ -198,26 +199,23 @@ void linksToOtherEndpointsWithRestrictedAccess() {
198
199
.doesNotExist ());
199
200
}
200
201
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 ()));
203
213
}
204
214
205
215
private int getPort (AnnotationConfigServletWebServerApplicationContext context ) {
206
216
return context .getWebServer ().getPort ();
207
217
}
208
218
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
-
221
219
private String mockAccessToken () {
222
220
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu"
223
221
+ "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ."
@@ -229,7 +227,8 @@ private String mockAccessToken() {
229
227
static class CloudFoundryMvcConfiguration {
230
228
231
229
@ Bean
232
- CloudFoundrySecurityInterceptor interceptor () {
230
+ CloudFoundrySecurityInterceptor interceptor (TokenValidator tokenValidator ,
231
+ CloudFoundrySecurityService securityService ) {
233
232
return new CloudFoundrySecurityInterceptor (tokenValidator , securityService , "app-id" );
234
233
}
235
234
0 commit comments