Skip to content

Commit

Permalink
Add support for DynamodbEvent.DynamodbStreamRecord serialization (#455)
Browse files Browse the repository at this point in the history
Add support for DynamodbEvent.DynamodbStreamRecord serialization
  • Loading branch information
mo7ty authored Dec 1, 2023
1 parent a6f5115 commit cbe0ea6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<NestedClass>> NESTED_CLASS_MAP = Stream.of(
private static final Map<String, List<? extends NestedClass>> 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"))),
Expand All @@ -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(
Expand Down Expand Up @@ -236,7 +244,7 @@ public static <T> PojoSerializer<T> serializerFor(Class<T> 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<NestedClass> nestedClasses = NESTED_CLASS_MAP.get(eventClass.getName());
List<? extends NestedClass> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

DynamodbEvent.DynamodbStreamRecord record = event.getRecords().get(0);
@Test
public void testLoadDynamoDbStreamRecord() {
assertDynamoDbStreamRecord(EventLoader.loadDynamoDbStreamRecord("ddb/dynamo_ddb_stream_record.json"));
}

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"))
);
.returns(Date.from(ofEpochSecond(1635734407).plusNanos(123456789)), StreamRecord::getApproximateCreationDateTime);

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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": 1.635734407123456789E9,
"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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"N": "101"
}
},
"ApproximateCreationDateTime": 1428537600,
"ApproximateCreationDateTime": 1.635734407123456789E9,
"SequenceNumber": "4421584500000000017450439092",
"SizeBytes": 59,
"StreamViewType": "NEW_AND_OLD_IMAGES"
Expand Down

0 comments on commit cbe0ea6

Please sign in to comment.