Replace DefaultConversionService in spring-reactive
This commit is contained in:
@@ -21,17 +21,11 @@ import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import reactor.io.buffer.Buffer;
|
||||
import reactor.rx.Promise;
|
||||
import reactor.rx.Promises;
|
||||
@@ -40,14 +34,22 @@ import reactor.rx.Streams;
|
||||
import rx.Observable;
|
||||
import rx.Single;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.core.convert.support.ReactiveStreamsToCompletableFutureConverter;
|
||||
import org.springframework.core.convert.support.ReactiveStreamsToReactorConverter;
|
||||
import org.springframework.core.convert.support.ReactiveStreamsToRxJava1Converter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.reactive.codec.encoder.ByteBufferEncoder;
|
||||
import org.springframework.reactive.codec.encoder.JacksonJsonEncoder;
|
||||
import org.springframework.reactive.codec.encoder.JsonObjectEncoder;
|
||||
import org.springframework.reactive.codec.encoder.StringEncoder;
|
||||
import org.springframework.reactive.web.dispatch.DispatcherHandler;
|
||||
import org.springframework.reactive.web.dispatch.SimpleHandlerResultHandler;
|
||||
@@ -59,7 +61,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -68,26 +72,17 @@ import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
*/
|
||||
public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
|
||||
private TestController controller;
|
||||
private AnnotationConfigWebApplicationContext wac;
|
||||
|
||||
|
||||
@Override
|
||||
protected HttpHandler createHttpHandler() {
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
DefaultListableBeanFactory factory = wac.getDefaultListableBeanFactory();
|
||||
wac.registerSingleton("handlerMapping", RequestMappingHandlerMapping.class);
|
||||
wac.registerSingleton("handlerAdapter", RequestMappingHandlerAdapter.class);
|
||||
factory.registerSingleton("responseBodyResultHandler", new ResponseBodyResultHandler(
|
||||
Arrays.asList(new ByteBufferEncoder(), new StringEncoder(),new JacksonJsonEncoder()),
|
||||
Collections.singletonList(new JsonObjectEncoder())));
|
||||
wac.registerSingleton("simpleResultHandler", SimpleHandlerResultHandler.class);
|
||||
this.controller = new TestController();
|
||||
factory.registerSingleton("controller", this.controller);
|
||||
wac.refresh();
|
||||
this.wac = new AnnotationConfigWebApplicationContext();
|
||||
this.wac.register(FrameworkConfig.class, ApplicationConfig.class);
|
||||
this.wac.refresh();
|
||||
|
||||
DispatcherHandler dispatcherHandler = new DispatcherHandler();
|
||||
dispatcherHandler.setApplicationContext(wac);
|
||||
dispatcherHandler.setApplicationContext(this.wac);
|
||||
return dispatcherHandler;
|
||||
}
|
||||
|
||||
@@ -212,11 +207,11 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
|
||||
ResponseEntity<Void> response = restTemplate.exchange(request, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals(2, this.controller.persons.size());
|
||||
assertEquals(2, this.wac.getBean(TestController.class).persons.size());
|
||||
}
|
||||
|
||||
|
||||
public void serializeAsPojo(String requestUrl) throws Exception {
|
||||
private void serializeAsPojo(String requestUrl) throws Exception {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
RequestEntity<Void> request = RequestEntity.get(new URI(requestUrl))
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
@@ -226,17 +221,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
|
||||
assertEquals(new Person("Robert"), response.getBody());
|
||||
}
|
||||
|
||||
public void postAsPojo(String requestUrl) throws Exception {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
RequestEntity<Person> request = RequestEntity.post(new URI(requestUrl))
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.body(new Person("Robert"));
|
||||
ResponseEntity<Person> response = restTemplate.exchange(request, Person.class);
|
||||
|
||||
assertEquals(new Person("Robert"), response.getBody());
|
||||
}
|
||||
|
||||
public void serializeAsCollection(String requestUrl) throws Exception {
|
||||
private void serializeAsCollection(String requestUrl) throws Exception {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
RequestEntity<Void> request = RequestEntity.get(new URI(requestUrl))
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
@@ -250,7 +235,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
|
||||
}
|
||||
|
||||
|
||||
public void capitalizePojo(String requestUrl) throws Exception {
|
||||
private void capitalizePojo(String requestUrl) throws Exception {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
RequestEntity<Person> request = RequestEntity.post(new URI(requestUrl))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
@@ -261,8 +246,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
|
||||
assertEquals(new Person("ROBERT"), response.getBody());
|
||||
}
|
||||
|
||||
|
||||
public void capitalizeCollection(String requestUrl) throws Exception {
|
||||
private void capitalizeCollection(String requestUrl) throws Exception {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
RequestEntity<List<Person>> request = RequestEntity.post(new URI(requestUrl))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
@@ -277,6 +261,57 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@SuppressWarnings("unused")
|
||||
static class FrameworkConfig {
|
||||
|
||||
@Bean
|
||||
public RequestMappingHandlerMapping handlerMapping() {
|
||||
return new RequestMappingHandlerMapping();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RequestMappingHandlerAdapter handlerAdapter() {
|
||||
RequestMappingHandlerAdapter handlerAdapter = new RequestMappingHandlerAdapter();
|
||||
handlerAdapter.setConversionService(conversionService());
|
||||
return handlerAdapter;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConversionService conversionService() {
|
||||
// TODO: test failures with DefaultConversionService
|
||||
GenericConversionService service = new GenericConversionService();
|
||||
service.addConverter(new ReactiveStreamsToCompletableFutureConverter());
|
||||
service.addConverter(new ReactiveStreamsToReactorConverter());
|
||||
service.addConverter(new ReactiveStreamsToRxJava1Converter());
|
||||
return service;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ResponseBodyResultHandler responseBodyResultHandler() {
|
||||
return new ResponseBodyResultHandler(Arrays.asList(
|
||||
new ByteBufferEncoder(), new StringEncoder(),new JacksonJsonEncoder()),
|
||||
conversionService());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleHandlerResultHandler simpleHandlerResultHandler() {
|
||||
return new SimpleHandlerResultHandler();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@SuppressWarnings("unused")
|
||||
static class ApplicationConfig {
|
||||
|
||||
@Bean
|
||||
public TestController testController() {
|
||||
return new TestController();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Controller
|
||||
@SuppressWarnings("unused")
|
||||
private static class TestController {
|
||||
@@ -427,6 +462,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
|
||||
|
||||
private String name;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public Person() {
|
||||
}
|
||||
|
||||
@@ -444,12 +480,19 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return name.equals(((Person)o).name);
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Person person = (Person) o;
|
||||
return !(this.name != null ? !this.name.equals(person.name) : person.name != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
return this.name != null ? this.name.hashCode() : 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,15 +18,18 @@ package org.springframework.reactive.web.dispatch.method.annotation;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.reactive.codec.encoder.StringEncoder;
|
||||
import org.springframework.reactive.web.dispatch.HandlerResult;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@@ -35,17 +38,18 @@ public class ResponseBodyResultHandlerTests {
|
||||
|
||||
@Test
|
||||
public void supports() throws NoSuchMethodException {
|
||||
ResponseBodyResultHandler resultHandler = new ResponseBodyResultHandler(Collections.emptyList());
|
||||
ResponseBodyResultHandler handler = new ResponseBodyResultHandler(Collections.singletonList(
|
||||
new StringEncoder()), new DefaultConversionService());
|
||||
TestController controller = new TestController();
|
||||
|
||||
HandlerMethod hm = new HandlerMethod(controller,TestController.class.getMethod("notAnnotated"));
|
||||
assertFalse(resultHandler.supports(new HandlerResult(hm, null)));
|
||||
assertFalse(handler.supports(new HandlerResult(hm, null)));
|
||||
|
||||
hm = new HandlerMethod(controller, TestController.class.getMethod("publisherString"));
|
||||
assertTrue(resultHandler.supports(new HandlerResult(hm, null)));
|
||||
assertTrue(handler.supports(new HandlerResult(hm, null)));
|
||||
|
||||
hm = new HandlerMethod(controller, TestController.class.getMethod("publisherVoid"));
|
||||
assertTrue(resultHandler.supports(new HandlerResult(hm, null)));
|
||||
assertTrue(handler.supports(new HandlerResult(hm, null)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user