Skip to content

Commit c03b71e

Browse files
committed
Add nullability annotations to tests in module/spring-boot-security-oauth2-resource-server
See gh-47263
1 parent b0b5b1b commit c03b71e

3 files changed

Lines changed: 43 additions & 7 deletions

File tree

module/spring-boot-security-oauth2-resource-server/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ dependencies {
4747
testImplementation(testFixtures(project(":core:spring-boot-autoconfigure")))
4848
testImplementation("com.squareup.okhttp3:mockwebserver")
4949

50+
testCompileOnly("com.google.code.findbugs:jsr305")
51+
5052
testRuntimeOnly("ch.qos.logback:logback-classic")
5153
testRuntimeOnly("org.springframework:spring-webflux")
5254
}
55+
56+
tasks.named("compileTestJava") {
57+
options.nullability.checking = "tests"
58+
}

module/spring-boot-security-oauth2-resource-server/src/test/java/org/springframework/boot/security/oauth2/server/resource/autoconfigure/reactive/ReactiveOAuth2ResourceServerAutoConfigurationTests.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import okhttp3.mockwebserver.MockWebServer;
4040
import org.assertj.core.api.InstanceOfAssertFactories;
4141
import org.assertj.core.api.ThrowingConsumer;
42+
import org.jspecify.annotations.Nullable;
4243
import org.junit.jupiter.api.AfterEach;
4344
import org.junit.jupiter.api.Test;
4445
import org.junit.jupiter.params.ParameterizedTest;
@@ -87,6 +88,7 @@
8788
import org.springframework.security.web.server.SecurityWebFilterChain;
8889
import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
8990
import org.springframework.test.util.ReflectionTestUtils;
91+
import org.springframework.web.server.ServerWebExchange;
9092
import org.springframework.web.server.WebFilter;
9193

