Fixes #621 - updated Documentation, test and sample code
This commit is contained in:
committed by
Oleg Zhurakousky
parent
ffbc7fec78
commit
4a79072b53
@@ -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<String, String> 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<Foo, Bar> 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 `<function url>/api/<function name>`
|
||||
----
|
||||
# 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://<azure-function-url-from-the-log>/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://<azure-function-url-from-the-log>/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.
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-dependencies</artifactId>
|
||||
<version>3.1.0-SNAPSHOT</version>
|
||||
<version>3.1.0.BUILD-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -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<String, String> uppercase(ExecutionContext context) {
|
||||
return value -> {
|
||||
context.getLogger().info("Uppercasing " + value);
|
||||
return value.toUpperCase();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
try {
|
||||
Map<String, String> 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");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String, String> 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\"}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user