Add support for injecting the ExecutionContext as metadata
This commit is contained in:
committed by
Oleg Zhurakousky
parent
d5bff8e7ee
commit
bb066b9b3f
@@ -160,7 +160,7 @@ public class HttpFunctionInvokerTests {
|
||||
|
||||
}
|
||||
|
||||
class HttpRequestMessageStub<I> implements HttpRequestMessage<I> {
|
||||
public static class HttpRequestMessageStub<I> implements HttpRequestMessage<I> {
|
||||
|
||||
private URI uri;
|
||||
private HttpMethod httpMethod;
|
||||
@@ -225,7 +225,7 @@ public class HttpFunctionInvokerTests {
|
||||
|
||||
}
|
||||
|
||||
class BuilderStub implements Builder {
|
||||
public static class BuilderStub implements Builder {
|
||||
|
||||
private HttpStatusType status;
|
||||
private Map<String, String> headers = new HashMap<>();
|
||||
@@ -256,7 +256,7 @@ public class HttpFunctionInvokerTests {
|
||||
|
||||
}
|
||||
|
||||
class HttpResponseMessageStub implements HttpResponseMessage {
|
||||
public static class HttpResponseMessageStub implements HttpResponseMessage {
|
||||
|
||||
private HttpStatusType status;
|
||||
private Map<String, String> headers = new HashMap<>();
|
||||
|
||||
@@ -14,103 +14,144 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.function.adapter.azure.injector;
|
||||
package org.springframework.cloud.function.adapter.azure.injector;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
import com.microsoft.azure.functions.HttpMethod;
|
||||
import com.microsoft.azure.functions.HttpRequestMessage;
|
||||
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
|
||||
import com.microsoft.azure.functions.annotation.FunctionName;
|
||||
import com.microsoft.azure.functions.annotation.HttpTrigger;
|
||||
import com.microsoft.azure.functions.spi.inject.FunctionInstanceInjector;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
|
||||
import org.springframework.boot.context.TypeExcludeFilter;
|
||||
import org.springframework.cloud.function.adapter.azure.AzureFunctionInstanceInjector;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
public class AzureFunctionInstanceInjectorTest {
|
||||
|
||||
@Test
|
||||
public void testFunctionInjector() throws Exception {
|
||||
|
||||
FunctionInstanceInjector injector = initializeFunctionInstanceInjector();
|
||||
Assertions.assertThat(injector).isNotNull();
|
||||
Assertions.assertThat(injector).isInstanceOf(AzureFunctionInstanceInjector.class);
|
||||
|
||||
System.setProperty("MAIN_CLASS", MyMainConfig.class.getName());
|
||||
|
||||
MyAzureTestFunction functionInstance = injector.getInstance(MyAzureTestFunction.class);
|
||||
Assertions.assertThat(functionInstance).isNotNull();
|
||||
Assertions.assertThat(functionInstance).isInstanceOf(MyAzureTestFunction.class);
|
||||
}
|
||||
|
||||
private static FunctionInstanceInjector initializeFunctionInstanceInjector() {
|
||||
FunctionInstanceInjector functionInstanceInjector = null;
|
||||
ClassLoader prevContextClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
try {
|
||||
Iterator<FunctionInstanceInjector> iterator = ServiceLoader.load(FunctionInstanceInjector.class).iterator();
|
||||
if (iterator.hasNext()) {
|
||||
functionInstanceInjector = iterator.next();
|
||||
if (iterator.hasNext()) {
|
||||
throw new RuntimeException(
|
||||
"Customer function app has multiple FunctionInstanceInjector implementations");
|
||||
}
|
||||
}
|
||||
else {
|
||||
functionInstanceInjector = new FunctionInstanceInjector() {
|
||||
@Override
|
||||
public <T> T getInstance(Class<T> functionClass) throws Exception {
|
||||
return functionClass.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Thread.currentThread().setContextClassLoader(prevContextClassLoader);
|
||||
}
|
||||
return functionInstanceInjector;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
|
||||
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
|
||||
public static class MyMainConfig {
|
||||
|
||||
@Bean
|
||||
public Function<String, String> uppercase() {
|
||||
return message -> message.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class MyAzureTestFunction {
|
||||
|
||||
@Autowired
|
||||
private Function<String, String> uppercase;
|
||||
|
||||
@FunctionName("ditest")
|
||||
public String execute(
|
||||
@HttpTrigger(name = "req", methods = { HttpMethod.GET,
|
||||
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
|
||||
ExecutionContext context) {
|
||||
|
||||
return uppercase.apply(request.getBody().get());
|
||||
}
|
||||
}
|
||||
}
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
import com.microsoft.azure.functions.HttpMethod;
|
||||
import com.microsoft.azure.functions.HttpRequestMessage;
|
||||
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
|
||||
import com.microsoft.azure.functions.annotation.FunctionName;
|
||||
import com.microsoft.azure.functions.annotation.HttpTrigger;
|
||||
import com.microsoft.azure.functions.spi.inject.FunctionInstanceInjector;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
|
||||
import org.springframework.boot.context.TypeExcludeFilter;
|
||||
import org.springframework.cloud.function.adapter.azure.AzureFunctionInstanceInjector;
|
||||
import org.springframework.cloud.function.adapter.azure.AzureFunctionUtil;
|
||||
import org.springframework.cloud.function.adapter.azure.HttpFunctionInvokerTests;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
public class AzureFunctionInstanceInjectorTest {
|
||||
|
||||
static ExecutionContext executionContext = new ExecutionContext() {
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
return Logger.getLogger(AzureFunctionInstanceInjectorTest.class.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInvocationId() {
|
||||
return "id1";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFunctionName() {
|
||||
return "hello";
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
public void testFunctionInjector() throws Exception {
|
||||
|
||||
FunctionInstanceInjector injector = initializeFunctionInstanceInjector();
|
||||
Assertions.assertThat(injector).isNotNull();
|
||||
Assertions.assertThat(injector).isInstanceOf(AzureFunctionInstanceInjector.class);
|
||||
|
||||
System.setProperty("MAIN_CLASS", MyMainConfig.class.getName());
|
||||
|
||||
MyAzureTestFunction functionInstance = injector.getInstance(MyAzureTestFunction.class);
|
||||
|
||||
HttpFunctionInvokerTests.HttpRequestMessageStub<Optional<String>> request = new HttpFunctionInvokerTests.HttpRequestMessageStub<Optional<String>>();
|
||||
|
||||
request.setBody(Optional.of("test"));
|
||||
|
||||
String result = functionInstance.execute(request, executionContext);
|
||||
|
||||
Assertions.assertThat(result).isEqualTo("TEST");
|
||||
|
||||
Assertions.assertThat(functionInstance).isNotNull();
|
||||
Assertions.assertThat(functionInstance).isInstanceOf(MyAzureTestFunction.class);
|
||||
}
|
||||
|
||||
private static FunctionInstanceInjector initializeFunctionInstanceInjector() {
|
||||
FunctionInstanceInjector functionInstanceInjector = null;
|
||||
ClassLoader prevContextClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
try {
|
||||
Iterator<FunctionInstanceInjector> iterator = ServiceLoader.load(FunctionInstanceInjector.class).iterator();
|
||||
if (iterator.hasNext()) {
|
||||
functionInstanceInjector = iterator.next();
|
||||
if (iterator.hasNext()) {
|
||||
throw new RuntimeException(
|
||||
"Customer function app has multiple FunctionInstanceInjector implementations");
|
||||
}
|
||||
}
|
||||
else {
|
||||
functionInstanceInjector = new FunctionInstanceInjector() {
|
||||
@Override
|
||||
public <T> T getInstance(Class<T> functionClass) throws Exception {
|
||||
return functionClass.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Thread.currentThread().setContextClassLoader(prevContextClassLoader);
|
||||
}
|
||||
return functionInstanceInjector;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
|
||||
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
|
||||
public static class MyMainConfig {
|
||||
|
||||
@Bean
|
||||
public Function<Message<String>, String> uppercase() {
|
||||
return message -> {
|
||||
ExecutionContext context = (ExecutionContext) message.getHeaders()
|
||||
.get(AzureFunctionUtil.EXECUTION_CONTEXT);
|
||||
Assertions.assertThat(context).isNotNull();
|
||||
Assertions.assertThat(context.getFunctionName()).isEqualTo("hello");
|
||||
return message.getPayload().toUpperCase();
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class MyAzureTestFunction {
|
||||
|
||||
@Autowired
|
||||
private Function<Message<String>, String> uppercase;
|
||||
|
||||
@FunctionName("ditest")
|
||||
public String execute(
|
||||
@HttpTrigger(name = "req", methods = { HttpMethod.GET,
|
||||
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
|
||||
ExecutionContext context) {
|
||||
|
||||
Message<String> enhancedRequest = (Message<String>) AzureFunctionUtil.enhanceInputIfNecessary(
|
||||
request.getBody().get(),
|
||||
context);
|
||||
return uppercase.apply(enhancedRequest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user