Search in sources :

Example 1 with OperationContext

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

the class UpdateItemOperationTest method generateTransactWriteItem_basicRequest.

@Test
public void generateTransactWriteItem_basicRequest() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
    OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
    String updateExpression = "update-expression";
    Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
    Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
    UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(fakeItemMap).updateExpression(updateExpression).expressionAttributeValues(attributeValues).expressionAttributeNames(attributeNames).build();
    doReturn(updateItemRequest).when(updateItemOperation).generateRequest(any(), any(), any());
    TransactWriteItem actualResult = updateItemOperation.generateTransactWriteItem(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
    TransactWriteItem expectedResult = TransactWriteItem.builder().update(Update.builder().key(fakeItemMap).tableName(TABLE_NAME).updateExpression(updateExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
    assertThat(actualResult, is(expectedResult));
    verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Also used : OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Matchers.containsString(org.hamcrest.Matchers.containsString) 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 OperationContext

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

the class UpdateItemOperationTest method generateTransactWriteItem_conditionalRequest.

@Test
public void generateTransactWriteItem_conditionalRequest() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
    OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
    String updateExpression = "update-expression";
    String conditionExpression = "condition-expression";
    Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
    Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
    UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(fakeItemMap).updateExpression(updateExpression).conditionExpression(conditionExpression).expressionAttributeValues(attributeValues).expressionAttributeNames(attributeNames).build();
    doReturn(updateItemRequest).when(updateItemOperation).generateRequest(any(), any(), any());
    TransactWriteItem actualResult = updateItemOperation.generateTransactWriteItem(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
    TransactWriteItem expectedResult = TransactWriteItem.builder().update(Update.builder().key(fakeItemMap).tableName(TABLE_NAME).updateExpression(updateExpression).conditionExpression(conditionExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
    assertThat(actualResult, is(expectedResult));
    verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Also used : OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Matchers.containsString(org.hamcrest.Matchers.containsString) 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 3 with OperationContext

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

the class CreateTableOperation method generateRequest.

@Override
public CreateTableRequest generateRequest(TableSchema<T> tableSchema, OperationContext operationContext, DynamoDbEnhancedClientExtension extension) {
    if (!TableMetadata.primaryIndexName().equals(operationContext.indexName())) {
        throw new IllegalArgumentException("PutItem cannot be executed against a secondary index.");
    }
    String primaryPartitionKey = tableSchema.tableMetadata().primaryPartitionKey();
    Optional<String> primarySortKey = tableSchema.tableMetadata().primarySortKey();
    Set<String> dedupedIndexKeys = new HashSet<>();
    dedupedIndexKeys.add(primaryPartitionKey);
    primarySortKey.ifPresent(dedupedIndexKeys::add);
    List<software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex> sdkGlobalSecondaryIndices = null;
    List<software.amazon.awssdk.services.dynamodb.model.LocalSecondaryIndex> sdkLocalSecondaryIndices = null;
    if (this.request.globalSecondaryIndices() != null) {
        sdkGlobalSecondaryIndices = this.request.globalSecondaryIndices().stream().map(gsi -> {
            String indexPartitionKey = tableSchema.tableMetadata().indexPartitionKey(gsi.indexName());
            Optional<String> indexSortKey = tableSchema.tableMetadata().indexSortKey(gsi.indexName());
            dedupedIndexKeys.add(indexPartitionKey);
            indexSortKey.ifPresent(dedupedIndexKeys::add);
            return software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex.builder().indexName(gsi.indexName()).keySchema(generateKeySchema(indexPartitionKey, indexSortKey.orElse(null))).projection(gsi.projection()).provisionedThroughput(gsi.provisionedThroughput()).build();
        }).collect(Collectors.toList());
    }
    if (this.request.localSecondaryIndices() != null) {
        sdkLocalSecondaryIndices = this.request.localSecondaryIndices().stream().map(lsi -> {
            Optional<String> indexSortKey = tableSchema.tableMetadata().indexSortKey(lsi.indexName());
            indexSortKey.ifPresent(dedupedIndexKeys::add);
            if (!primaryPartitionKey.equals(tableSchema.tableMetadata().indexPartitionKey(lsi.indexName()))) {
                throw new IllegalArgumentException("Attempt to create a local secondary index with a partition " + "key that is not the primary partition key. Index name: " + lsi.indexName());
            }
            return software.amazon.awssdk.services.dynamodb.model.LocalSecondaryIndex.builder().indexName(lsi.indexName()).keySchema(generateKeySchema(primaryPartitionKey, indexSortKey.orElse(null))).projection(lsi.projection()).build();
        }).collect(Collectors.toList());
    }
    List<AttributeDefinition> attributeDefinitions = dedupedIndexKeys.stream().map(attribute -> AttributeDefinition.builder().attributeName(attribute).attributeType(tableSchema.tableMetadata().scalarAttributeType(attribute).orElseThrow(() -> new IllegalArgumentException("Could not map the key attribute '" + attribute + "' to a valid scalar type."))).build()).collect(Collectors.toList());
    BillingMode billingMode = this.request.provisionedThroughput() == null ? BillingMode.PAY_PER_REQUEST : BillingMode.PROVISIONED;
    return CreateTableRequest.builder().tableName(operationContext.tableName()).keySchema(generateKeySchema(primaryPartitionKey, primarySortKey.orElse(null))).globalSecondaryIndexes(sdkGlobalSecondaryIndices).localSecondaryIndexes(sdkLocalSecondaryIndices).attributeDefinitions(attributeDefinitions).billingMode(billingMode).provisionedThroughput(this.request.provisionedThroughput()).build();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Arrays(java.util.Arrays) Collection(java.util.Collection) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) KeyType(software.amazon.awssdk.services.dynamodb.model.KeyType) Set(java.util.Set) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) CompletableFuture(java.util.concurrent.CompletableFuture) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) CreateTableEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.CreateTableEnhancedRequest) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) Optional(java.util.Optional) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) Collections(java.util.Collections) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) HashSet(java.util.HashSet)

