GH-768 Add tests to validate proper map values conversion
Resolves #768
This commit is contained in:
@@ -26,6 +26,7 @@ import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -644,6 +645,16 @@ public class BeanFactoryAwareFunctionRegistryTests {
|
||||
assertThat(resultList).isEmpty();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Test
|
||||
public void testGH_768() throws Exception {
|
||||
FunctionCatalog catalog = this.configureCatalog(SCF_GH_768ConfigurationAsFunction.class);
|
||||
Function function = catalog.lookup("echo");
|
||||
|
||||
String result = (String) function.apply("{\"ricky\":{\"name\":\"ricky\"}}");
|
||||
assertThat(result).isEqualTo("{ricky=Person: ricky/0}");
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Test
|
||||
public void testArrayPayloadOnFluxFunction() throws Exception {
|
||||
@@ -1121,6 +1132,19 @@ public class BeanFactoryAwareFunctionRegistryTests {
|
||||
}
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
public static class SCF_GH_768ConfigurationAsFunction {
|
||||
@Bean
|
||||
public Function<Map<String, Person>, String> echoToString() {
|
||||
return persons -> {
|
||||
for (Entry<String, Person> entry : persons.entrySet()) {
|
||||
assertThat(entry.getValue().getName()).isNotEmpty(); // would fail if value would not be converted to Person
|
||||
}
|
||||
return persons.toString();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static class Person {
|
||||
private String name;
|
||||
private int id;
|
||||
|
||||
@@ -16,9 +16,12 @@
|
||||
|
||||
package org.springframework.cloud.function.context.catalog;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
@@ -119,6 +122,29 @@ public class SimpleFunctionRegistryTests {
|
||||
assertThat(instanceA).isNotSameAs(instanceb).isNotSameAs(instanceC);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSCF768() {
|
||||
ResolvableType map = ResolvableType.forClassWithGenerics(Map.class, String.class, Person.class);
|
||||
Type functionType = ResolvableType.forClassWithGenerics(Function.class, map, ResolvableType.forClass(String.class)).getType();
|
||||
|
||||
Function<Map<String, Person>, String> function = persons -> {
|
||||
for (Entry<String, Person> entry : persons.entrySet()) {
|
||||
assertThat(entry.getValue().getName()).isNotEmpty(); // would fail if value would not be converted to Person
|
||||
}
|
||||
return persons.toString();
|
||||
};
|
||||
|
||||
FunctionRegistration<Function<Map<String, Person>, String>> registration = new FunctionRegistration<>(
|
||||
function, "echo").type(FunctionType.of(functionType));
|
||||
SimpleFunctionRegistry catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter,
|
||||
new JacksonMapper(new ObjectMapper()));
|
||||
catalog.register(registration);
|
||||
|
||||
FunctionInvocationWrapper lookedUpFunction = catalog.lookup("echo");
|
||||
String result = (String) lookedUpFunction.apply("{\"ricky\":{\"name\":\"ricky\"}}");
|
||||
assertThat(result).isEqualTo("{ricky=ricky}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSCF640() {
|
||||
Echo function = new Echo();
|
||||
@@ -578,6 +604,11 @@ public class SimpleFunctionRegistryTests {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Words implements Supplier<String> {
|
||||
|
||||
Reference in New Issue
Block a user