SessionAttributeMethodArgumentResolverTests.java 5.8 KB
Newer Older
1
/*
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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
 *
 *      http://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.
 */
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 44 45

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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

	private SessionAttributeMethodArgumentResolver resolver;

	private ServerWebExchange exchange;

	private WebSession session;

	private Method handleMethod;


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

74

75
	@Test
76
	public void supportsParameter() {
77 78 79 80 81
		assertTrue(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 0)));
		assertFalse(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 4)));
	}

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

		Foo foo = new Foo();
88
		when(this.session.getAttribute("foo")).thenReturn(foo);
R
Rossen Stoyanchev 已提交
89
		mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
90
		assertSame(foo, mono.block());
91 92 93
	}

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

	@Test
103
	public void resolveNotRequired() {
104
		MethodParameter param = initMethodParameter(2);
R
Rossen Stoyanchev 已提交
105
		Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
106
		assertNull(mono.block());
107 108

		Foo foo = new Foo();
109
		when(this.session.getAttribute("foo")).thenReturn(foo);
R
Rossen Stoyanchev 已提交
110
		mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
111
		assertSame(foo, mono.block());
112 113
	}

114
	@SuppressWarnings("unchecked")
115
	@Test
116
	public void resolveOptional() {
117
		MethodParameter param = initMethodParameter(3);
118 119 120 121 122
		Optional<Object> actual = (Optional<Object>) this.resolver
				.resolveArgument(param, new BindingContext(), this.exchange).block();

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

124 125 126 127
		ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
		initializer.setConversionService(new DefaultFormattingConversionService());
		BindingContext bindingContext = new BindingContext(initializer);

128
		Foo foo = new Foo();
129 130 131 132 133 134
		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());
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	}


	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,
151 152
			@SessionAttribute(name="foo") Optional<Foo> optionalFoo,
			String notSupported) {
153 154 155 156 157 158
	}

	private static class Foo {
	}

}