GH-434 Added generic FunctionInvoker for AWS

- Added generic FunctionInvoker capable of handling the request generically without requiring user to implemen specific AWS request handler

Resolves #434
This commit is contained in:
Oleg Zhurakousky
2019-12-05 19:28:54 +01:00
parent 0f38ea47b8
commit 52b0fdea50
15 changed files with 641 additions and 234 deletions

View File

@@ -26,7 +26,6 @@
<wrapper.version>1.0.17.RELEASE</wrapper.version>
<aws-lambda-events.version>2.0.2</aws-lambda-events.version>
<spring-cloud-function.version>3.0.1.BUILD-SNAPSHOT</spring-cloud-function.version>
<start-class>example.Config</start-class>
</properties>
<dependencies>
@@ -34,16 +33,18 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-webflux</artifactId>
<scope>provided</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.springframework.cloud</groupId> -->
<!-- <artifactId>spring-cloud-starter-function-webflux</artifactId> -->
<!-- <scope>provided</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>${aws-lambda-events.version}</version>
<scope>provided</scope>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>

View File

@@ -1,116 +0,0 @@
/*
* Copyright 2012-2019 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 example;
import java.util.function.Function;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionType;
import org.springframework.cloud.function.context.FunctionalSpringApplication;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.GenericApplicationContext;
// @checkstyle:off
@SpringBootApplication
@EnableConfigurationProperties(Properties.class)
public class Config implements ApplicationContextInitializer<GenericApplicationContext> {
private Properties props;
public Config() {
}
public Config(Properties props) {
this.props = props;
}
public static void main(String[] args) throws Exception {
FunctionalSpringApplication.run(Config.class, args);
}
@Bean
public Function<Foo, Bar> function() {
return value -> new Bar(
value.uppercase() + (this.props.getFoo() != null ? "-" + this.props
.getFoo() : ""));
}
@Override
public void initialize(GenericApplicationContext context) {
Properties properties = new Properties();
this.props = properties;
context.registerBean(Properties.class, () -> properties);
context.registerBean("function", FunctionRegistration.class,
() -> new FunctionRegistration<Function<Foo, Bar>>(function())
.type(FunctionType.from(Foo.class).to(Bar.class).getType()));
}
}
// @checkstyle:on
class Foo {
private String value;
Foo() {
}
Foo(String value) {
this.value = value;
}
public String lowercase() {
return this.value.toLowerCase();
}
public String uppercase() {
return this.value.toUpperCase();
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
class Bar {
private String value;
Bar() {
}
Bar(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@@ -0,0 +1,24 @@
package example;
import java.util.function.Function;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class FunctionConfiguration {
/*
* You need this main method or explicit <start-class>example.FunjctionConfiguration</start-class>
* in the POM to ensure boot plug-in makes the correct entry
*/
public static void main(String[] args) {
SpringApplication.run(FunctionConfiguration.class, args);
}
@Bean
public Function<String, String> uppercase() {
return value -> value.toUpperCase();
}
}

View File

@@ -1,27 +0,0 @@
/*
* Copyright 2012-2019 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 example;
import org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler;
/**
* @author Dave Syer
*
*/
public class Handler extends SpringBootRequestHandler<Foo, Bar> {
}

View File

@@ -1,38 +0,0 @@
/*
* Copyright 2012-2019 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 example;
import org.springframework.boot.context.properties.ConfigurationProperties;
// @checkstyle:off
@ConfigurationProperties("app")
public class Properties {
public String foo;
public String getFoo() {
return this.foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
// @checkstyle:on

View File

@@ -18,10 +18,6 @@ package example;
import org.junit.Test;
import org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
@@ -30,16 +26,6 @@ public class MapTests {
@Test
public void test() {
Bar result = new Config(new Properties()).function().apply(new Foo("foo"));
assertThat(result.getValue()).isEqualTo("FOO");
}
@Test
public void start() throws Exception {
SpringBootRequestHandler<Object, Object> handler = new SpringBootRequestHandler<>(
Config.class);
handler.handleRequest(new Foo("foo"), null);
handler.close();
}
}