Split TestRestController into use-case sub-classes
This commit is contained in:
parent
7e07fb16d8
commit
aa47616be2
|
|
@ -50,6 +50,7 @@ import org.springframework.http.server.reactive.ZeroCopyIntegrationTests;
|
|||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.config.WebReactiveConfiguration;
|
||||
|
||||
|
|
@ -84,75 +85,75 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
|
|||
|
||||
|
||||
@Test
|
||||
public void rawPojoResponse() throws Exception {
|
||||
public void byteBufferResponseBodyWithPublisher() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/raw", JSON, Person.class).getBody());
|
||||
assertEquals(expected, performGet("/raw-response/publisher", JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rawFluxResponse() throws Exception {
|
||||
public void byteBufferResponseBodyWithFlux() throws Exception {
|
||||
String expected = "Hello!";
|
||||
assertEquals(expected, performGet("/raw-flux", null, String.class).getBody());
|
||||
assertEquals(expected, performGet("/raw-response/flux", null, String.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rawObservableResponse() throws Exception {
|
||||
public void byteBufferResponseBodyWithObservable() throws Exception {
|
||||
String expected = "Hello!";
|
||||
assertEquals(expected, performGet("/raw-observable", null, String.class).getBody());
|
||||
assertEquals(expected, performGet("/raw-response/observable", null, String.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAsPojo() throws Exception {
|
||||
public void personResponseBody() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/person", JSON, Person.class).getBody());
|
||||
assertEquals(expected, performGet("/person-response/person", JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAsCompletableFuture() throws Exception {
|
||||
public void personResponseBodyWithCompletableFuture() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/completable-future", JSON, Person.class).getBody());
|
||||
assertEquals(expected, performGet("/person-response/completable-future", JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAsMono() throws Exception {
|
||||
public void personResponseBodyWithMono() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/mono", JSON, Person.class).getBody());
|
||||
assertEquals(expected, performGet("/person-response/mono", JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAsSingle() throws Exception {
|
||||
public void personResponseBodyWithSingle() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/single", JSON, Person.class).getBody());
|
||||
assertEquals(expected, performGet("/person-response/single", JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAsMonoResponseEntity() throws Exception {
|
||||
public void personResponseBodyWithMonoResponseEntity() throws Exception {
|
||||
Person expected = new Person("Robert");
|
||||
assertEquals(expected, performGet("/monoResponseEntity", JSON, Person.class).getBody());
|
||||
assertEquals(expected, performGet("/person-response/mono-response-entity", JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAsList() throws Exception {
|
||||
public void personResponseBodyWithList() throws Exception {
|
||||
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
|
||||
assertEquals(expected, performGet("/list", JSON, PERSON_LIST).getBody());
|
||||
assertEquals(expected, performGet("/person-response/list", JSON, PERSON_LIST).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAsPublisher() throws Exception {
|
||||
public void personResponseBodyWithPublisher() throws Exception {
|
||||
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
|
||||
assertEquals(expected, performGet("/publisher", JSON, PERSON_LIST).getBody());
|
||||
assertEquals(expected, performGet("/person-response/publisher", JSON, PERSON_LIST).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAsFlux() throws Exception {
|
||||
public void personResponseBodyWithFlux() throws Exception {
|
||||
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
|
||||
assertEquals(expected, performGet("/flux", JSON, PERSON_LIST).getBody());
|
||||
assertEquals(expected, performGet("/person-response/flux", JSON, PERSON_LIST).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeAsObservable() throws Exception {
|
||||
public void personResponseBodyWithObservable() throws Exception {
|
||||
List<?> expected = asList(new Person("Robert"), new Person("Marie"));
|
||||
assertEquals(expected, performGet("/observable", JSON, PERSON_LIST).getBody());
|
||||
assertEquals(expected, performGet("/person-response/observable", JSON, PERSON_LIST).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -167,106 +168,106 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
|
|||
}
|
||||
|
||||
@Test
|
||||
public void personCapitalize() throws Exception {
|
||||
public void personTransform() throws Exception {
|
||||
assertEquals(new Person("ROBERT"),
|
||||
performPost("/person-capitalize", JSON, new Person("Robert"),
|
||||
performPost("/person-transform/person", JSON, new Person("Robert"),
|
||||
JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void completableFutureCapitalize() throws Exception {
|
||||
public void personTransformWithCompletableFuture() throws Exception {
|
||||
assertEquals(new Person("ROBERT"),
|
||||
performPost("/completable-future-capitalize", JSON, new Person("Robert"),
|
||||
performPost("/person-transform/completable-future", JSON, new Person("Robert"),
|
||||
JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void monoCapitalize() throws Exception {
|
||||
public void personTransformWithMono() throws Exception {
|
||||
assertEquals(new Person("ROBERT"),
|
||||
performPost("/mono-capitalize", JSON, new Person("Robert"),
|
||||
performPost("/person-transform/mono", JSON, new Person("Robert"),
|
||||
JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleCapitalize() throws Exception {
|
||||
public void personTransformWithSingle() throws Exception {
|
||||
assertEquals(new Person("ROBERT"),
|
||||
performPost("/single-capitalize", JSON, new Person("Robert"),
|
||||
performPost("/person-transform/single", JSON, new Person("Robert"),
|
||||
JSON, Person.class).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void publisherCapitalize() throws Exception {
|
||||
public void personTransformWithPublisher() throws Exception {
|
||||
List<?> req = asList(new Person("Robert"), new Person("Marie"));
|
||||
List<?> res = asList(new Person("ROBERT"), new Person("MARIE"));
|
||||
assertEquals(res, performPost("/publisher-capitalize", JSON, req, JSON, PERSON_LIST).getBody());
|
||||
assertEquals(res, performPost("/person-transform/publisher", JSON, req, JSON, PERSON_LIST).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fluxCapitalize() throws Exception {
|
||||
public void personTransformWithFlux() throws Exception {
|
||||
List<?> req = asList(new Person("Robert"), new Person("Marie"));
|
||||
List<?> res = asList(new Person("ROBERT"), new Person("MARIE"));
|
||||
assertEquals(res, performPost("/flux-capitalize", JSON, req, JSON, PERSON_LIST).getBody());
|
||||
assertEquals(res, performPost("/person-transform/flux", JSON, req, JSON, PERSON_LIST).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void observableCapitalize() throws Exception {
|
||||
public void personTransformWithObservable() throws Exception {
|
||||
List<?> req = asList(new Person("Robert"), new Person("Marie"));
|
||||
List<?> res = asList(new Person("ROBERT"), new Person("MARIE"));
|
||||
assertEquals(res, performPost("/observable-capitalize", JSON, req, JSON, PERSON_LIST).getBody());
|
||||
assertEquals(res, performPost("/person-transform/observable", JSON, req, JSON, PERSON_LIST).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void publisherCreate() throws Exception {
|
||||
ResponseEntity<Void> entity = performPost("/publisher-create", JSON,
|
||||
public void personCreateWithPublisherJson() throws Exception {
|
||||
ResponseEntity<Void> entity = performPost("/person-create/publisher", JSON,
|
||||
asList(new Person("Robert"), new Person("Marie")), null, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size());
|
||||
assertEquals(2, getApplicationContext().getBean(PersonCreateController.class).persons.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void publisherCreateXml() throws Exception {
|
||||
public void personCreateWithPublisherXml() throws Exception {
|
||||
People people = new People(new Person("Robert"), new Person("Marie"));
|
||||
ResponseEntity<Void> response = performPost("/publisher-create", APPLICATION_XML, people, null, Void.class);
|
||||
ResponseEntity<Void> response = performPost("/person-create/publisher", APPLICATION_XML, people, null, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size());
|
||||
assertEquals(2, getApplicationContext().getBean(PersonCreateController.class).persons.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fluxCreate() throws Exception {
|
||||
ResponseEntity<Void> entity = performPost("/flux-create", JSON,
|
||||
public void personCreateWithFluxJson() throws Exception {
|
||||
ResponseEntity<Void> entity = performPost("/person-create/flux", JSON,
|
||||
asList(new Person("Robert"), new Person("Marie")), null, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size());
|
||||
assertEquals(2, getApplicationContext().getBean(PersonCreateController.class).persons.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fluxCreateXml() throws Exception {
|
||||
public void personCreateWithFluxXml() throws Exception {
|
||||
People people = new People(new Person("Robert"), new Person("Marie"));
|
||||
ResponseEntity<Void> response = performPost("/flux-create", APPLICATION_XML, people, null, Void.class);
|
||||
ResponseEntity<Void> response = performPost("/person-create/flux", APPLICATION_XML, people, null, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size());
|
||||
assertEquals(2, getApplicationContext().getBean(PersonCreateController.class).persons.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void observableCreate() throws Exception {
|
||||
ResponseEntity<Void> entity = performPost("/observable-create", JSON,
|
||||
public void personCreateWithObservableJson() throws Exception {
|
||||
ResponseEntity<Void> entity = performPost("/person-create/observable", JSON,
|
||||
asList(new Person("Robert"), new Person("Marie")), null, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size());
|
||||
assertEquals(2, getApplicationContext().getBean(PersonCreateController.class).persons.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void observableCreateXml() throws Exception {
|
||||
public void personCreateWithObservableXml() throws Exception {
|
||||
People people = new People(new Person("Robert"), new Person("Marie"));
|
||||
ResponseEntity<Void> response = performPost("/observable-create", APPLICATION_XML, people, null, Void.class);
|
||||
ResponseEntity<Void> response = performPost("/person-create/observable", APPLICATION_XML, people, null, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals(2, getApplicationContext().getBean(TestRestController.class).persons.size());
|
||||
assertEquals(2, getApplicationContext().getBean(PersonCreateController.class).persons.size());
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -278,145 +279,154 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
|
|||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/raw-response")
|
||||
@SuppressWarnings("unused")
|
||||
private static class TestRestController {
|
||||
private static class RawResponseBodyController {
|
||||
|
||||
final List<Person> persons = new ArrayList<>();
|
||||
|
||||
// GET with "raw" data (DataBuffer) response body
|
||||
|
||||
@GetMapping("/raw")
|
||||
public Publisher<ByteBuffer> rawResponseBody() {
|
||||
@GetMapping("/publisher")
|
||||
public Publisher<ByteBuffer> getPublisher() {
|
||||
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
|
||||
JacksonJsonEncoder encoder = new JacksonJsonEncoder();
|
||||
return encoder.encode(Mono.just(new Person("Robert")), dataBufferFactory,
|
||||
ResolvableType.forClass(Person.class), JSON).map(DataBuffer::asByteBuffer);
|
||||
}
|
||||
|
||||
@GetMapping("/raw-flux")
|
||||
public Flux<ByteBuffer> rawFluxResponseBody() {
|
||||
@GetMapping("/flux")
|
||||
public Flux<ByteBuffer> getFlux() {
|
||||
return Flux.just(ByteBuffer.wrap("Hello!".getBytes()));
|
||||
}
|
||||
|
||||
@GetMapping("/raw-observable")
|
||||
public Observable<ByteBuffer> rawObservableResponseBody() {
|
||||
@GetMapping("/observable")
|
||||
public Observable<ByteBuffer> getObservable() {
|
||||
return Observable.just(ByteBuffer.wrap("Hello!".getBytes()));
|
||||
}
|
||||
}
|
||||
|
||||
// GET with Person Object(s) response body to "serialize"
|
||||
@RestController
|
||||
@RequestMapping("/person-response")
|
||||
@SuppressWarnings("unused")
|
||||
private static class PersonResponseBodyController {
|
||||
|
||||
@GetMapping("/person")
|
||||
public Person personResponseBody() {
|
||||
public Person getPerson() {
|
||||
return new Person("Robert");
|
||||
}
|
||||
|
||||
@GetMapping("/completable-future")
|
||||
public CompletableFuture<Person> completableFutureResponseBody() {
|
||||
public CompletableFuture<Person> getCompletableFuture() {
|
||||
return CompletableFuture.completedFuture(new Person("Robert"));
|
||||
}
|
||||
|
||||
@GetMapping("/mono")
|
||||
public Mono<Person> monoResponseBody() {
|
||||
public Mono<Person> getMono() {
|
||||
return Mono.just(new Person("Robert"));
|
||||
}
|
||||
|
||||
@GetMapping("/single")
|
||||
public Single<Person> singleResponseBody() {
|
||||
public Single<Person> getSingle() {
|
||||
return Single.just(new Person("Robert"));
|
||||
}
|
||||
|
||||
@GetMapping("/monoResponseEntity")
|
||||
public ResponseEntity<Mono<Person>> monoResponseEntity() {
|
||||
@GetMapping("/mono-response-entity")
|
||||
public ResponseEntity<Mono<Person>> getMonoResponseEntity() {
|
||||
Mono<Person> body = Mono.just(new Person("Robert"));
|
||||
return ResponseEntity.ok(body);
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public List<Person> listResponseBody() {
|
||||
public List<Person> getList() {
|
||||
return asList(new Person("Robert"), new Person("Marie"));
|
||||
}
|
||||
|
||||
@GetMapping("/publisher")
|
||||
public Publisher<Person> publisherResponseBody() {
|
||||
public Publisher<Person> getPublisher() {
|
||||
return Flux.just(new Person("Robert"), new Person("Marie"));
|
||||
}
|
||||
|
||||
@GetMapping("/flux")
|
||||
public Flux<Person> fluxResponseBody() {
|
||||
public Flux<Person> getFlux() {
|
||||
return Flux.just(new Person("Robert"), new Person("Marie"));
|
||||
}
|
||||
|
||||
@GetMapping("/observable")
|
||||
public Observable<Person> observableResponseBody() {
|
||||
public Observable<Person> getObservable() {
|
||||
return Observable.just(new Person("Robert"), new Person("Marie"));
|
||||
}
|
||||
}
|
||||
|
||||
// GET with Resource response body
|
||||
@RestController
|
||||
@SuppressWarnings("unused")
|
||||
private static class ResourceController {
|
||||
|
||||
@GetMapping("/resource")
|
||||
public Resource resource() {
|
||||
return new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class);
|
||||
}
|
||||
}
|
||||
|
||||
// POST with Person "capitalize" name transformation
|
||||
@RestController
|
||||
@RequestMapping("/person-transform")
|
||||
@SuppressWarnings("unused")
|
||||
private static class PersonTransformationController {
|
||||
|
||||
@PostMapping("/person-capitalize")
|
||||
public Person personCapitalize(@RequestBody Person person) {
|
||||
@PostMapping("/person")
|
||||
public Person transformPerson(@RequestBody Person person) {
|
||||
return new Person(person.getName().toUpperCase());
|
||||
}
|
||||
|
||||
@PostMapping("/completable-future-capitalize")
|
||||
public CompletableFuture<Person> completableFutureCapitalize(
|
||||
@PostMapping("/completable-future")
|
||||
public CompletableFuture<Person> transformCompletableFuture(
|
||||
@RequestBody CompletableFuture<Person> personFuture) {
|
||||
return personFuture.thenApply(person -> new Person(person.getName().toUpperCase()));
|
||||
}
|
||||
|
||||
@PostMapping("/mono-capitalize")
|
||||
public Mono<Person> monoCapitalize(@RequestBody Mono<Person> personFuture) {
|
||||
@PostMapping("/mono")
|
||||
public Mono<Person> transformMono(@RequestBody Mono<Person> personFuture) {
|
||||
return personFuture.map(person -> new Person(person.getName().toUpperCase()));
|
||||
}
|
||||
|
||||
@PostMapping("/single-capitalize")
|
||||
public Single<Person> singleCapitalize(@RequestBody Single<Person> personFuture) {
|
||||
@PostMapping("/single")
|
||||
public Single<Person> transformSingle(@RequestBody Single<Person> personFuture) {
|
||||
return personFuture.map(person -> new Person(person.getName().toUpperCase()));
|
||||
}
|
||||
|
||||
@PostMapping("/publisher-capitalize")
|
||||
public Publisher<Person> publisherCapitalize(@RequestBody Publisher<Person> persons) {
|
||||
@PostMapping("/publisher")
|
||||
public Publisher<Person> transformPublisher(@RequestBody Publisher<Person> persons) {
|
||||
return Flux
|
||||
.from(persons)
|
||||
.map(person -> new Person(person.getName().toUpperCase()));
|
||||
}
|
||||
|
||||
@PostMapping("/flux-capitalize")
|
||||
public Flux<Person> fluxCapitalize(@RequestBody Flux<Person> persons) {
|
||||
@PostMapping("/flux")
|
||||
public Flux<Person> transformFlux(@RequestBody Flux<Person> persons) {
|
||||
return persons.map(person -> new Person(person.getName().toUpperCase()));
|
||||
}
|
||||
|
||||
@PostMapping("/observable-capitalize")
|
||||
public Observable<Person> observableCapitalize(@RequestBody Observable<Person> persons) {
|
||||
@PostMapping("/observable")
|
||||
public Observable<Person> transformObservable(@RequestBody Observable<Person> persons) {
|
||||
return persons.map(person -> new Person(person.getName().toUpperCase()));
|
||||
}
|
||||
}
|
||||
|
||||
// POST with Objects to "create"
|
||||
@RestController
|
||||
@RequestMapping("/person-create")
|
||||
@SuppressWarnings("unused")
|
||||
private static class PersonCreateController {
|
||||
|
||||
@PostMapping("/stream-create")
|
||||
public Publisher<Void> streamCreate(@RequestBody Flux<Person> personStream) {
|
||||
return personStream.collectList().doOnSuccess(persons::addAll).then();
|
||||
}
|
||||
final List<Person> persons = new ArrayList<>();
|
||||
|
||||
@PostMapping("/publisher-create")
|
||||
public Publisher<Void> publisherCreate(@RequestBody Publisher<Person> personStream) {
|
||||
@PostMapping("/publisher")
|
||||
public Publisher<Void> createWithPublisher(@RequestBody Publisher<Person> personStream) {
|
||||
return Flux.from(personStream).doOnNext(persons::add).then();
|
||||
}
|
||||
|
||||
@PostMapping("/flux-create")
|
||||
public Mono<Void> fluxCreate(@RequestBody Flux<Person> personStream) {
|
||||
@PostMapping("/flux")
|
||||
public Mono<Void> createWithFlux(@RequestBody Flux<Person> personStream) {
|
||||
return personStream.doOnNext(persons::add).then();
|
||||
}
|
||||
|
||||
@PostMapping("/observable-create")
|
||||
public Observable<Void> observableCreate(@RequestBody Observable<Person> personStream) {
|
||||
@PostMapping("/observable")
|
||||
public Observable<Void> createWithObservable(@RequestBody Observable<Person> personStream) {
|
||||
return personStream.toList().doOnNext(persons::addAll).flatMap(document -> Observable.empty());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue