SessionAttributeMethodArgumentResolverTests.java 6.2 KB
Newer Older
1
/*
2
 * Copyright 2002-2017 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 31

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.SynthesizingMethodParameter;
32 33
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.http.server.reactive.ServerHttpRequest;
34 35
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
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 43
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.WebSession;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
R
Rossen Stoyanchev 已提交
44
import org.springframework.web.server.session.MockWebSessionManager;
45 46
import org.springframework.web.server.session.WebSessionManager;

47 48
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
49 50 51

/**
 * Unit tests for {@link SessionAttributeMethodArgumentResolver}.
52
 *
53 54 55 56 57 58 59 60 61 62 63 64 65 66
 * @author Rossen Stoyanchev
 */
public class SessionAttributeMethodArgumentResolverTests {

	private SessionAttributeMethodArgumentResolver resolver;

	private ServerWebExchange exchange;

	private WebSession session;

	private Method handleMethod;


	@Before
67
	public void setup() throws Exception {
68 69
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.refresh();
70
		this.resolver = new SessionAttributeMethodArgumentResolver(context.getBeanFactory());
71 72

		this.session = mock(WebSession.class);
R
Rossen Stoyanchev 已提交
73
		WebSessionManager sessionManager = new MockWebSessionManager(this.session);
74 75

		ServerHttpRequest request = MockServerHttpRequest.get("/").build();
R
Rossen Stoyanchev 已提交
76 77
		this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager);

78 79 80
		this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithSessionAttribute", (Class<?>[]) null);
	}

81

82 83 84 85 86 87 88 89 90
	@Test
	public void supportsParameter() throws Exception {
		assertTrue(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 0)));
		assertFalse(this.resolver.supportsParameter(new MethodParameter(this.handleMethod, 4)));
	}

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

		Foo foo = new Foo();
		when(this.session.getAttribute("foo")).thenReturn(Optional.of(foo));
R
Rossen Stoyanchev 已提交
96
		mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
97
		assertSame(foo, mono.block());
98 99 100 101 102 103 104
	}

	@Test
	public void resolveWithName() throws Exception {
		MethodParameter param = initMethodParameter(1);
		Foo foo = new Foo();
		when(this.session.getAttribute("specialFoo")).thenReturn(Optional.of(foo));
R
Rossen Stoyanchev 已提交
105
		Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
106
		assertSame(foo, mono.block());
107 108 109 110 111
	}

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

		Foo foo = new Foo();
		when(this.session.getAttribute("foo")).thenReturn(Optional.of(foo));
R
Rossen Stoyanchev 已提交
117
		mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
118
		assertSame(foo, mono.block());
119 120 121 122 123
	}

	@Test
	public void resolveOptional() throws Exception {
		MethodParameter param = initMethodParameter(3);
R
Rossen Stoyanchev 已提交
124
		Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
125 126 127
		assertNotNull(mono.block());
		assertEquals(Optional.class, mono.block().getClass());
		assertFalse(((Optional) mono.block()).isPresent());
128

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

133 134
		Foo foo = new Foo();
		when(this.session.getAttribute("foo")).thenReturn(Optional.of(foo));
135
		mono = this.resolver.resolveArgument(param, bindingContext, this.exchange);
136

137 138 139
		assertNotNull(mono.block());
		assertEquals(Optional.class, mono.block().getClass());
		Optional optional = (Optional) mono.block();
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
		assertTrue(optional.isPresent());
		assertSame(foo, optional.get());
	}


	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,
158 159
			@SessionAttribute(name="foo") Optional<Foo> optionalFoo,
			String notSupported) {
160 161 162 163 164 165
	}

	private static class Foo {
	}

}