mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat: DynamoDB support ReturnValuesOnConditionCheckFailure (#505)
This commit is contained in:
1 parent
7f82a28abb
commit
9b5f364e47
8 files changed
+392
-35
No files matched your search
+133
@@ -1195,6 +1195,139 @@ class DynamoDbIntegrationTest {
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemConditionalCheckFailedNoReturnValues() {
|
||||
// Create a table for this test
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "ConditionCheckTable1",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "ConditionCheckTable1",
|
||||
"Item": {"pk": {"S": "k1"}, "testAttr": {"S": "abc"}}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "ConditionCheckTable1",
|
||||
"Key": {"pk": {"S": "k1"}},
|
||||
"UpdateExpression": "SET testAttr = :val",
|
||||
"ExpressionAttributeValues": {":val": {"S": "123"}},
|
||||
"ConditionExpression": "attribute_exists(nonExistent)"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ConditionalCheckFailedException"))
|
||||
.body("message", equalTo("The conditional request failed"))
|
||||
.body("Item", is(nullValue()));
|
||||
|
||||
// Cleanup
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "ConditionCheckTable1"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void updateItemConditionalCheckFailedAllOldReturnValues() {
|
||||
// Create a table for this test
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "ConditionCheckTable2",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "ConditionCheckTable2",
|
||||
"Item": {"pk": {"S": "k1"}, "testAttr": {"S": "abc"}}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "ConditionCheckTable2",
|
||||
"Key": {"pk": {"S": "k1"}},
|
||||
"UpdateExpression": "SET testAttr = :val",
|
||||
"ExpressionAttributeValues": {":val": {"S": "123"}},
|
||||
"ConditionExpression": "attribute_exists(nonExistent)",
|
||||
"ReturnValuesOnConditionCheckFailure" : "ALL_OLD"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.body("__type", equalTo("ConditionalCheckFailedException"))
|
||||
.body("message", equalTo("The conditional request failed"))
|
||||
.body("Item.testAttr.S", equalTo("abc"));
|
||||
|
||||
// Cleanup
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"TableName": "ConditionCheckTable2"}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAndDescribeContinuousBackups() {
|
||||
given()
|
||||
|
||||
+174
-4
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.dynamodb;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@@ -1580,7 +1581,7 @@ class DynamoDbServiceTest {
|
||||
key.set("orderId", attributeValue("S", "o1"));
|
||||
|
||||
var result = service.updateItem("Orders", key, null,
|
||||
"SET #details.subkey = :val, #status = :s", exprNames, exprValues, "ALL_NEW", null, "us-east-1");
|
||||
"SET #details.subkey = :val, #status = :s", exprNames, exprValues, "ALL_NEW", null, "us-east-1", "NONE");
|
||||
|
||||
JsonNode updated = result.newItem();
|
||||
assertNotNull(updated);
|
||||
@@ -1593,7 +1594,7 @@ class DynamoDbServiceTest {
|
||||
|
||||
// Now REMOVE the nested field
|
||||
result = service.updateItem("Orders", key, null,
|
||||
"REMOVE #details.subkey", exprNames, null, "ALL_NEW", null, "us-east-1");
|
||||
"REMOVE #details.subkey", exprNames, null, "ALL_NEW", null, "us-east-1", "NONE");
|
||||
|
||||
updated = result.newItem();
|
||||
// The subkey should be removed from the nested map
|
||||
@@ -1625,11 +1626,11 @@ class DynamoDbServiceTest {
|
||||
|
||||
// First, set all four fields
|
||||
service.updateItem("Orders", key, null,
|
||||
"SET #a = :v1, #b = :v1, #c = :v1, #d = :v1", exprNames, exprValues, "NONE", null, "us-east-1");
|
||||
"SET #a = :v1, #b = :v1, #c = :v1, #d = :v1", exprNames, exprValues, "NONE", null, "us-east-1", "NONE");
|
||||
|
||||
// SET last two assignments, then REMOVE two fields (comma-separated)
|
||||
var result = service.updateItem("Orders", key, null,
|
||||
"SET #a = :v1, #b = :v2 REMOVE #c, #d", exprNames, exprValues, "ALL_NEW", null, "us-east-1");
|
||||
"SET #a = :v1, #b = :v2 REMOVE #c, #d", exprNames, exprValues, "ALL_NEW", null, "us-east-1", "NONE");
|
||||
|
||||
JsonNode updated = result.newItem();
|
||||
assertNotNull(updated);
|
||||
@@ -1638,4 +1639,173 @@ class DynamoDbServiceTest {
|
||||
assertNull(updated.get("fieldC"), "fieldC should be removed");
|
||||
assertNull(updated.get("fieldD"), "fieldD should be removed");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemConditionFailedReturnValuesNone() {
|
||||
createOrdersTable();
|
||||
|
||||
ObjectNode order = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");
|
||||
ObjectNode key = item("customerId", "1", "orderId", "sort1");
|
||||
service.putItem("Orders", order);
|
||||
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
ObjectNode newVal = attributeValue("S", "newVal");
|
||||
ObjectNode conditionVal = attributeValue("S", "testVal");
|
||||
exprValues.set(":val", newVal);
|
||||
exprValues.set(":test", conditionVal);
|
||||
|
||||
ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () -> service.updateItem("Orders", key, null,
|
||||
"SET newAttr = :val",
|
||||
null, exprValues, "NONE", "testAttr <> :test", "us-east-1", "NONE"));
|
||||
|
||||
JsonNode stored = service.getItem("Orders", key);
|
||||
assertNotNull(stored, "item should exist");
|
||||
assertFalse(stored.has("newAttr"), "new attribute should not have been added");
|
||||
|
||||
assertNull(ex.getItem());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemConditionFailedReturnValuesAllOld() {
|
||||
createOrdersTable();
|
||||
|
||||
ObjectNode order = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");
|
||||
ObjectNode key = item("customerId", "1", "orderId", "sort1");
|
||||
service.putItem("Orders", order);
|
||||
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
ObjectNode newVal = attributeValue("S", "newVal");
|
||||
ObjectNode conditionVal = attributeValue("S", "testVal");
|
||||
exprValues.set(":val", newVal);
|
||||
exprValues.set(":test", conditionVal);
|
||||
|
||||
ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () -> service.updateItem("Orders", key, null,
|
||||
"SET newAttr = :val",
|
||||
null, exprValues, "NONE", "testAttr <> :test", "us-east-1", "ALL_OLD"));
|
||||
|
||||
JsonNode stored = service.getItem("Orders", key);
|
||||
assertNotNull(stored, "item should exist");
|
||||
assertFalse(stored.has("newAttr"), "new attribute should not have been added");
|
||||
|
||||
JsonNode returnedItem = ex.getItem();
|
||||
assertNotNull(returnedItem);
|
||||
assertTrue(returnedItem.has("testAttr"), "returned item should have testAttr");
|
||||
assertEquals("testVal", returnedItem.get("testAttr").get("S").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemNetNewConditionFailedReturnValuesNone() {
|
||||
createOrdersTable();
|
||||
|
||||
ObjectNode order = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");
|
||||
ObjectNode key = item("customerId", "1", "orderId", "sort1");
|
||||
ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->
|
||||
service.putItem("Orders", order, "attribute_exists(customerId)", null, null, "us-east-1", "NONE"));
|
||||
|
||||
JsonNode stored = service.getItem("Orders", key);
|
||||
assertNull(stored, "item should not exist");
|
||||
|
||||
assertNull(ex.getItem());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemNetNewConditionFailedReturnValuesAllOld() {
|
||||
createOrdersTable();
|
||||
|
||||
ObjectNode order = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");
|
||||
ObjectNode key = item("customerId", "1", "orderId", "sort1");
|
||||
ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->
|
||||
service.putItem("Orders", order, "attribute_exists(customerId)", null, null, "us-east-1", "ALL_OLD"));
|
||||
|
||||
JsonNode stored = service.getItem("Orders", key);
|
||||
assertNull(stored, "item should not exist");
|
||||
|
||||
assertNull(ex.getItem());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemExistingConditionFailedReturnValuesNone() {
|
||||
createOrdersTable();
|
||||
|
||||
ObjectNode order1 = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");
|
||||
ObjectNode order2 = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal1");
|
||||
ObjectNode key = item("customerId", "1", "orderId", "sort1");
|
||||
|
||||
service.putItem("Orders", order1);
|
||||
|
||||
ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->
|
||||
service.putItem("Orders", order2, "attribute_exists(someAttr)", null, null, "us-east-1", "NONE"));
|
||||
|
||||
JsonNode stored = service.getItem("Orders", key);
|
||||
assertNotNull(stored, "item should exist");
|
||||
assertTrue(stored.has("testAttr"), "item should have testAttr");
|
||||
assertEquals("testVal", stored.get("testAttr").get("S").asText());
|
||||
|
||||
assertNull(ex.getItem());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemExistingConditionFailedReturnValuesAllOld() {
|
||||
createOrdersTable();
|
||||
|
||||
ObjectNode order1 = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");
|
||||
ObjectNode order2 = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal1");
|
||||
ObjectNode key = item("customerId", "1", "orderId", "sort1");
|
||||
|
||||
service.putItem("Orders", order1);
|
||||
|
||||
ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->
|
||||
service.putItem("Orders", order2, "attribute_exists(someAttr)", null, null, "us-east-1", "ALL_OLD"));
|
||||
|
||||
JsonNode stored = service.getItem("Orders", key);
|
||||
assertNotNull(stored, "item should exist");
|
||||
assertTrue(stored.has("testAttr"), "item should have testAttr");
|
||||
assertEquals("testVal", stored.get("testAttr").get("S").asText());
|
||||
|
||||
JsonNode returnedItem = ex.getItem();
|
||||
assertNotNull(returnedItem);
|
||||
assertTrue(returnedItem.has("testAttr"), "returned item should have testAttr");
|
||||
assertEquals("testVal", returnedItem.get("testAttr").get("S").asText());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void deleteItemConditionFailedReturnValuesNone() {
|
||||
createOrdersTable();
|
||||
|
||||
ObjectNode order = item("customerId", "1", "orderId", "sort1");
|
||||
ObjectNode key = item("customerId", "1", "orderId", "sort1");
|
||||
|
||||
service.putItem("Orders", order);
|
||||
|
||||
ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->
|
||||
service.deleteItem("Orders", key, "attribute_exists(someAttr)", null, null, "us-east-1", "NONE"));
|
||||
|
||||
JsonNode stored = service.getItem("Orders", key);
|
||||
assertNotNull(stored, "item should exist");
|
||||
|
||||
assertNull(ex.getItem());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteItemConditionFailedReturnValuesAllOld() {
|
||||
createOrdersTable();
|
||||
|
||||
ObjectNode order = item("customerId", "1", "orderId", "sort1");
|
||||
ObjectNode key = item("customerId", "1", "orderId", "sort1");
|
||||
|
||||
service.putItem("Orders", order);
|
||||
|
||||
ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->
|
||||
service.deleteItem("Orders", key, "attribute_exists(someAttr)", null, null, "us-east-1", "ALL_OLD"));
|
||||
|
||||
JsonNode stored = service.getItem("Orders", key);
|
||||
assertNotNull(stored, "item should exist");
|
||||
|
||||
JsonNode returnedItem = ex.getItem();
|
||||
assertNotNull(returnedItem);
|
||||
assertTrue(returnedItem.has("customerId"), "returned item should have customerId");
|
||||
assertEquals("1", returnedItem.get("customerId").get("S").asText());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user