Skip to content

Commit bf22e31

Browse files
committed
Adding additional test coverage to account for all isInitialVersion codepaths
1 parent 4e82d7e commit bf22e31

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/extensions/VersionedRecordExtension.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex
147147

148148
Pair<AttributeValue, Expression> updates = getRecordUpdates(versionAttributeKey.get(), context);
149149

150-
// Unpack values from Pair
151150
AttributeValue newVersionValue = updates.left();
152151
Expression condition = updates.right();
153152

@@ -168,7 +167,6 @@ private Pair<AttributeValue, Expression> getRecordUpdates(String versionAttribut
168167
AttributeValue existingVersionValue = itemToTransform.getOrDefault(versionAttributeKey, DEFAULT_VALUE);
169168

170169
if (isInitialVersion(existingVersionValue, context)) {
171-
// First version of the record ensure it does not exist
172170
return createInitialRecord(versionAttributeKey, context);
173171
}
174172
// Existing record, increment version
@@ -230,7 +228,6 @@ private Pair<AttributeValue, Expression> updateExistingRecord(String versionAttr
230228

231229
private int getExistingVersion(AttributeValue existingVersionValue) {
232230
if (existingVersionValue.n() == null) {
233-
// In this case a non-null version attribute is present, but it's not an N
234231
throw new IllegalArgumentException("Version attribute appears to be the wrong type. N is required.");
235232
}
236233

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/extensions/VersionedRecordExtensionTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,34 @@ public void beforeWrite_versionNotEqualsAnnotationStartAt_notTreatedAsInitialVer
294294
.tableMetadata(schema.tableMetadata())
295295
.operationContext(PRIMARY_CONTEXT).build());
296296

297-
// Should be treated as existing version
298297
assertThat(result.additionalConditionalExpression().expression(),
299298
is("#AMZN_MAPPED_version = :old_version_value"));
300299
}
301300

301+
@Test
302+
public void beforeWrite_versionEqualsAnnotationStartAt_isTreatedAsInitialVersion() {
303+
FakeVersionedThroughAnnotationItem item = new FakeVersionedThroughAnnotationItem();
304+
item.setId(UUID.randomUUID().toString());
305+
item.setVersion(3);
306+
307+
TableSchema<FakeVersionedThroughAnnotationItem> schema =
308+
TableSchema.fromBean(FakeVersionedThroughAnnotationItem.class);
309+
310+
Map<String, AttributeValue> inputMap = new HashMap<>(schema.itemToMap(item, true));
311+
312+
VersionedRecordExtension recordExtension = VersionedRecordExtension.builder().build();
313+
314+
WriteModification result =
315+
recordExtension.beforeWrite(DefaultDynamoDbExtensionContext
316+
.builder()
317+
.items(inputMap)
318+
.tableMetadata(schema.tableMetadata())
319+
.operationContext(PRIMARY_CONTEXT).build());
320+
321+
assertThat(result.additionalConditionalExpression().expression(),
322+
is("attribute_not_exists(#AMZN_MAPPED_version)"));
323+
}
324+
302325

303326
@DynamoDbBean
304327
public static class FakeVersionedThroughAnnotationItem {

0 commit comments

Comments
 (0)