Skip to content

Commit f082af9

Browse files
authored
Implement return values for enhanced DeleteItem (#2719)
This commit introduces DynamoDbTable#deleteItemWithResponse() that allows customers to specify additional parameters on the request such as ReturnConsumedCapacity to get additional information the service response.
1 parent 90a0b9b commit f082af9

File tree

16 files changed

+1440
-22
lines changed

16 files changed

+1440
-22
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "DynamoDB Enhanced Client",
3+
"contributor": "",
4+
"type": "feature",
5+
"description": "This commit introduces DynamoDbTable#deleteItemWithResponse() that allows customers to specify additional parameters on the request such as ReturnConsumedCapacity to get additional information the service response."
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.enhanced.dynamodb;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey;
20+
import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primarySortKey;
21+
import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.secondaryPartitionKey;
22+
import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.secondarySortKey;
23+
24+
import java.util.Objects;
25+
import org.junit.AfterClass;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema;
29+
import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedResponse;
30+
import software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex;
31+
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
32+
import software.amazon.awssdk.services.dynamodb.model.Projection;
33+
import software.amazon.awssdk.services.dynamodb.model.ProjectionType;
34+
import software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity;
35+
import software.amazon.awssdk.services.dynamodb.model.ReturnItemCollectionMetrics;
36+
37+
public class AsyncDeleteItemWithResponseIntegrationTest extends DynamoDbEnhancedIntegrationTestBase {
38+
private static class Record {
39+
private Integer id;
40+
private Integer id2;
41+
private String stringAttr1;
42+
43+
private Integer getId() {
44+
return id;
45+
}
46+
47+
private Record setId(Integer id) {
48+
this.id = id;
49+
return this;
50+
}
51+
52+
private Integer getId2() {
53+
return id2;
54+
}
55+
56+
private Record setId2(Integer id2) {
57+
this.id2 = id2;
58+
return this;
59+
}
60+
61+
private String getStringAttr1() {
62+
return stringAttr1;
63+
}
64+
65+
private Record setStringAttr1(String stringAttr1) {
66+
this.stringAttr1 = stringAttr1;
67+
return this;
68+
}
69+
70+
@Override
71+
public boolean equals(Object o) {
72+
if (this == o) {
73+
return true;
74+
}
75+
if (o == null || getClass() != o.getClass()) {
76+
return false;
77+
}
78+
Record record = (Record) o;
79+
return Objects.equals(id, record.id)
80+
&& Objects.equals(id2, record.id2)
81+
&& Objects.equals(stringAttr1, record.stringAttr1);
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return Objects.hash(id, id2, stringAttr1);
87+
}
88+
}
89+
90+
private static final String TABLE_NAME = createTestTableName();
91+
92+
private static final TableSchema<Record> TABLE_SCHEMA =
93+
StaticTableSchema.builder(Record.class)
94+
.newItemSupplier(Record::new)
95+
.addAttribute(Integer.class, a -> a.name("id_1")
96+
.getter(Record::getId)
97+
.setter(Record::setId)
98+
.tags(primaryPartitionKey(), secondaryPartitionKey("index1")))
99+
.addAttribute(Integer.class, a -> a.name("id_2")
100+
.getter(Record::getId2)
101+
.setter(Record::setId2)
102+
.tags(primarySortKey(), secondarySortKey("index1")))
103+
.addAttribute(String.class, a -> a.name("stringAttr1")
104+
.getter(Record::getStringAttr1)
105+
.setter(Record::setStringAttr1))
106+
.build();
107+
108+
private static final EnhancedLocalSecondaryIndex LOCAL_SECONDARY_INDEX = EnhancedLocalSecondaryIndex.builder()
109+
.indexName("index1")
110+
.projection(Projection.builder()
111+
.projectionType(ProjectionType.ALL)
112+
.build())
113+
.build();
114+
115+
private static DynamoDbAsyncClient dynamoDbClient;
116+
private static DynamoDbEnhancedAsyncClient enhancedClient;
117+
private static DynamoDbAsyncTable<Record> mappedTable;
118+
119+
@BeforeClass
120+
public static void setup() {
121+
dynamoDbClient = createAsyncDynamoDbClient();
122+
enhancedClient = DynamoDbEnhancedAsyncClient.builder().dynamoDbClient(dynamoDbClient).build();
123+
mappedTable = enhancedClient.table(TABLE_NAME, TABLE_SCHEMA);
124+
mappedTable.createTable(r -> r.localSecondaryIndices(LOCAL_SECONDARY_INDEX)).join();
125+
dynamoDbClient.waiter().waitUntilTableExists(r -> r.tableName(TABLE_NAME)).join();
126+
}
127+
128+
@AfterClass
129+
public static void teardown() {
130+
try {
131+
dynamoDbClient.deleteTable(r -> r.tableName(TABLE_NAME));
132+
} finally {
133+
dynamoDbClient.close();
134+
}
135+
}
136+
137+
@Test
138+
public void deleteItem_returnConsumedCapacity_unset_consumedCapacityNull() {
139+
Key key = Key.builder().partitionValue(1).sortValue(10).build();
140+
141+
DeleteItemEnhancedResponse<Record> response = mappedTable.deleteItemWithResponse(r -> r.key(key)).join();
142+
143+
assertThat(response.consumedCapacity()).isNull();
144+
}
145+
146+
@Test
147+
public void deleteItem_returnConsumedCapacity_set_consumedCapacityNotNull() {
148+
Key key = Key.builder().partitionValue(1).sortValue(10).build();
149+
150+
DeleteItemEnhancedResponse<Record> response =
151+
mappedTable.deleteItemWithResponse(r -> r.key(key).returnConsumedCapacity(ReturnConsumedCapacity.TOTAL)).join();
152+
153+
assertThat(response.consumedCapacity()).isNotNull();
154+
}
155+
156+
@Test
157+
public void delete_returnItemCollectionMetrics_set_itemCollectionMetricsNotNull() {
158+
Key key = Key.builder().partitionValue(1).sortValue(10).build();
159+
160+
DeleteItemEnhancedResponse<Record> response =
161+
mappedTable.deleteItemWithResponse(r -> r.key(key).returnItemCollectionMetrics(ReturnItemCollectionMetrics.SIZE))
162+
.join();
163+
164+
assertThat(response.itemCollectionMetrics()).isNotNull();
165+
}
166+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.enhanced.dynamodb;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey;
20+
import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primarySortKey;
21+
import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.secondaryPartitionKey;
22+
import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.secondarySortKey;
23+
24+
import java.util.Objects;
25+
import org.junit.AfterClass;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema;
29+
import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedResponse;
30+
import software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex;
31+
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
32+
import software.amazon.awssdk.services.dynamodb.model.Projection;
33+
import software.amazon.awssdk.services.dynamodb.model.ProjectionType;
34+
import software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity;
35+
import software.amazon.awssdk.services.dynamodb.model.ReturnItemCollectionMetrics;
36+
37+
public class DeleteItemWithResponseIntegrationTest extends DynamoDbEnhancedIntegrationTestBase {
38+
private static class Record {
39+
private Integer id;
40+
private Integer id2;
41+
private String stringAttr1;
42+
43+
private Integer getId() {
44+
return id;
45+
}
46+
47+
private Record setId(Integer id) {
48+
this.id = id;
49+
return this;
50+
}
51+
52+
private Integer getId2() {
53+
return id2;
54+
}
55+
56+
private Record setId2(Integer id2) {
57+
this.id2 = id2;
58+
return this;
59+
}
60+
61+
private String getStringAttr1() {
62+
return stringAttr1;
63+
}
64+
65+
private Record setStringAttr1(String stringAttr1) {
66+
this.stringAttr1 = stringAttr1;
67+
return this;
68+
}
69+
70+
@Override
71+
public boolean equals(Object o) {
72+
if (this == o) {
73+
return true;
74+
}
75+
if (o == null || getClass() != o.getClass()) {
76+
return false;
77+
}
78+
Record record = (Record) o;
79+
return Objects.equals(id, record.id)
80+
&& Objects.equals(id2, record.id2)
81+
&& Objects.equals(stringAttr1, record.stringAttr1);
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return Objects.hash(id, id2, stringAttr1);
87+
}
88+
}
89+
90+
private static final String TABLE_NAME = createTestTableName();
91+
92+
private static final TableSchema<Record> TABLE_SCHEMA =
93+
StaticTableSchema.builder(Record.class)
94+
.newItemSupplier(Record::new)
95+
.addAttribute(Integer.class, a -> a.name("id_1")
96+
.getter(Record::getId)
97+
.setter(Record::setId)
98+
.tags(primaryPartitionKey(), secondaryPartitionKey("index1")))
99+
.addAttribute(Integer.class, a -> a.name("id_2")
100+
.getter(Record::getId2)
101+
.setter(Record::setId2)
102+
.tags(primarySortKey(), secondarySortKey("index1")))
103+
.addAttribute(String.class, a -> a.name("stringAttr1")
104+
.getter(Record::getStringAttr1)
105+
.setter(Record::setStringAttr1))
106+
.build();
107+
108+
private static final EnhancedLocalSecondaryIndex LOCAL_SECONDARY_INDEX = EnhancedLocalSecondaryIndex.builder()
109+
.indexName("index1")
110+
.projection(Projection.builder()
111+
.projectionType(ProjectionType.ALL)
112+
.build())
113+
.build();
114+
115+
private static DynamoDbClient dynamoDbClient;
116+
private static DynamoDbEnhancedClient enhancedClient;
117+
private static DynamoDbTable<Record> mappedTable;
118+
119+
@BeforeClass
120+
public static void setup() {
121+
dynamoDbClient = createDynamoDbClient();
122+
enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(dynamoDbClient).build();
123+
mappedTable = enhancedClient.table(TABLE_NAME, TABLE_SCHEMA);
124+
mappedTable.createTable(r -> r.localSecondaryIndices(LOCAL_SECONDARY_INDEX));
125+
dynamoDbClient.waiter().waitUntilTableExists(r -> r.tableName(TABLE_NAME));
126+
}
127+
128+
@AfterClass
129+
public static void teardown() {
130+
try {
131+
dynamoDbClient.deleteTable(r -> r.tableName(TABLE_NAME));
132+
} finally {
133+
dynamoDbClient.close();
134+
}
135+
}
136+
137+
@Test
138+
public void deleteItem_returnConsumedCapacity_unset_consumedCapacityNull() {
139+
Key key = Key.builder().partitionValue(1).sortValue(10).build();
140+
141+
DeleteItemEnhancedResponse<Record> response = mappedTable.deleteItemWithResponse(r -> r.key(key));
142+
143+
assertThat(response.consumedCapacity()).isNull();
144+
}
145+
146+
@Test
147+
public void deleteItem_returnConsumedCapacity_set_consumedCapacityNotNull() {
148+
Key key = Key.builder().partitionValue(1).sortValue(10).build();
149+
150+
DeleteItemEnhancedResponse<Record> response =
151+
mappedTable.deleteItemWithResponse(r -> r.key(key).returnConsumedCapacity(ReturnConsumedCapacity.TOTAL));
152+
153+
assertThat(response.consumedCapacity()).isNotNull();
154+
}
155+
156+
@Test
157+
public void delete_returnItemCollectionMetrics_set_itemCollectionMetricsNotNull() {
158+
Key key = Key.builder().partitionValue(1).sortValue(10).build();
159+
160+
DeleteItemEnhancedResponse<Record> response =
161+
mappedTable.deleteItemWithResponse(r -> r.key(key).returnItemCollectionMetrics(ReturnItemCollectionMetrics.SIZE));
162+
163+
assertThat(response.itemCollectionMetrics()).isNotNull();
164+
}
165+
}

0 commit comments

Comments
 (0)