Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for DynamodbEvent.DynamodbStreamRecord serialization #455

Merged
merged 7 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
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,
smirnoal marked this conversation as resolved.
Show resolved Hide resolved
"SequenceNumber": "4421584500000000017450439092",
"SizeBytes": 59,
"StreamViewType": "NEW_AND_OLD_IMAGES"
Expand Down
Loading