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 d3a3e839d2..2126fe1469 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 @@ -128,24 +128,6 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati assertEquals(expected, performGet("/raw-observable", null, String.class).getBody()); } - @Test - public void handleWithThrownException() throws Exception { - String expected = "Recovered from error: Boo"; - assertEquals(expected, performGet("/thrown-exception", null, String.class).getBody()); - } - - @Test - public void handleWithErrorSignal() throws Exception { - String expected = "Recovered from error: Boo"; - assertEquals(expected, performGet("/error-signal", null, String.class).getBody()); - } - - @Test - public void streamResult() throws Exception { - String[] expected = {"0", "1", "2", "3", "4"}; - assertArrayEquals(expected, performGet("/stream-result", null, String[].class).getBody()); - } - @Test public void serializeAsPojo() throws Exception { Person expected = new Person("Robert"); @@ -158,12 +140,6 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati assertEquals(expected, performGet("/completable-future", JSON, Person.class).getBody()); } - @Test - public void serializeAsMonoResponseEntity() throws Exception { - Person expected = new Person("Robert"); - assertEquals(expected, performGet("/monoResponseEntity", JSON, Person.class).getBody()); - } - @Test public void serializeAsMono() throws Exception { Person expected = new Person("Robert"); @@ -176,6 +152,12 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati assertEquals(expected, performGet("/single", JSON, Person.class).getBody()); } + @Test + public void serializeAsMonoResponseEntity() throws Exception { + Person expected = new Person("Robert"); + assertEquals(expected, performGet("/monoResponseEntity", JSON, Person.class).getBody()); + } + @Test public void serializeAsList() throws Exception { List expected = asList(new Person("Robert"), new Person("Marie")); @@ -201,9 +183,42 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati } @Test - public void serializeAsReactorStream() throws Exception { - List expected = asList(new Person("Robert"), new Person("Marie")); - assertEquals(expected, performGet("/stream", JSON, PERSON_LIST).getBody()); + public void resource() throws Exception { + ResponseEntity response = performGet("/resource", null, byte[].class); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertTrue(response.hasBody()); + assertEquals(951, response.getHeaders().getContentLength()); + assertEquals(951, response.getBody().length); + assertEquals(new MediaType("image", "x-png"), response.getHeaders().getContentType()); + } + + @Test + public void personCapitalize() throws Exception { + assertEquals(new Person("ROBERT"), + performPost("/person-capitalize", JSON, new Person("Robert"), + JSON, Person.class).getBody()); + } + + @Test + public void completableFutureCapitalize() throws Exception { + assertEquals(new Person("ROBERT"), + performPost("/completable-future-capitalize", JSON, new Person("Robert"), + JSON, Person.class).getBody()); + } + + @Test + public void monoCapitalize() throws Exception { + assertEquals(new Person("ROBERT"), + performPost("/mono-capitalize", JSON, new Person("Robert"), + JSON, Person.class).getBody()); + } + + @Test + public void singleCapitalize() throws Exception { + assertEquals(new Person("ROBERT"), + performPost("/single-capitalize", JSON, new Person("Robert"), + JSON, Person.class).getBody()); } @Test @@ -227,34 +242,6 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati assertEquals(res, performPost("/observable-capitalize", JSON, req, JSON, PERSON_LIST).getBody()); } - @Test - public void personCapitalize() throws Exception { - assertEquals(new Person("ROBERT"), - performPost("/person-capitalize", JSON, new Person("Robert"), - JSON, Person.class).getBody()); - } - - @Test - public void completableFutureCapitalize() throws Exception { - assertEquals(new Person("ROBERT"), - performPost("/completable-future-capitalize", JSON, new Person("Robert"), - JSON, Person.class).getBody()); - } - - @Test - public void monoCapitalize() throws Exception { - assertEquals(new Person("ROBERT"), - performPost("/mono-capitalize", JSON, new Person("Robert"), - JSON, Person.class).getBody()); - } - - @Test - public void singleCapitalize() throws Exception { - assertEquals(new Person("ROBERT"), - performPost("/single-capitalize", JSON, new Person("Robert"), - JSON, Person.class).getBody()); - } - @Test public void publisherCreate() throws Exception { ResponseEntity entity = performPost("/publisher-create", JSON, @@ -310,20 +297,27 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati } @Test - public void html() throws Exception { - String expected = "Hello: Jason!"; - assertEquals(expected, performGet("/html?name=Jason", MediaType.TEXT_HTML, String.class).getBody()); + public void streamResult() throws Exception { + String[] expected = {"0", "1", "2", "3", "4"}; + assertArrayEquals(expected, performGet("/stream-result", null, String[].class).getBody()); } @Test - public void resource() throws Exception { - ResponseEntity response = performGet("/resource", null, byte[].class); + public void handleWithThrownException() throws Exception { + String expected = "Recovered from error: Boo"; + assertEquals(expected, performGet("/thrown-exception", null, String.class).getBody()); + } - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertTrue(response.hasBody()); - assertEquals(951, response.getHeaders().getContentLength()); - assertEquals(951, response.getBody().length); - assertEquals(new MediaType("image", "x-png"), response.getHeaders().getContentType()); + @Test + public void handleWithErrorSignal() throws Exception { + String expected = "Recovered from error: Boo"; + assertEquals(expected, performGet("/error-signal", null, String.class).getBody()); + } + + @Test + public void html() throws Exception { + String expected = "Hello: Jason!"; + assertEquals(expected, performGet("/html?name=Jason", MediaType.TEXT_HTML, String.class).getBody()); } @@ -410,15 +404,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati return Flux.just("Hello ", name, "!"); } - @RequestMapping("/person") - public Person personResponseBody() { - return new Person("Robert"); - } - - @RequestMapping("/completable-future") - public CompletableFuture completableFutureResponseBody() { - return CompletableFuture.completedFuture(new Person("Robert")); - } + // Response body with "raw" data (DataBuffer) @RequestMapping("/raw") public Publisher rawResponseBody() { @@ -428,11 +414,6 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati ResolvableType.forClass(Person.class), JSON).map(DataBuffer::asByteBuffer); } - @RequestMapping("/stream-result") - public Publisher stringStreamResponseBody() { - return Flux.interval(100).take(5); - } - @RequestMapping("/raw-flux") public Flux rawFluxResponseBody() { return Flux.just(ByteBuffer.wrap("Hello!".getBytes())); @@ -443,10 +424,16 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati return Observable.just(ByteBuffer.wrap("Hello!".getBytes())); } - @RequestMapping("/monoResponseEntity") - public ResponseEntity> monoResponseEntity() { - Mono body = Mono.just(new Person("Robert")); - return ResponseEntity.ok(body); + // Response body with Person Object(s) to "serialize" + + @RequestMapping("/person") + public Person personResponseBody() { + return new Person("Robert"); + } + + @RequestMapping("/completable-future") + public CompletableFuture completableFutureResponseBody() { + return CompletableFuture.completedFuture(new Person("Robert")); } @RequestMapping("/mono") @@ -459,6 +446,12 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati return Single.just(new Person("Robert")); } + @RequestMapping("/monoResponseEntity") + public ResponseEntity> monoResponseEntity() { + Mono body = Mono.just(new Person("Robert")); + return ResponseEntity.ok(body); + } + @RequestMapping("/list") public List listResponseBody() { return asList(new Person("Robert"), new Person("Marie")); @@ -479,9 +472,35 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati return Observable.just(new Person("Robert"), new Person("Marie")); } - @RequestMapping("/stream") - public Flux reactorStreamResponseBody() { - return Flux.just(new Person("Robert"), new Person("Marie")); + // ResponseBody with Resource + + @RequestMapping("/resource") + @ResponseBody + public Resource resource() { + return new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class); + } + + // RequestBody -> ResponseBody with Person "capitalize" name transformation + + @RequestMapping("/person-capitalize") + public Person personCapitalize(@RequestBody Person person) { + return new Person(person.getName().toUpperCase()); + } + + @RequestMapping("/completable-future-capitalize") + public CompletableFuture completableFutureCapitalize( + @RequestBody CompletableFuture personFuture) { + return personFuture.thenApply(person -> new Person(person.getName().toUpperCase())); + } + + @RequestMapping("/mono-capitalize") + public Mono monoCapitalize(@RequestBody Mono personFuture) { + return personFuture.map(person -> new Person(person.getName().toUpperCase())); + } + + @RequestMapping("/single-capitalize") + public Single singleCapitalize(@RequestBody Single personFuture) { + return personFuture.map(person -> new Person(person.getName().toUpperCase())); } @RequestMapping("/publisher-capitalize") @@ -501,32 +520,13 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati return persons.map(person -> new Person(person.getName().toUpperCase())); } + // Request body with Objects to "create" + @RequestMapping("/stream-create") public Publisher streamCreate(@RequestBody Flux personStream) { return personStream.collectList().doOnSuccess(persons::addAll).then(); } - @RequestMapping("/person-capitalize") - public Person personCapitalize(@RequestBody Person person) { - return new Person(person.getName().toUpperCase()); - } - - @RequestMapping("/completable-future-capitalize") - public CompletableFuture completableFutureCapitalize( - @RequestBody CompletableFuture personFuture) { - return personFuture.thenApply(person -> new Person(person.getName().toUpperCase())); - } - - @RequestMapping("/mono-capitalize") - public Mono monoCapitalize(@RequestBody Mono personFuture) { - return personFuture.map(person -> new Person(person.getName().toUpperCase())); - } - - @RequestMapping("/single-capitalize") - public Single singleCapitalize(@RequestBody Single personFuture) { - return personFuture.map(person -> new Person(person.getName().toUpperCase())); - } - @RequestMapping("/publisher-create") public Publisher publisherCreate(@RequestBody Publisher personStream) { return Flux.from(personStream).doOnNext(persons::add).then(); @@ -542,6 +542,15 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati return personStream.toList().doOnNext(persons::addAll).flatMap(document -> Observable.empty()); } + // Async stream + + @RequestMapping("/stream-result") + public Publisher stringStreamResponseBody() { + return Flux.interval(100).take(5); + } + + // Error handling + @RequestMapping("/thrown-exception") public Publisher handleAndThrowException() { throw new IllegalStateException("Boo"); @@ -557,12 +566,6 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati return Mono.just("Recovered from error: " + ex.getMessage()); } - @RequestMapping("/resource") - @ResponseBody - public Resource resource() { - return new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class); - } - //TODO add mixed and T request mappings tests }