diff --git a/spring-cloud-function-samples/function-sample-azure/README.adoc b/spring-cloud-function-samples/function-sample-azure/README.adoc index 23a6d1dd0..a3fa89466 100644 --- a/spring-cloud-function-samples/function-sample-azure/README.adoc +++ b/spring-cloud-function-samples/function-sample-azure/README.adoc @@ -1,35 +1,102 @@ -You can run this sample locally, just like the other Spring Cloud Function samples: +You can run this Azure function locally, similar to other Spring Cloud Function samples, however this time by using the Azure Maven plugin, as the Microsoft Azure functions execution context must be available. ---- -mvn spring-boot:run +# Build and package +$ mvn clean package + +# Previously, for other examples +$ mvn spring-boot:run + +# For Azure Functions +$ mvn clean package azure-functions:run + +or + +$ mvn azure-functions:run ---- -and `curl -H "Content-Type: application/json" localhost:7071/uppercase -d '{"value": "hello foobar"}'`. +The `uppercase` function takes `Function uppercase()` and it's input is JSON, therefore we need to +provide the appropriate content-type (in this case `application/json`). The function iterates then over each element and returns its `uppercase` mapped value. -Given that our function takes POJO `Function uppercase()` and it's input is JSON we need to -provide the appropriate content-type (in this case `application/json`). +Test the function using cURL or HTTPie and notice that the URL is formed by concatenating `/api/` +---- +# testing with cURL +$ curl -H "Content-Type: application/json" localhost:7071/api/uppercase -d '{"greeting": "hello", "name": "your name"}' + +# testing with HTTPie +$ http POST localhost:7071/api/uppercase greeting=hello name='your name' + +# result +{ + "greeting": "HELLO", + "name": "YOUR NAME" +} +---- To run locally on top of Azure Functions, and to deploy to your live Azure environment, you will need the Azure Functions Core Tools installed along with the Azure CLI (see https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven for more details). -Run Azure Function locally: - ----- -mvn azure-functions:run ----- - -To deploy the function on your live Azure environment: - +To deploy the function to your live Azure environment, including an automatic provisioning of an HTTPTrigger for the function: ---- +# login to Azure from the CLI $ az login + +# deploy the function $ mvn azure-functions:deploy + +[INFO] Authenticate with Azure CLI 2.0 +[INFO] The specified function app does not exist. Creating a new function app... +[INFO] Successfully created the function app: function-sample-azure +[INFO] Trying to deploy the function app... +[INFO] Trying to deploy artifact to function-sample-azure... +[INFO] Successfully deployed the artifact to https://function-sample-azure.azurewebsites.net +[INFO] Successfully deployed the function app at https://function-sample-azure.azurewebsites.net +[INFO] ------------------------------------------------------------------------ +[INFO] BUILD SUCCESS +[INFO] ------------------------------------------------------------------------ + +# Note: +# the function URL is: https://function-sample-azure.azurewebsites.net +# the function can be accessed at: https://function-sample-azure.azurewebsites.net/api/uppercase ---- -On another terminal try this: `curl https:///api/uppercase -d '{"value": "hello foobar!"}'`. Please ensure that you use the right URL for the function above. Alternatively you can test the function in the Azure Dashboard UI (click on the function name, go to the right hand side and click "Test" and to the bottom right, "Run"). - -The input type for the function in the Azure sample is a Foo with a single property called "value". So you need this to test it with something like below: - +On another terminal try this: ---- +# testing +curl https:///api/uppercase -d '{"greeting": "hello", "name": "your name"}' + +# testing with cURL +$ curl -H "Content-Type: application/json" https://function-sample-azure.azurewebsites.net/api/uppercase -d '{"greeting": "hello", "name": "your name"}' + +# testing with HTTPie +$ http POST https://function-sample-azure.azurewebsites.net/api/uppercase greeting=hello name='your name' + +# result { - "value": "foobar" + "greeting": "HELLO", + "name": "YOUR NAME" } ---- + +Please ensure that you use the right URL for the function above. + +Alternatively you can test the function in the Azure Dashboard UI: + +* click on the Dashboard +* click on the function app `function-sample-azure` +* click on the left nav `Functions` and click the function name `uppercase` +* click on the left nav `Code and Test` and at the top of the page `Test/Run` +* In the body of the request, on the right-hand side, paste the same example we have used above: +---- +{ + "greeting": "hello", + "name": "your name" +} + +# observe the HTTP response content +{ + "greeting": "HELLO", + "name": "YOUR NAME" +} +---- + +Please note that the Dashhboard provides by default information on Function Execution Count, Memory Consumption and Execution Time. diff --git a/spring-cloud-function-samples/function-sample-azure/pom.xml b/spring-cloud-function-samples/function-sample-azure/pom.xml index 4cd53dbe7..20d312a20 100644 --- a/spring-cloud-function-samples/function-sample-azure/pom.xml +++ b/spring-cloud-function-samples/function-sample-azure/pom.xml @@ -54,7 +54,7 @@ org.springframework.cloud spring-cloud-function-dependencies - 3.1.0-SNAPSHOT + 3.1.0.BUILD-SNAPSHOT pom import diff --git a/spring-cloud-function-samples/function-sample-azure/src/main/java/example/Config.java b/spring-cloud-function-samples/function-sample-azure/src/main/java/example/Config.java index 57c856b65..6aaaad99d 100644 --- a/spring-cloud-function-samples/function-sample-azure/src/main/java/example/Config.java +++ b/spring-cloud-function-samples/function-sample-azure/src/main/java/example/Config.java @@ -16,8 +16,11 @@ package example; +import java.io.IOException; +import java.util.Map; import java.util.function.Function; +import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @@ -34,8 +37,24 @@ public class Config { @Bean public Function uppercase(ExecutionContext context) { return value -> { - context.getLogger().info("Uppercasing " + value); - return value.toUpperCase(); + ObjectMapper mapper = new ObjectMapper(); + + try { + Map map = mapper.readValue(value, Map.class); + + if(map != null) + map.forEach((k, v) -> map.put(k, v != null ? v.toUpperCase() : null)); + + if(context != null) + context.getLogger().info(new StringBuilder().append("Function: ").append(context.getFunctionName()).append(" is uppercasing ").append(value.toString()).toString()); + + return mapper.writeValueAsString(map); + } catch (IOException e) { + if(context != null) + context.getLogger().severe("Function could not parse incoming request"); + + return ("Function error: - bad request"); + } }; } diff --git a/spring-cloud-function-samples/function-sample-azure/src/test/java/example/MapTests.java b/spring-cloud-function-samples/function-sample-azure/src/test/java/example/MapTests.java index 42fb1c416..b786d0f2b 100644 --- a/spring-cloud-function-samples/function-sample-azure/src/test/java/example/MapTests.java +++ b/spring-cloud-function-samples/function-sample-azure/src/test/java/example/MapTests.java @@ -16,12 +16,13 @@ package example; +import com.microsoft.azure.functions.ExecutionContext; import org.junit.jupiter.api.Test; +import java.util.logging.Logger; +import static org.assertj.core.api.Assertions.assertThat; import org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHandler; -import static org.assertj.core.api.Assertions.assertThat; - /** * @author Dave Syer * @@ -32,9 +33,25 @@ public class MapTests { public void start() throws Exception { AzureSpringBootRequestHandler handler = new AzureSpringBootRequestHandler<>( Config.class); - String result = handler.handleRequest("foo", null); - handler.close(); - assertThat(result).isEqualTo("FOO"); - } + ExecutionContext ec = new ExecutionContext() { + @Override + public Logger getLogger() { + return Logger.getAnonymousLogger(); + } + @Override + public String getInvocationId() { + return "id1"; + } + + @Override + public String getFunctionName() { + return "uppercase"; + } + }; + + String result = handler.handleRequest("{\"greeting\": \"hello\",\"name\": \"your_name\"}", ec); + handler.close(); + assertThat(result).isEqualTo("{\"greeting\":\"HELLO\",\"name\":\"YOUR_NAME\"}"); + } }