use of software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest in project aws-sdk-java-v2 by aws.
the class AsyncTransactWriteItemsTest method mixedCommands.
@Test
public void mixedCommands() {
mappedTable1.putItem(r -> r.item(RECORDS_1.get(0))).join();
mappedTable2.putItem(r -> r.item(RECORDS_2.get(0))).join();
Expression conditionExpression = Expression.builder().expression("#attribute = :attribute").expressionValues(singletonMap(":attribute", stringValue("0"))).expressionNames(singletonMap("#attribute", "attribute")).build();
Key key = Key.builder().partitionValue(0).build();
TransactWriteItemsEnhancedRequest transactWriteItemsEnhancedRequest = TransactWriteItemsEnhancedRequest.builder().addConditionCheck(mappedTable1, ConditionCheck.builder().key(key).conditionExpression(conditionExpression).build()).addPutItem(mappedTable2, RECORDS_2.get(1)).addUpdateItem(mappedTable1, RECORDS_1.get(1)).addDeleteItem(mappedTable2, RECORDS_2.get(0)).build();
enhancedAsyncClient.transactWriteItems(transactWriteItemsEnhancedRequest).join();
assertThat(mappedTable1.getItem(r -> r.key(k -> k.partitionValue(1))).join(), is(RECORDS_1.get(1)));
assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(0))).join(), is(nullValue()));
assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(1))).join(), is(RECORDS_2.get(1)));
}
use of software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest in project aws-sdk-java-v2 by aws.
the class AsyncTransactWriteItemsTest method mixedCommands_conditionCheckFailsTransaction.
@Test
public void mixedCommands_conditionCheckFailsTransaction() {
mappedTable1.putItem(r -> r.item(RECORDS_1.get(0))).join();
mappedTable2.putItem(r -> r.item(RECORDS_2.get(0))).join();
Expression conditionExpression = Expression.builder().expression("#attribute = :attribute").expressionValues(singletonMap(":attribute", stringValue("1"))).expressionNames(singletonMap("#attribute", "attribute")).build();
Key key = Key.builder().partitionValue(0).build();
TransactWriteItemsEnhancedRequest transactWriteItemsEnhancedRequest = TransactWriteItemsEnhancedRequest.builder().addPutItem(mappedTable2, RECORDS_2.get(1)).addUpdateItem(mappedTable1, RECORDS_1.get(1)).addConditionCheck(mappedTable1, ConditionCheck.builder().key(key).conditionExpression(conditionExpression).build()).addDeleteItem(mappedTable2, RECORDS_2.get(0)).build();
try {
enhancedAsyncClient.transactWriteItems(transactWriteItemsEnhancedRequest).join();
fail("Expected CompletionException to be thrown");
} catch (CompletionException e) {
assertThat(e.getCause(), instanceOf(TransactionCanceledException.class));
}
assertThat(mappedTable1.getItem(r -> r.key(k -> k.partitionValue(1))).join(), is(nullValue()));
assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(0))).join(), is(RECORDS_2.get(0)));
assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(1))).join(), is(nullValue()));
}
use of software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest in project aws-sdk-java-v2 by aws.
the class TransactWriteItemsTest method mixedCommands_returnValuesOnConditionCheckFailureSet_allConditionsFail.
@Test
public void mixedCommands_returnValuesOnConditionCheckFailureSet_allConditionsFail() {
mappedTable1.putItem(r -> r.item(RECORDS_1.get(0)));
mappedTable1.putItem(r -> r.item(RECORDS_1.get(1)));
mappedTable2.putItem(r -> r.item(RECORDS_2.get(0)));
Expression conditionExpression = Expression.builder().expression("#attribute = :attribute").expressionValues(singletonMap(":attribute", stringValue("99"))).expressionNames(singletonMap("#attribute", "attribute")).build();
Key key0 = Key.builder().partitionValue(0).build();
Key key1 = Key.builder().partitionValue(1).build();
ReturnValuesOnConditionCheckFailure returnValues = ReturnValuesOnConditionCheckFailure.ALL_OLD;
TransactPutItemEnhancedRequest<Record2> putItemRequest = TransactPutItemEnhancedRequest.builder(Record2.class).conditionExpression(conditionExpression).item(RECORDS_2.get(0)).returnValuesOnConditionCheckFailure(returnValues).build();
TransactUpdateItemEnhancedRequest<Record1> updateItemRequest = TransactUpdateItemEnhancedRequest.builder(Record1.class).conditionExpression(conditionExpression).item(RECORDS_1.get(0)).returnValuesOnConditionCheckFailure(returnValues).build();
TransactDeleteItemEnhancedRequest deleteItemRequest = TransactDeleteItemEnhancedRequest.builder().key(key1).conditionExpression(conditionExpression).returnValuesOnConditionCheckFailure(returnValues).build();
TransactWriteItemsEnhancedRequest transactWriteItemsEnhancedRequest = TransactWriteItemsEnhancedRequest.builder().addPutItem(mappedTable2, putItemRequest).addUpdateItem(mappedTable1, updateItemRequest).addConditionCheck(mappedTable1, ConditionCheck.builder().key(key0).conditionExpression(conditionExpression).returnValuesOnConditionCheckFailure(returnValues).build()).addDeleteItem(mappedTable1, deleteItemRequest).build();
try {
enhancedClient.transactWriteItems(transactWriteItemsEnhancedRequest);
fail("Expected TransactionCanceledException to be thrown");
} catch (TransactionCanceledException e) {
List<CancellationReason> cancellationReasons = e.cancellationReasons();
assertThat(cancellationReasons.size(), is(4));
cancellationReasons.forEach(r -> assertThat(r.item().isEmpty(), is(false)));
}
}
use of software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest in project aws-sdk-java-v2 by aws.
the class TransactWriteItemsTest method mixedCommands_conditionCheckFailsTransaction.
@Test
public void mixedCommands_conditionCheckFailsTransaction() {
mappedTable1.putItem(r -> r.item(RECORDS_1.get(0)));
mappedTable2.putItem(r -> r.item(RECORDS_2.get(0)));
Expression conditionExpression = Expression.builder().expression("#attribute = :attribute").expressionValues(singletonMap(":attribute", stringValue("1"))).expressionNames(singletonMap("#attribute", "attribute")).build();
Key key = Key.builder().partitionValue(0).build();
TransactWriteItemsEnhancedRequest transactWriteItemsEnhancedRequest = TransactWriteItemsEnhancedRequest.builder().addPutItem(mappedTable2, RECORDS_2.get(1)).addUpdateItem(mappedTable1, RECORDS_1.get(1)).addConditionCheck(mappedTable1, ConditionCheck.builder().key(key).conditionExpression(conditionExpression).build()).addDeleteItem(mappedTable2, RECORDS_2.get(0)).build();
try {
enhancedClient.transactWriteItems(transactWriteItemsEnhancedRequest);
fail("Expected TransactionCanceledException to be thrown");
} catch (TransactionCanceledException ignored) {
}
assertThat(mappedTable1.getItem(r -> r.key(k -> k.partitionValue(1))), is(nullValue()));
assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(0))), is(RECORDS_2.get(0)));
assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(1))), is(nullValue()));
}
use of software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest in project aws-sdk-java-v2 by aws.
the class TransactWriteItemsTest method mixedCommands.
@Test
public void mixedCommands() {
mappedTable1.putItem(r -> r.item(RECORDS_1.get(0)));
mappedTable2.putItem(r -> r.item(RECORDS_2.get(0)));
Expression conditionExpression = Expression.builder().expression("#attribute = :attribute").expressionValues(singletonMap(":attribute", stringValue("0"))).expressionNames(singletonMap("#attribute", "attribute")).build();
Key key = Key.builder().partitionValue(0).build();
TransactWriteItemsEnhancedRequest transactWriteItemsEnhancedRequest = TransactWriteItemsEnhancedRequest.builder().addConditionCheck(mappedTable1, ConditionCheck.builder().key(key).conditionExpression(conditionExpression).build()).addPutItem(mappedTable2, RECORDS_2.get(1)).addUpdateItem(mappedTable1, RECORDS_1.get(1)).addDeleteItem(mappedTable2, RECORDS_2.get(0)).build();
enhancedClient.transactWriteItems(transactWriteItemsEnhancedRequest);
assertThat(mappedTable1.getItem(r -> r.key(k -> k.partitionValue(1))), is(RECORDS_1.get(1)));
assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(0))), is(nullValue()));
assertThat(mappedTable2.getItem(r -> r.key(k -> k.partitionValue(1))), is(RECORDS_2.get(1)));
}
Aggregations