From aa1397b8ba73d993d213b87b5a7f17d0fcf8776e Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 11 Jul 2016 13:56:00 -0400 Subject: [PATCH] Polish method order in RequestMappingIntegrationTests --- .../RequestMappingIntegrationTests.java | 215 +++++++++--------- 1 file changed, 109 insertions(+), 106 deletions(-) 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 d3a3e839d2c..2126fe14690 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,30 +183,14 @@ 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()); - } - - @Test - public void publisherCapitalize() 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()); - } - - @Test - public void fluxCapitalize() 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()); - } + public void resource() throws Exception { + ResponseEntity response = performGet("/resource", null, byte[].class); - @Test - public void observableCapitalize() 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(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 @@ -233,7 +199,7 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati performPost("/person-capitalize", JSON, new Person("Robert"), JSON, Person.class).getBody()); } - + @Test public void completableFutureCapitalize() throws Exception { assertEquals(new Person("ROBERT"), @@ -255,6 +221,27 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati JSON, Person.class).getBody()); } + @Test + public void publisherCapitalize() 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()); + } + + @Test + public void fluxCapitalize() 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()); + } + + @Test + public void observableCapitalize() 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()); + } + @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,38 +472,21 @@ 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")); - } - - @RequestMapping("/publisher-capitalize") - public Publisher publisherCapitalize(@RequestBody Publisher persons) { - return Flux - .from(persons) - .map(person -> new Person(person.getName().toUpperCase())); - } + // ResponseBody with Resource - @RequestMapping("/flux-capitalize") - public Flux fluxCapitalize(@RequestBody Flux persons) { - return persons.map(person -> new Person(person.getName().toUpperCase())); - } - - @RequestMapping("/observable-capitalize") - public Observable observableCapitalize(@RequestBody Observable persons) { - return persons.map(person -> new Person(person.getName().toUpperCase())); + @RequestMapping("/resource") + @ResponseBody + public Resource resource() { + return new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class); } - @RequestMapping("/stream-create") - public Publisher streamCreate(@RequestBody Flux personStream) { - return personStream.collectList().doOnSuccess(persons::addAll).then(); - } + // 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) { @@ -527,6 +503,30 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati return personFuture.map(person -> new Person(person.getName().toUpperCase())); } + @RequestMapping("/publisher-capitalize") + public Publisher publisherCapitalize(@RequestBody Publisher persons) { + return Flux + .from(persons) + .map(person -> new Person(person.getName().toUpperCase())); + } + + @RequestMapping("/flux-capitalize") + public Flux fluxCapitalize(@RequestBody Flux persons) { + return persons.map(person -> new Person(person.getName().toUpperCase())); + } + + @RequestMapping("/observable-capitalize") + public Observable observableCapitalize(@RequestBody Observable persons) { + 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("/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 }