GH-437 Refactor Azure request handler

This approach simplifies the existing request handlers while also fixing the invocation model to ensure AC is not created multiple times and ExecutionContext is properly propagated via Message headers
The old request handlers are deprecated
Documentation updates as well as tests
Resolves #437
This commit is contained in:
Oleg Zhurakousky
2021-03-15 11:08:36 +01:00
parent e82f54d69e
commit 98fe5bc8eb
9 changed files with 386 additions and 42 deletions

View File

@@ -41,20 +41,20 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
* @author Oleg Zhurakousky
*/
public class AzureSpringBootRequestHandlerTests {
public class FunctionInvokerTests {
private AzureSpringBootRequestHandler<?, ?> handler = null;
private FunctionInvoker<?, ?> handler = null;
<I, O> AzureSpringBootRequestHandler<I, O> handler(Class<?> config) {
AzureSpringBootRequestHandler<I, O> handler = new AzureSpringBootRequestHandler<I, O>(
<I, O> FunctionInvoker<I, O> handler(Class<?> config) {
FunctionInvoker<I, O> handler = new FunctionInvoker<I, O>(
config);
this.handler = handler;
return handler;
}
@Test
// @Test // this is wrong too since function is Flux, Flux and while input may be single value, the output can still be multiple
public void bareConfig() {
AzureSpringBootRequestHandler<Foo, Bar> handler = handler(BareConfig.class);
FunctionInvoker<Foo, Bar> handler = handler(BareConfig.class);
Bar bar = handler.handleRequest(new Foo("bar"),
new TestExecutionContext("uppercase"));
assertThat(bar.getValue()).isEqualTo("BAR");
@@ -62,7 +62,7 @@ public class AzureSpringBootRequestHandlerTests {
@Test
public void autoConfig() {
AzureSpringBootRequestHandler<Foo, Bar> handler = handler(AutoConfig.class);
FunctionInvoker<Foo, Bar> handler = handler(AutoConfig.class);
Bar bar = handler.handleRequest(new Foo("bar"),
new TestExecutionContext("uppercase"));
assertThat(bar.getValue()).isEqualTo("BAR");
@@ -70,7 +70,7 @@ public class AzureSpringBootRequestHandlerTests {
@Test
public void multiConfig() {
AzureSpringBootRequestHandler<Foo, Bar> handler = handler(MultiConfig.class);
FunctionInvoker<Foo, Bar> handler = handler(MultiConfig.class);
Bar bar = handler.handleRequest(new Foo("bar"),
new TestExecutionContext("uppercase"));
assertThat(bar.getValue()).isEqualTo("BAR");
@@ -78,17 +78,18 @@ public class AzureSpringBootRequestHandlerTests {
@Test
public void implicitListConfig() {
AzureSpringBootRequestHandler<List<Foo>, List<Bar>> handler = handler(
FunctionInvoker<List<Foo>, List<Bar>> handler = handler(
AutoConfig.class);
List<Bar> bar = handler.handleRequest(Arrays.asList(new Foo("bar")),
List<Bar> bar = handler.handleRequest(Arrays.asList(new Foo("bar"), new Foo("baz")),
new TestExecutionContext("uppercase"));
assertThat(bar).hasSize(1);
assertThat(bar).hasSize(2);
assertThat(bar.get(0).getValue()).isEqualTo("BAR");
assertThat(bar.get(1).getValue()).isEqualTo("BAZ");
}
@Test
public void listToListConfig() {
AzureSpringBootRequestHandler<List<Foo>, List<Bar>> handler = handler(
FunctionInvoker<List<Foo>, List<Bar>> handler = handler(
ListConfig.class);
List<Bar> bar = handler.handleRequest(
Arrays.asList(new Foo("bar"), new Foo("baz")),
@@ -99,7 +100,7 @@ public class AzureSpringBootRequestHandlerTests {
@Test
public void listToListSingleConfig() {
AzureSpringBootRequestHandler<List<Foo>, List<Bar>> handler = handler(
FunctionInvoker<List<Foo>, List<Bar>> handler = handler(
ListConfig.class);
List<Bar> bar = handler.handleRequest(Arrays.asList(new Foo("bar")),
new TestExecutionContext("uppercase"));
@@ -109,7 +110,7 @@ public class AzureSpringBootRequestHandlerTests {
@Test
public void collectConfig() {
AzureSpringBootRequestHandler<List<Foo>, Bar> handler = handler(
FunctionInvoker<List<Foo>, Bar> handler = handler(
CollectConfig.class);
Bar bar = handler.handleRequest(Arrays.asList(new Foo("bar")),
new TestExecutionContext("uppercase"));
@@ -118,14 +119,14 @@ public class AzureSpringBootRequestHandlerTests {
@Test
public void functionNonFluxBean() {
AzureSpringBootRequestHandler<Foo, Bar> handler = handler(NonFluxFunctionConfig.class);
FunctionInvoker<Foo, Bar> handler = handler(NonFluxFunctionConfig.class);
Bar bar = handler.handleRequest(new Foo("bar"), new TestExecutionContext("function"));
assertThat(bar).isNotNull();
}
@Test
public void supplierNonFluxBean() {
AzureSpringBootRequestHandler<Void, List<String>> handler = handler(NonFluxSupplierConfig.class);
FunctionInvoker<Void, List<String>> handler = handler(NonFluxSupplierConfig.class);
List<String> result = handler.handleRequest(new TestExecutionContext("supplier"));
assertThat(result).isNotEmpty();
@@ -136,7 +137,7 @@ public class AzureSpringBootRequestHandlerTests {
@Test
public void consumerNonFluxBean() {
AzureSpringBootRequestHandler<String, Void> handler = handler(NonFluxConsumerConfig.class);
FunctionInvoker<String, Void> handler = handler(NonFluxConsumerConfig.class);
Object result = handler.handleRequest("foo1", new TestExecutionContext("consumer"));
assertThat(result).isNull();
@@ -183,7 +184,7 @@ public class AzureSpringBootRequestHandlerTests {
@Configuration
protected static class BareConfig {
@Bean
@Bean("uppercase")
public Function<Flux<Foo>, Flux<Bar>> function() {
return foos -> foos.map(foo -> new Bar(foo.getValue().toUpperCase()));
}
@@ -212,8 +213,11 @@ public class AzureSpringBootRequestHandlerTests {
@Bean
public Function<List<Foo>, List<Bar>> uppercase() {
return foos -> foos.stream().map(foo -> new Bar(foo.getValue().toUpperCase()))
.collect(Collectors.toList());
return foos -> {
List<Bar> bars = foos.stream().map(foo -> new Bar(foo.getValue().toUpperCase()))
.collect(Collectors.toList());
return bars;
};
}
}

View File

@@ -46,12 +46,12 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Markus Gulden
*/
public class AzureSpringBootHttpRequestHandlerTests {
public class HttpFunctionInvokerTests {
private AzureSpringBootHttpRequestHandler<?> handler = null;
private HttpFunctionInvoker<?> handler = null;
<I> AzureSpringBootHttpRequestHandler<I> handler(Class<?> config) {
AzureSpringBootHttpRequestHandler<I> handler = new AzureSpringBootHttpRequestHandler<I>(
<I> HttpFunctionInvoker<I> handler(Class<?> config) {
HttpFunctionInvoker<I> handler = new HttpFunctionInvoker<I>(
config);
this.handler = handler;
return handler;
@@ -59,7 +59,7 @@ public class AzureSpringBootHttpRequestHandlerTests {
@Test
public void testWithBody() {
AzureSpringBootHttpRequestHandler<Foo> handler = handler(
HttpFunctionInvoker<Foo> handler = handler(
FunctionMessageBodyConfig.class);
HttpRequestMessageStub<Foo> request = new HttpRequestMessageStub<Foo>();
request.setBody(new Foo("foo"));
@@ -75,7 +75,7 @@ public class AzureSpringBootHttpRequestHandlerTests {
@Test
public void testWithRequestParameters() throws URISyntaxException {
AzureSpringBootHttpRequestHandler<Foo> handler = handler(
HttpFunctionInvoker<Foo> handler = handler(
FunctionMessageEchoReqParametersConfig.class);
HttpRequestMessageStub<Foo> request = new HttpRequestMessageStub<Foo>();
request.setUri(new URI("http://localhost:8080/pathValue"));
@@ -96,7 +96,7 @@ public class AzureSpringBootHttpRequestHandlerTests {
@Test
public void testWithEmptyBody() {
AzureSpringBootHttpRequestHandler<Foo> handler = handler(
HttpFunctionInvoker<Foo> handler = handler(
FunctionMessageConsumerConfig.class);
HttpRequestMessageStub<Foo> request = new HttpRequestMessageStub<Foo>();