use of software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression in project aws-sdk-java-v2 by aws.
the class UpdateItemOperationTest method generateRequest_withExtensions_conditionAndUpdateExpression.
@Test
public void generateRequest_withExtensions_conditionAndUpdateExpression() {
FakeItem baseFakeItem = createUniqueFakeItem();
FakeItem fakeItem = createUniqueFakeItem();
Map<String, AttributeValue> fakeMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
Expression condition = Expression.builder().expression("condition").expressionValues(fakeMap).build();
Map<String, AttributeValue> deleteActionMap = singletonMap(":val", AttributeValue.builder().s("s").build());
DeleteAction deleteAction = DeleteAction.builder().path("attr1").value(":val").expressionValues(deleteActionMap).build();
UpdateExpression updateExpression = UpdateExpression.builder().addAction(deleteAction).build();
when(mockDynamoDbEnhancedClientExtension.beforeWrite(any(DynamoDbExtensionContext.BeforeWrite.class))).thenReturn(WriteModification.builder().additionalConditionalExpression(condition).updateExpression(updateExpression).build());
UpdateItemOperation<FakeItem> updateItemOperation = UpdateItemOperation.create(requestFakeItem(baseFakeItem, b -> b.ignoreNulls(true)));
UpdateItemRequest request = updateItemOperation.generateRequest(FakeItem.getTableSchema(), PRIMARY_CONTEXT, mockDynamoDbEnhancedClientExtension);
assertThat(request.conditionExpression(), is("condition"));
assertThat(request.updateExpression(), is("DELETE attr1 :val"));
assertThat(request.expressionAttributeValues(), is(Expression.joinValues(fakeMap, deleteActionMap)));
}
use of software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression in project aws-sdk-java-v2 by aws.
the class UpdateExpressionConverterTest method findAttributeNames_composedNames_duplicates.
@Test
void findAttributeNames_composedNames_duplicates() {
UpdateExpression updateExpression = createUpdateExpression(addAction("attribute1[1]", string("val1"), null, VALUE_TOKEN), deleteAction("attribute1.nested", string("val2"), KEY_TOKEN, VALUE_TOKEN));
List<String> attributes = UpdateExpressionConverter.findAttributeNames(updateExpression);
assertThat(attributes).containsExactlyInAnyOrder("attribute1", "attribute1");
}
use of software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression in project aws-sdk-java-v2 by aws.
the class UpdateExpressionConverterTest method convert_emptyExpression.
@Test
void convert_emptyExpression() {
UpdateExpression updateExpression = UpdateExpression.builder().build();
Expression expression = UpdateExpressionConverter.toExpression(updateExpression);
assertThat(expression.expression()).isEmpty();
assertThat(expression.expressionNames()).isEmpty();
assertThat(expression.expressionValues()).isEmpty();
}
use of software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression in project aws-sdk-java-v2 by aws.
the class UpdateExpressionConverterTest method convert_setActions.
@Test
void convert_setActions() {
UpdateExpression updateExpression = createUpdateExpression(setAction("attribute1", string("val1"), null, VALUE_TOKEN), setAction("attribute2", string("val2"), null, VALUE_TOKEN));
Expression expression = UpdateExpressionConverter.toExpression(updateExpression);
Map<String, AttributeValue> expectedExpressionValues = new HashMap<>();
expectedExpressionValues.put(":PRE_attribute1", string("val1"));
expectedExpressionValues.put(":PRE_attribute2", string("val2"));
assertThat(expression.expression()).isEqualTo("SET attribute1 = :PRE_attribute1, attribute2 = :PRE_attribute2");
assertThat(expression.expressionNames()).isEmpty();
assertThat(expression.expressionValues()).containsExactlyEntriesOf(expectedExpressionValues);
}
use of software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression in project aws-sdk-java-v2 by aws.
the class UpdateExpressionConverterTest method convert_deleteActions_duplicateAttributes_duplicateValueTokens_createsDdbFailExpression.
/**
* The joining logic in {@link Expression#joinValues(Map, Map)} will be merge the two entries, since here the
* same key will point at the same value.
*
* This would fail when calling DDB; note that UpdateItemOperation always prefixes attribute names and the probability of
* this use case is low.
*/
@Test
void convert_deleteActions_duplicateAttributes_duplicateValueTokens_createsDdbFailExpression() {
UpdateExpression updateExpression = createUpdateExpression(DeleteAction.builder().path("attribute1").value("attribute_ref").putExpressionValue("attribute_ref", string("val1")).build(), DeleteAction.builder().path("attribute1").value("attribute_ref").putExpressionValue("attribute_ref", string("val1")).build());
Expression expression = UpdateExpressionConverter.toExpression(updateExpression);
assertThat(expression.expression()).isEqualTo("DELETE attribute1 attribute_ref, attribute1 attribute_ref");
assertThat(expression.expressionNames()).isEmpty();
assertThat(expression.expressionValues()).containsEntry("attribute_ref", string("val1"));
}
Aggregations