GH-422 Add test and documentation for pure function interaction

This commit is contained in:
Oleg Zhurakousky
2020-11-11 12:26:09 +01:00
parent e2db46818d
commit daa3c27226
4 changed files with 100 additions and 34 deletions

View File

@@ -15,7 +15,25 @@ The example provides dependencies and instructions to demonstrate several distin
The POM file defines all the necessary dependency in a segregated way, so you can choose the one you're interested in.
### Direct function invocation
TBD
By looking up user declared functions in `FunctionCatalog` you can interact (i.e., for testing purposes) with functions directly
while enjoying all the features of _spring-cloud-function_ such as transparent type conversion, function composition and more.
[source, java]
----
Message<String> binaryCloudEventMessage = MessageBuilder
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
.setHeader("ce-specversion", "1.0")
.setHeader("ce-type", "com.example.springevent")
.setHeader("ce-source", "spring.io/spring-event")
.setHeader("ce-id", "123-456-9876-09")
.build();
Function<Message<String>, String> asPojoMessage = catalog.lookup("asPOJOMessage");
System.out.println(asPojoMessage.apply(binaryCloudEventMessage));
----
The test case link:src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java[CloudeventDemoApplicationFunctionTests]
provides a good example on how to accomplish this.
### Function as a REST endpoint
@@ -114,6 +132,17 @@ entire structure of Cloud Event message as payload (see the screenshot below)._
image::images\rabbit-send-structured.png[structured,700,700]
You can follow similar approach with Apache Kafka or any other binder. All you need is bring a required binder dependency.
For example for Apache Kafka
[source, xml]
----
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
<version>3.1.0-SNAPSHOT</version>
</dependency>
----
### Function invocation via RSocket
TBD

View File

@@ -35,7 +35,7 @@
<version>3.1.0-SNAPSHOT</version>
</dependency>
<!-- end REST -->
<!-- RSocket - only needed if you intend to invoke via RSocket -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.cloud</groupId> -->
@@ -45,11 +45,11 @@
<!-- end RSocket -->
<!-- RabbitMQ - only needed if you intend to invoke via RabbitMQ -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
<version>3.1.0-SNAPSHOT</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.springframework.cloud</groupId> -->
<!-- <artifactId>spring-cloud-stream-binder-rabbit</artifactId> -->
<!-- <version>3.1.0-SNAPSHOT</version> -->
<!-- </dependency> -->
<!-- end RabbitMQ -->
<!-- Kafka - only needed if you intend to invoke via RabbitMQ -->

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2020-2020 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
*
* https://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 io.spring.cloudevent;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.context.ApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
/**
*
* @author Oleg Zhurakousky
*
*/
public class CloudeventDemoApplicationFunctionTests {
@Test
public void demoPureFunctionInvocation() {
ApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class);
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
Message<String> binaryCloudEventMessage = MessageBuilder
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
.setHeader("ce-specversion", "1.0")
.setHeader("ce-type", "com.example.springevent")
.setHeader("ce-source", "spring.io/spring-event")
.setHeader("ce-id", "123-456-9876-09")
.build();
/*
* NOTE how it makes no difference what the actual function signature
* is (see `asPOJOMessage` and `asPOJO` specifically). Type conversion will happen
* inside spring-cloud-function.
*/
Function<Message<String>, String> asPojoMessage = catalog.lookup("asPOJOMessage");
System.out.println(asPojoMessage.apply(binaryCloudEventMessage));
Function<Message<String>, String> asPojo = catalog.lookup("asPOJO");
System.out.println(asPojo.apply(binaryCloudEventMessage));
Function<Message<String>, String> asString = catalog.lookup("asString");
System.out.println(asString.apply(binaryCloudEventMessage));
Function<Message<String>, String> asStringMessage = catalog.lookup("asStringMessage");
System.out.println(asStringMessage.apply(binaryCloudEventMessage));
}
}

View File

@@ -1,27 +0,0 @@
/*
* Copyright 2020-2020 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
*
* https://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 io.spring.cloudevent;
/**
*
* @author Oleg Zhurakousky
*
*/
public class CloudeventDemoApplicationStreamTests {
}