Add CompletableFuture/Single/Promise support

This commit is contained in:
Sebastien Deleuze
2015-09-24 23:02:49 +02:00
parent 6716f969d6
commit f816cc6a51
7 changed files with 413 additions and 65 deletions

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2002-2015 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.reactive.util;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.rx.Streams;
/**
* @author Sebastien Deleuze
*/
public class CompletableFutureUtilsTests {
private CountDownLatch lock = new CountDownLatch(1);
private final List<Boolean> results = new ArrayList<>();
private final List<Throwable> errors = new ArrayList<>();
@Test
public void fromPublisher() throws InterruptedException {
Publisher<Boolean> publisher = Streams.just(true, false);
CompletableFuture<List<Boolean>> future = CompletableFutureUtils.fromPublisher(publisher);
future.whenComplete((result, error) -> {
if (error != null) {
errors.add(error);
}
else {
results.addAll(result);
}
lock.countDown();
});
lock.await(2000, TimeUnit.MILLISECONDS);
assertEquals("onError not expected: " + errors.toString(), 0, errors.size());
assertEquals(2, results.size());
assertTrue(results.get(0));
assertFalse(results.get(1));
}
@Test
public void fromSinglePublisher() throws InterruptedException {
Publisher<Boolean> publisher = Streams.just(true);
CompletableFuture<Boolean> future = CompletableFutureUtils.fromSinglePublisher(publisher);
future.whenComplete((result, error) -> {
if (error != null) {
errors.add(error);
}
else {
results.add(result);
}
lock.countDown();
});
lock.await(2000, TimeUnit.MILLISECONDS);
assertEquals("onError not expected: " + errors.toString(), 0, errors.size());
assertEquals(1, results.size());
assertTrue(results.get(0));
}
@Test
public void fromSinglePublisherWithMultipleValues() throws InterruptedException {
Publisher<Boolean> publisher = Streams.just(true, false);
CompletableFuture<Boolean> future = CompletableFutureUtils.fromSinglePublisher(publisher);
future.whenComplete((result, error) -> {
if (error != null) {
errors.add(error);
}
else {
results.add(result);
}
lock.countDown();
});
lock.await(2000, TimeUnit.MILLISECONDS);
assertEquals(1, errors.size());
assertEquals(IllegalStateException.class, errors.get(0).getClass());
assertEquals(0, results.size());
}
}

View File

