Add tests for PrincipalMethodArgumentResolver

This commit is contained in:
Johnny Lim 2019-11-06 11:07:59 +09:00 committed by Rossen Stoyanchev
parent c7b9988033
commit 91f5f42d13
2 changed files with 77 additions and 1 deletions

View File

@ -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<String> 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<Principal> optionalUser) {
}
}

View File

@ -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