SessionAttributeMethodArgumentResolverTests.java 6.0 KB
Newer Older
1
/*
P
Phillip Webb 已提交
2
 * Copyright 2002-2019 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
S
Spring Operator 已提交
8
 *      https://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15
 *
 * 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.
 */
J
Juergen Hoeller 已提交
16

17 18 19 20 21 22 23 24
package org.springframework.web.reactive.result.method.annotation;

import java.lang.reflect.Method;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Mono;
25
import reactor.test.StepVerifier;
26 27 28 29 30

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.MethodParameter;
31
import org.springframework.core.ReactiveAdapterRegistry;
32
import org.springframework.core.annotation.SynthesizingMethodParameter;
33
import org.springframework.format.support.DefaultFormattingConversionService;
34
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
35
import org.springframework.mock.web.test.server.MockServerWebExchange;
36 37
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.SessionAttribute;
38
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
39
import org.springframework.web.reactive.BindingContext;
40 41 42
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.WebSession;
43

P
Phillip Webb 已提交
44 45 46 47 48 49 50
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

/**
 * Unit tests for {@link SessionAttributeMethodArgumentResolver}.
 * @author Rossen Stoyanchev
 */
public class SessionAttributeMethodArgumentResolverTests {

	private SessionAttributeMethodArgumentResolver resolver;

	private ServerWebExchange exchange;

	private WebSession session;

	private Method handleMethod;


	@Before
68
	@SuppressWarnings("resource")
69
	public void setup() {
70 71
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.refresh();
72
		ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
73
		this.resolver = new SessionAttributeMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
74
		this.session = mock(WebSession.class);
75
		this.exchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/")).session(this.session).build();
76 77 78
		this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithSessionAttribute", (Class<?>[]) null);
	}

79

80
	@Test
81
	public void supportsParameter() {
82 83 84 85 86
		assertTrue(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 0)));
		assertFalse(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 4)));
	}

	@Test
87
	public void resolve() {
88
		MethodParameter param = initMethodParameter(0);
R
Rossen Stoyanchev 已提交
89
		Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
90
		StepVerifier.create(mono).expectError(ServerWebInputException.class).verify();
91 92

		Foo foo = new Foo();
93
		when(this.session.getAttribute("foo")).thenReturn(foo);
R
Rossen Stoyanchev 已提交
94
		mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
95
		assertSame(foo, mono.block());
96 97 98
	}

	@Test
99
	public void resolveWithName() {
100 101
		MethodParameter param = initMethodParameter(1);
		Foo foo = new Foo();
102
		when(this.session.getAttribute("specialFoo")).thenReturn(foo);
R
Rossen Stoyanchev 已提交
103
		Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
104
		assertSame(foo, mono.block());
105 106 107
	}

	@Test
108
	public void resolveNotRequired() {
109
		MethodParameter param = initMethodParameter(2);
R
Rossen Stoyanchev 已提交
110
		Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
111
		assertNull(mono.block());
112 113

		Foo foo = new Foo();
114
		when(this.session.getAttribute("foo")).thenReturn(foo);
R
Rossen Stoyanchev 已提交
115
		mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
116
		assertSame(foo, mono.block());
117 118
	}

119
	@SuppressWarnings("unchecked")
120
	@Test
121
	public void resolveOptional() {
122
		MethodParameter param = initMethodParameter(3);
123 124 125 126 127
		Optional<Object> actual = (Optional<Object>) this.resolver
				.resolveArgument(param, new BindingContext(), this.exchange).block();

		assertNotNull(actual);
		assertFalse(actual.isPresent());
128

129 130 131 132
		ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
		initializer.setConversionService(new DefaultFormattingConversionService());
		BindingContext bindingContext = new BindingContext(initializer);

133
		Foo foo = new Foo();
134 135 136 137 138 139
		when(this.session.getAttribute("foo")).thenReturn(foo);
		actual = (Optional<Object>) this.resolver.resolveArgument(param, bindingContext, this.exchange).block();

		assertNotNull(actual);
		assertTrue(actual.isPresent());
		assertSame(foo, actual.get());
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	}


	private MethodParameter initMethodParameter(int parameterIndex) {
		MethodParameter param = new SynthesizingMethodParameter(this.handleMethod, parameterIndex);
		param.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
		GenericTypeResolver.resolveParameterType(param, this.resolver.getClass());
		return param;
	}


	@SuppressWarnings({"unused", "OptionalUsedAsFieldOrParameterType"})
	private void handleWithSessionAttribute(
			@SessionAttribute Foo foo,
			@SessionAttribute("specialFoo") Foo namedFoo,
			@SessionAttribute(name="foo", required = false) Foo notRequiredFoo,
156 157
			@SessionAttribute(name="foo") Optional<Foo> optionalFoo,
			String notSupported) {
158 159 160 161 162 163
	}

	private static class Foo {
	}

}