Search in sources :

Example 1 with UpdateExpression

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)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Matchers.either(org.hamcrest.Matchers.either) AttributeValues.numberValue(software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.numberValue) Mock(org.mockito.Mock) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) RunWith(org.junit.runner.RunWith) FakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort) HashMap(java.util.HashMap) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) Collections.singletonList(java.util.Collections.singletonList) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Assert.fail(org.junit.Assert.fail) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) ReadModification(software.amazon.awssdk.enhanced.dynamodb.extensions.ReadModification) ReturnConsumedCapacity(software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) WriteModification(software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) UpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest) Consumer(java.util.function.Consumer) DefaultDynamoDbExtensionContext(software.amazon.awssdk.enhanced.dynamodb.internal.extensions.DefaultDynamoDbExtensionContext) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) Stream(java.util.stream.Stream) DeleteAction(software.amazon.awssdk.enhanced.dynamodb.update.DeleteAction) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) FakeItemWithSort.createUniqueFakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort.createUniqueFakeItemWithSort) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Matchers.is(org.hamcrest.Matchers.is) DynamoDbExtensionContext(software.amazon.awssdk.enhanced.dynamodb.DynamoDbExtensionContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ReturnValue(software.amazon.awssdk.services.dynamodb.model.ReturnValue) ReturnItemCollectionMetrics(software.amazon.awssdk.services.dynamodb.model.ReturnItemCollectionMetrics) UpdateItemResponse(software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) DeleteAction(software.amazon.awssdk.enhanced.dynamodb.update.DeleteAction) Matchers.containsString(org.hamcrest.Matchers.containsString) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Example 2 with UpdateExpression

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");
}
Also used : UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) Test(org.junit.jupiter.api.Test)

Example 3 with UpdateExpression

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();
}
Also used : Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) Test(org.junit.jupiter.api.Test)

Example 4 with UpdateExpression

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);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) HashMap(java.util.HashMap) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) Test(org.junit.jupiter.api.Test)

Example 5 with UpdateExpression

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"));
}
Also used : Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) Test(org.junit.jupiter.api.Test)

Aggregations

UpdateExpression (software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression)38 Test (org.junit.jupiter.api.Test)31 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)24 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)12 HashMap (java.util.HashMap)10 DynamoDbEnhancedClientExtension (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension)4 WriteModification (software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification)4 Map (java.util.Map)3 Test (org.junit.Test)3 DynamoDbExtensionContext (software.amazon.awssdk.enhanced.dynamodb.DynamoDbExtensionContext)3 OperationContext (software.amazon.awssdk.enhanced.dynamodb.OperationContext)3 TableMetadata (software.amazon.awssdk.enhanced.dynamodb.TableMetadata)3 DefaultDynamoDbExtensionContext (software.amazon.awssdk.enhanced.dynamodb.internal.extensions.DefaultDynamoDbExtensionContext)3 Collections (java.util.Collections)2 Collections.singletonList (java.util.Collections.singletonList)2 Collections.singletonMap (java.util.Collections.singletonMap)2 UUID (java.util.UUID)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2