From 35f791acf8a89ab017b25ed98596d4366cd76ca0 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 11 Jul 2016 14:41:15 -0400 Subject: [PATCH] Extract base class for RequestMappingIntegrationTests --- ...bstractRequestMappingIntegrationTests.java | 95 +++++++++++++++++++ .../RequestMappingIntegrationTests.java | 71 +++----------- 2 files changed, 108 insertions(+), 58 deletions(-) create mode 100644 spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/AbstractRequestMappingIntegrationTests.java diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/AbstractRequestMappingIntegrationTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/AbstractRequestMappingIntegrationTests.java new file mode 100644 index 0000000000..2beb97217f --- /dev/null +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/AbstractRequestMappingIntegrationTests.java @@ -0,0 +1,95 @@ +/* + * 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.net.URI; + +import org.springframework.context.ApplicationContext; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.MediaType; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.http.server.reactive.AbstractHttpHandlerIntegrationTests; +import org.springframework.http.server.reactive.HttpHandler; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.reactive.DispatcherHandler; +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; + +import static org.springframework.http.RequestEntity.get; + +/** + * + * @author Rossen Stoyanchev + */ +public abstract class AbstractRequestMappingIntegrationTests extends AbstractHttpHandlerIntegrationTests { + + private ApplicationContext applicationContext; + + private RestTemplate restTemplate = new RestTemplate(); + + + @Override + protected HttpHandler createHttpHandler() { + this.applicationContext = initApplicationContext(); + DispatcherHandler handler = new DispatcherHandler(); + handler.setApplicationContext(this.applicationContext); + return WebHttpHandlerBuilder.webHandler(handler).build(); + } + + protected abstract ApplicationContext initApplicationContext(); + + + ApplicationContext getApplicationContext() { + return this.applicationContext; + } + + + ResponseEntity performGet(String url, MediaType out, + Class type) throws Exception { + + return this.restTemplate.exchange(prepareGet(url, out), type); + } + + ResponseEntity performGet(String url, MediaType out, + ParameterizedTypeReference type) throws Exception { + + return this.restTemplate.exchange(prepareGet(url, out), type); + } + + ResponseEntity performPost(String url, MediaType in, Object body, MediaType out, + Class type) throws Exception { + + return this.restTemplate.exchange(preparePost(url, in, body, out), type); + } + + ResponseEntity performPost(String url, MediaType in, Object body, + MediaType out, ParameterizedTypeReference type) throws Exception { + + return this.restTemplate.exchange(preparePost(url, in, body, out), type); + } + + private RequestEntity prepareGet(String url, MediaType accept) throws Exception { + URI uri = new URI("http://localhost:" + this.port + url); + return (accept != null ? get(uri).accept(accept).build() : get(uri).build()); + } + + private RequestEntity preparePost(String url, MediaType in, Object body, MediaType out) throws Exception { + URI uri = new URI("http://localhost:" + this.port + url); + return (out != null ? + RequestEntity.post(uri).contentType(in).accept(out).body(body) : + RequestEntity.post(uri).contentType(in).body(body)); + } +} diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java index 069ece27da..61aa66e608 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java @@ -32,6 +32,7 @@ import reactor.core.publisher.Mono; import rx.Observable; import rx.Single; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -79,7 +80,7 @@ import static org.springframework.http.RequestEntity.get; * @author Sebastien Deleuze * @author Stephane Maldini */ -public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrationTests { +public class RequestMappingIntegrationTests extends AbstractRequestMappingIntegrationTests { private static final ParameterizedTypeReference> PERSON_LIST = new ParameterizedTypeReference>() {}; @@ -87,21 +88,12 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati private static final MediaType JSON = MediaType.APPLICATION_JSON; - private AnnotationConfigApplicationContext wac; - - private RestTemplate restTemplate = new RestTemplate(); - - @Override - protected HttpHandler createHttpHandler() { - this.wac = new AnnotationConfigApplicationContext(); - this.wac.register(FrameworkConfig.class, ApplicationConfig.class); - this.wac.refresh(); - - DispatcherHandler handler = new DispatcherHandler(); - handler.setApplicationContext(this.wac); - - return WebHttpHandlerBuilder.webHandler(handler).build(); + protected ApplicationContext initApplicationContext() { + AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); + wac.register(FrameworkConfig.class, ApplicationConfig.class); + wac.refresh(); + return wac; } @Test @@ -248,7 +240,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati asList(new Person("Robert"), new Person("Marie")), null, Void.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); - assertEquals(2, this.wac.getBean(TestRestController.class).persons.size()); + assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size()); } @Test @@ -257,7 +249,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati ResponseEntity response = performPost("/publisher-create", APPLICATION_XML, people, null, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(2, this.wac.getBean(TestRestController.class).persons.size()); + assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size()); } @Test @@ -266,7 +258,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati asList(new Person("Robert"), new Person("Marie")), null, Void.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); - assertEquals(2, this.wac.getBean(TestRestController.class).persons.size()); + assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size()); } @Test @@ -275,7 +267,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati ResponseEntity response = performPost("/flux-create", APPLICATION_XML, people, null, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(2, this.wac.getBean(TestRestController.class).persons.size()); + assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size()); } @Test @@ -284,7 +276,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati asList(new Person("Robert"), new Person("Marie")), null, Void.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); - assertEquals(2, this.wac.getBean(TestRestController.class).persons.size()); + assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size()); } @Test @@ -293,7 +285,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati ResponseEntity response = performPost("/observable-create", APPLICATION_XML, people, null, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(2, this.wac.getBean(TestRestController.class).persons.size()); + assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size()); } @Test @@ -321,43 +313,6 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati } - private ResponseEntity performGet(String url, MediaType acceptHeader, - Class type) throws Exception { - - return this.restTemplate.exchange(prepareGet(url, acceptHeader), type); - } - - private ResponseEntity performGet(String url, MediaType acceptHeader, - ParameterizedTypeReference type) throws Exception { - - return this.restTemplate.exchange(prepareGet(url, acceptHeader), type); - } - - private ResponseEntity performPost(String url, MediaType in, Object body, - MediaType out, Class type) throws Exception { - - return this.restTemplate.exchange(preparePost(url, in, body, out), type); - } - - private ResponseEntity performPost(String url, MediaType in, Object body, - MediaType out, ParameterizedTypeReference type) throws Exception { - - return this.restTemplate.exchange(preparePost(url, in, body, out), type); - } - - private RequestEntity prepareGet(String url, MediaType accept) throws Exception { - URI uri = new URI("http://localhost:" + this.port + url); - return (accept != null ? get(uri).accept(accept).build() : get(uri).build()); - } - - private RequestEntity preparePost(String url, MediaType in, Object body, MediaType out) throws Exception { - URI uri = new URI("http://localhost:" + this.port + url); - return (out != null ? - RequestEntity.post(uri).contentType(in).accept(out).body(body) : - RequestEntity.post(uri).contentType(in).body(body)); - } - - @Configuration @SuppressWarnings({"unused", "WeakerAccess"}) static class FrameworkConfig extends WebReactiveConfiguration {