GH-685 Update Kotlin documentation

Add test for collections

polishing
This commit is contained in:
Oleg Zhurakousky
2021-04-13 09:02:23 +02:00
parent ecd9902ced
commit b7edd1e1b7
5 changed files with 39 additions and 2 deletions

View File

@@ -467,7 +467,7 @@ The above represents Kotlin lambdas configured as Spring beans. The signature of
While mechanics of Kotlin-to-Java mapping are outside of the scope of this documentation, it is important to understand that the
same rules for signature transformation outlined in "Java 8 function support" section are applied here as well.
To enable Kotlin support all you need is to add `spring-cloud-function-kotlin` module to your classpath which contains the appropriate
To enable Kotlin support all you need is to add Kotlin SDK libraries on the classpath which will trigger appropriate
autoconfiguration and supporting classes.
=== Function Component Scan

View File

@@ -51,8 +51,8 @@
<module>spring-cloud-function-samples</module>
<module>spring-cloud-function-deployer</module>
<module>spring-cloud-function-adapters</module>
<module>spring-cloud-function-kotlin</module>
<module>spring-cloud-function-rsocket</module>
<module>spring-cloud-function-kotlin</module>
<module>docs</module>
</modules>

View File

@@ -0,0 +1,4 @@
!!! INTERNAL !!!
Contains only Kotlin tests.
Since version 3.1.3, user's should not be declaring explicit dependency on this module.

View File

@@ -33,6 +33,7 @@ import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.messaging.support.MessageBuilder;
import static org.assertj.core.api.Assertions.assertThat;
@@ -83,6 +84,30 @@ public class ContextFunctionCatalogAutoConfigurationKotlinTests {
assertThat(functionType.getActualTypeArguments().length).isEqualTo(2);
assertThat(functionType.getActualTypeArguments()[0].getTypeName()).isEqualTo(Person.class.getName());
assertThat(functionType.getActualTypeArguments()[1].getTypeName()).isEqualTo(String.class.getName());
function = this.context.getBean("kotlinListPojoFunction");
functionType = (ParameterizedType) FunctionTypeUtils.discoverFunctionType(function, "kotlinListPojoFunction", this.context);
assertThat(functionType.getRawType().getTypeName()).isEqualTo(Function.class.getName());
assertThat(functionType.getActualTypeArguments().length).isEqualTo(2);
assertThat(functionType.getActualTypeArguments()[0].getTypeName()).isEqualTo("java.util.List<org.springframework.cloud.function.kotlin.Person>");
assertThat(functionType.getActualTypeArguments()[1].getTypeName()).isEqualTo(String.class.getName());
}
@Test
public void testWithComplexTypesAndRouting() {
create(new Class[] { KotlinLambdasConfiguration.class,
SimpleConfiguration.class });
FunctionInvocationWrapper function = this.catalog.lookup("kotlinListPojoFunction");
String result = (String) function.apply("[{\"name\":\"Ricky\"}]");
assertThat(result).isEqualTo("List of: Ricky");
function = this.catalog.lookup(Function.class, "functionRouter");
result = (String) function.apply(MessageBuilder.withPayload("[{\"name\":\"Ricky\"}]")
.setHeader("spring.cloud.function.definition", "kotlinListPojoFunction").build());
assertThat(result).isEqualTo("List of: Ricky");
}
@Test

View File

@@ -20,6 +20,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.util.function.Function
import java.util.List
/**
* @author Oleg Zhurakousky
@@ -37,6 +38,13 @@ class KotlinLambdasConfiguration {
fun kotlinPojoFunction(): (Person) -> String {
return { it.name.toString()}
}
@Bean
fun kotlinListPojoFunction(): (List<Person>) -> String {
return {
"List of: " + it.get(0).name
}
}
@Bean
fun kotlinConsumer(): (String) -> Unit {