Remove deprecated AWS classes

This commit is contained in:
Oleg Zhurakousky
2021-11-08 18:27:14 +01:00
parent 0e2334b154
commit afb06fcc2b
11 changed files with 3 additions and 1340 deletions

View File

@@ -1,273 +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 org.springframework.cloud.function.adapter.aws;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dimitry Declercq
* @author Markus Gulden
*/
public class SpringBootApiGatewayRequestHandlerTests {
private SpringBootApiGatewayRequestHandler handler;
@AfterEach
public void after() {
System.clearProperty("function.name");
}
@Test
public void supplierBean() {
System.setProperty("function.name", "supplier");
this.handler = new SpringBootApiGatewayRequestHandler(FunctionConfig.class);
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent();
Object output = this.handler.handleRequest(request, null);
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
.isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getBody())
.isEqualTo("\"hello!\"");
}
@Test
public void functionBean() {
System.setProperty("function.name", "function");
this.handler = new SpringBootApiGatewayRequestHandler(FunctionConfig.class);
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent();
request.setBody("{\"value\":\"foo\"}");
Object output = this.handler.handleRequest(request, null);
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
.isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getBody())
.isEqualTo("{\"value\":\"FOO\"}");
APIGatewayProxyRequestEvent bodyEncryptedRequest = new APIGatewayProxyRequestEvent();
bodyEncryptedRequest.setBody(
Base64.getEncoder().encodeToString("{\"value\":\"foo\"}".getBytes()));
bodyEncryptedRequest.setIsBase64Encoded(true);
output = this.handler.handleRequest(bodyEncryptedRequest, null);
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
.isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getBody())
.isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void consumerBean() {
System.setProperty("function.name", "consumer");
this.handler = new SpringBootApiGatewayRequestHandler(FunctionConfig.class);
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent();
request.setBody("\"strVal\":\"test for consumer\"");
Object output = this.handler.handleRequest(request, null);
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
.isEqualTo(200);
}
@Test
public void functionMessageBean() {
this.handler = new SpringBootApiGatewayRequestHandler(
FunctionMessageConfig.class);
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent();
request.setBody("{\"value\":\"foo\"}");
Object output = this.handler.handleRequest(request, null);
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
.isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("spring"))
.isEqualTo("cloud");
assertThat(((APIGatewayProxyResponseEvent) output).getBody())
.isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void functionMessageBeanWithRequestParameters() {
this.handler = new SpringBootApiGatewayRequestHandler(
FunctionMessageEchoReqParametersConfig.class);
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent();
request.setPathParameters(Collections.singletonMap("path", "pathValue"));
request.setQueryStringParameters(Collections.singletonMap("query", "queryValue"));
request.setHeaders(Collections.singletonMap("test-header", "headerValue"));
request.setHttpMethod("GET");
Object output = this.handler.handleRequest(request, null);
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
.isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("path"))
.isEqualTo("pathValue");
assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("query"))
.isEqualTo("queryValue");
assertThat(
((APIGatewayProxyResponseEvent) output).getHeaders().get("test-header"))
.isEqualTo("headerValue");
assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("httpMethod"))
.isEqualTo("GET");
assertThat(((APIGatewayProxyResponseEvent) output).getBody())
.isEqualTo("{\"value\":\"body\"}");
}
@Test
public void functionMessageBeanWithEmptyResponse() {
this.handler = new SpringBootApiGatewayRequestHandler(
FunctionMessageConsumerConfig.class);
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent();
Object output = this.handler.handleRequest(request, null);
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
.isEqualTo(200);
assertThat(((APIGatewayProxyResponseEvent) output).getBody()).isNull();
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
@Bean
public Consumer<String> consumer() {
return v -> System.out.println(v);
}
@Bean
public Supplier<String> supplier() {
return () -> "hello!";
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionMessageConfig {
@Bean
public Function<Message<Foo>, Message<Bar>> function() {
return (foo -> {
Map<String, Object> headers = Collections.singletonMap("spring", "cloud");
return new GenericMessage<>(
new Bar(foo.getPayload().getValue().toUpperCase()), headers);
});
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionMessageEchoReqParametersConfig {
@Bean
public Function<Message<Foo>, Message<Bar>> function() {
return (message -> {
Map<String, Object> headers = new HashMap<>();
headers.put("path", message.getHeaders().get("path"));
headers.put("query", message.getHeaders().get("query"));
headers.put("test-header", message.getHeaders().get("test-header"));
headers.put("httpMethod", message.getHeaders().get("httpMethod"));
return new GenericMessage<>(new Bar("body"), headers);
});
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionMessageConsumerConfig {
@Bean
public Consumer<Message<Foo>> function() {
return (foo -> {
});
}
}
protected static class Foo {
private String value;
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
protected static class Bar {
private String value;
public Bar() {
}
public Bar(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,231 +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 org.springframework.cloud.function.adapter.aws;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import com.amazonaws.kinesis.agg.AggRecord;
import com.amazonaws.kinesis.agg.RecordAggregator;
import com.amazonaws.services.lambda.runtime.events.KinesisEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.messaging.Message;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
/**
* @author Halvdan Hoem Grelland
*/
@Disabled
public class SpringBootKinesisEventHandlerTests {
private static final ObjectMapper mapper = new ObjectMapper();
private SpringBootKinesisEventHandler<Foo, Bar> handler;
private static KinesisEvent asKinesisEvent(List<Object> payloads) {
KinesisEvent kinesisEvent = new KinesisEvent();
List<KinesisEvent.KinesisEventRecord> kinesisEventRecords = new ArrayList<>();
for (Object payload : payloads) {
KinesisEvent.Record record = new KinesisEvent.Record();
record.setData(asJsonByteBuffer(payload));
KinesisEvent.KinesisEventRecord kinesisEventRecord = new KinesisEvent.KinesisEventRecord();
kinesisEventRecord.setKinesis(record);
kinesisEventRecords.add(kinesisEventRecord);
}
kinesisEvent.setRecords(kinesisEventRecords);
return kinesisEvent;
}
private static KinesisEvent asAggregatedKinesisEvent(List<?> payloads) {
RecordAggregator aggregator = new RecordAggregator();
payloads.stream().map(SpringBootKinesisEventHandlerTests::asJsonByteBuffer)
.forEach(buffer -> {
try {
aggregator.addUserRecord("fakePartitionKey", buffer.array());
}
catch (Exception e) {
fail("Creating aggregated record failed");
}
});
AggRecord aggRecord = aggregator.clearAndGet();
KinesisEvent.Record record = new KinesisEvent.Record();
record.setData(ByteBuffer.wrap(aggRecord.toRecordBytes()));
KinesisEvent.KinesisEventRecord wrappingRecord = new KinesisEvent.KinesisEventRecord();
wrappingRecord.setKinesis(record);
wrappingRecord.setEventVersion("1.0");
KinesisEvent event = new KinesisEvent();
event.setRecords(singletonList(wrappingRecord));
return event;
}
private static ByteBuffer asJsonByteBuffer(Object object) {
try {
return ByteBuffer.wrap(mapper.writeValueAsBytes(object));
}
catch (JsonProcessingException e) {
fail("Setting up test data failed", e);
throw new RuntimeException(e);
}
}
@Test
public void functionBeanHandlesKinesisEvent() throws Exception {
this.handler = new SpringBootKinesisEventHandler<>(FunctionConfig.class);
KinesisEvent event = asKinesisEvent(singletonList(new Foo("foo")));
List<Bar> output = this.handler.handleRequest(event, null);
assertThat(output).containsExactly(new Bar("FOO"));
}
@Test
public void functionBeanHandlesAggregatedKinesisEvent() throws Exception {
this.handler = new SpringBootKinesisEventHandler<>(FunctionConfig.class);
List<Foo> events = asList(new Foo("foo"), new Foo("bar"), new Foo("baz"));
KinesisEvent aggregatedEvent = asAggregatedKinesisEvent(events);
List<Bar> output = this.handler.handleRequest(aggregatedEvent, null);
assertThat(output).containsExactly(new Bar("FOO"), new Bar("BAR"),
new Bar("BAZ"));
}
@Test
public void functionMessageBean() throws Exception {
this.handler = new SpringBootKinesisEventHandler<>(FunctionMessageConfig.class);
KinesisEvent event = asKinesisEvent(asList(new Foo("foo"), new Foo("bar")));
List<Bar> output = this.handler.handleRequest(event, null);
assertThat(output).containsExactly(new Bar("FOO"), new Bar("BAR"));
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionMessageConfig {
@Bean
public Function<Message<Foo>, Bar> function() {
return foo -> new Bar(foo.getPayload().getValue().toUpperCase());
}
}
protected static class Foo {
private String value;
public Foo() {
}
public Foo(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
protected static class Bar {
private String value;
public Bar() {
}
public Bar(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Bar bar = (Bar) o;
return Objects.equals(this.value, bar.value);
}
@Override
public int hashCode() {
return Objects.hash(this.value);
}
}
}

View File

@@ -1,103 +0,0 @@
/*
* Copyright 2017-2020 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.aws;
import java.util.function.Function;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class SpringBootRequestHandlerTests {
private SpringBootRequestHandler<Foo, Bar> handler;
@BeforeEach
public void after() {
System.clearProperty("spring.cloud.function.definition");
}
@Test
public void functionBean() throws Exception {
this.handler = new SpringBootRequestHandler<Foo, Bar>(FunctionConfig.class);
Object output = this.handler.handleRequest(new Foo("foo"), null);
assertThat(output).isInstanceOf(Bar.class);
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
protected static class Foo {
private String value;
public Foo(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
protected static class Bar {
private String value;
public Bar() {
}
public Bar(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,229 +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 org.springframework.cloud.function.adapter.aws;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.function.Function;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.util.Assert;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
* @author Oleg Zhurakousky
*/
public class SpringBootStreamHandlerTests {
private SpringBootStreamHandler handler;
@BeforeEach
public void before() {
System.clearProperty("function.name");
}
@Test
public void functionBeanWithJacksonConfig() throws Exception {
this.handler = new SpringBootStreamHandler(FunctionConfigWithJackson.class);
this.handler.initialize(null);
ByteArrayOutputStream output = new ByteArrayOutputStream();
this.handler.handleRequest(
new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()), output, null);
assertThat(output.toString()).isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void functionBeanWithoutJacksonConfig() throws Exception {
this.handler = new SpringBootStreamHandler(FunctionConfigWithoutJackson.class);
this.handler.initialize(null);
ByteArrayOutputStream output = new ByteArrayOutputStream();
this.handler.handleRequest(
new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()), output, null);
assertThat(output.toString()).isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void functionNonFluxBeanNoCatalog() throws Exception {
this.handler = new SpringBootStreamHandler(NoCatalogNonFluxFunctionConfig.class);
this.handler.initialize(null);
ByteArrayOutputStream output = new ByteArrayOutputStream();
this.handler.handleRequest(
new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()), output, null);
assertThat(output.toString()).isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void functionFluxBeanNoCatalog() throws Exception {
this.handler = new SpringBootStreamHandler(NoCatalogFluxFunctionConfig.class);
this.handler.initialize(null);
ByteArrayOutputStream output = new ByteArrayOutputStream();
this.handler.handleRequest(
new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()), output, null);
assertThat(output.toString()).isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void typelessFunctionConfig() throws Exception {
this.handler = new SpringBootStreamHandler(TypelessFunctionConfig.class);
this.handler.initialize(null);
ByteArrayOutputStream output = new ByteArrayOutputStream();
this.handler.handleRequest(
new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()), output, null);
assertThat(output.toString()).isEqualTo("{\"value\":\"foo\"}");
}
@Test
public void inputStreamFunctionConfig() throws Exception {
this.handler = new SpringBootStreamHandler(InputStreamFunctionConfig.class);
this.handler.initialize(null);
ByteArrayOutputStream output = new ByteArrayOutputStream();
this.handler.handleRequest(
new ByteArrayInputStream("{\"value\":\"foo\"}".getBytes()), output, null);
assertThat(output.toString()).isEqualTo("{\"value\":\"FOO\"}");
}
@Configuration
protected static class NoCatalogNonFluxFunctionConfig {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
@Configuration
protected static class NoCatalogFluxFunctionConfig {
@Bean
public Function<Flux<Foo>, Flux<Bar>> function() {
return flux -> flux.map(foo -> new Bar(foo.getValue().toUpperCase()));
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class FunctionConfigWithJackson {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class })
protected static class FunctionConfigWithoutJackson {
@Bean
public Function<Foo, Bar> function() {
return foo -> new Bar(foo.getValue().toUpperCase());
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class TypelessFunctionConfig {
@Bean
public Function<?, ?> function() {
return value -> {
Assert.isTrue(value instanceof Map, "Expected value should be Map");
return value;
};
}
}
@Configuration
@Import({ ContextFunctionCatalogAutoConfiguration.class,
JacksonAutoConfiguration.class })
protected static class InputStreamFunctionConfig {
@Autowired
private ObjectMapper mapper;
@Bean
public Function<InputStream, ?> function() {
return value -> {
try {
Foo foo = this.mapper.readValue((InputStream) value, Foo.class);
return new Bar(foo.getValue().toUpperCase());
}
catch (Exception e) {
throw new IllegalStateException("Failed test", e);
}
};
}
}
protected static class Foo {
private String value;
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
protected static class Bar {
private String value;
public Bar() {
}
public Bar(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}