Example 4 with OperationContext

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

the class UpdateItemOperationTransactTest method generateTransactWriteItem_basicRequest.

@Test
public void generateTransactWriteItem_basicRequest() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
    OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
    String updateExpression = "update-expression";
    Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
    Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
    UpdateItemRequest.Builder builder = ddbRequestBuilder(fakeItemMap);
    builder.updateExpression(updateExpression);
    builder.expressionAttributeValues(attributeValues);
    builder.expressionAttributeNames(attributeNames);
    UpdateItemRequest updateItemRequest = builder.build();
    doReturn(updateItemRequest).when(updateItemOperation).generateRequest(any(), any(), any());
    TransactWriteItem actualResult = updateItemOperation.generateTransactWriteItem(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
    TransactWriteItem expectedResult = TransactWriteItem.builder().update(Update.builder().key(fakeItemMap).tableName(TABLE_NAME).updateExpression(updateExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
    assertThat(actualResult, is(expectedResult));
    verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Also used : OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) 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 5 with OperationContext

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

the class UpdateItemOperation method generateRequest.

@Override
public UpdateItemRequest generateRequest(TableSchema<T> tableSchema, OperationContext operationContext, DynamoDbEnhancedClientExtension extension) {
    if (!TableMetadata.primaryIndexName().equals(operationContext.indexName())) {
        throw new IllegalArgumentException("UpdateItem cannot be executed against a secondary index.");
    }
    T item = request.map(UpdateItemEnhancedRequest::item, TransactUpdateItemEnhancedRequest::item);
    Boolean ignoreNulls = request.map(r -> Optional.ofNullable(r.ignoreNulls()), r -> Optional.ofNullable(r.ignoreNulls())).orElse(null);
    Map<String, AttributeValue> itemMap = tableSchema.itemToMap(item, Boolean.TRUE.equals(ignoreNulls));
    TableMetadata tableMetadata = tableSchema.tableMetadata();
    WriteModification transformation = extension != null ? extension.beforeWrite(DefaultDynamoDbExtensionContext.builder().items(itemMap).operationContext(operationContext).tableMetadata(tableMetadata).tableSchema(tableSchema).operationName(operationName()).build()) : null;
    if (transformation != null && transformation.transformedItem() != null) {
        itemMap = transformation.transformedItem();
    }
    Collection<String> primaryKeys = tableSchema.tableMetadata().primaryKeys();
    Map<String, AttributeValue> keyAttributes = filterMap(itemMap, entry -> primaryKeys.contains(entry.getKey()));
    Map<String, AttributeValue> nonKeyAttributes = filterMap(itemMap, entry -> !primaryKeys.contains(entry.getKey()));
    Expression updateExpression = generateUpdateExpressionIfExist(tableMetadata, transformation, nonKeyAttributes);
    Expression conditionExpression = generateConditionExpressionIfExist(transformation, request);
    Map<String, String> expressionNames = coalesceExpressionNames(updateExpression, conditionExpression);
    Map<String, AttributeValue> expressionValues = coalesceExpressionValues(updateExpression, conditionExpression);
    UpdateItemRequest.Builder requestBuilder = UpdateItemRequest.builder().tableName(operationContext.tableName()).key(keyAttributes).returnValues(ReturnValue.ALL_NEW);
    if (request.left().isPresent()) {
        addPlainUpdateItemParameters(requestBuilder, request.left().get());
    }
    if (updateExpression != null) {
        requestBuilder.updateExpression(updateExpression.expression());
    }
    if (conditionExpression != null) {
        requestBuilder.conditionExpression(conditionExpression.expression());
    }
    if (CollectionUtils.isNotEmpty(expressionNames)) {
        requestBuilder = requestBuilder.expressionAttributeNames(expressionNames);
    }
    if (CollectionUtils.isNotEmpty(expressionValues)) {
        requestBuilder = requestBuilder.expressionAttributeValues(expressionValues);
    }
    return requestBuilder.build();
}
Also used : TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) CompletableFuture(java.util.concurrent.CompletableFuture) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) Function(java.util.function.Function) Update(software.amazon.awssdk.services.dynamodb.model.Update) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) CollectionUtils(software.amazon.awssdk.utils.CollectionUtils) Map(java.util.Map) CollectionUtils.filterMap(software.amazon.awssdk.utils.CollectionUtils.filterMap) UpdateExpressionConverter(software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionConverter) UpdateExpressionUtils.operationExpression(software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionUtils.operationExpression) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) EnhancedClientUtils.readAndTransformSingleItem(software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.readAndTransformSingleItem) Collection(java.util.Collection) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) WriteModification(software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification) Either(software.amazon.awssdk.utils.Either) UpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest) DefaultDynamoDbExtensionContext(software.amazon.awssdk.enhanced.dynamodb.internal.extensions.DefaultDynamoDbExtensionContext) List(java.util.List) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) UpdateItemEnhancedResponse(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedResponse) OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Optional(java.util.Optional) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) ReturnValue(software.amazon.awssdk.services.dynamodb.model.ReturnValue) UpdateItemResponse(software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) WriteModification(software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) UpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) TransactUpdateItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.TransactUpdateItemEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) UpdateExpression(software.amazon.awssdk.enhanced.dynamodb.update.UpdateExpression) UpdateExpressionUtils.operationExpression(software.amazon.awssdk.enhanced.dynamodb.internal.update.UpdateExpressionUtils.operationExpression)

Aggregations

OperationContext (software.amazon.awssdk.enhanced.dynamodb.OperationContext)16 Test (org.junit.Test)14 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)14 TransactWriteItem (software.amazon.awssdk.services.dynamodb.model.TransactWriteItem)14 FakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem)13 FakeItem.createUniqueFakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem)12 UpdateItemRequest (software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest)7 DynamoDbEnhancedClientExtension (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension)6 TableMetadata (software.amazon.awssdk.enhanced.dynamodb.TableMetadata)6 Map (java.util.Map)5 Collections (java.util.Collections)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.is (org.hamcrest.Matchers.is)4 RunWith (org.junit.runner.RunWith)4 Mock (org.mockito.Mock)4 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)4 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)4 AttributeValues.stringValue (software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.stringValue)4 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)4 ReturnValue (software.amazon.awssdk.services.dynamodb.model.ReturnValue)4