Search in sources :

Example 1 with DeleteAction

use of software.amazon.awssdk.enhanced.dynamodb.update.DeleteAction 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 DeleteAction

use of software.amazon.awssdk.enhanced.dynamodb.update.DeleteAction in project aws-sdk-java-v2 by aws.

the class UpdateItemOperationTest method generateRequest_withExtensions_singleUpdateExpression.

@Test
public void generateRequest_withExtensions_singleUpdateExpression() {
    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();
    FakeItem item = createUniqueFakeItem();
    when(mockDynamoDbEnhancedClientExtension.beforeWrite(any(DynamoDbExtensionContext.BeforeWrite.class))).thenReturn(WriteModification.builder().updateExpression(updateExpression).build());
    UpdateItemOperation<FakeItem> updateItemOperation = UpdateItemOperation.create(requestFakeItem(item, b -> b.ignoreNulls(true)));
    UpdateItemRequest request = updateItemOperation.generateRequest(FakeItem.getTableSchema(), PRIMARY_CONTEXT, mockDynamoDbEnhancedClientExtension);
    assertThat(request.updateExpression(), is("DELETE attr1 :val"));
    assertThat(request.expressionAttributeValues(), is(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) 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)

Aggregations

Collections (java.util.Collections)2 Collections.singletonList (java.util.Collections.singletonList)2 Collections.singletonMap (java.util.Collections.singletonMap)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UUID (java.util.UUID)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.either (org.hamcrest.Matchers.either)2 Matchers.hasEntry (org.hamcrest.Matchers.hasEntry)2 Matchers.is (org.hamcrest.Matchers.is)2 Matchers.nullValue (org.hamcrest.Matchers.nullValue)2 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)2 Assert.fail (org.junit.Assert.fail)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2