Skip to content

Commit 3291863

Browse files
committed
GH-422 Add test and documentation for pure function interaction
1 parent 505df32 commit 3291863

File tree

4 files changed

+100
-34
lines changed

4 files changed

+100
-34
lines changed

spring-cloud-function-samples/function-sample-cloudevent/README.adoc

+30-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,25 @@ The example provides dependencies and instructions to demonstrate several distin
1515
The POM file defines all the necessary dependency in a segregated way, so you can choose the one you're interested in.
1616

1717
### Direct function invocation
18-
TBD
18+
19+
By looking up user declared functions in `FunctionCatalog` you can interact (i.e., for testing purposes) with functions directly
20+
while enjoying all the features of _spring-cloud-function_ such as transparent type conversion, function composition and more.
21+
22+
[source, java]
23+
----
24+
Message<String> binaryCloudEventMessage = MessageBuilder
25+
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
26+
.setHeader("ce-specversion", "1.0")
27+
.setHeader("ce-type", "com.example.springevent")
28+
.setHeader("ce-source", "spring.io/spring-event")
29+
.setHeader("ce-id", "123-456-9876-09")
30+
.build();
31+
Function<Message<String>, String> asPojoMessage = catalog.lookup("asPOJOMessage");
32+
System.out.println(asPojoMessage.apply(binaryCloudEventMessage));
33+
----
34+
35+
The test case link:src/test/java/io/spring/cloudevent/CloudeventDemoApplicationFunctionTests.java[CloudeventDemoApplicationFunctionTests]
36+
provides a good example on how to accomplish this.
1937

2038
### Function as a REST endpoint
2139

@@ -114,6 +132,17 @@ entire structure of Cloud Event message as payload (see the screenshot below)._
114132

115133
image::images\rabbit-send-structured.png[structured,700,700]
116134

135+
You can follow similar approach with Apache Kafka or any other binder. All you need is bring a required binder dependency.
136+
For example for Apache Kafka
137+
[source, xml]
138+
----
139+
<dependency>
140+
<groupId>org.springframework.cloud</groupId>
141+
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
142+
<version>3.1.0-SNAPSHOT</version>
143+
</dependency>
144+
----
145+
117146
### Function invocation via RSocket
118147

119148
TBD

spring-cloud-function-samples/function-sample-cloudevent/pom.xml

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<version>3.1.0-SNAPSHOT</version>
3636
</dependency>
3737
<!-- end REST -->
38-
38+
3939
<!-- RSocket - only needed if you intend to invoke via RSocket -->
4040
<!-- <dependency> -->
4141
<!-- <groupId>org.springframework.cloud</groupId> -->
@@ -45,11 +45,11 @@
4545
<!-- end RSocket -->
4646

4747
<!-- RabbitMQ - only needed if you intend to invoke via RabbitMQ -->
48-
<dependency>
49-
<groupId>org.springframework.cloud</groupId>
50-
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
51-
<version>3.1.0-SNAPSHOT</version>
52-
</dependency>
48+
<!-- <dependency> -->
49+
<!-- <groupId>org.springframework.cloud</groupId> -->
50+
<!-- <artifactId>spring-cloud-stream-binder-rabbit</artifactId> -->
51+
<!-- <version>3.1.0-SNAPSHOT</version> -->
52+
<!-- </dependency> -->
5353
<!-- end RabbitMQ -->
5454

5555
<!-- Kafka - only needed if you intend to invoke via RabbitMQ -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2020-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.cloudevent;
18+
19+
import java.util.function.Function;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.boot.SpringApplication;
23+
import org.springframework.cloud.function.context.FunctionCatalog;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.messaging.Message;
26+
import org.springframework.messaging.support.MessageBuilder;
27+
28+
/**
29+
*
30+
* @author Oleg Zhurakousky
31+
*
32+
*/
33+
public class CloudeventDemoApplicationFunctionTests {
34+
35+
@Test
36+
public void demoPureFunctionInvocation() {
37+
ApplicationContext context = SpringApplication.run(CloudeventDemoApplication.class);
38+
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
39+
Message<String> binaryCloudEventMessage = MessageBuilder
40+
.withPayload("{\"releaseDate\":\"24-03-2004\", \"releaseName\":\"Spring Framework\", \"version\":\"1.0\"}")
41+
.setHeader("ce-specversion", "1.0")
42+
.setHeader("ce-type", "com.example.springevent")
43+
.setHeader("ce-source", "spring.io/spring-event")
44+
.setHeader("ce-id", "123-456-9876-09")
45+
.build();
46+
47+
/*
48+
* NOTE how it makes no difference what the actual function signature
49+
* is (see `asPOJOMessage` and `asPOJO` specifically). Type conversion will happen
50+
* inside spring-cloud-function.
51+
*/
52+
Function<Message<String>, String> asPojoMessage = catalog.lookup("asPOJOMessage");
53+
System.out.println(asPojoMessage.apply(binaryCloudEventMessage));
54+
55+
Function<Message<String>, String> asPojo = catalog.lookup("asPOJO");
56+
System.out.println(asPojo.apply(binaryCloudEventMessage));
57+
58+
Function<Message<String>, String> asString = catalog.lookup("asString");
59+
System.out.println(asString.apply(binaryCloudEventMessage));
60+
61+
Function<Message<String>, String> asStringMessage = catalog.lookup("asStringMessage");
62+
System.out.println(asStringMessage.apply(binaryCloudEventMessage));
63+
}
64+
}

spring-cloud-function-samples/function-sample-cloudevent/src/test/java/io/spring/cloudevent/CloudeventDemoApplicationStreamTests.java

-27
This file was deleted.

0 commit comments

Comments
 (0)