|
| 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.EnhancedLocalSecondaryIndex; |
| 30 | +import software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedRequest; |
| 31 | +import software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedResponse; |
| 32 | +import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; |
| 33 | +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; |
| 34 | +import software.amazon.awssdk.services.dynamodb.model.Projection; |
| 35 | +import software.amazon.awssdk.services.dynamodb.model.ProjectionType; |
| 36 | +import software.amazon.awssdk.services.dynamodb.model.ReturnItemCollectionMetrics; |
| 37 | + |
| 38 | +public class AsyncPutItemWithResponseIntegrationTest extends DynamoDbEnhancedIntegrationTestBase { |
| 39 | + private static class Record { |
| 40 | + private Integer id; |
| 41 | + private Integer id2; |
| 42 | + private String stringAttr1; |
| 43 | + |
| 44 | + private Integer getId() { |
| 45 | + return id; |
| 46 | + } |
| 47 | + |
| 48 | + private Record setId(Integer id) { |
| 49 | + this.id = id; |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + private Integer getId2() { |
| 54 | + return id2; |
| 55 | + } |
| 56 | + |
| 57 | + private Record setId2(Integer id2) { |
| 58 | + this.id2 = id2; |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + private String getStringAttr1() { |
| 63 | + return stringAttr1; |
| 64 | + } |
| 65 | + |
| 66 | + private Record setStringAttr1(String stringAttr1) { |
| 67 | + this.stringAttr1 = stringAttr1; |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean equals(Object o) { |
| 73 | + if (this == o) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + if (o == null || getClass() != o.getClass()) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + Record record = (Record) o; |
| 80 | + return Objects.equals(id, record.id) |
| 81 | + && Objects.equals(id2, record.id2) |
| 82 | + && Objects.equals(stringAttr1, record.stringAttr1); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public int hashCode() { |
| 87 | + return Objects.hash(id, id2, stringAttr1); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static final String TABLE_NAME = createTestTableName(); |
| 92 | + |
| 93 | + private static final TableSchema<Record> TABLE_SCHEMA = |
| 94 | + StaticTableSchema.builder(Record.class) |
| 95 | + .newItemSupplier(Record::new) |
| 96 | + .addAttribute(Integer.class, a -> a.name("id_1") |
| 97 | + .getter(Record::getId) |
| 98 | + .setter(Record::setId) |
| 99 | + .tags(primaryPartitionKey(), secondaryPartitionKey("index1"))) |
| 100 | + .addAttribute(Integer.class, a -> a.name("id_2") |
| 101 | + .getter(Record::getId2) |
| 102 | + .setter(Record::setId2) |
| 103 | + .tags(primarySortKey(), secondarySortKey("index1"))) |
| 104 | + .addAttribute(String.class, a -> a.name("stringAttr1") |
| 105 | + .getter(Record::getStringAttr1) |
| 106 | + .setter(Record::setStringAttr1)) |
| 107 | + .build(); |
| 108 | + |
| 109 | + private static final EnhancedLocalSecondaryIndex LOCAL_SECONDARY_INDEX = EnhancedLocalSecondaryIndex.builder() |
| 110 | + .indexName("index1") |
| 111 | + .projection(Projection.builder() |
| 112 | + .projectionType(ProjectionType.ALL) |
| 113 | + .build()) |
| 114 | + .build(); |
| 115 | + |
| 116 | + private static DynamoDbClient dynamoDbClient; |
| 117 | + private static DynamoDbAsyncClient asyncDynamoDbClient; |
| 118 | + private static DynamoDbEnhancedAsyncClient enhancedClient; |
| 119 | + |
| 120 | + private static DynamoDbAsyncTable<Record> mappedTable; |
| 121 | + |
| 122 | + @BeforeClass |
| 123 | + public static void setup() { |
| 124 | + dynamoDbClient = createDynamoDbClient(); |
| 125 | + asyncDynamoDbClient = createAsyncDynamoDbClient(); |
| 126 | + enhancedClient = DynamoDbEnhancedAsyncClient.builder().dynamoDbClient(asyncDynamoDbClient).build(); |
| 127 | + |
| 128 | + mappedTable = enhancedClient.table(TABLE_NAME, TABLE_SCHEMA); |
| 129 | + mappedTable.createTable(r -> r.localSecondaryIndices(LOCAL_SECONDARY_INDEX)).join(); |
| 130 | + |
| 131 | + dynamoDbClient.waiter().waitUntilTableExists(r -> r.tableName(TABLE_NAME)); |
| 132 | + } |
| 133 | + |
| 134 | + @AfterClass |
| 135 | + public static void teardown() { |
| 136 | + try { |
| 137 | + dynamoDbClient.deleteTable(r -> r.tableName(TABLE_NAME)); |
| 138 | + } finally { |
| 139 | + dynamoDbClient.close(); |
| 140 | + } |
| 141 | + asyncDynamoDbClient.close(); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void putItem_returnItemCollectionMetrics_set_itemCollectionMetricsNull() { |
| 146 | + Record record = new Record().setId(1).setId2(10); |
| 147 | + PutItemEnhancedRequest<Record> request = PutItemEnhancedRequest.builder(Record.class) |
| 148 | + .item(record) |
| 149 | + .build(); |
| 150 | + |
| 151 | + PutItemEnhancedResponse<Record> response = mappedTable.putItemWithResponse(request).join(); |
| 152 | + |
| 153 | + assertThat(response.itemCollectionMetrics()).isNull(); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void putItem_returnItemCollectionMetrics_set_itemCollectionMetricsNotNull() { |
| 158 | + Record record = new Record().setId(1).setId2(10); |
| 159 | + PutItemEnhancedRequest<Record> request = PutItemEnhancedRequest.builder(Record.class) |
| 160 | + .item(record) |
| 161 | + .returnItemCollectionMetrics(ReturnItemCollectionMetrics.SIZE) |
| 162 | + .build(); |
| 163 | + |
| 164 | + PutItemEnhancedResponse<Record> response = mappedTable.putItemWithResponse(request).join(); |
| 165 | + |
| 166 | + assertThat(response.itemCollectionMetrics()).isNotNull(); |
| 167 | + } |
| 168 | +} |
0 commit comments