From 02e3cdc3caa6a4aee2fb4c3c221191994709e080 Mon Sep 17 00:00:00 2001 From: mo7ty Date: Wed, 25 Oct 2023 23:04:36 +0100 Subject: [PATCH 1/5] Add support for DynamodbEvent.DynamodbStreamRecord serialization --- .../events/LambdaEventSerializers.java | 8 +++++ .../lambda/runtime/tests/EventLoader.java | 4 +++ .../lambda/runtime/tests/EventLoaderTest.java | 36 +++++++++++++------ .../ddb/dynamo_ddb_stream_record.json | 35 ++++++++++++++++++ .../resources/{ => ddb}/dynamo_event.json | 0 5 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json rename aws-lambda-java-tests/src/test/resources/{ => ddb}/dynamo_event.json (100%) diff --git a/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/LambdaEventSerializers.java b/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/LambdaEventSerializers.java index b24b4060..ef98f091 100644 --- a/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/LambdaEventSerializers.java +++ b/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/LambdaEventSerializers.java @@ -180,6 +180,14 @@ public class LambdaEventSerializers { "com.amazonaws.services.lambda.runtime.events.models.dynamodb.StreamRecord", "com.amazonaws.services.dynamodbv2.model.StreamRecord"), new NestedClass("com.amazonaws.services.lambda.runtime.events.DynamodbEvent$DynamodbStreamRecord"))), + new SimpleEntry<>("com.amazonaws.services.lambda.runtime.events.DynamodbEvent$DynamodbStreamRecord", + Arrays.asList( + new AlternateNestedClass( + "com.amazonaws.services.lambda.runtime.events.models.dynamodb.AttributeValue", + "com.amazonaws.services.dynamodbv2.model.AttributeValue"), + new AlternateNestedClass( + "com.amazonaws.services.lambda.runtime.events.models.dynamodb.StreamRecord", + "com.amazonaws.services.dynamodbv2.model.StreamRecord"))), new SimpleEntry<>("com.amazonaws.services.lambda.runtime.events.DynamodbTimeWindowEvent", Arrays.asList( new AlternateNestedClass( diff --git a/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java b/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java index 68cd37d3..b4ab86d0 100644 --- a/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java +++ b/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java @@ -65,6 +65,10 @@ public static DynamodbEvent loadDynamoDbEvent(String filename) { return loadEvent(filename, DynamodbEvent.class); } + public static DynamodbEvent.DynamodbStreamRecord loadDynamodbStreamRecord(String filename) { + return loadEvent(filename, DynamodbEvent.DynamodbStreamRecord.class); + } + public static KafkaEvent loadKafkaEvent(String filename) { return loadEvent(filename, KafkaEvent.class); } diff --git a/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java index e36016a1..cf1e37e2 100644 --- a/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java +++ b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java @@ -160,27 +160,43 @@ public void testLoadSNSEvent() { @Test public void testLoadDynamoEvent() { - DynamodbEvent event = EventLoader.loadDynamoDbEvent("dynamo_event.json"); + DynamodbEvent event = EventLoader.loadDynamoDbEvent("ddb/dynamo_event.json"); assertThat(event).isNotNull(); assertThat(event.getRecords()).hasSize(3); + assertDynamodbStreamRecord(event.getRecords().get(1)); + } + + @Test + public void testLoadDynamodbDDBStreamRecord() { + assertDynamodbStreamRecord(EventLoader.loadDynamodbStreamRecord("ddb/dynamo_ddb_stream_record.json")); + } - DynamodbEvent.DynamodbStreamRecord record = event.getRecords().get(0); + private static void assertDynamodbStreamRecord(final DynamodbEvent.DynamodbStreamRecord record) { assertThat(record) + .isNotNull() .returns("arn:aws:dynamodb:eu-central-1:123456789012:table/ExampleTableWithStream/stream/2015-06-27T00:48:05.899", from(DynamodbEvent.DynamodbStreamRecord::getEventSourceARN)) - .returns("INSERT", from(Record::getEventName)); + .returns("MODIFY", from(Record::getEventName)); StreamRecord streamRecord = record.getDynamodb(); assertThat(streamRecord) - .returns("4421584500000000017450439091", StreamRecord::getSequenceNumber) - .returns(26L, StreamRecord::getSizeBytes) + .returns("4421584500000000017450439092", StreamRecord::getSequenceNumber) + .returns(59L, StreamRecord::getSizeBytes) .returns("NEW_AND_OLD_IMAGES", StreamRecord::getStreamViewType) .returns(Date.from(ofEpochSecond(1428537600)), StreamRecord::getApproximateCreationDateTime); - assertThat(streamRecord.getKeys()).contains(entry("Id", new AttributeValue().withN("101"))); - assertThat(streamRecord.getNewImage()).containsAnyOf( - entry("Message", new AttributeValue("New item!")), - entry("Id", new AttributeValue().withN("101")) - ); + assertThat(streamRecord.getKeys()) + .isNotNull() + .contains(entry("Id", new AttributeValue().withN("101"))); + assertThat(streamRecord.getNewImage()) + .isNotNull() + .containsAnyOf( + entry("Message", new AttributeValue("This item has changed")), + entry("Id", new AttributeValue().withN("101"))); + assertThat(streamRecord.getOldImage()) + .isNotNull() + .containsAnyOf( + entry("Message", new AttributeValue("New item!")), + entry("Id", new AttributeValue().withN("101"))); } @Test diff --git a/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json b/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json new file mode 100644 index 00000000..f86a3313 --- /dev/null +++ b/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json @@ -0,0 +1,35 @@ +{ + "eventID": "c81e728d9d4c2f636f067f89cc14862c", + "eventName": "MODIFY", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "eu-central-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "NewImage": { + "Message": { + "S": "This item has changed" + }, + "Id": { + "N": "101" + } + }, + "OldImage": { + "Message": { + "S": "New item!" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439092", + "SizeBytes": 59, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:eu-central-1:123456789012:table/ExampleTableWithStream/stream/2015-06-27T00:48:05.899" +} diff --git a/aws-lambda-java-tests/src/test/resources/dynamo_event.json b/aws-lambda-java-tests/src/test/resources/ddb/dynamo_event.json similarity index 100% rename from aws-lambda-java-tests/src/test/resources/dynamo_event.json rename to aws-lambda-java-tests/src/test/resources/ddb/dynamo_event.json From 232497aca9d4c5ee01f590076bea097201986bc6 Mon Sep 17 00:00:00 2001 From: mo7ty Date: Thu, 26 Oct 2023 13:14:17 +0100 Subject: [PATCH 2/5] Update `ddb/dynamo_ddb_stream_record.json#ApproximateCreationDateTime` * Refer to [Add support for DynamodbEvent.DynamodbStreamRecord serialization #455 comment with sample from PROD](https://github.com/aws/aws-lambda-java-libs/pull/455#discussion_r1372878998) --- .../src/test/resources/ddb/dynamo_ddb_stream_record.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json b/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json index f86a3313..7908aa05 100644 --- a/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json +++ b/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json @@ -26,7 +26,7 @@ "N": "101" } }, - "ApproximateCreationDateTime": 1428537600, + "ApproximateCreationDateTime": 1428537600.0, "SequenceNumber": "4421584500000000017450439092", "SizeBytes": 59, "StreamViewType": "NEW_AND_OLD_IMAGES" From f92b0bc5735af9421c0fb261a2b7e95538ab3781 Mon Sep 17 00:00:00 2001 From: mo7ty Date: Wed, 1 Nov 2023 14:51:36 +0000 Subject: [PATCH 3/5] Increase `ddb/dynamo_*.json#ApproximateCreationDateTime` test precision to nanoseconds * Refer to [Add support for DynamodbEvent.DynamodbStreamRecord serialization #455 comment for additional precision test](https://github.com/aws/aws-lambda-java-libs/pull/455#discussion_r1378595685) --- .../services/lambda/runtime/tests/EventLoaderTest.java | 2 +- .../src/test/resources/ddb/dynamo_ddb_stream_record.json | 2 +- aws-lambda-java-tests/src/test/resources/ddb/dynamo_event.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java index cf1e37e2..a4aa58e6 100644 --- a/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java +++ b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java @@ -182,7 +182,7 @@ private static void assertDynamodbStreamRecord(final DynamodbEvent.DynamodbStrea .returns("4421584500000000017450439092", StreamRecord::getSequenceNumber) .returns(59L, StreamRecord::getSizeBytes) .returns("NEW_AND_OLD_IMAGES", StreamRecord::getStreamViewType) - .returns(Date.from(ofEpochSecond(1428537600)), StreamRecord::getApproximateCreationDateTime); + .returns(Date.from(ofEpochSecond(1635734407).plusNanos(123456789)), StreamRecord::getApproximateCreationDateTime); assertThat(streamRecord.getKeys()) .isNotNull() diff --git a/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json b/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json index 7908aa05..f5df23ff 100644 --- a/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json +++ b/aws-lambda-java-tests/src/test/resources/ddb/dynamo_ddb_stream_record.json @@ -26,7 +26,7 @@ "N": "101" } }, - "ApproximateCreationDateTime": 1428537600.0, + "ApproximateCreationDateTime": 1.635734407123456789E9, "SequenceNumber": "4421584500000000017450439092", "SizeBytes": 59, "StreamViewType": "NEW_AND_OLD_IMAGES" diff --git a/aws-lambda-java-tests/src/test/resources/ddb/dynamo_event.json b/aws-lambda-java-tests/src/test/resources/ddb/dynamo_event.json index f28ce0e6..2e43ba49 100644 --- a/aws-lambda-java-tests/src/test/resources/ddb/dynamo_event.json +++ b/aws-lambda-java-tests/src/test/resources/ddb/dynamo_event.json @@ -59,7 +59,7 @@ "N": "101" } }, - "ApproximateCreationDateTime": 1428537600, + "ApproximateCreationDateTime": 1.635734407123456789E9, "SequenceNumber": "4421584500000000017450439092", "SizeBytes": 59, "StreamViewType": "NEW_AND_OLD_IMAGES" From ffafb30d9583b5516451272833bdbc22cbb2c90b Mon Sep 17 00:00:00 2001 From: mo7ty Date: Sun, 26 Nov 2023 18:30:33 +0000 Subject: [PATCH 4/5] Rename methods names --- .../services/lambda/runtime/tests/EventLoader.java | 2 +- .../services/lambda/runtime/tests/EventLoaderTest.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java b/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java index b4ab86d0..7228fb90 100644 --- a/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java +++ b/aws-lambda-java-tests/src/main/java/com/amazonaws/services/lambda/runtime/tests/EventLoader.java @@ -65,7 +65,7 @@ public static DynamodbEvent loadDynamoDbEvent(String filename) { return loadEvent(filename, DynamodbEvent.class); } - public static DynamodbEvent.DynamodbStreamRecord loadDynamodbStreamRecord(String filename) { + public static DynamodbEvent.DynamodbStreamRecord loadDynamoDbStreamRecord(String filename) { return loadEvent(filename, DynamodbEvent.DynamodbStreamRecord.class); } diff --git a/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java index a4aa58e6..3177b9cc 100644 --- a/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java +++ b/aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java @@ -163,15 +163,15 @@ public void testLoadDynamoEvent() { DynamodbEvent event = EventLoader.loadDynamoDbEvent("ddb/dynamo_event.json"); assertThat(event).isNotNull(); assertThat(event.getRecords()).hasSize(3); - assertDynamodbStreamRecord(event.getRecords().get(1)); + assertDynamoDbStreamRecord(event.getRecords().get(1)); } @Test - public void testLoadDynamodbDDBStreamRecord() { - assertDynamodbStreamRecord(EventLoader.loadDynamodbStreamRecord("ddb/dynamo_ddb_stream_record.json")); + public void testLoadDynamoDbStreamRecord() { + assertDynamoDbStreamRecord(EventLoader.loadDynamoDbStreamRecord("ddb/dynamo_ddb_stream_record.json")); } - private static void assertDynamodbStreamRecord(final DynamodbEvent.DynamodbStreamRecord record) { + private static void assertDynamoDbStreamRecord(final DynamodbEvent.DynamodbStreamRecord record) { assertThat(record) .isNotNull() .returns("arn:aws:dynamodb:eu-central-1:123456789012:table/ExampleTableWithStream/stream/2015-06-27T00:48:05.899", from(DynamodbEvent.DynamodbStreamRecord::getEventSourceARN)) From 83048d588672accbcb5e76b9ce67be15e4a26527 Mon Sep 17 00:00:00 2001 From: mo7ty Date: Wed, 29 Nov 2023 12:13:06 +0000 Subject: [PATCH 5/5] Update `LambdaEventSerializers#NESTED_CLASS_MAP` keys data type --- .../runtime/serialization/events/LambdaEventSerializers.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/LambdaEventSerializers.java b/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/LambdaEventSerializers.java index ef98f091..4173211e 100644 --- a/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/LambdaEventSerializers.java +++ b/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/LambdaEventSerializers.java @@ -158,7 +158,7 @@ public class LambdaEventSerializers { * If mixins are required for inner classes of an event, then those nested classes must be specified here. */ @SuppressWarnings("rawtypes") - private static final Map> NESTED_CLASS_MAP = Stream.of( + private static final Map> NESTED_CLASS_MAP = Stream.of( new SimpleEntry<>("com.amazonaws.services.lambda.runtime.events.CodeCommitEvent", Arrays.asList( new NestedClass("com.amazonaws.services.lambda.runtime.events.CodeCommitEvent$Record"))), @@ -244,7 +244,7 @@ public static PojoSerializer serializerFor(Class eventClass, ClassLoad } // if event model has nested classes then load those classes and check if mixins apply if (NESTED_CLASS_MAP.containsKey(eventClass.getName())) { - List nestedClasses = NESTED_CLASS_MAP.get(eventClass.getName()); + List nestedClasses = NESTED_CLASS_MAP.get(eventClass.getName()); for (NestedClass nestedClass: nestedClasses) { // if mixin exists for nested class then apply if (MIXIN_MAP.containsKey(nestedClass.className)) {