9294
import static org.assertj.core.api.Assertions.assertThat;
@@ -112,7 +114,7 @@ class ReactiveOAuth2ResourceServerAutoConfigurationTests {
112114
.withConfiguration(AutoConfigurations.of(ReactiveOAuth2ResourceServerAutoConfiguration.class))
113115
.withUserConfiguration(TestConfig.class);
114116

115-
private MockWebServer server;
117+
private @Nullable MockWebServer server;
116118

117119
private static final Duration TIMEOUT = Duration.ofSeconds(5000000);
118120

@@ -236,6 +238,7 @@ private void decodeJwt(AssertableReactiveWebApplicationContext context) {
236238
SupplierReactiveJwtDecoder supplierReactiveJwtDecoder = context.getBean(SupplierReactiveJwtDecoder.class);
237239
Mono<ReactiveJwtDecoder> reactiveJwtDecoderSupplier = (Mono<ReactiveJwtDecoder>) ReflectionTestUtils
238240
.getField(supplierReactiveJwtDecoder, "jwtDecoderMono");
241+
assertThat(reactiveJwtDecoderSupplier).isNotNull();
239242
try {
240243
reactiveJwtDecoderSupplier.flatMap((decoder) -> decoder.decode("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9."
241244
+ "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0."
@@ -538,7 +541,9 @@ void autoConfigurationShouldConfigureAudienceValidatorIfPropertyProvidedAndIssue
538541
SupplierReactiveJwtDecoder supplierJwtDecoderBean = context.getBean(SupplierReactiveJwtDecoder.class);
539542
Mono<ReactiveJwtDecoder> jwtDecoderSupplier = (Mono<ReactiveJwtDecoder>) ReflectionTestUtils
540543
.getField(supplierJwtDecoderBean, "jwtDecoderMono");
544+
assertThat(jwtDecoderSupplier).isNotNull();
541545
ReactiveJwtDecoder jwtDecoder = jwtDecoderSupplier.block();
546+
assertThat(jwtDecoder).isNotNull();
542547
validate(
543548
jwt().claim("iss", URI.create(issuerUri).toURL())
544549
.claim("aud", List.of("https://test-audience.com")),
@@ -612,6 +617,7 @@ void audienceValidatorWhenAudienceInvalid() throws Exception {
612617
ReactiveJwtDecoder jwtDecoder = context.getBean(ReactiveJwtDecoder.class);
613618
DelegatingOAuth2TokenValidator<Jwt> jwtValidator = (DelegatingOAuth2TokenValidator<Jwt>) ReflectionTestUtils
614619
.getField(jwtDecoder, "jwtValidator");
620+
assertThat(jwtValidator).isNotNull();
615621
Jwt jwt = jwt().claim("iss", new URL(issuerUri))
616622
.claim("aud", Collections.singletonList("https://other-audience.com"))
617623
.build();
@@ -638,6 +644,7 @@ void customValidatorWhenInvalid() throws Exception {
638644
ReactiveJwtDecoder jwtDecoder = context.getBean(ReactiveJwtDecoder.class);
639645
DelegatingOAuth2TokenValidator<Jwt> jwtValidator = (DelegatingOAuth2TokenValidator<Jwt>) ReflectionTestUtils
640646
.getField(jwtDecoder, "jwtValidator");
647+
assertThat(jwtValidator).isNotNull();
641648
Jwt jwt = jwt().claim("iss", new URL(issuerUri)).claim("custom_claim", "invalid_value").build();
642649
assertThat(jwtValidator.validate(jwt).hasErrors()).isTrue();
643650
});
@@ -720,6 +727,7 @@ void causesReactiveManagementWebSecurityAutoConfigurationToBackOff() {
720727
.doesNotHaveBean(ReactiveManagementWebSecurityAutoConfiguration.class));
721728
}
722729

730+
@SuppressWarnings("unchecked")
723731
private void assertFilterConfiguredWithJwtAuthenticationManager(AssertableReactiveWebApplicationContext context) {
724732
MatcherSecurityWebFilterChain filterChain = (MatcherSecurityWebFilterChain) context
725733
.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN);
@@ -728,12 +736,16 @@ private void assertFilterConfiguredWithJwtAuthenticationManager(AssertableReacti
728736
.filter((f) -> f instanceof AuthenticationWebFilter)
729737
.findFirst()
730738
.orElse(null);
731-
ReactiveAuthenticationManagerResolver<?> authenticationManagerResolver = (ReactiveAuthenticationManagerResolver<?>) ReflectionTestUtils
739+
assertThat(webFilter).isNotNull();
740+
ReactiveAuthenticationManagerResolver<ServerWebExchange> authenticationManagerResolver = (ReactiveAuthenticationManagerResolver<ServerWebExchange>) ReflectionTestUtils
732741
.getField(webFilter, "authenticationManagerResolver");
733-
Object authenticationManager = authenticationManagerResolver.resolve(null).block(TIMEOUT);
742+
assertThat(authenticationManagerResolver).isNotNull();
743+
Object authenticationManager = authenticationManagerResolver.resolve(mock(ServerWebExchange.class))
744+
.block(TIMEOUT);
734745
assertThat(authenticationManager).isInstanceOf(JwtReactiveAuthenticationManager.class);
735746
}
736747

748+
@SuppressWarnings("unchecked")
737749
private void assertFilterConfiguredWithOpaqueTokenAuthenticationManager(
738750
AssertableReactiveWebApplicationContext context) {
739751
MatcherSecurityWebFilterChain filterChain = (MatcherSecurityWebFilterChain) context
@@ -743,9 +755,12 @@ private void assertFilterConfiguredWithOpaqueTokenAuthenticationManager(
743755
.filter((f) -> f instanceof AuthenticationWebFilter)
744756
.findFirst()
745757
.orElse(null);
746-
ReactiveAuthenticationManagerResolver<?> authenticationManagerResolver = (ReactiveAuthenticationManagerResolver<?>) ReflectionTestUtils
758+
assertThat(webFilter).isNotNull();
759+
ReactiveAuthenticationManagerResolver<ServerWebExchange> authenticationManagerResolver = (ReactiveAuthenticationManagerResolver<ServerWebExchange>) ReflectionTestUtils
747760
.getField(webFilter, "authenticationManagerResolver");
748-
Object authenticationManager = authenticationManagerResolver.resolve(null).block(TIMEOUT);
761+
assertThat(authenticationManagerResolver).isNotNull();
762+
Object authenticationManager = authenticationManagerResolver.resolve(mock(ServerWebExchange.class))
763+
.block(TIMEOUT);
749764
assertThat(authenticationManager).isInstanceOf(OpaqueTokenReactiveAuthenticationManager.class);
750765
}
751766

@@ -760,12 +775,14 @@ private void setupMockResponse(String issuer) {
760775
MockResponse mockResponse = new MockResponse().setResponseCode(HttpStatus.OK.value())
761776
.setBody(new ObjectMapper().writeValueAsString(getResponse(issuer)))
762777
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
778+
assertThat(this.server).isNotNull();
763779
this.server.enqueue(mockResponse);
764780
this.server.enqueue(
765781
new MockResponse().setResponseCode(200).setHeader("Content-Type", "application/json").setBody(JWK_SET));
766782
}
767783

768784
private void setupMockResponsesWithErrors(String issuer, int errorResponseCount) {
785+
assertThat(this.server).isNotNull();
769786
for (int i = 0; i < errorResponseCount; i++) {
770787
MockResponse emptyResponse = new MockResponse().setResponseCode(HttpStatus.NOT_FOUND.value());
771788
this.server.enqueue(emptyResponse);
@@ -808,6 +825,7 @@ private void validate(Jwt.Builder builder, ReactiveJwtDecoder jwtDecoder,
808825
ThrowingConsumer<List<OAuth2TokenValidator<Jwt>>> validatorsConsumer) {
809826
DelegatingOAuth2TokenValidator<Jwt> jwtValidator = (DelegatingOAuth2TokenValidator<Jwt>) ReflectionTestUtils
810827
.getField(jwtDecoder, "jwtValidator");
828+
assertThat(jwtValidator).isNotNull();
811829
assertThat(jwtValidator.validate(builder.build()).hasErrors()).isFalse();
812830
validatorsConsumer.accept(extractValidators(jwtValidator));
813831
}
@@ -816,6 +834,7 @@ private void validate(Jwt.Builder builder, ReactiveJwtDecoder jwtDecoder,
816834
private List<OAuth2TokenValidator<Jwt>> extractValidators(DelegatingOAuth2TokenValidator<Jwt> delegatingValidator) {
817835
Collection<OAuth2TokenValidator<Jwt>> delegates = (Collection<OAuth2TokenValidator<Jwt>>) ReflectionTestUtils
818836
.getField(delegatingValidator, "tokenValidators");
837+
assertThat(delegates).isNotNull();
819838
List<OAuth2TokenValidator<Jwt>> extracted = new ArrayList<>();
820839
for (OAuth2TokenValidator<Jwt> delegate : delegates) {
821840
if (delegate instanceof DelegatingOAuth2TokenValidator<Jwt> delegatingDelegate) {

module/spring-boot-security-oauth2-resource-server/src/test/java/org/springframework/boot/security/oauth2/server/resource/autoconfigure/servlet/OAuth2ResourceServerAutoConfigurationTests.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import okhttp3.mockwebserver.MockWebServer;
3939
import org.assertj.core.api.InstanceOfAssertFactories;
4040
import org.assertj.core.api.ThrowingConsumer;
41+
import org.jspecify.annotations.Nullable;
4142
import org.junit.jupiter.api.AfterEach;
4243
import org.junit.jupiter.api.Test;
4344
import org.junit.jupiter.params.ParameterizedTest;
@@ -105,7 +106,7 @@ class OAuth2ResourceServerAutoConfigurationTests {
105106
.withConfiguration(AutoConfigurations.of(OAuth2ResourceServerAutoConfiguration.class))
106107
.withUserConfiguration(TestConfig.class);
107108

108-
private MockWebServer server;
109+
private @Nullable MockWebServer server;
109110

110111
private static final String JWK_SET = "{\"keys\":[{\"kty\":\"RSA\",\"e\":\"AQAB\",\"use\":\"sig\","
111112
+ "\"kid\":\"one\",\"n\":\"oXJ8OyOv_eRnce4akdanR4KYRfnC2zLV4uYNQpcFn6oHL0dj7D6kxQmsXoYgJV8ZVDn71KGm"
@@ -228,6 +229,7 @@ void autoConfigurationShouldConfigureResourceServerUsingOidcIssuerUri() throws E
228229
SupplierJwtDecoder supplierJwtDecoderBean = context.getBean(SupplierJwtDecoder.class);
229230
Supplier<JwtDecoder> jwtDecoderSupplier = (Supplier<JwtDecoder>) ReflectionTestUtils
230231
.getField(supplierJwtDecoderBean, "delegate");
232+
assertThat(jwtDecoderSupplier).isNotNull();
231233
jwtDecoderSupplier.get();
232234
assertJwkSetUriJwtDecoderBuilderCustomization(context);
233235
});
@@ -253,6 +255,7 @@ void autoConfigurationShouldConfigureResourceServerUsingOidcRfc8414IssuerUri() t
253255
SupplierJwtDecoder supplierJwtDecoderBean = context.getBean(SupplierJwtDecoder.class);
254256
Supplier<JwtDecoder> jwtDecoderSupplier = (Supplier<JwtDecoder>) ReflectionTestUtils
255257
.getField(supplierJwtDecoderBean, "delegate");
258+
assertThat(jwtDecoderSupplier).isNotNull();
256259
jwtDecoderSupplier.get();
257260
assertJwkSetUriJwtDecoderBuilderCustomization(context);
258261
});
@@ -279,6 +282,7 @@ void autoConfigurationShouldConfigureResourceServerUsingOAuthIssuerUri() throws
279282
SupplierJwtDecoder supplierJwtDecoderBean = context.getBean(SupplierJwtDecoder.class);
280283
Supplier<JwtDecoder> jwtDecoderSupplier = (Supplier<JwtDecoder>) ReflectionTestUtils
281284
.getField(supplierJwtDecoderBean, "delegate");
285+
assertThat(jwtDecoderSupplier).isNotNull();
282286
jwtDecoderSupplier.get();
283287
assertJwkSetUriJwtDecoderBuilderCustomization(context);
284288
});
@@ -558,6 +562,7 @@ void autoConfigurationShouldConfigureAudienceValidatorIfPropertyProvidedAndIssue
558562
SupplierJwtDecoder supplierJwtDecoderBean = context.getBean(SupplierJwtDecoder.class);
559563
Supplier<JwtDecoder> jwtDecoderSupplier = (Supplier<JwtDecoder>) ReflectionTestUtils
560564
.getField(supplierJwtDecoderBean, "delegate");
565+
assertThat(jwtDecoderSupplier).isNotNull();
561566
JwtDecoder jwtDecoder = jwtDecoderSupplier.get();
562567
validate(
563568
jwt().claim("iss", URI.create(issuerUri).toURL())
@@ -584,6 +589,7 @@ void autoConfigurationShouldConfigureCustomValidators() throws Exception {
584589
SupplierJwtDecoder supplierJwtDecoderBean = context.getBean(SupplierJwtDecoder.class);
585590
Supplier<JwtDecoder> jwtDecoderSupplier = (Supplier<JwtDecoder>) ReflectionTestUtils
586591
.getField(supplierJwtDecoderBean, "delegate");
592+
assertThat(jwtDecoderSupplier).isNotNull();
587593
JwtDecoder jwtDecoder = jwtDecoderSupplier.get();
588594
assertThat(context).hasBean("customJwtClaimValidator");
589595
OAuth2TokenValidator<Jwt> customValidator = (OAuth2TokenValidator<Jwt>) context
@@ -633,6 +639,7 @@ void audienceValidatorWhenAudienceInvalid() throws Exception {
633639
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
634640
DelegatingOAuth2TokenValidator<Jwt> jwtValidator = (DelegatingOAuth2TokenValidator<Jwt>) ReflectionTestUtils
635641
.getField(jwtDecoder, "jwtValidator");
642+
assertThat(jwtValidator).isNotNull();
636643
Jwt jwt = jwt().claim("iss", new URL(issuerUri))
637644
.claim("aud", Collections.singletonList("https://other-audience.com"))
638645
.build();
@@ -733,7 +740,7 @@ void causesManagementWebSecurityAutoConfigurationToBackOff() {
733740
.doesNotHaveBean(MANAGEMENT_SECURITY_FILTER_CHAIN_BEAN));
734741
}
735742

736-
private Filter getBearerTokenFilter(AssertableWebApplicationContext context) {
743+
private @Nullable Filter getBearerTokenFilter(AssertableWebApplicationContext context) {
737744
FilterChainProxy filterChain = (FilterChainProxy) context.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN);
738745
List<SecurityFilterChain> filterChains = filterChain.getFilterChains();
739746
List<Filter> filters = filterChains.get(0).getFilters();
@@ -751,12 +758,14 @@ private void setupMockResponse(String issuer) {
751758
MockResponse mockResponse = new MockResponse().setResponseCode(HttpStatus.OK.value())
752759
.setBody(new ObjectMapper().writeValueAsString(getResponse(issuer)))
753760
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
761+
assertThat(this.server).isNotNull();
754762
this.server.enqueue(mockResponse);
755763
this.server.enqueue(
756764
new MockResponse().setResponseCode(200).setHeader("Content-Type", "application/json").setBody(JWK_SET));
757765
}
758766

759767
private void setupMockResponsesWithErrors(String issuer, int errorResponseCount) {
768+
assertThat(this.server).isNotNull();
760769
for (int i = 0; i < errorResponseCount; i++) {
761770
MockResponse emptyResponse = new MockResponse().setResponseCode(HttpStatus.NOT_FOUND.value());
762771
this.server.enqueue(emptyResponse);
@@ -799,6 +808,7 @@ private void validate(Jwt.Builder builder, JwtDecoder jwtDecoder,
799808
ThrowingConsumer<List<OAuth2TokenValidator<Jwt>>> validatorsConsumer) {
800809
DelegatingOAuth2TokenValidator<Jwt> jwtValidator = (DelegatingOAuth2TokenValidator<Jwt>) ReflectionTestUtils
801810
.getField(jwtDecoder, "jwtValidator");
811+
assertThat(jwtValidator).isNotNull();
802812
assertThat(jwtValidator.validate(builder.build()).hasErrors()).isFalse();
803813
validatorsConsumer.accept(extractValidators(jwtValidator));
804814
}
@@ -807,6 +817,7 @@ private void validate(Jwt.Builder builder, JwtDecoder jwtDecoder,
807817
private List<OAuth2TokenValidator<Jwt>> extractValidators(DelegatingOAuth2TokenValidator<Jwt> delegatingValidator) {
808818
Collection<OAuth2TokenValidator<Jwt>> delegates = (Collection<OAuth2TokenValidator<Jwt>>) ReflectionTestUtils
809819
.getField(delegatingValidator, "tokenValidators");
820+
assertThat(delegates).isNotNull();
810821
List<OAuth2TokenValidator<Jwt>> extracted = new ArrayList<>();
811822
for (OAuth2TokenValidator<Jwt> delegate : delegates) {
812823
if (delegate instanceof DelegatingOAuth2TokenValidator<Jwt> delegatingDelegate) {

0 commit comments

Comments
 (0)