SimpleFunctionRegistryTests: reactive function test case

FunctionTypeUtils#isTypeCollection: unwrap publisher

JsonMessageConverter: handler for a ParameterizedType conversionHint

refactor
This commit is contained in:
anshlykov
2020-06-13 03:02:52 +03:00
committed by Oleg Zhurakousky
parent fdbbcc4e45
commit a97bdcafd4
5 changed files with 65 additions and 5 deletions

View File

@@ -32,6 +32,7 @@ import reactor.util.function.Tuple2;
import reactor.util.function.Tuple3;
import org.springframework.cloud.function.context.FunctionType;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.messaging.Message;
import static org.assertj.core.api.Assertions.assertThat;
@@ -138,6 +139,14 @@ public class FunctionTypeUtilsTests {
assertThat(Integer.class).isAssignableFrom(type.getOutputType());
}
@Test
public void testIsTypeCollection() {
assertThat(FunctionTypeUtils.isTypeCollection(new ParameterizedTypeReference<String>() { }.getType())).isFalse();
assertThat(FunctionTypeUtils.isTypeCollection(new ParameterizedTypeReference<List<String>>() { }.getType())).isTrue();
assertThat(FunctionTypeUtils.isTypeCollection(new ParameterizedTypeReference<Flux<List<String>>>() { }.getType())).isTrue();
assertThat(FunctionTypeUtils.isTypeCollection(new ParameterizedTypeReference<Flux<Message<List<String>>>>() { }.getType())).isTrue();
}
private static Function<String, Integer> function() {
return null;
}

View File

@@ -17,11 +17,14 @@
package org.springframework.cloud.function.context.catalog;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -204,6 +207,27 @@ public class SimpleFunctionRegistryTests {
assertThat(result).isEqualTo("RATS");
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testReactiveFunctionMessages() {
FunctionRegistration<ReactiveFunction> registration = new FunctionRegistration<>(new ReactiveFunction(), "reactive")
.type(FunctionType.of(ReactiveFunction.class));
SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter);
catalog.register(registration);
Function lookedUpFunction = catalog.lookup("reactive");
assertThat(lookedUpFunction).isNotNull();
Flux<List<String>> result = (Flux<List<String>>) lookedUpFunction
.apply(Flux.just(MessageBuilder
.withPayload("[{\"name\":\"item1\"},{\"name\":\"item2\"}]")
.setHeader(MessageHeaders.CONTENT_TYPE, "application/json")
.build()
));
Assertions.assertIterableEquals(result.blockFirst(), Arrays.asList("item1", "item2"));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testWithCustomMessageConverter() {
@@ -334,4 +358,14 @@ public class SimpleFunctionRegistryTests {
}
private static class ReactiveFunction implements Function<Flux<Message<List<Person>>>, Flux<List<String>>> {
@Override
public Flux<List<String>> apply(Flux<Message<List<Person>>> listFlux) {
return listFlux
.map(Message::getPayload)
.map(lst -> lst.stream().map(Person::getName).collect(Collectors.toList()));
}
}
}