diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsErrorResponseWithItem.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsErrorResponseWithItem.java new file mode 100644 index 0000000000..e48e266491 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsErrorResponseWithItem.java @@ -0,0 +1,10 @@ +package io.github.hectorvent.floci.core.common; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.JsonNode; +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public record AwsErrorResponseWithItem(@JsonProperty("__type") String type, @JsonProperty("message") String message, + @JsonProperty("Item") JsonNode item) { +} \ No newline at end of file diff --git a/src/main/java/io/github/hectorvent/floci/core/common/JsonErrorResponseUtils.java b/src/main/java/io/github/hectorvent/floci/core/common/JsonErrorResponseUtils.java index 0b636725a5..d51ba0fdc0 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/JsonErrorResponseUtils.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/JsonErrorResponseUtils.java @@ -1,4 +1,6 @@ package io.github.hectorvent.floci.core.common; +import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException; +import com.fasterxml.jackson.databind.JsonNode; import jakarta.ws.rs.core.Response; @@ -9,22 +11,32 @@ public class JsonErrorResponseUtils { } public static Response createErrorResponse(Exception e) { - return JsonErrorResponseUtils.createErrorResponse(500, "InternalFailure", "InternalFailure", e.getMessage()); + return JsonErrorResponseUtils.createErrorResponse(500, "InternalFailure", "InternalFailure", e.getMessage(), null); } public static Response createErrorResponse(AwsException e) { - return createErrorResponse(e.getHttpStatus(), e.getErrorCode(), e.jsonType(), e.getMessage()); + JsonNode item = null; + if (e instanceof ConditionalCheckFailedException){ + item = ((ConditionalCheckFailedException) e).getItem(); + } + return createErrorResponse(e.getHttpStatus(), e.getErrorCode(), e.jsonType(), e.getMessage(), item); } public static Response createUnknownOperationErrorResponse(String target) { return createErrorResponse(404, "UnknownOperationException", "UnknownOperationException", - "Unknown operation: " + target); + "Unknown operation: " + target, null); } - public static Response createErrorResponse(int httpStatusCode, String queryError, String errorType, String errorMessage) { + public static Response createErrorResponse(int httpStatusCode, String queryError, String errorType, String errorMessage, JsonNode item) { String queryErrorFault = (httpStatusCode < 500) ? "Sender" : "Receiver"; + if (item != null) { + return Response.status(httpStatusCode) + .header("x-amzn-query-error", queryError + ";" + queryErrorFault) + .entity(new AwsErrorResponseWithItem(errorType, errorMessage, item)) + .build(); + } return Response.status(httpStatusCode) .header("x-amzn-query-error", queryError + ";" + queryErrorFault) .entity(new AwsErrorResponse(errorType, errorMessage)) diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java index 198af1fc16..8a90e5d9ee 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java @@ -207,6 +207,7 @@ public class DynamoDbJsonHandler { String tableName = request.path("TableName").asText(); JsonNode item = request.path("Item"); String returnValues = request.path("ReturnValues").asText("NONE"); + String returnValuesOnConditionCheckFailure = request.path("ReturnValuesOnConditionCheckFailure").asText("NONE"); String conditionExpression = request.has("ConditionExpression") ? request.get("ConditionExpression").asText() : null; JsonNode exprAttrNames = request.has("ExpressionAttributeNames") @@ -220,7 +221,7 @@ public class DynamoDbJsonHandler { oldItem = dynamoDbService.getItem(tableName, item, region); } - dynamoDbService.putItem(tableName, item, conditionExpression, exprAttrNames, exprAttrValues, region); + dynamoDbService.putItem(tableName, item, conditionExpression, exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure); ObjectNode response = objectMapper.createObjectNode(); if ("ALL_OLD" .equals(returnValues) && oldItem != null) { @@ -247,7 +248,8 @@ public class DynamoDbJsonHandler { private Response handleDeleteItem(JsonNode request, String region) { String tableName = request.path("TableName").asText(); JsonNode key = request.path("Key"); - String returnValues = request.path("ReturnValues").asText("NONE"); + String returnValues = request.path("ReturnValues").asText("NONE"); + String returnValuesOnConditionCheckFailure = request.path("ReturnValuesOnConditionCheckFailure").asText("NONE"); String conditionExpression = request.has("ConditionExpression") ? request.get("ConditionExpression").asText() : null; JsonNode exprAttrNames = request.has("ExpressionAttributeNames") @@ -256,7 +258,7 @@ public class DynamoDbJsonHandler { ? request.get("ExpressionAttributeValues") : null; JsonNode oldItem = dynamoDbService.deleteItem(tableName, key, conditionExpression, - exprAttrNames, exprAttrValues, region); + exprAttrNames, exprAttrValues, region, returnValuesOnConditionCheckFailure); ObjectNode response = objectMapper.createObjectNode(); if ("ALL_OLD" .equals(returnValues) && oldItem != null) { @@ -279,12 +281,13 @@ public class DynamoDbJsonHandler { String conditionExpression = request.has("ConditionExpression") ? request.get("ConditionExpression").asText() : null; String returnValues = request.path("ReturnValues").asText("NONE"); + String returnValuesOnConditionCheckFailure = request.path("ReturnValuesOnConditionCheckFailure").asText("NONE"); JsonNode updateData = attributeUpdates.isMissingNode() ? null : attributeUpdates; DynamoDbService.UpdateResult result = dynamoDbService.updateItem( tableName, key, updateData, updateExpression, exprAttrNames, exprAttrValues, - returnValues, conditionExpression, region); + returnValues, conditionExpression, region, returnValuesOnConditionCheckFailure); ObjectNode response = objectMapper.createObjectNode(); if ("ALL_NEW" .equals(returnValues) && result.newItem() != null) { diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index b5c39cc01b..2a07a11c5c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex; import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex; import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition; +import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -221,17 +222,17 @@ public class DynamoDbService { } public void putItem(String tableName, JsonNode item) { - putItem(tableName, item, null, null, null, regionResolver.getDefaultRegion()); + putItem(tableName, item, null, null, null, regionResolver.getDefaultRegion(), "NONE"); } public void putItem(String tableName, JsonNode item, String region) { - putItem(tableName, item, null, null, null, region); + putItem(tableName, item, null, null, null, region, "NONE"); } public void putItem(String tableName, JsonNode item, String conditionExpression, JsonNode exprAttrNames, JsonNode exprAttrValues, - String region) { + String region, String returnValuesOnConditionCheckFailure) { String storageKey = regionKey(region, tableName); TableDefinition table = tableStore.get(storageKey) .orElseThrow(() -> resourceNotFoundException(tableName)); @@ -242,7 +243,7 @@ public class DynamoDbService { JsonNode existing = tableItems.get(itemKey); if (conditionExpression != null) { - evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues); + evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues, returnValuesOnConditionCheckFailure); } tableItems.put(itemKey, item); @@ -276,17 +277,17 @@ public class DynamoDbService { } public JsonNode deleteItem(String tableName, JsonNode key) { - return deleteItem(tableName, key, null, null, null, regionResolver.getDefaultRegion()); + return deleteItem(tableName, key, null, null, null, regionResolver.getDefaultRegion(), "NONE"); } public JsonNode deleteItem(String tableName, JsonNode key, String region) { - return deleteItem(tableName, key, null, null, null, region); + return deleteItem(tableName, key, null, null, null, region, "NONE"); } public JsonNode deleteItem(String tableName, JsonNode key, String conditionExpression, JsonNode exprAttrNames, JsonNode exprAttrValues, - String region) { + String region, String returnValuesOnConditionCheckFailure) { String storageKey = regionKey(region, tableName); TableDefinition table = tableStore.get(storageKey) .orElseThrow(() -> resourceNotFoundException(tableName)); @@ -297,7 +298,7 @@ public class DynamoDbService { if (conditionExpression != null) { JsonNode existing = items.get(itemKey); - evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues); + evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues, returnValuesOnConditionCheckFailure); } JsonNode removed = items.remove(itemKey); @@ -317,11 +318,11 @@ public class DynamoDbService { } public UpdateResult updateItem(String tableName, JsonNode key, JsonNode attributeUpdates, - String updateExpression, - JsonNode expressionAttrNames, JsonNode expressionAttrValues, - String returnValues) { + String updateExpression, + JsonNode expressionAttrNames, JsonNode expressionAttrValues, + String returnValues) { return updateItem(tableName, key, attributeUpdates, updateExpression, expressionAttrNames, - expressionAttrValues, returnValues, null, regionResolver.getDefaultRegion()); + expressionAttrValues, returnValues, null, regionResolver.getDefaultRegion(), "NONE"); } public UpdateResult updateItem(String tableName, JsonNode key, JsonNode attributeUpdates, @@ -329,13 +330,14 @@ public class DynamoDbService { JsonNode expressionAttrNames, JsonNode expressionAttrValues, String returnValues, String region) { return updateItem(tableName, key, attributeUpdates, updateExpression, expressionAttrNames, - expressionAttrValues, returnValues, null, region); + expressionAttrValues, returnValues, null, region, "NONE"); } public UpdateResult updateItem(String tableName, JsonNode key, JsonNode attributeUpdates, String updateExpression, JsonNode expressionAttrNames, JsonNode expressionAttrValues, - String returnValues, String conditionExpression, String region) { + String returnValues, String conditionExpression, String region, + String returnValuesOnConditionCheckFailure) { String storageKey = regionKey(region, tableName); TableDefinition table = tableStore.get(storageKey) .orElseThrow(() -> resourceNotFoundException(tableName)); @@ -347,7 +349,7 @@ public class DynamoDbService { JsonNode existing = items.get(itemKey); if (conditionExpression != null) { - evaluateCondition(existing, conditionExpression, expressionAttrNames, expressionAttrValues); + evaluateCondition(existing, conditionExpression, expressionAttrNames, expressionAttrValues, returnValuesOnConditionCheckFailure); } ObjectNode item; @@ -684,8 +686,9 @@ public class DynamoDbService { String updateExpression = upd.has("UpdateExpression") ? upd.get("UpdateExpression").asText() : null; JsonNode exprAttrNames = upd.has("ExpressionAttributeNames") ? upd.get("ExpressionAttributeNames") : null; JsonNode exprAttrValues = upd.has("ExpressionAttributeValues") ? upd.get("ExpressionAttributeValues") : null; + //there is no ConditionExpression, so setting returnValuesOnConditionCheckFailure = "NONE" updateItem(tableName, key, null, updateExpression, exprAttrNames, exprAttrValues, - "NONE", null, region); + "NONE", null, region, "NONE"); } // ConditionCheck-only items are handled in the first pass only } @@ -710,6 +713,8 @@ public class DynamoDbService { if (conditionExpression == null) { return null; } + String returnValuesOnConditionCheckFailure = target.has("ReturnValuesOnConditionCheckFailure") + ? target.get("ReturnValuesOnConditionCheckFailure").asText() : null; String tableName = target.path("TableName").asText(); JsonNode key = transactItem.has("Put") ? target.get("Item") : target.get("Key"); @@ -725,7 +730,7 @@ public class DynamoDbService { JsonNode existing = tableItems != null ? tableItems.get(itemKey) : null; try { - evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues); + evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues, returnValuesOnConditionCheckFailure); return null; } catch (AwsException e) { return e.getMessage(); @@ -908,10 +913,14 @@ public class DynamoDbService { // --- Condition expression evaluation --- private void evaluateCondition(JsonNode existingItem, String conditionExpression, - JsonNode exprAttrNames, JsonNode exprAttrValues) { + JsonNode exprAttrNames, JsonNode exprAttrValues, String returnValuesOnConditionCheckFailure) { if (!matchesFilterExpression(existingItem, conditionExpression, exprAttrNames, exprAttrValues)) { - throw new AwsException("ConditionalCheckFailedException", - "The conditional request failed", 400); + if ("ALL_OLD".equals(returnValuesOnConditionCheckFailure)){ + throw new ConditionalCheckFailedException(existingItem); + } + else { + throw new ConditionalCheckFailedException(null); + } } } diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/ConditionalCheckFailedException.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/ConditionalCheckFailedException.java new file mode 100644 index 0000000000..1f3ce2058a --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/ConditionalCheckFailedException.java @@ -0,0 +1,20 @@ +package io.github.hectorvent.floci.services.dynamodb.model; + +import io.github.hectorvent.floci.core.common.AwsException; +import com.fasterxml.jackson.databind.JsonNode; + +import java.util.Map; + +public class ConditionalCheckFailedException extends io.github.hectorvent.floci.core.common.AwsException { + + private final JsonNode item; + + public ConditionalCheckFailedException(JsonNode Item) { + super("ConditionalCheckFailedException", "The conditional request failed", 400); + this.item = Item; + } + + public JsonNode getItem() { + return item; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java index a5555cd985..f60fdc6090 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java @@ -464,7 +464,7 @@ public class AslExecutor { ? input.get("ExpressionAttributeNames") : null; JsonNode exprAttrValues = input.has("ExpressionAttributeValues") ? input.get("ExpressionAttributeValues") : null; - dynamoDbService.putItem(tableName, item, conditionExpr, exprAttrNames, exprAttrValues, region); + dynamoDbService.putItem(tableName, item, conditionExpr, exprAttrNames, exprAttrValues, region, "NONE"); return objectMapper.createObjectNode(); } case "getItem" -> { @@ -484,7 +484,7 @@ public class AslExecutor { ? input.get("ExpressionAttributeNames") : null; JsonNode exprAttrValues = input.has("ExpressionAttributeValues") ? input.get("ExpressionAttributeValues") : null; - dynamoDbService.deleteItem(tableName, key, conditionExpr, exprAttrNames, exprAttrValues, region); + dynamoDbService.deleteItem(tableName, key, conditionExpr, exprAttrNames, exprAttrValues, region, "NONE"); return objectMapper.createObjectNode(); } case "scan" -> { @@ -523,7 +523,7 @@ public class AslExecutor { DynamoDbService.UpdateResult result = dynamoDbService.updateItem( tableName, key, attributeUpdates, updateExpression, exprAttrNames, exprAttrValues, returnValues, - conditionExpression, region); + conditionExpression, region, "NONE"); ObjectNode response = objectMapper.createObjectNode(); if ("ALL_NEW".equals(returnValues) && result.newItem() != null) { diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java index a5bc3562a2..46a3484cc8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java @@ -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() diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java index b50e4a7c90..3d68e40f4e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java @@ -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()); + } }