RequestBodyArgumentResolverTests.java 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2002-2016 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
 *
 *      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.
 */
package org.springframework.web.reactive.result.method.annotation;

import java.util.ArrayList;
import java.util.List;
20

21 22 23 24
import org.junit.Test;
import reactor.core.publisher.Mono;

import org.springframework.core.MethodParameter;
R
Rossen Stoyanchev 已提交
25
import org.springframework.core.ResolvableType;
26
import org.springframework.core.codec.StringDecoder;
27 28
import org.springframework.core.convert.support.MonoToCompletableFutureConverter;
import org.springframework.core.convert.support.ReactorToRxJava1Converter;
29 30
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
31 32 33
import org.springframework.http.converter.reactive.CodecHttpMessageConverter;
import org.springframework.http.converter.reactive.HttpMessageConverter;
import org.springframework.web.bind.annotation.RequestBody;
R
Rossen Stoyanchev 已提交
34
import org.springframework.web.reactive.result.ResolvableMethod;
35 36 37

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
R
Rossen Stoyanchev 已提交
38
import static org.springframework.core.ResolvableType.forClassWithGenerics;
39 40

/**
41 42 43 44
 * Unit tests for {@link RequestBodyArgumentResolver}.When adding a test also
 * consider whether the logic under test is in a parent class, then see:
 * {@link MessageConverterArgumentResolverTests}.
 *
45 46 47 48 49 50 51 52
 * @author Rossen Stoyanchev
 */
public class RequestBodyArgumentResolverTests {


	@Test
	public void supports() throws Exception {

53 54 55 56 57
		ResolvableMethod testMethod = ResolvableMethod.on(getClass()).name("handle");
		RequestBodyArgumentResolver resolver = resolver();

		ResolvableType type = forClassWithGenerics(Mono.class, String.class);
		MethodParameter param = testMethod.resolveParam(type);
R
Rossen Stoyanchev 已提交
58 59
		assertTrue(resolver.supportsParameter(param));

60
		MethodParameter parameter = testMethod.resolveParam(p -> !p.hasParameterAnnotations());
R
Rossen Stoyanchev 已提交
61
		assertFalse(resolver.supportsParameter(parameter));
62 63
	}

64
	private RequestBodyArgumentResolver resolver() {
65
		List<HttpMessageConverter<?>> converters = new ArrayList<>();
66
		converters.add(new CodecHttpMessageConverter<>(new StringDecoder()));
67 68

		FormattingConversionService service = new DefaultFormattingConversionService();
69 70
		service.addConverter(new MonoToCompletableFutureConverter());
		service.addConverter(new ReactorToRxJava1Converter());
71

72
		return new RequestBodyArgumentResolver(converters, service);
73 74 75 76
	}


	@SuppressWarnings("unused")
77
	void handle(@RequestBody Mono<String> monoString, String paramWithoutAnnotation) {}
R
Rossen Stoyanchev 已提交
78

79
}