@@ -19,13 +19,17 @@ package org.springframework.reactive.web.dispatch.method.annotation;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.reactivestreams.Publisher;
import reactor.rx.Promise;
import reactor.rx.Promises;
import reactor.rx.Stream;
import reactor.rx.Streams;
import rx.Observable;
import rx.Single;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
@@ -81,22 +85,89 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
@Test
public void serializeAsPojo() throws Exception {
serializeAsPojo("http://localhost:" + port + "/person");
}
@Test
public void serializeAsCompletableFuture() throws Exception {
serializeAsPojo("http://localhost:" + port + "/completable-future");
}
@Test
public void serializeAsSingle() throws Exception {
serializeAsPojo("http://localhost:" + port + "/single");
}
@Test
public void serializeAsPromise() throws Exception {
serializeAsPojo("http://localhost:" + port + "/promise");
}
@Test
public void serializeAsList() throws Exception {
serializeAsCollection("http://localhost:" + port + "/list");
}
@Test
public void serializeAsPublisher() throws Exception {
serializeAsCollection("http://localhost:" + port + "/publisher");
}
@Test
public void serializeAsObservable() throws Exception {
serializeAsCollection("http://localhost:" + port + "/observable");
}
@Test
public void serializeAsReactorStream() throws Exception {
serializeAsCollection("http://localhost:" + port + "/stream");
}
@Test
public void publisherCapitalize() throws Exception {
capitalizeCollection("http://localhost:" + port + "/publisher-capitalize");
}
@Test
public void observableCapitalize() throws Exception {
capitalizeCollection("http://localhost:" + port + "/observable-capitalize");
}
@Test
public void streamCapitalize() throws Exception {
capitalizeCollection("http://localhost:" + port + "/stream-capitalize");
}
@Test
public void completableFutureCapitalize() throws Exception {
capitalizePojo("http://localhost:" + port + "/completable-future-capitalize");
}
@Test
public void singleCapitalize() throws Exception {
capitalizePojo("http://localhost:" + port + "/single-capitalize");
}
@Test
public void promiseCapitalize() throws Exception {
capitalizePojo("http://localhost:" + port + "/promise-capitalize");
}
public void serializeAsPojo(String requestUrl) throws Exception {
RestTemplate restTemplate = new RestTemplate();
URI url = new URI("http://localhost:" + port + "/person");
URI url = new URI(requestUrl);
RequestEntity<Void> request = RequestEntity.get(url).accept(MediaType.APPLICATION_JSON).build();
ResponseEntity<Person> response = restTemplate.exchange(request, Person.class);
assertEquals(new Person("Robert"), response.getBody());
}
@Test
public void serializeAsList() throws Exception {
public void serializeAsCollection(String requestUrl) throws Exception {
RestTemplate restTemplate = new RestTemplate();
URI url = new URI("http://localhost:" + port + "/list");
URI url = new URI(requestUrl);
RequestEntity<Void> request = RequestEntity.get(url).accept(MediaType.APPLICATION_JSON).build();
List<Person> results = restTemplate.exchange(request, new ParameterizedTypeReference<List<Person>>(){}).getBody();
@@ -105,67 +176,23 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
assertEquals(new Person("Marie"), results.get(1));
}
@Test
public void serializeAsPublisher() throws Exception {
public void capitalizePojo(String requestUrl) throws Exception {
RestTemplate restTemplate = new RestTemplate();
URI url = new URI("http://localhost:" + port + "/publisher");
RequestEntity<Void> request = RequestEntity.get(url).accept(MediaType.APPLICATION_JSON).build();
List<Person> results = restTemplate.exchange(request, new ParameterizedTypeReference<List<Person>>(){}).getBody();
URI url = new URI(requestUrl);
RequestEntity<Person> request = RequestEntity
.post(url)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(new Person("Robert"));
ResponseEntity<Person> response = restTemplate.exchange(request, Person.class);
assertEquals(2, results.size());
assertEquals(new Person("Robert"), results.get(0));
assertEquals(new Person("Marie"), results.get(1));
}
@Test
public void serializeAsObservable() throws Exception {
RestTemplate restTemplate = new RestTemplate();
URI url = new URI("http://localhost:" + port + "/observable");
RequestEntity<Void> request = RequestEntity.get(url).accept(MediaType.APPLICATION_JSON).build();
List<Person> results = restTemplate.exchange(request, new ParameterizedTypeReference<List<Person>>() {
}).getBody();
assertEquals(2, results.size());
assertEquals(new Person("Robert"), results.get(0));
assertEquals(new Person("Marie"), results.get(1));
}
@Test
public void serializeAsReactorStream() throws Exception {
RestTemplate restTemplate = new RestTemplate();
URI url = new URI("http://localhost:" + port + "/stream");
RequestEntity<Void> request = RequestEntity.get(url).accept(MediaType.APPLICATION_JSON).build();
List<Person> results = restTemplate.exchange(request, new ParameterizedTypeReference<List<Person>>() {
}).getBody();
assertEquals(2, results.size());
assertEquals(new Person("Robert"), results.get(0));
assertEquals(new Person("Marie"), results.get(1));
}
@Test
public void publisherCapitalize() throws Exception {
capitalize("http://localhost:" + port + "/publisher-capitalize");
}
@Test
public void observableCapitalize() throws Exception {
capitalize("http://localhost:" + port + "/observable-capitalize");
}
@Test
public void streamCapitalize() throws Exception {
capitalize("http://localhost:" + port + "/stream-capitalize");
assertEquals(new Person("ROBERT"), response.getBody());
}
public void capitalize(String requestUrl) throws Exception {
public void capitalizeCollection(String requestUrl) throws Exception {
RestTemplate restTemplate = new RestTemplate();
URI url = new URI(requestUrl);
@@ -199,6 +226,24 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
return new Person("Robert");
}
@RequestMapping("/completable-future")
@ResponseBody
public CompletableFuture<Person> completableFutureResponseBody() {
return CompletableFuture.completedFuture(new Person("Robert"));
}
@RequestMapping("/single")
@ResponseBody
public Single<Person> singleResponseBody() {
return Single.just(new Person("Robert"));
}
@RequestMapping("/promise")
@ResponseBody
public Promise<Person> promiseResponseBody() {
return Promises.success(new Person("Robert"));
}
@RequestMapping("/list")
@ResponseBody
public List<Person> listResponseBody() {
@@ -250,6 +295,33 @@ public class RequestMappingIntegrationTests extends AbstractHttpHandlerIntegrati
});
}
@RequestMapping("/completable-future-capitalize")
@ResponseBody
public CompletableFuture<Person> completableFutureCapitalize(@RequestBody CompletableFuture<Person> personFuture) {
return personFuture.thenApply(person -> {
person.setName(person.getName().toUpperCase());
return person;
});
}
@RequestMapping("/single-capitalize")
@ResponseBody
public Single<Person> singleCapitalize(@RequestBody Single<Person> personFuture) {
return personFuture.map(person -> {
person.setName(person.getName().toUpperCase());
return person;
});
}
@RequestMapping("/promise-capitalize")
@ResponseBody
public Promise<Person> promiseCapitalize(@RequestBody Promise<Person> personFuture) {
return personFuture.map(person -> {
person.setName(person.getName().toUpperCase());
return person;
});
}
}
private static class Person {