Browse Source

Remove unnecessary lambda blocks

Remove lambda blocks that aren't needed and replace instead with a
simple expression.

Issue gh-8945
pull/8983/head
Phillip Webb 6 years ago committed by Rob Winch
parent
commit
612fb22a7f
  1. 11
      config/src/integration-test/java/org/springframework/security/config/annotation/rsocket/RSocketMessageHandlerITests.java
  2. 8
      config/src/test/java/org/springframework/security/config/web/server/HeaderSpecTests.java
  3. 4
      config/src/test/java/org/springframework/security/htmlunit/server/HtmlUnitWebTestClient.java
  4. 4
      core/src/test/java/org/springframework/security/jackson2/SecurityJackson2ModulesTests.java
  5. 3
      etc/checkstyle/checkstyle-suppressions.xml
  6. 18
      oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultReactiveOAuth2AuthorizedClientManager.java
  7. 24
      oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.java
  8. 16
      oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java
  9. 9
      oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java
  10. 10
      oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/HeaderBearerTokenResolverTests.java
  11. 5
      rsocket/src/test/java/org/springframework/security/rsocket/core/PayloadInterceptorRSocketTests.java
  12. 4
      web/src/main/java/org/springframework/security/web/server/header/StaticServerHttpHeadersWriter.java
  13. 5
      web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java

11
config/src/integration-test/java/org/springframework/security/config/annotation/rsocket/RSocketMessageHandlerITests.java

