Skip to content

Commit

Permalink
Add returnValuesOnConditionCheckFailure param to UpdateItemEnhancedRe…
Browse files Browse the repository at this point in the history
…quest (aws#4442)
  • Loading branch information
breader124 committed Nov 19, 2023
1 parent a2d7f98 commit 8ece964
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ private UpdateItemRequest.Builder addPlainUpdateItemParameters(UpdateItemRequest
UpdateItemEnhancedRequest<?> enhancedRequest) {
requestBuilder = requestBuilder.returnConsumedCapacity(enhancedRequest.returnConsumedCapacityAsString());
requestBuilder = requestBuilder.returnItemCollectionMetrics(enhancedRequest.returnItemCollectionMetricsAsString());
requestBuilder =
requestBuilder.returnValuesOnConditionCheckFailure(enhancedRequest.returnValuesOnConditionCheckFailureAsString());
return requestBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.ReturnItemCollectionMetrics;
import software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest;

/**
Expand All @@ -46,6 +47,7 @@ public final class UpdateItemEnhancedRequest<T> {
private final Expression conditionExpression;
private final String returnConsumedCapacity;
private final String returnItemCollectionMetrics;
private final String returnValuesOnConditionCheckFailure;


private UpdateItemEnhancedRequest(Builder<T> builder) {
Expand All @@ -54,6 +56,7 @@ private UpdateItemEnhancedRequest(Builder<T> builder) {
this.conditionExpression = builder.conditionExpression;
this.returnConsumedCapacity = builder.returnConsumedCapacity;
this.returnItemCollectionMetrics = builder.returnItemCollectionMetrics;
this.returnValuesOnConditionCheckFailure = builder.returnValuesOnConditionCheckFailure;
}

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

/**
Expand Down Expand Up @@ -138,6 +142,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 @@ -151,7 +163,8 @@ public boolean equals(Object o) {
&& Objects.equals(ignoreNulls, that.ignoreNulls)
&& Objects.equals(conditionExpression, that.conditionExpression)
&& Objects.equals(returnConsumedCapacity, that.returnConsumedCapacity)
&& Objects.equals(returnItemCollectionMetrics, that.returnItemCollectionMetrics);
&& Objects.equals(returnItemCollectionMetrics, that.returnItemCollectionMetrics)
&& Objects.equals(returnValuesOnConditionCheckFailure, that.returnValuesOnConditionCheckFailure);
}

@Override
Expand All @@ -161,6 +174,7 @@ public int hashCode() {
result = 31 * result + (conditionExpression != null ? conditionExpression.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 @@ -176,6 +190,7 @@ public static final class Builder<T> {
private Expression conditionExpression;
private String returnConsumedCapacity;
private String returnItemCollectionMetrics;
private String returnValuesOnConditionCheckFailure;

private Builder() {
}
Expand Down Expand Up @@ -261,6 +276,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 UpdateItemEnhancedRequest<T> build() {
return new UpdateItemEnhancedRequest<>(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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.UpdateItemRequest;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse;

Expand Down Expand Up @@ -587,6 +588,36 @@ public void generateRequest_withReturnItemCollectionMetrics_knownValue_generates
assertThat(request, is(expectedRequest));
}

@Test
public void generateRequest_withReturnValuesOnConditionCheckFailure_unknownValue_generatesCorrectRequest() {
FakeItem item = createUniqueFakeItem();
String returnValuesOnConditionCheckFailure = UUID.randomUUID().toString();

UpdateItemOperation<FakeItem> updateItemOperation =
UpdateItemOperation.create(requestFakeItem(item, b -> b.ignoreNulls(true)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)));

UpdateItemRequest request = updateItemOperation.generateRequest(FakeItem.getTableSchema(), PRIMARY_CONTEXT, null);
UpdateItemRequest expectedRequest = ddbRequest(ddbKey(item.getId()), b -> b.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure));

assertThat(request, is(expectedRequest));
}

@Test
public void generateRequest_withReturnValuesOnConditionCheckFailure_knownValue_generatesCorrectRequest() {
FakeItem item = createUniqueFakeItem();
ReturnValuesOnConditionCheckFailure returnValuesOnConditionCheckFailure = ReturnValuesOnConditionCheckFailure.ALL_OLD;

UpdateItemOperation<FakeItem> updateItemOperation =
UpdateItemOperation.create(requestFakeItem(item, b -> b.ignoreNulls(true)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)));

UpdateItemRequest request = updateItemOperation.generateRequest(FakeItem.getTableSchema(), PRIMARY_CONTEXT, null);
UpdateItemRequest expectedRequest = ddbRequest(ddbKey(item.getId()), b -> b.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure));

assertThat(request, is(expectedRequest));
}

@Test
public void transformResponse_withExtension_returnsCorrectTransformedItem() {
FakeItem baseFakeItem = createUniqueFakeItem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem;
import software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.ReturnItemCollectionMetrics;
import software.amazon.awssdk.services.dynamodb.model.ReturnValuesOnConditionCheckFailure;

@RunWith(MockitoJUnitRunner.class)
public class UpdateItemEnhancedRequestTest {
Expand All @@ -45,6 +46,7 @@ public void builder_minimal() {
assertThat(builtObject.returnConsumedCapacityAsString(), is(nullValue()));
assertThat(builtObject.returnItemCollectionMetrics(), is(nullValue()));
assertThat(builtObject.returnItemCollectionMetricsAsString(), is(nullValue()));
assertThat(builtObject.returnValuesOnConditionCheckFailure(), is(nullValue()));
}

@Test
Expand All @@ -61,13 +63,15 @@ public void builder_maximal() {

ReturnConsumedCapacity returnConsumedCapacity = ReturnConsumedCapacity.TOTAL;
ReturnItemCollectionMetrics returnItemCollectionMetrics = ReturnItemCollectionMetrics.SIZE;
ReturnValuesOnConditionCheckFailure returnValuesOnConditionCheckFailure = ReturnValuesOnConditionCheckFailure.ALL_OLD;

UpdateItemEnhancedRequest<FakeItem> builtObject = UpdateItemEnhancedRequest.builder(FakeItem.class)
.item(fakeItem)
.ignoreNulls(true)
.conditionExpression(conditionExpression)
.returnConsumedCapacity(returnConsumedCapacity)
.returnItemCollectionMetrics(returnItemCollectionMetrics)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build();

assertThat(builtObject.item(), is(fakeItem));
Expand All @@ -77,6 +81,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 @@ -93,13 +98,15 @@ public void toBuilder() {

ReturnConsumedCapacity returnConsumedCapacity = ReturnConsumedCapacity.TOTAL;
ReturnItemCollectionMetrics returnItemCollectionMetrics = ReturnItemCollectionMetrics.SIZE;
ReturnValuesOnConditionCheckFailure returnValuesOnConditionCheckFailure = ReturnValuesOnConditionCheckFailure.ALL_OLD;

UpdateItemEnhancedRequest<FakeItem> builtObject = UpdateItemEnhancedRequest.builder(FakeItem.class)
.item(fakeItem)
.ignoreNulls(true)
.conditionExpression(conditionExpression)
.returnConsumedCapacity(returnConsumedCapacity)
.returnItemCollectionMetrics(returnItemCollectionMetrics)
.returnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure)
.build();

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

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

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

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

@Test
public void hashCode_minimal() {
UpdateItemEnhancedRequest<FakeItem> emptyRequest = UpdateItemEnhancedRequest.builder(FakeItem.class).build();
Expand Down Expand Up @@ -273,4 +293,15 @@ public void hashCode_includesReturnItemCollectionMetrics() {

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

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

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

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

0 comments on commit 8ece964

Please sign in to comment.