diff --git a/spring-cloud-function-samples/function-sample-cloudevent/README.adoc b/spring-cloud-function-samples/function-sample-cloudevent/README.adoc index adf4f0a82..470dbdc54 100644 --- a/spring-cloud-function-samples/function-sample-cloudevent/README.adoc +++ b/spring-cloud-function-samples/function-sample-cloudevent/README.adoc @@ -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 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, 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] +---- + + org.springframework.cloud + spring-cloud-stream-binder-kafka + 3.1.0-SNAPSHOT + +---- + ### Function invocation via RSocket TBD diff --git a/spring-cloud-function-samples/function-sample-cloudevent/pom.xml b/spring-cloud-function-samples/function-sample-cloudevent/pom.xml index 0c61fc292..4076b43a1 100644 --- a/spring-cloud-function-samples/function-sample-cloudevent/pom.xml +++ b/spring-cloud-function-samples/function-sample-cloudevent/pom.xml @@ -35,7 +35,7 @@ 3.1.0-SNAPSHOT - + @@ -45,11 +45,11 @@ - - org.springframework.cloud - spring-cloud-stream-binder-rabbit - 3.1.0-SNAPSHOT - + + + + + diff --git a/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java b/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java new file mode 100644 index 000000000..21053b6e4 --- /dev/null +++ b/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java @@ -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 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, String> asPojoMessage = catalog.lookup("asPOJOMessage"); + System.out.println(asPojoMessage.apply(binaryCloudEventMessage)); + + Function, String> asPojo = catalog.lookup("asPOJO"); + System.out.println(asPojo.apply(binaryCloudEventMessage)); + + Function, String> asString = catalog.lookup("asString"); + System.out.println(asString.apply(binaryCloudEventMessage)); + + Function, String> asStringMessage = catalog.lookup("asStringMessage"); + System.out.println(asStringMessage.apply(binaryCloudEventMessage)); + } +} diff --git a/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationStreamTests.java b/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationStreamTests.java deleted file mode 100644 index b976f3ff6..000000000 --- a/spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationStreamTests.java +++ /dev/null @@ -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 { - - -}