Extract base class for RequestMappingIntegrationTests
This commit is contained in:
parent
6fde86903d
commit
35f791acf8
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
<T> ResponseEntity<T> performGet(String url, MediaType out,
|
||||
Class<T> type) throws Exception {
|
||||
|
||||
return this.restTemplate.exchange(prepareGet(url, out), type);
|
||||
}
|
||||
|
||||
<T> ResponseEntity<T> performGet(String url, MediaType out,
|
||||
ParameterizedTypeReference<T> type) throws Exception {
|
||||
|
||||
return this.restTemplate.exchange(prepareGet(url, out), type);
|
||||
}
|
||||
|
||||
<T> ResponseEntity<T> performPost(String url, MediaType in, Object body, MediaType out,
|
||||
Class<T> type) throws Exception {
|
||||
|
||||
return this.restTemplate.exchange(preparePost(url, in, body, out), type);
|
||||
}
|
||||
|
||||
<T> ResponseEntity<T> performPost(String url, MediaType in, Object body,
|
||||
MediaType out, ParameterizedTypeReference<T> type) throws Exception {
|
||||
|
||||
return this.restTemplate.exchange(preparePost(url, in, body, out), type);
|
||||
}
|
||||
|
||||
private RequestEntity<Void> 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));
|
||||
}
|
||||
}
|
|
@ -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<List<Person>> PERSON_LIST =
|
||||
new ParameterizedTypeReference<List<Person>>() {};
|
||||
|
@ -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<Void> 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<Void> 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<Void> 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 <T> ResponseEntity<T> performGet(String url, MediaType acceptHeader,
|
||||
Class<T> type) throws Exception {
|
||||
|
||||
return this.restTemplate.exchange(prepareGet(url, acceptHeader), type);
|
||||
}
|
||||
|
||||
private <T> ResponseEntity<T> performGet(String url, MediaType acceptHeader,
|
||||
ParameterizedTypeReference<T> type) throws Exception {
|
||||
|
||||
return this.restTemplate.exchange(prepareGet(url, acceptHeader), type);
|
||||
}
|
||||
|
||||
private <T> ResponseEntity<T> performPost(String url, MediaType in, Object body,
|
||||
MediaType out, Class<T> type) throws Exception {
|
||||
|
||||
return this.restTemplate.exchange(preparePost(url, in, body, out), type);
|
||||
}
|
||||
|
||||
private <T> ResponseEntity<T> performPost(String url, MediaType in, Object body,
|
||||
MediaType out, ParameterizedTypeReference<T> type) throws Exception {
|
||||
|
||||
return this.restTemplate.exchange(preparePost(url, in, body, out), type);
|
||||
}
|
||||
|
||||
private RequestEntity<Void> 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 {
|
||||
|
|
Loading…
Reference in New Issue