DATACMNS-1430 - Support for returning wrapper types of Streamable.

Extended IterableToStreamableConverter in use in QueryExecutionConverters so that it can not only return Streamable instances but also instances of types that have a factory method or constructor taking a Streamable (by making use of the ObjectToObjectConverter in a default ConversionService).
This commit is contained in:
Oliver Drotbohm
2018-12-04 11:47:56 +01:00
parent 840addcdbe
commit cf1eaff784
3 changed files with 125 additions and 8 deletions

View File

@@ -19,6 +19,7 @@ import static org.assertj.core.api.Assertions.*;
import io.vavr.control.Try;
import javaslang.control.Option;
import lombok.Value;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import rx.Completable;
@@ -28,6 +29,7 @@ import rx.Single;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -372,6 +374,16 @@ public class QueryExecutionResultHandlerUnitTests {
assertThat(result).isInstanceOfSatisfying(Option.class, it -> assertThat(it.get()).isEqualTo(entity));
}
@Test // DATACMNS-1430
public void convertsElementsAndValueIntoCustomStreamable() throws Exception {
Object result = handler.postProcessInvocationResult(Arrays.asList("foo"), getMethod("customStreamable"));
assertThat(result).isInstanceOfSatisfying(CustomStreamableWrapper.class, it -> {
assertThat(it).containsExactly("foo");
});
}
private static Method getMethod(String methodName) throws Exception {
return Sample.class.getMethod(methodName);
}
@@ -406,7 +418,23 @@ public class QueryExecutionResultHandlerUnitTests {
// DATACMNS-938
Try<Option<Entity>> tryOfOption();
// DATACMNS-1430
CustomStreamableWrapper<String> customStreamable();
}
static class Entity {}
// DATACMNS-1430
@Value
static class CustomStreamableWrapper<T> implements Streamable<T> {
Streamable<T> source;
@Override
public Iterator<T> iterator() {
return source.iterator();
}
}
}

View File

@@ -24,6 +24,7 @@ import javaslang.collection.Seq;
import javaslang.collection.Traversable;
import javaslang.control.Try;
import javaslang.control.Try.Failure;
import lombok.Value;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import rx.Completable;
@@ -35,6 +36,7 @@ import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -48,6 +50,7 @@ import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.Streamable;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.concurrent.ListenableFuture;
@@ -412,6 +415,29 @@ public class QueryExecutionConvertersUnitTests {
}
}
@Test // DATACMNS-1430
public void returnsStreamableForIterable() throws Exception {
assertThat(conversionService.canConvert(Iterable.class, Streamable.class)).isTrue();
assertThat(conversionService.convert(Arrays.asList("foo"), Streamable.class)).containsExactly("foo");
}
@Test // DATACMNS-1430
public void convertsToStreamableWrapper() throws Exception {
assertThat(conversionService.canConvert(Iterable.class, StreamableWrapper.class)).isTrue();
assertThat(conversionService.convert(Arrays.asList("foo"), StreamableWrapper.class).getStreamable()) //
.containsExactly("foo");
}
@Test // DATACMNS-1430
public void convertsToStreamableWrapperImplementingStreamable() throws Exception {
assertThat(conversionService.canConvert(Iterable.class, CustomStreamableWrapper.class)).isTrue();
assertThat(conversionService.convert(Arrays.asList("foo"), CustomStreamableWrapper.class)) //
.containsExactly("foo");
}
interface Sample {
Page<String> pages();
@@ -424,4 +450,22 @@ public class QueryExecutionConvertersUnitTests {
io.vavr.control.Try<io.vavr.collection.Seq<Sample>> vavrTryForSeqMethod();
}
// DATACMNS-1430
@Value(staticConstructor = "of")
static class StreamableWrapper {
Streamable<String> streamable;
}
@Value
static class CustomStreamableWrapper<T> implements Streamable<T> {
Streamable<T> source;
@Override
public Iterator<T> iterator() {
return source.iterator();
}
}
}