Skip to content

Commit 936726a

Browse files
committed
GH-1222 Add initial support for AWS ScheduledEvent
Resolves #1222
1 parent 399a4f4 commit 936726a

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/FunctionInvokerTests.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.amazonaws.services.lambda.runtime.events.S3Event;
4646
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
4747
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
48+
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;
4849
import com.fasterxml.jackson.databind.ObjectMapper;
4950
import org.junit.jupiter.api.BeforeEach;
5051
import org.junit.jupiter.api.Test;
@@ -107,6 +108,36 @@ public class FunctionInvokerTests {
107108
+ " \"timestamp\": 1712716805129\n"
108109
+ " }\n"
109110
+ "}";
111+
112+
String scheduleEvent = "{\n"
113+
+ " \"version\": \"0\",\n"
114+
+ " \"id\": \"17793124-05d4-b198-2fde-7ededc63b103\",\n"
115+
+ " \"detail-type\": \"Object Created\",\n"
116+
+ " \"source\": \"aws.s3\",\n"
117+
+ " \"account\": \"111122223333\",\n"
118+
+ " \"time\": \"2021-11-12T00:00:00Z\",\n"
119+
+ " \"region\": \"ca-central-1\",\n"
120+
+ " \"resources\": [\n"
121+
+ " \"arn:aws:s3:::amzn-s3-demo-bucket1\"\n"
122+
+ " ],\n"
123+
+ " \"detail\": {\n"
124+
+ " \"version\": \"0\",\n"
125+
+ " \"bucket\": {\n"
126+
+ " \"name\": \"amzn-s3-demo-bucket1\"\n"
127+
+ " },\n"
128+
+ " \"object\": {\n"
129+
+ " \"key\": \"example-key\",\n"
130+
+ " \"size\": 5,\n"
131+
+ " \"etag\": \"b1946ac92492d2347c6235b4d2611184\",\n"
132+
+ " \"version-id\": \"IYV3p45BT0ac8hjHg1houSdS1a.Mro8e\",\n"
133+
+ " \"sequencer\": \"617f08299329d189\"\n"
134+
+ " },\n"
135+
+ " \"request-id\": \"N4N7GDK58NMKJ12R\",\n"
136+
+ " \"requester\": \"123456789012\",\n"
137+
+ " \"source-ip-address\": \"1.2.3.4\",\n"
138+
+ " \"reason\": \"PutObject\"\n"
139+
+ " }\n"
140+
+ "} ";
110141

111142
String dynamoDbEvent = "{\n"
112143
+ " \"Records\": [\n"
@@ -735,6 +766,20 @@ public void before() throws Exception {
735766
System.clearProperty("spring.cloud.function.definition");
736767
//this.getEnvironment().clear();
737768
}
769+
770+
@Test
771+
public void testScheduledEvent() throws Exception {
772+
System.setProperty("MAIN_CLASS", ScheduledEventConfiguration.class.getName());
773+
System.setProperty("spring.cloud.function.definition", "event");
774+
FunctionInvoker invoker = new FunctionInvoker();
775+
776+
InputStream targetStream = new ByteArrayInputStream(this.scheduleEvent.getBytes());
777+
ByteArrayOutputStream output = new ByteArrayOutputStream();
778+
invoker.handleRequest(targetStream, output, null);
779+
String result = new String(output.toByteArray(), StandardCharsets.UTF_8);
780+
System.out.println("Result: " + result);
781+
//assertThat(result).contains("APIGatewayCustomAuthorizerEvent(version=null, type=TOKEN");
782+
}
738783

739784
@SuppressWarnings({ "rawtypes", "unchecked" })
740785
@Test
@@ -1969,4 +2014,17 @@ public String toString() {
19692014
return this.name;
19702015
}
19712016
}
2017+
2018+
@EnableAutoConfiguration
2019+
@Configuration
2020+
public static class ScheduledEventConfiguration {
2021+
2022+
@Bean
2023+
public Function<ScheduledEvent, ScheduledEvent> event() {
2024+
return event -> {
2025+
System.out.println("Event: " + event);
2026+
return event;
2027+
};
2028+
}
2029+
}
19722030
}

spring-cloud-function-context/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
<groupId>com.fasterxml.jackson.datatype</groupId>
5656
<artifactId>jackson-datatype-jsr310</artifactId>
5757
</dependency>
58+
<dependency>
59+
<groupId>com.fasterxml.jackson.datatype</groupId>
60+
<artifactId>jackson-datatype-joda</artifactId>
61+
</dependency>
5862
<dependency>
5963
<groupId>org.springframework.boot</groupId>
6064
<artifactId>spring-boot-starter-test</artifactId>

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.fasterxml.jackson.databind.Module;
2929
import com.fasterxml.jackson.databind.ObjectMapper;
3030
import com.fasterxml.jackson.databind.SerializationFeature;
31+
import com.fasterxml.jackson.datatype.joda.JodaModule;
3132
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
3233
import com.google.gson.Gson;
3334
import io.cloudevents.spring.messaging.CloudEventMessageConverter;
@@ -232,7 +233,7 @@ private JsonMapper jackson(ApplicationContext context) {
232233
mapper = new ObjectMapper();
233234
mapper.registerModule(new JavaTimeModule());
234235
}
235-
236+
mapper.registerModule(new JodaModule());
236237
if (KotlinDetector.isKotlinPresent()) {
237238
try {
238239
if (!mapper.getRegisteredModuleIds().contains("com.fasterxml.jackson.module.kotlin.KotlinModule")) {

0 commit comments

Comments
 (0)