Fixes #621 - updated Documentation, test and sample code

This commit is contained in:
Dan Dobrin
2021-01-10 11:32:20 -05:00
committed by Oleg Zhurakousky
parent ffbc7fec78
commit 4a79072b53
4 changed files with 130 additions and 27 deletions

View File

@@ -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");
}
};
}

View File

@@ -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\"}");
}
}