Refactor the azure adapter tests
This commit is contained in:
@@ -61,6 +61,13 @@
|
||||
<version>${azure.functions.java.spi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>2.0.7</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
||||
@@ -30,6 +30,7 @@ import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.function.adapter.azure.helper.TestExecutionContext;
|
||||
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -31,6 +31,7 @@ import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.function.adapter.azure.helper.TestExecutionContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
@@ -26,14 +26,12 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.microsoft.azure.functions.HttpMethod;
|
||||
import com.microsoft.azure.functions.HttpRequestMessage;
|
||||
import com.microsoft.azure.functions.HttpResponseMessage;
|
||||
import com.microsoft.azure.functions.HttpResponseMessage.Builder;
|
||||
import com.microsoft.azure.functions.HttpStatus;
|
||||
import com.microsoft.azure.functions.HttpStatusType;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.cloud.function.adapter.azure.helper.HttpRequestMessageStub;
|
||||
import org.springframework.cloud.function.adapter.azure.helper.TestExecutionContext;
|
||||
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -159,130 +157,4 @@ public class HttpFunctionInvokerTests {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class HttpRequestMessageStub<I> implements HttpRequestMessage<I> {
|
||||
|
||||
private URI uri;
|
||||
private HttpMethod httpMethod;
|
||||
private Map<String, String> headers;
|
||||
private Map<String, String> queryParameters;
|
||||
private I body;
|
||||
|
||||
public void setUri(URI uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public void setHttpMethod(HttpMethod httpMethod) {
|
||||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
public void setHeaders(Map<String, String> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public void setQueryParameters(Map<String, String> queryParameters) {
|
||||
this.queryParameters = queryParameters;
|
||||
}
|
||||
|
||||
public void setBody(I body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpMethod getHttpMethod() {
|
||||
return this.httpMethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getQueryParameters() {
|
||||
return this.queryParameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public I getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpResponseMessage.Builder createResponseBuilder(HttpStatusType status) {
|
||||
return new BuilderStub().status(status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder createResponseBuilder(HttpStatus status) {
|
||||
return new BuilderStub().status(status);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class BuilderStub implements Builder {
|
||||
|
||||
private HttpStatusType status;
|
||||
private Map<String, String> headers = new HashMap<>();
|
||||
private Object body;
|
||||
|
||||
@Override
|
||||
public Builder status(HttpStatusType status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder header(String key, String value) {
|
||||
headers.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder body(Object body) {
|
||||
this.body = body;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpResponseMessage build() {
|
||||
return new HttpResponseMessageStub(this.status, this.headers, this.body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class HttpResponseMessageStub implements HttpResponseMessage {
|
||||
|
||||
private HttpStatusType status;
|
||||
private Map<String, String> headers = new HashMap<>();
|
||||
private Object body;
|
||||
|
||||
HttpResponseMessageStub(HttpStatusType status, Map<String, String> headers,
|
||||
Object body) {
|
||||
this.status = status;
|
||||
this.headers = headers;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpStatusType getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeader(String key) {
|
||||
return this.headers.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.springframework.cloud.function.adapter.azure.helper;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.microsoft.azure.functions.HttpResponseMessage;
|
||||
import com.microsoft.azure.functions.HttpStatusType;
|
||||
import com.microsoft.azure.functions.HttpResponseMessage.Builder;
|
||||
|
||||
public class BuilderStub implements Builder {
|
||||
|
||||
private HttpStatusType status;
|
||||
private Map<String, String> headers = new HashMap<>();
|
||||
private Object body;
|
||||
|
||||
@Override
|
||||
public Builder status(HttpStatusType status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder header(String key, String value) {
|
||||
headers.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder body(Object body) {
|
||||
this.body = body;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpResponseMessage build() {
|
||||
return new HttpResponseMessageStub(this.status, this.headers, this.body);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.springframework.cloud.function.adapter.azure.helper;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import com.microsoft.azure.functions.HttpMethod;
|
||||
import com.microsoft.azure.functions.HttpRequestMessage;
|
||||
import com.microsoft.azure.functions.HttpResponseMessage;
|
||||
import com.microsoft.azure.functions.HttpResponseMessage.Builder;
|
||||
import com.microsoft.azure.functions.HttpStatus;
|
||||
import com.microsoft.azure.functions.HttpStatusType;
|
||||
|
||||
public class HttpRequestMessageStub<I> implements HttpRequestMessage<I> {
|
||||
|
||||
private URI uri;
|
||||
private HttpMethod httpMethod;
|
||||
private Map<String, String> headers;
|
||||
private Map<String, String> queryParameters;
|
||||
private I body;
|
||||
|
||||
public void setUri(URI uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public void setHttpMethod(HttpMethod httpMethod) {
|
||||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
public void setHeaders(Map<String, String> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public void setQueryParameters(Map<String, String> queryParameters) {
|
||||
this.queryParameters = queryParameters;
|
||||
}
|
||||
|
||||
public void setBody(I body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpMethod getHttpMethod() {
|
||||
return this.httpMethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getQueryParameters() {
|
||||
return this.queryParameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public I getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpResponseMessage.Builder createResponseBuilder(HttpStatusType status) {
|
||||
return new BuilderStub().status(status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder createResponseBuilder(HttpStatus status) {
|
||||
return new BuilderStub().status(status);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.springframework.cloud.function.adapter.azure.helper;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.microsoft.azure.functions.HttpResponseMessage;
|
||||
import com.microsoft.azure.functions.HttpStatusType;
|
||||
|
||||
public class HttpResponseMessageStub implements HttpResponseMessage {
|
||||
|
||||
private HttpStatusType status;
|
||||
private Map<String, String> headers = new HashMap<>();
|
||||
private Object body;
|
||||
|
||||
public HttpResponseMessageStub(HttpStatusType status, Map<String, String> headers,
|
||||
Object body) {
|
||||
this.status = status;
|
||||
this.headers = headers;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpStatusType getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeader(String key) {
|
||||
return this.headers.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.function.adapter.azure;
|
||||
package org.springframework.cloud.function.adapter.azure.helper;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
@@ -16,11 +16,8 @@
|
||||
|
||||
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 java.util.logging.Logger;
|
||||
|
||||
import com.microsoft.azure.functions.ExecutionContext;
|
||||
import com.microsoft.azure.functions.HttpMethod;
|
||||
@@ -33,123 +30,84 @@ 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.cloud.function.adapter.azure.AzureFunctionInstanceInjector;
|
||||
import org.springframework.cloud.function.adapter.azure.AzureFunctionUtil;
|
||||
import org.springframework.cloud.function.adapter.azure.HttpFunctionInvokerTests;
|
||||
import org.springframework.cloud.function.adapter.azure.helper.HttpRequestMessageStub;
|
||||
import org.springframework.cloud.function.adapter.azure.helper.TestExecutionContext;
|
||||
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;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* This is an example JUnit test for Azure Adapter.
|
||||
*
|
||||
* @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";
|
||||
}
|
||||
};
|
||||
static ExecutionContext TEST_EXECUTION_CONTEXT = new TestExecutionContext("hello");
|
||||
|
||||
@Test
|
||||
public void testFunctionInjector() throws Exception {
|
||||
|
||||
FunctionInstanceInjector injector = initializeFunctionInstanceInjector();
|
||||
Assertions.assertThat(injector).isNotNull();
|
||||
Assertions.assertThat(injector).isInstanceOf(AzureFunctionInstanceInjector.class);
|
||||
// The SpringBootApplication class
|
||||
System.setProperty("MAIN_CLASS", MySpringConfig.class.getName());
|
||||
|
||||
System.setProperty("MAIN_CLASS", MyMainConfig.class.getName());
|
||||
FunctionInstanceInjector injector = new AzureFunctionInstanceInjector();
|
||||
|
||||
MyAzureTestFunction functionInstance = injector.getInstance(MyAzureTestFunction.class);
|
||||
// Emulates the Azure Function Java Runtime DI call
|
||||
MyAzureFunction azureFunction = injector.getInstance(MyAzureFunction.class);
|
||||
|
||||
HttpFunctionInvokerTests.HttpRequestMessageStub<Optional<String>> request = new HttpFunctionInvokerTests.HttpRequestMessageStub<Optional<String>>();
|
||||
Assertions.assertThat(azureFunction).isNotNull();
|
||||
|
||||
request.setBody(Optional.of("test"));
|
||||
HttpRequestMessageStub<Optional<String>> requestStub = new HttpRequestMessageStub<Optional<String>>();
|
||||
|
||||
String result = functionInstance.execute(request, executionContext);
|
||||
requestStub.setBody(Optional.of("payload"));
|
||||
|
||||
Assertions.assertThat(result).isEqualTo("TEST");
|
||||
String result = azureFunction.execute(requestStub, TEST_EXECUTION_CONTEXT);
|
||||
|
||||
Assertions.assertThat(functionInstance).isNotNull();
|
||||
Assertions.assertThat(functionInstance).isInstanceOf(MyAzureTestFunction.class);
|
||||
}
|
||||
Assertions.assertThat(result).isEqualTo("PAYLOAD");
|
||||
|
||||
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 = 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();
|
||||
};
|
||||
|
||||
}
|
||||
Assertions.assertThat(azureFunction).isInstanceOf(MyAzureFunction.class);
|
||||
}
|
||||
|
||||
@Component
|
||||
public static class MyAzureTestFunction {
|
||||
public static class MyAzureFunction {
|
||||
|
||||
@Autowired
|
||||
private Function<Message<String>, String> uppercase;
|
||||
|
||||
@FunctionName("ditest")
|
||||
@FunctionName("hello")
|
||||
public String execute(
|
||||
@HttpTrigger(name = "req", methods = { HttpMethod.GET,
|
||||
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
|
||||
@HttpTrigger(name = "req", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
|
||||
ExecutionContext context) {
|
||||
|
||||
Message<String> enhancedRequest = (Message<String>) AzureFunctionUtil.enhanceInputIfNecessary(
|
||||
request.getBody().get(),
|
||||
context);
|
||||
|
||||
return uppercase.apply(enhancedRequest);
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public static class MySpringConfig {
|
||||
|
||||
@Bean
|
||||
public Function<Message<String>, String> uppercaseBean() {
|
||||
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();
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 2022-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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 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.cloud.function.adapter.azure.AzureFunctionInstanceInjector;
|
||||
import org.springframework.cloud.function.adapter.azure.AzureFunctionUtil;
|
||||
import org.springframework.cloud.function.adapter.azure.helper.HttpRequestMessageStub;
|
||||
import org.springframework.cloud.function.adapter.azure.helper.TestExecutionContext;
|
||||
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;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Christian Tzolov
|
||||
*/
|
||||
public class FunctionInstanceInjectorServiceLoadingTest {
|
||||
|
||||
static ExecutionContext executionContext = new TestExecutionContext("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);
|
||||
|
||||
HttpRequestMessageStub<Optional<String>> request = new 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 {
|
||||
throw new IllegalStateException("Failed to resolve the AzureFunctionInstanceInjector as java service!");
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Thread.currentThread().setContextClassLoader(prevContextClassLoader);
|
||||
}
|
||||
return functionInstanceInjector;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(excludeFilters = { @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();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Component
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<log4j:configuration debug="false">
|
||||
<!--Console appender -->
|
||||
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<root>
|
||||
<level value="DEBUG"/>
|
||||
<appender-ref ref="stdout"/>
|
||||
</root>
|
||||
</log4j:configuration>
|
||||
Reference in New Issue
Block a user