Skip to content

Commit

Permalink
Add returnValuesOnConditionCheckFailure param to PutItemEnhancedReque…
Browse files Browse the repository at this point in the history
…st (aws#4442)
  • Loading branch information
breader124 committed Nov 19, 2023
1 parent f40b36d commit 4c7f2bb
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ private PutItemRequest.Builder addPlainPutItemParameters(PutItemRequest.Builder
requestBuilder = requestBuilder.returnValues(enhancedRequest.returnValuesAsString());
requestBuilder = requestBuilder.returnConsumedCapacity(enhancedRequest.returnConsumedCapacityAsString());
requestBuilder = requestBuilder.returnItemCollectionMetrics(enhancedRequest.returnItemCollectionMetricsAsString());
requestBuilder = requestBuilder.returnValuesOnConditionCheckFailure(enhancedRequest.returnValuesOnConditionCheckFailureAsString());
return requestBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.ReturnItemCollectionMetrics;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
import software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure;

/**
* Defines parameters used to write an item to a DynamoDb table using the putItem() operation (such as
Expand All @@ -43,13 +44,15 @@ public final class PutItemEnhancedRequest<T> {
private final String returnValues;
private final String returnConsumedCapacity;
private final String returnItemCollectionMetrics;
private final String returnValuesOnConditionCheckFailure;

private PutItemEnhancedRequest(Builder<T> builder) {
this.item = builder.item;
this.conditionExpression = builder.conditionExpression;
this.returnValues = builder.returnValues;
this.returnConsumedCapacity = builder.returnConsumedCapacity;
this.returnItemCollectionMetrics = builder.returnItemCollectionMetrics;
this.returnValuesOnConditionCheckFailure = builder.returnValuesOnConditionCheckFailure;
}

/**
Expand All @@ -71,7 +74,8 @@ public Builder<T> toBuilder() {
.conditionExpression(conditionExpression)
.returnValues(returnValues)
.returnConsumedCapacity(returnConsumedCapacity)
.returnItemCollectionMetrics(returnItemCollectionMetrics);
.returnItemCollectionMetrics(returnItemCollectionMetrics)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure);
}

/**
Expand Down Expand Up @@ -146,6 +150,14 @@ public String returnItemCollectionMetricsAsString() {
return returnItemCollectionMetrics;
}

public ReturnValuesOnConditionCheckFailure returnValuesOnConditionCheckFailure() {
return ReturnValuesOnConditionCheckFailure.fromValue(returnValuesOnConditionCheckFailure);
}

public String returnValuesOnConditionCheckFailureAsString() {
return returnValuesOnConditionCheckFailure;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -159,7 +171,8 @@ public boolean equals(Object o) {
&& Objects.equals(conditionExpression, that.conditionExpression)
&& Objects.equals(returnValues, that.returnValues)
&& Objects.equals(returnConsumedCapacity, that.returnConsumedCapacity)
&& Objects.equals(returnItemCollectionMetrics, that.returnItemCollectionMetrics);
&& Objects.equals(returnItemCollectionMetrics, that.returnItemCollectionMetrics)
&& Objects.equals(returnValuesOnConditionCheckFailure, that.returnValuesOnConditionCheckFailure);
}

@Override
Expand All @@ -169,6 +182,7 @@ public int hashCode() {
result = 31 * result + (returnValues != null ? returnValues.hashCode() : 0);
result = 31 * result + (returnConsumedCapacity != null ? returnConsumedCapacity.hashCode() : 0);
result = 31 * result + (returnItemCollectionMetrics != null ? returnItemCollectionMetrics.hashCode() : 0);
result = 31 * result + (returnValuesOnConditionCheckFailure != null ? returnValuesOnConditionCheckFailure.hashCode() : 0);
return result;
}

Expand All @@ -184,6 +198,7 @@ public static final class Builder<T> {
private String returnValues;
private String returnConsumedCapacity;
private String returnItemCollectionMetrics;
private String returnValuesOnConditionCheckFailure;

private Builder() {
}
Expand Down Expand Up @@ -274,6 +289,17 @@ public Builder<T> returnItemCollectionMetrics(String returnItemCollectionMetrics
return this;
}

public Builder<T> returnValuesOnConditionCheckFailure(ReturnValuesOnConditionCheckFailure returnValuesOnConditionCheckFailure) {
this.returnValuesOnConditionCheckFailure = returnValuesOnConditionCheckFailure == null ? null :
returnValuesOnConditionCheckFailure.toString();
return this;
}

public Builder<T> returnValuesOnConditionCheckFailure(String returnValuesOnConditionCheckFailure) {
this.returnValuesOnConditionCheckFailure = returnValuesOnConditionCheckFailure;
return this;
}

public PutItemEnhancedRequest<T> build() {
return new PutItemEnhancedRequest<>(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.ReturnItemCollectionMetrics;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
import software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure;
import software.amazon.awssdk.services.dynamodb.model.TransactWriteItem;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -351,6 +352,65 @@ public void generateRequest_withReturnItemCollectionMetrics_knownValue_generates
assertThat(request, is(expectedRequest));
}


@Test
public void generateRequest_withReturnValuesOnConditionCheckFailure_unknownValue_generatesCorrectRequest() {
FakeItem fakeItem = createUniqueFakeItem();
fakeItem.setSubclassAttribute("subclass-value");

String returnValuesOnConditionCheckFailure = UUID.randomUUID().toString();

PutItemOperation<FakeItem> putItemOperation =
PutItemOperation.create(PutItemEnhancedRequest.builder(FakeItem.class)
.item(fakeItem)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build());

PutItemRequest request = putItemOperation.generateRequest(FakeItem.getTableSchema(),
PRIMARY_CONTEXT,
null);

Map<String, AttributeValue> expectedItemMap = new HashMap<>();
expectedItemMap.put("id", AttributeValue.builder().s(fakeItem.getId()).build());
expectedItemMap.put("subclass_attribute", AttributeValue.builder().s("subclass-value").build());
PutItemRequest expectedRequest = PutItemRequest.builder()
.tableName(TABLE_NAME)
.item(expectedItemMap)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build();

assertThat(request, is(expectedRequest));
}

@Test
public void generateRequest_withReturnValuesOnConditionCheckFailure_knownValue_generatesCorrectRequest() {
FakeItem fakeItem = createUniqueFakeItem();
fakeItem.setSubclassAttribute("subclass-value");

ReturnValuesOnConditionCheckFailure returnValuesOnConditionCheckFailure = ReturnValuesOnConditionCheckFailure.ALL_OLD;

PutItemOperation<FakeItem> putItemOperation =
PutItemOperation.create(PutItemEnhancedRequest.builder(FakeItem.class)
.item(fakeItem)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build());

PutItemRequest request = putItemOperation.generateRequest(FakeItem.getTableSchema(),
PRIMARY_CONTEXT,
null);

Map<String, AttributeValue> expectedItemMap = new HashMap<>();
expectedItemMap.put("id", AttributeValue.builder().s(fakeItem.getId()).build());
expectedItemMap.put("subclass_attribute", AttributeValue.builder().s("subclass-value").build());
PutItemRequest expectedRequest = PutItemRequest.builder()
.tableName(TABLE_NAME)
.item(expectedItemMap)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build();

assertThat(request, is(expectedRequest));
}

@Test
public void generateRequest_withMinimalConditionExpression() {
FakeItem fakeItem = createUniqueFakeItem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.ReturnItemCollectionMetrics;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
import software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure;

@RunWith(MockitoJUnitRunner.class)
public class PutItemEnhancedRequestTest {
Expand All @@ -48,6 +49,7 @@ public void builder_minimal() {
assertThat(builtObject.returnConsumedCapacityAsString(), is(nullValue()));
assertThat(builtObject.returnItemCollectionMetrics(), is(nullValue()));
assertThat(builtObject.returnItemCollectionMetricsAsString(), is(nullValue()));
assertThat(builtObject.returnValuesOnConditionCheckFailureAsString(), is(nullValue()));
}

@Test
Expand All @@ -65,13 +67,15 @@ public void builder_maximal() {
ReturnValue returnValues = ReturnValue.ALL_OLD;
ReturnConsumedCapacity returnConsumedCapacity = ReturnConsumedCapacity.INDEXES;
ReturnItemCollectionMetrics returnItemCollectionMetrics = ReturnItemCollectionMetrics.SIZE;
ReturnValuesOnConditionCheckFailure returnValuesOnConditionCheckFailure = ReturnValuesOnConditionCheckFailure.ALL_OLD;

PutItemEnhancedRequest<FakeItem> builtObject = PutItemEnhancedRequest.builder(FakeItem.class)
.item(fakeItem)
.conditionExpression(conditionExpression)
.returnValues(returnValues)
.returnConsumedCapacity(returnConsumedCapacity)
.returnItemCollectionMetrics(returnItemCollectionMetrics)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build();

assertThat(builtObject.item(), is(fakeItem));
Expand All @@ -82,6 +86,7 @@ public void builder_maximal() {
assertThat(builtObject.returnConsumedCapacityAsString(), is(returnConsumedCapacity.toString()));
assertThat(builtObject.returnItemCollectionMetrics(), is(returnItemCollectionMetrics));
assertThat(builtObject.returnItemCollectionMetricsAsString(), is(returnItemCollectionMetrics.toString()));
assertThat(builtObject.returnValuesOnConditionCheckFailureAsString(), is(returnValuesOnConditionCheckFailure.toString()));
}

@Test
Expand All @@ -99,13 +104,15 @@ public void toBuilder() {
ReturnValue returnValues = ReturnValue.ALL_OLD;
ReturnConsumedCapacity returnConsumedCapacity = ReturnConsumedCapacity.INDEXES;
ReturnItemCollectionMetrics returnItemCollectionMetrics = ReturnItemCollectionMetrics.SIZE;
ReturnValuesOnConditionCheckFailure returnValuesOnConditionCheckFailure = ReturnValuesOnConditionCheckFailure.ALL_OLD;

PutItemEnhancedRequest<FakeItem> builtObject = PutItemEnhancedRequest.builder(FakeItem.class)
.item(fakeItem)
.conditionExpression(conditionExpression)
.returnValues(returnValues)
.returnConsumedCapacity(returnConsumedCapacity)
.returnItemCollectionMetrics(returnItemCollectionMetrics)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build();

PutItemEnhancedRequest<FakeItem> copiedObject = builtObject.toBuilder().build();
Expand Down Expand Up @@ -195,6 +202,19 @@ public void equals_returnItemCollectionMetricsNotEqual() {
assertThat(builtObject1, not(equalTo(builtObject2)));
}

@Test
public void equals_returnValuesOnConditionCheckFailureNotEqual() {
PutItemEnhancedRequest<FakeItem> builtObject1 = PutItemEnhancedRequest.builder(FakeItem.class)
.returnValuesOnConditionCheckFailure("return1")
.build();

PutItemEnhancedRequest<FakeItem> builtObject2 = PutItemEnhancedRequest.builder(FakeItem.class)
.returnValuesOnConditionCheckFailure("return2")
.build();

assertThat(builtObject1, not(equalTo(builtObject2)));
}

@Test
public void hashCode_minimal() {
PutItemEnhancedRequest<FakeItem> emptyRequest = PutItemEnhancedRequest.builder(FakeItem.class).build();
Expand Down Expand Up @@ -264,6 +284,17 @@ public void hashCode_includesReturnItemCollectionMetrics() {
assertThat(cotnainsReturnItemCollectionMetrics.hashCode(), not(equalTo(emptyRequest.hashCode())));
}

@Test
public void hashCode_returnValuesOnConditionCheckFailure() {
PutItemEnhancedRequest<FakeItem> emptyRequest = PutItemEnhancedRequest.builder(FakeItem.class).build();

PutItemEnhancedRequest<FakeItem> containsItem = PutItemEnhancedRequest.builder(FakeItem.class)
.returnValuesOnConditionCheckFailure("return1")
.build();

assertThat(containsItem.hashCode(), not(equalTo(emptyRequest.hashCode())));
}

@Test
public void builder_returnValueEnumSetter_paramNull_NoNpe() {
PutItemEnhancedRequest.builder(FakeItem.class).returnValues((ReturnValue) null).build();
Expand Down Expand Up @@ -344,4 +375,27 @@ public void returnItemCollectionMetrics_newValue_stringGetter_returnsValue() {

assertThat(builtObject.returnItemCollectionMetricsAsString(), equalTo(newReturnItemCollectionMetrics));
}


@Test
public void returnValuesOnConditionCheckFailure_newValue_returnsUnknownToSdkVersion() {
String returnValuesOnConditionCheckFailure = UUID.randomUUID().toString();

PutItemEnhancedRequest<FakeItem> builtObject = PutItemEnhancedRequest.builder(FakeItem.class)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build();

assertThat(builtObject.returnValuesOnConditionCheckFailure(), equalTo(ReturnValuesOnConditionCheckFailure.UNKNOWN_TO_SDK_VERSION));
}

@Test
public void returnValuesOnConditionCheckFailure_newValue_stringGetter_returnsValue() {
String returnValuesOnConditionCheckFailure = UUID.randomUUID().toString();

PutItemEnhancedRequest<FakeItem> builtObject = PutItemEnhancedRequest.builder(FakeItem.class)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build();

assertThat(builtObject.returnValuesOnConditionCheckFailureAsString(), equalTo(returnValuesOnConditionCheckFailure));
}
}

0 comments on commit 4c7f2bb

Please sign in to comment.