diff --git a/examples/README.md b/examples/README.md index 4995a246..0514760b 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,39 +1,39 @@ -# `fn` Java FDK Example Projects +# Fn Java FDK Example Projects In this directory you will find some example projects demonstrating different -features of the `fn` Java FDK: +features of the Fn Java FDK: * Plain java code support (`string-reverse`) * Functional testing of your functions (`regex-query` and `qr-code`) * Built-in JSON coercion (`regex-query`) * [InputEvent and OutputEvent](/docs/DataBinding.md) handling (`qr-code`) -## 1. String reverse +## (1) [String reverse](string-reverse/README.md) This function takes a string and returns the reverse of the string. -The `fn` Java FDK runtime will handle marshalling data into your -functions without the function having to have any knowledge of the FDK API. +The Fn Java FDK handles marshalling data into your +functions without the function having any knowledge of the FDK API. -## 2. Regex query +## (2) Regex query This function takes a JSON object containing a `text` field and a `regex` field and return a JSON object with a list of matches in the `matches` field. It demonstrates the builtin JSON support of the fn Java -wrapper (provided through Jackson) and how the platform handles serialisation +wrapper (provided through Jackson) and how the platform handles serialization of POJO return values. -## 3. QR Code gen +## (3) QR Code gen This function parses the query parameters of a GET request (through the `InputEvent` passed into the function) to generate a QR code. It demonstrates the `InputEvent` and `OutputEvent` interfaces which provide low level access to data entering the `fn` Java FDK. -## 4. Asynchronous thumbnails generation +## (4) Asynchronous thumbnails generation This example showcases the Fn Flow asynchronous execution API, by creating a workflow that takes an image and asynchronously generates three thumbnails for it, then uploads them to an object storage. -## 5. Gradle build +## (5) Gradle build This shows how to use Gradle to build functions using the Java FDK. diff --git a/examples/string-reverse/README.md b/examples/string-reverse/README.md index 4ae5f707..97eb79a0 100644 --- a/examples/string-reverse/README.md +++ b/examples/string-reverse/README.md @@ -1,71 +1,75 @@ -# Example oFunctions Project: String Reverse +# Example Java Function: String Reverse -This example provides an HTTP endpoint for reversing strings +This example provides an HTTP trigger endpoint for reversing strings. ```bash -$ curl -d "Hello, World!" "http://localhost:8080/r/string-reverse-app/reverse" -!dlroW ,olleH +$ curl -d "Hello World" http://localhost:8080/t/string-reverse-app/string-reverse +dlroW olleH ``` ## Demonstrated FDK features -This example uses **no** features of the fn Java FDK; in fact it doesn't have -a dependency on the fn Java FDK, it just plain old Java code. +This example uses **none** of the Fn Java FDK features, in fact it doesn't have +any dependency on the Fn Java FDK. It is just plain old Java code. ## Step by step -Ensure you have the functions server running using, this will host your -function and provide the HTTP endpoints that invoke it: +Ensure you have the Fn server running to host your +function and provide the HTTP endpoint that invokes it: -```bash +(1) Start the server + +```sh $ fn start ``` -Build the function locally +(2) Create an app for the function -```bash -$ fn build +```sh +$ fn create app string-reverse-app ``` -Create an app and route to host the function +(3) Deploy the function to your app from the `string-reverse` directory. -```bash -$ fn create app string-reverse-app -$ fn create route string-reverse-app /reverse +```sh +fn deploy --app string-reverse-app --local +``` + +(4) Invoke the function and reverse the string. + +```sh +echo "Hello World" | fn invoke string-reverse-app string-reverse +dlroW olleH ``` -Invoke the function to reverse a string +(5) Invoke the function using curl and a trigger to reverse a string. ```bash -$ curl -d "Hello, World!" "http://localhost:8080/r/string-reverse-app/reverse" -!dlroW ,olleH +$ curl -d "Hello World" http://localhost:8080/t/string-reverse-app/string-reverse +dlroW olleH ``` ## Code walkthrough The entrypoint to the function is specified in `func.yaml` in the `cmd` key. -It is set this to `com.fnproject.fn.examples.StringReverse::reverse`. The whole class +It is set this to `com.example.fn.StringReverse::reverse`. The whole class `StringReverse` is shown below: ```java -package com.fnproject.fn.examples; +package com.example.fn; public class StringReverse { public String reverse(String str) { - StringBuilder builder = new StringBuilder(); - for (int i = str.length() - 1; i >= 0; i--) { - builder.append(str.charAt(i)); - } - return builder.toString(); + return new StringBuilder(str).reverse().toString(); } } ``` -As you can see, this is plain java with no references to the fn API. The -fn Java FDK handles the marshalling of the HTTP body into the `str` +As you can see, this is plain java with no references to the Fn API. The +Fn Java FDK handles the marshalling of the HTTP body into the `str` parameter as well as the marshalling of the returned reversed string into the HTTP response body (see [Data Binding](/docs/DataBinding.md) for more information on how marshalling is performed). diff --git a/examples/string-reverse/func.yaml b/examples/string-reverse/func.yaml index 197eebe2..4d56d44f 100644 --- a/examples/string-reverse/func.yaml +++ b/examples/string-reverse/func.yaml @@ -1,7 +1,11 @@ -name: fn-example/string-reverse -version: 0.0.1 +schema_version: 20180708 +name: string-reverse +version: 0.0.2 runtime: java -timeout: 30 -format: http -cmd: com.fnproject.fn.examples.StringReverse::reverse -path: /reverse +build_image: fnproject/fn-java-fdk-build:jdk11-1.0.87 +run_image: fnproject/fn-java-fdk:jre11-1.0.87 +cmd: com.example.fn.StringReverse::reverse +triggers: +- name: string-reverse + type: http + source: /string-reverse diff --git a/examples/string-reverse/pom.xml b/examples/string-reverse/pom.xml index 296d9d7f..e41c77fa 100644 --- a/examples/string-reverse/pom.xml +++ b/examples/string-reverse/pom.xml @@ -3,23 +3,50 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - - UTF-8 - UTF-8 - + 1.0.87 - - com.fnproject.fn.examples + com.example.fn string-reverse - 1.0.0-SNAPSHOT + 1.0.0 + + + + fn-release-repo + https://dl.bintray.com/fnproject/fnproject + + true + + + false + + + + + com.fnproject.fn + api + ${fdk.version} + + + com.fnproject.fn + testing-core + ${fdk.version} + test + + + com.fnproject.fn + testing-junit4 + ${fdk.version} + test + junit junit 4.12 + test @@ -28,27 +55,20 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.0 + 3.3 - 1.8 - 1.8 + 8 + 8 - org.apache.maven.plugins - maven-deploy-plugin - 2.8.2 - - true - + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + false + - - - - fn-maven-releases - https://dl.bintray.com/fnproject/fnproject - - diff --git a/examples/string-reverse/src/main/java/com/example/fn/StringReverse.java b/examples/string-reverse/src/main/java/com/example/fn/StringReverse.java new file mode 100644 index 00000000..46d0f5fb --- /dev/null +++ b/examples/string-reverse/src/main/java/com/example/fn/StringReverse.java @@ -0,0 +1,7 @@ +package com.example.fn; + +public class StringReverse { + public String reverse(String str) { + return new StringBuilder(str).reverse().toString(); + } +} diff --git a/examples/string-reverse/src/main/java/com/fnproject/fn/examples/StringReverse.java b/examples/string-reverse/src/main/java/com/fnproject/fn/examples/StringReverse.java deleted file mode 100644 index e95ce08e..00000000 --- a/examples/string-reverse/src/main/java/com/fnproject/fn/examples/StringReverse.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.fnproject.fn.examples; - -public class StringReverse { - public String reverse(String str) { - StringBuilder builder = new StringBuilder(); - for (int i = str.length() - 1; i >= 0; i--) { - builder.append(str.charAt(i)); - } - return builder.toString(); - } -} diff --git a/examples/string-reverse/src/test/java/com/fnproject/examples/StringReverseTest.java b/examples/string-reverse/src/test/java/com/example/fn/testing/StringReverseTest.java similarity index 87% rename from examples/string-reverse/src/test/java/com/fnproject/examples/StringReverseTest.java rename to examples/string-reverse/src/test/java/com/example/fn/testing/StringReverseTest.java index 9f67dd2c..e9b59af0 100644 --- a/examples/string-reverse/src/test/java/com/fnproject/examples/StringReverseTest.java +++ b/examples/string-reverse/src/test/java/com/example/fn/testing/StringReverseTest.java @@ -1,6 +1,6 @@ -package com.fnproject.examples; +package com.example.fn.testing; -import com.fnproject.fn.examples.StringReverse; +import com.example.fn.StringReverse; import org.junit.Test; import static junit.framework.TestCase.assertEquals;