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

bugfix for auto generate UUID extension #5702

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -116,8 +116,21 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex
.build();
}

/**
* Inserts a new UUID into the given map of attributes if the specified attribute key is absent or has an empty value.
*
* This method checks whether the attribute with the specified key is already present in the `itemToTransform` map
* and if its value is a non-empty string. If the attribute exists and has a value, the method does nothing.
* Otherwise, it generates a new UUID using {@link UUID#randomUUID()} and sets it as the value for the given key.
*
* @param itemToTransform The map containing attributes of the item being transformed.
* @param key The attribute key that should be checked and potentially updated with a new UUID.
*/
private void insertUuidInItemToTransform(Map<String, AttributeValue> itemToTransform,
String key) {
if (itemToTransform.containsKey(key) && itemToTransform.get(key).s() != null && !itemToTransform.get(key).s().isEmpty()) {
return;
}
itemToTransform.put(key, AttributeValue.builder().s(UUID.randomUUID().toString()).build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class AutoGeneratedUuidExtensionTest {
private static final OperationContext PRIMARY_CONTEXT =
DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());

private final AutoGeneratedUuidExtension atomicCounterExtension = AutoGeneratedUuidExtension.create();
private final AutoGeneratedUuidExtension autoGeneratedUuidExtension = AutoGeneratedUuidExtension.create();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed name of the extension object to be meaningful



private static final StaticTableSchema<ItemWithUuid> ITEM_WITH_UUID_MAPPER =
Expand All @@ -54,17 +54,43 @@ public class AutoGeneratedUuidExtensionTest {
.addAttribute(String.class, a -> a.name("id")
.getter(ItemWithUuid::getId)
.setter(ItemWithUuid::setId)
.addTag(primaryPartitionKey()))
.addTag(primaryPartitionKey())
.addTag(AutoGeneratedUuidExtension.AttributeTags.autoGeneratedUuidAttribute())
)
.addAttribute(String.class, a -> a.name("uuidAttribute")
.getter(ItemWithUuid::getUuidAttribute)
.setter(ItemWithUuid::setUuidAttribute)
.addTag(AutoGeneratedUuidExtension.AttributeTags.autoGeneratedUuidAttribute())
)
.addAttribute(String.class, a -> a.name("simpleString")
.getter(ItemWithUuid::getSimpleString)
.setter(ItemWithUuid::setSimpleString))
.setter(ItemWithUuid::setSimpleString)
)
.build();

@Test
public void beforeWrite_updateItemOperation_hasUuidInItem_Pk_doesNotCreateUpdateExpressionAndFilters() {
ItemWithUuid SimpleItem = new ItemWithUuid();
SimpleItem.setId(RECORD_ID);

Map<String, AttributeValue> items = ITEM_WITH_UUID_MAPPER.itemToMap(SimpleItem, true);
assertThat(items).hasSize(1);

WriteModification result =
autoGeneratedUuidExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder()
.items(items)
.tableMetadata(ITEM_WITH_UUID_MAPPER.tableMetadata())
.operationName(OperationName.UPDATE_ITEM)
.operationContext(PRIMARY_CONTEXT).build());

Map<String, AttributeValue> transformedItem = result.transformedItem();
assertThat(transformedItem).isNotNull().hasSize(2);
assertThat(transformedItem).containsEntry("id", AttributeValue.fromS(RECORD_ID));
isValidUuid(transformedItem.get("uuidAttribute").s());
assertThat(result.updateExpression()).isNull();

}

@Test
public void beforeWrite_updateItemOperation_hasUuidInItem_doesNotCreateUpdateExpressionAndFilters() {
ItemWithUuid SimpleItem = new ItemWithUuid();
Expand All @@ -76,7 +102,7 @@ public void beforeWrite_updateItemOperation_hasUuidInItem_doesNotCreateUpdateExp
assertThat(items).hasSize(2);

WriteModification result =
atomicCounterExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder()
autoGeneratedUuidExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder()
.items(items)
.tableMetadata(ITEM_WITH_UUID_MAPPER.tableMetadata())
.operationName(OperationName.UPDATE_ITEM)
Expand All @@ -99,7 +125,7 @@ public void beforeWrite_updateItemOperation_hasNoUuidInItem_doesNotCreatesUpdate
assertThat(items).hasSize(1);

WriteModification result =
atomicCounterExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder()
autoGeneratedUuidExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder()
.items(items)
.tableMetadata(ITEM_WITH_UUID_MAPPER.tableMetadata())
.operationName(OperationName.UPDATE_ITEM)
Expand All @@ -121,7 +147,7 @@ public void beforeWrite_updateItemOperation_UuidNotPresent_newUuidCreated() {
assertThat(items).hasSize(1);

WriteModification result =
atomicCounterExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder()
autoGeneratedUuidExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder()
.items(items)
.tableMetadata(ITEM_WITH_UUID_MAPPER.tableMetadata())
.operationName(OperationName.UPDATE_ITEM)
Expand Down