PathVariableMapMethodArgumentResolverTests.java 3.8 KB
Newer Older
1
/*
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 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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.web.reactive.result.method.annotation;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Mono;

import org.springframework.core.MethodParameter;
29
import org.springframework.core.ReactiveAdapterRegistry;
30
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
31
import org.springframework.mock.web.test.server.MockServerWebExchange;
32
import org.springframework.util.ReflectionUtils;
33
import org.springframework.web.bind.annotation.PathVariable;
34
import org.springframework.web.reactive.BindingContext;
35 36
import org.springframework.web.reactive.HandlerMapping;

37
import static org.junit.Assert.*;
38 39 40 41 42 43 44 45 46 47

/**
 * Unit tests for {@link PathVariableMapMethodArgumentResolver}.
 *
 * @author Rossen Stoyanchev
 */
public class PathVariableMapMethodArgumentResolverTests {

	private PathVariableMapMethodArgumentResolver resolver;

48
	private final MockServerWebExchange exchange= MockServerWebExchange.from(MockServerHttpRequest.get("/"));
49 50 51 52

	private MethodParameter paramMap;
	private MethodParameter paramNamedMap;
	private MethodParameter paramMapNoAnnot;
53
	private MethodParameter paramMonoMap;
54 55 56


	@Before
57
	public void setup() throws Exception {
58
		this.resolver = new PathVariableMapMethodArgumentResolver(ReactiveAdapterRegistry.getSharedInstance());
59

60
		Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
61 62 63
		this.paramMap = new MethodParameter(method, 0);
		this.paramNamedMap = new MethodParameter(method, 1);
		this.paramMapNoAnnot = new MethodParameter(method, 2);
64
		this.paramMonoMap = new MethodParameter(method, 3);
65 66
	}

67

68 69 70 71 72
	@Test
	public void supportsParameter() {
		assertTrue(resolver.supportsParameter(paramMap));
		assertFalse(resolver.supportsParameter(paramNamedMap));
		assertFalse(resolver.supportsParameter(paramMapNoAnnot));
73 74 75 76 77 78 79
		try {
			this.resolver.supportsParameter(this.paramMonoMap);
			fail();
		}
		catch (IllegalStateException ex) {
			assertTrue("Unexpected error message:\n" + ex.getMessage(),
					ex.getMessage().startsWith(
80
							"PathVariableMapMethodArgumentResolver does not support reactive type wrapper"));
81
		}
82 83 84 85 86 87 88 89 90
	}

	@Test
	public void resolveArgument() throws Exception {
		Map<String, String> uriTemplateVars = new HashMap<>();
		uriTemplateVars.put("name1", "value1");
		uriTemplateVars.put("name2", "value2");
		this.exchange.getAttributes().put(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);

R
Rossen Stoyanchev 已提交
91
		Mono<Object> mono = this.resolver.resolveArgument(this.paramMap, new BindingContext(), this.exchange);
92
		Object result = mono.block();
93 94 95 96 97 98

		assertEquals(uriTemplateVars, result);
	}

	@Test
	public void resolveArgumentNoUriVars() throws Exception {
R
Rossen Stoyanchev 已提交
99
		Mono<Object> mono = this.resolver.resolveArgument(this.paramMap, new BindingContext(), this.exchange);
100
		Object result = mono.block();
101 102 103 104 105 106 107 108 109

		assertEquals(Collections.emptyMap(), result);
	}


	@SuppressWarnings("unused")
	public void handle(
			@PathVariable Map<String, String> map,
			@PathVariable(value = "name") Map<String, String> namedMap,
110 111
			Map<String, String> mapWithoutAnnotat,
			@PathVariable Mono<Map<?, ?>> monoMap) {
112 113
	}

114
}