@ -221,9 +221,9 @@ public class RSocketMessageHandlerITests { @@ -221,9 +221,9 @@ public class RSocketMessageHandlerITests {
@Bean
PayloadSocketAcceptorInterceptor rsocketInterceptor(RSocketSecurity rsocket) {
rsocket.authorizePayload((authorize) -> {
authorize.route("secure.*").authenticated().anyExchange().permitAll();
}).basicAuthentication(Customizer.withDefaults());
rsocket.authorizePayload(
(authorize) -> authorize.route("secure.*").authenticated().anyExchange().permitAll())
.basicAuthentication(Customizer.withDefaults());
return rsocket.build();
}
@ -247,9 +247,8 @@ public class RSocketMessageHandlerITests { @@ -247,9 +247,8 @@ public class RSocketMessageHandlerITests {
@MessageMapping({ "secure.send", "send" })
Mono<Void> send(Mono<String> payload) {
return payload.doOnNext(this::add).then(Mono.fromRunnable(() -> {
doNotifyAll();
}));
return payload.doOnNext(this::add).then(Mono.fromRunnable(() -> doNotifyAll()));
}
private synchronized void doNotifyAll() {

8
config/src/test/java/org/springframework/security/config/web/server/HeaderSpecTests.java

@ -336,12 +336,10 @@ public class HeaderSpecTests { @@ -336,12 +336,10 @@ public class HeaderSpecTests {
@Test
public void headersWhenCustomHeadersWriter() {
this.expectedHeaders.add(CUSTOM_HEADER, CUSTOM_VALUE);
this.http.headers((headers) -> headers.writer((exchange) -> {
return Mono.just(exchange).doOnNext((it) -> {
it.getResponse().getHeaders().add(CUSTOM_HEADER, CUSTOM_VALUE);
}).then();
this.http.headers((headers) -> headers.writer((exchange) -> Mono.just(exchange)
.doOnNext((it) -> it.getResponse().getHeaders().add(CUSTOM_HEADER, CUSTOM_VALUE)).then()
}));
));
assertHeaders();
}

4
config/src/test/java/org/springframework/security/htmlunit/server/HtmlUnitWebTestClient.java

@ -186,9 +186,7 @@ final class HtmlUnitWebTestClient { @@ -186,9 +186,7 @@ final class HtmlUnitWebTestClient {
}
private ClientRequest withClientCookies(ClientRequest request) {
return ClientRequest.from(request).cookies((c) -> {
c.addAll(clientCookies());
}).build();
return ClientRequest.from(request).cookies((c) -> c.addAll(clientCookies())).build();
}
private MultiValueMap<String, String> clientCookies() {

4
core/src/test/java/org/springframework/security/jackson2/SecurityJackson2ModulesTests.java

@ -51,9 +51,7 @@ public class SecurityJackson2ModulesTests { @@ -51,9 +51,7 @@ public class SecurityJackson2ModulesTests {
@Test
public void readValueWhenNotAllowedOrMappedThenThrowsException() {
String content = "{\"@class\":\"org.springframework.security.jackson2.SecurityJackson2ModulesTests$NotAllowlisted\",\"property\":\"bar\"}";
assertThatThrownBy(() -> {
this.mapper.readValue(content, Object.class);
}).hasStackTraceContaining("allowlist");
assertThatThrownBy(() -> this.mapper.readValue(content, Object.class)).hasStackTraceContaining("allowlist");
}
@Test

3
etc/checkstyle/checkstyle-suppressions.xml

@ -3,8 +3,6 @@ @@ -3,8 +3,6 @@
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<suppress files=".*" checks="SpringJavadoc" />
<suppress files=".*" checks="SpringLambda" />
<suppress files=".*" checks="SpringMethodOrder" />
<suppress files=".*" checks="SpringMethodVisibility" />
<suppress files=".*" checks="SpringTernary" />
@ -16,6 +14,7 @@ @@ -16,6 +14,7 @@
<suppress files=".*" checks="JavadocType" />
<suppress files=".*" checks="JavadocVariable" />
<suppress files=".*" checks="NonEmptyAtclauseDescription" />
<suppress files=".*" checks="SpringJavadoc" />
<!-- Ignore third-party code -->
<suppress files="BCrypt\.java|BCryptTests\.java" checks=".*"/>

18
oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultReactiveOAuth2AuthorizedClientManager.java

@ -146,16 +146,14 @@ public final class DefaultReactiveOAuth2AuthorizedClientManager implements React @@ -146,16 +146,14 @@ public final class DefaultReactiveOAuth2AuthorizedClientManager implements React
.flatMap((serverWebExchange) -> Mono.justOrEmpty(authorizeRequest.getAuthorizedClient())
.switchIfEmpty(Mono
.defer(() -> loadAuthorizedClient(clientRegistrationId, principal, serverWebExchange)))
.flatMap((authorizedClient) -> {
// Re-authorize
return authorizationContext(authorizeRequest, authorizedClient)
.flatMap((authorizationContext) -> authorize(authorizationContext, principal,
serverWebExchange))
// Default to the existing authorizedClient if the
// client was not re-authorized
.defaultIfEmpty(authorizeRequest.getAuthorizedClient() != null
? authorizeRequest.getAuthorizedClient() : authorizedClient);
}).switchIfEmpty(Mono.defer(() ->
.flatMap((authorizedClient) -> // Re-authorize
authorizationContext(authorizeRequest, authorizedClient).flatMap(
(authorizationContext) -> authorize(authorizationContext, principal, serverWebExchange))
// Default to the existing authorizedClient if the
// client was not re-authorized
.defaultIfEmpty(authorizeRequest.getAuthorizedClient() != null
? authorizeRequest.getAuthorizedClient() : authorizedClient))
.switchIfEmpty(Mono.defer(() ->
// Authorize
this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId)
.switchIfEmpty(Mono.error(() -> new IllegalArgumentException(

24
oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.java

@ -565,20 +565,16 @@ public final class ServerOAuth2AuthorizedClientExchangeFilterFunction implements @@ -565,20 +565,16 @@ public final class ServerOAuth2AuthorizedClientExchangeFilterFunction implements
String clientRegistrationId = authorizeRequest.getClientRegistrationId();
Authentication principal = authorizeRequest.getPrincipal();
return Mono.justOrEmpty(authorizeRequest.getAuthorizedClient())
.switchIfEmpty(Mono.defer(() -> this.authorizedClientRepository
.loadAuthorizedClient(clientRegistrationId, principal, null)))
.flatMap((authorizedClient) -> {
// Re-authorize
return Mono
.just(OAuth2AuthorizationContext.withAuthorizedClient(authorizedClient)
.principal(principal).build())
.flatMap((authorizationContext) -> authorize(authorizationContext, principal))
// Default to the existing authorizedClient if the client
// was not re-authorized
.defaultIfEmpty(authorizeRequest.getAuthorizedClient() != null
? authorizeRequest.getAuthorizedClient() : authorizedClient);
}).switchIfEmpty(Mono.defer(() ->
return Mono.justOrEmpty(authorizeRequest.getAuthorizedClient()).switchIfEmpty(Mono.defer(
() -> this.authorizedClientRepository.loadAuthorizedClient(clientRegistrationId, principal, null)))
.flatMap((authorizedClient) -> // Re-authorize
Mono.just(OAuth2AuthorizationContext.withAuthorizedClient(authorizedClient).principal(principal)
.build()).flatMap((authorizationContext) -> authorize(authorizationContext, principal))
// Default to the existing authorizedClient if the client
// was not re-authorized
.defaultIfEmpty(authorizeRequest.getAuthorizedClient() != null
? authorizeRequest.getAuthorizedClient() : authorizedClient))
.switchIfEmpty(Mono.defer(() ->
// Authorize
this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId)
.switchIfEmpty(Mono.error(() -> new IllegalArgumentException(

16
oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java

@ -85,10 +85,10 @@ public class OAuth2AuthorizationCodeAuthenticationProviderTests { @@ -85,10 +85,10 @@ public class OAuth2AuthorizationCodeAuthenticationProviderTests {
OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange(this.authorizationRequest,
authorizationResponse);
assertThatThrownBy(() -> {
this.authenticationProvider.authenticate(
new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, authorizationExchange));
}).isInstanceOf(OAuth2AuthorizationException.class).hasMessageContaining(OAuth2ErrorCodes.INVALID_REQUEST);
assertThatThrownBy(() -> this.authenticationProvider.authenticate(
new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, authorizationExchange)))
.isInstanceOf(OAuth2AuthorizationException.class)
.hasMessageContaining(OAuth2ErrorCodes.INVALID_REQUEST);
}
@Test
@ -98,10 +98,10 @@ public class OAuth2AuthorizationCodeAuthenticationProviderTests { @@ -98,10 +98,10 @@ public class OAuth2AuthorizationCodeAuthenticationProviderTests {
OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange(this.authorizationRequest,
authorizationResponse);
assertThatThrownBy(() -> {
this.authenticationProvider.authenticate(
new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, authorizationExchange));
}).isInstanceOf(OAuth2AuthorizationException.class).hasMessageContaining("invalid_state_parameter");
assertThatThrownBy(() -> this.authenticationProvider.authenticate(
new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, authorizationExchange)))
.isInstanceOf(OAuth2AuthorizationException.class)
.hasMessageContaining("invalid_state_parameter");
}
@Test

9
oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java

@ -27,7 +27,7 @@ import org.junit.Before; @@ -27,7 +27,7 @@ import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assertions.assertThatObject;
/**
* Tests for {@link ClaimAccessor}.
@ -142,12 +142,7 @@ public class ClaimAccessorTests { @@ -142,12 +142,7 @@ public class ClaimAccessorTests {
String expectedClaimValue = "true";
String claimName = "boolean";
this.claims.put(claimName, expectedClaimValue);
Throwable thrown = catchThrowable(() -> {
boolean actualClaimValue = this.claimAccessor.getClaim(claimName);
});
assertThat(thrown).isInstanceOf(ClassCastException.class);
assertThatObject(this.claimAccessor.getClaim(claimName)).isNotInstanceOf(Boolean.class);
}
}

10
oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/HeaderBearerTokenResolverTests.java

@ -38,16 +38,14 @@ public class HeaderBearerTokenResolverTests { @@ -38,16 +38,14 @@ public class HeaderBearerTokenResolverTests {
@Test
public void constructorWhenHeaderNullThenThrowIllegalArgumentException() {
assertThatCode(() -> {
new HeaderBearerTokenResolver(null);
}).isInstanceOf(IllegalArgumentException.class).hasMessage("header cannot be empty");
assertThatCode(() -> new HeaderBearerTokenResolver(null)).isInstanceOf(IllegalArgumentException.class)
.hasMessage("header cannot be empty");
}
@Test
public void constructorWhenHeaderEmptyThenThrowIllegalArgumentException() {
assertThatCode(() -> {
new HeaderBearerTokenResolver("");
}).isInstanceOf(IllegalArgumentException.class).hasMessage("header cannot be empty");
assertThatCode(() -> new HeaderBearerTokenResolver("")).isInstanceOf(IllegalArgumentException.class)
.hasMessage("header cannot be empty");
}
@Test

5
rsocket/src/test/java/org/springframework/security/rsocket/core/PayloadInterceptorRSocketTests.java

@ -94,9 +94,8 @@ public class PayloadInterceptorRSocketTests { @@ -94,9 +94,8 @@ public class PayloadInterceptorRSocketTests {
public void constructorWhenNullDelegateThenException() {
this.delegate = null;
List<PayloadInterceptor> interceptors = Arrays.asList(this.interceptor);
assertThatCode(() -> {
new PayloadInterceptorRSocket(this.delegate, interceptors, this.metadataMimeType, this.dataMimeType);
}).isInstanceOf(IllegalArgumentException.class);
assertThatCode(() -> new PayloadInterceptorRSocket(this.delegate, interceptors, this.metadataMimeType,
this.dataMimeType)).isInstanceOf(IllegalArgumentException.class);
}
@Test

4
web/src/main/java/org/springframework/security/web/server/header/StaticServerHttpHeadersWriter.java

@ -43,9 +43,7 @@ public class StaticServerHttpHeadersWriter implements ServerHttpHeadersWriter { @@ -43,9 +43,7 @@ public class StaticServerHttpHeadersWriter implements ServerHttpHeadersWriter {
HttpHeaders headers = exchange.getResponse().getHeaders();
boolean containsOneHeaderToAdd = Collections.disjoint(headers.keySet(), this.headersToAdd.keySet());
if (containsOneHeaderToAdd) {
this.headersToAdd.forEach((name, values) -> {
headers.put(name, values);
});
this.headersToAdd.forEach(headers::put);
}
return Mono.empty();
}

5
web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java

@ -111,9 +111,8 @@ public class CurrentSecurityContextArgumentResolverTests { @@ -111,9 +111,8 @@ public class CurrentSecurityContextArgumentResolverTests {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
context.setAuthentication(null);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> {
this.resolver.resolveArgument(showSecurityContextAuthenticationWithPrincipal(), null, null, null);
});
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> this.resolver
.resolveArgument(showSecurityContextAuthenticationWithPrincipal(), null, null, null));
context.setAuthentication(authentication);
}

Loading…
Cancel
Save