From 91f5f42d136ccae798e01e71356787d4a12913e6 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Wed, 6 Nov 2019 11:07:59 +0900 Subject: [PATCH 1/2] Add tests for PrincipalMethodArgumentResolver --- .../PrincipalMethodArgumentResolverTests.java | 76 +++++++++++++++++++ .../PrincipalMethodArgumentResolverTests.java | 2 +- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolverTests.java diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolverTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolverTests.java new file mode 100644 index 0000000000..0f9664aa40 --- /dev/null +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolverTests.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.messaging.simp.annotation.support; + +import java.security.Principal; +import java.util.Collections; +import java.util.Optional; + +import org.junit.jupiter.api.Test; + +import org.springframework.core.MethodParameter; +import org.springframework.messaging.Message; +import org.springframework.messaging.handler.invocation.ResolvableMethod; +import org.springframework.messaging.simp.SimpMessageHeaderAccessor; +import org.springframework.messaging.support.GenericMessage; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for {@link PrincipalMethodArgumentResolver}. + * + * @author Rossen Stoyanchev + * @author Johnny Lim + */ +public class PrincipalMethodArgumentResolverTests { + + private final PrincipalMethodArgumentResolver resolver = new PrincipalMethodArgumentResolver(); + + private final ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build(); + + + @Test + public void supportsParameter() { + assertThat(this.resolver.supportsParameter(this.testMethod.arg(Principal.class))).isTrue(); + assertThat(this.resolver.supportsParameter(this.testMethod.arg(Optional.class, Principal.class))).isTrue(); + } + + + @Test + public void resolverArgument() { + Principal user = () -> "Joe"; + Message message = new GenericMessage<>("Hello, world!", + Collections.singletonMap(SimpMessageHeaderAccessor.USER_HEADER, user)); + + MethodParameter param = this.testMethod.arg(Principal.class); + Object actual = this.resolver.resolveArgument(param, message); + assertThat(actual).isSameAs(user); + + param = this.testMethod.arg(Optional.class, Principal.class); + actual = this.resolver.resolveArgument(param, message); + assertThat(Optional.class.isAssignableFrom(actual.getClass())).isTrue(); + assertThat(((Optional) actual).get()).isSameAs(user); + } + + + @SuppressWarnings("unused") + void handle( + Principal user, + Optional optionalUser) { + } + +} diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/PrincipalMethodArgumentResolverTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/PrincipalMethodArgumentResolverTests.java index 4ad26e732e..e12ab79928 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/PrincipalMethodArgumentResolverTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/PrincipalMethodArgumentResolverTests.java @@ -42,7 +42,7 @@ public class PrincipalMethodArgumentResolverTests { private final PrincipalMethodArgumentResolver resolver = new PrincipalMethodArgumentResolver(ReactiveAdapterRegistry.getSharedInstance()); - private ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build(); + private final ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build(); @Test From 0e1a237139e8035860457d129bd93451291e11f5 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 8 Nov 2019 09:17:25 +0000 Subject: [PATCH 2/2] Polishing --- .../support/PrincipalMethodArgumentResolverTests.java | 7 ++----- .../annotation/PrincipalMethodArgumentResolverTests.java | 8 +++----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolverTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolverTests.java index 0f9664aa40..7ccbb7e79a 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolverTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/PrincipalMethodArgumentResolverTests.java @@ -62,15 +62,12 @@ public class PrincipalMethodArgumentResolverTests { param = this.testMethod.arg(Optional.class, Principal.class); actual = this.resolver.resolveArgument(param, message); - assertThat(Optional.class.isAssignableFrom(actual.getClass())).isTrue(); - assertThat(((Optional) actual).get()).isSameAs(user); + assertThat(actual).isInstanceOf(Optional.class).extracting(o -> ((Optional) o).get()).isSameAs(user); } @SuppressWarnings("unused") - void handle( - Principal user, - Optional optionalUser) { + void handle(Principal user, Optional optionalUser) { } } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/PrincipalMethodArgumentResolverTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/PrincipalMethodArgumentResolverTests.java index e12ab79928..32078174ff 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/PrincipalMethodArgumentResolverTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/PrincipalMethodArgumentResolverTests.java @@ -55,24 +55,22 @@ public class PrincipalMethodArgumentResolverTests { @Test public void resolverArgument() { - BindingContext context = new BindingContext(); Principal user = () -> "Joe"; ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")) .mutate().principal(Mono.just(user)).build(); + BindingContext context = new BindingContext(); MethodParameter param = this.testMethod.arg(Principal.class); Object actual = this.resolver.resolveArgument(param, context, exchange).block(); assertThat(actual).isSameAs(user); param = this.testMethod.arg(Mono.class, Principal.class); actual = this.resolver.resolveArgument(param, context, exchange).block(); - assertThat(Mono.class.isAssignableFrom(actual.getClass())).isTrue(); - assertThat(((Mono) actual).block()).isSameAs(user); + assertThat(actual).isInstanceOf(Mono.class).extracting(o -> ((Mono) o).block()).isSameAs(user); param = this.testMethod.arg(Single.class, Principal.class); actual = this.resolver.resolveArgument(param, context, exchange).block(); - assertThat(Single.class.isAssignableFrom(actual.getClass())).isTrue(); - assertThat(((Single) actual).blockingGet()).isSameAs(user); + assertThat(actual).isInstanceOf(Single.class).extracting(o -> ((Single) o).blockingGet()).isSameAs(user); }