|
| 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.functionaltests.document; |
| 17 | + |
| 18 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | +import static org.hamcrest.Matchers.is; |
| 20 | +import static software.amazon.awssdk.enhanced.dynamodb.AttributeConverterProvider.defaultProvider; |
| 21 | + |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.LinkedHashSet; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import java.util.stream.Stream; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import software.amazon.awssdk.core.SdkBytes; |
| 33 | +import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType; |
| 34 | +import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; |
| 35 | +import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; |
| 36 | +import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; |
| 37 | +import software.amazon.awssdk.enhanced.dynamodb.TableMetadata; |
| 38 | +import software.amazon.awssdk.enhanced.dynamodb.TableSchema; |
| 39 | +import software.amazon.awssdk.enhanced.dynamodb.document.EnhancedDocument; |
| 40 | +import software.amazon.awssdk.enhanced.dynamodb.functionaltests.LocalDynamoDbSyncTestBase; |
| 41 | +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; |
| 42 | +import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest; |
| 43 | + |
| 44 | +public class ComplexInputItemsTests extends LocalDynamoDbSyncTestBase { |
| 45 | + private final String tableName = getConcreteTableName("table-name"); |
| 46 | + private DynamoDbEnhancedClient enhancedClient; |
| 47 | + private DynamoDbClient lowLevelClient; |
| 48 | + private DynamoDbTable<EnhancedDocument> docMappedtable; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setUp() { |
| 52 | + lowLevelClient = getDynamoDbClient(); |
| 53 | + enhancedClient = DynamoDbEnhancedClient.builder() |
| 54 | + .dynamoDbClient(lowLevelClient) |
| 55 | + .build(); |
| 56 | + docMappedtable = enhancedClient.table(tableName, |
| 57 | + TableSchema.documentSchemaBuilder() |
| 58 | + .addIndexPartitionKey(TableMetadata.primaryIndexName(), |
| 59 | + "id", |
| 60 | + AttributeValueType.S) |
| 61 | + .addIndexSortKey(TableMetadata.primaryIndexName(), "sort", |
| 62 | + AttributeValueType.N) |
| 63 | + .attributeConverterProviders(defaultProvider()) |
| 64 | + .build()); |
| 65 | + docMappedtable.createTable(); |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void deleteTable() { |
| 70 | + getDynamoDbClient().deleteTable(DeleteTableRequest.builder() |
| 71 | + .tableName(tableName) |
| 72 | + .build()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void getAndPutDocumentWithNoAttributeConverters() { |
| 77 | + |
| 78 | + docMappedtable.putItem(EnhancedDocument.builder() |
| 79 | + .putString("id", "one") |
| 80 | + .putNumber("sort", 1) |
| 81 | + .putString("element", "noAttributeConverter") |
| 82 | + .build()); |
| 83 | + |
| 84 | + EnhancedDocument noConverterInGetItem = docMappedtable.getItem(EnhancedDocument.builder() |
| 85 | + .putString("id", "one") |
| 86 | + .putNumber("sort", 1) |
| 87 | + .build()); |
| 88 | + assertThat(noConverterInGetItem.toJson(), is("{\"id\":\"one\",\"sort\":1,\"element\":\"noAttributeConverter\"}")); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + @Test |
| 94 | + public void bytesInAlTypes() { |
| 95 | + |
| 96 | + Map<String, SdkBytes> bytesMap = new LinkedHashMap<>(); |
| 97 | + bytesMap.put("key1", SdkBytes.fromByteArray("1".getBytes(StandardCharsets.UTF_8))); |
| 98 | + bytesMap.put("key2", SdkBytes.fromByteArray("2".getBytes(StandardCharsets.UTF_8))); |
| 99 | + EnhancedDocument enhancedDocument = EnhancedDocument.builder() |
| 100 | + .putString("id", "allTypesBytes") |
| 101 | + .putNumber("sort", 2) |
| 102 | + .put("put", SdkBytes.fromUtf8String("1"), SdkBytes.class) |
| 103 | + .putBytes("putBytes", |
| 104 | + SdkBytes.fromByteBuffer(ByteBuffer.wrap("1".getBytes(StandardCharsets.UTF_8)))) |
| 105 | + .putBytesSet("putBytesSet", |
| 106 | + Stream.of(SdkBytes.fromUtf8String("1"), |
| 107 | + SdkBytes.fromUtf8String("2")).collect(Collectors.toCollection(LinkedHashSet::new))) |
| 108 | + .putList("putBytesList", |
| 109 | + Stream.of(SdkBytes.fromUtf8String("1"), |
| 110 | + SdkBytes.fromUtf8String("2")).collect(Collectors.toList()), |
| 111 | + EnhancedType.of(SdkBytes.class)) |
| 112 | + .putMap("bytesMap", bytesMap, EnhancedType.of(String.class), |
| 113 | + EnhancedType.of(SdkBytes.class)) |
| 114 | + .build(); |
| 115 | + |
| 116 | + docMappedtable.putItem(enhancedDocument); |
| 117 | + |
| 118 | + |
| 119 | + EnhancedDocument retrievedItem = docMappedtable.getItem(EnhancedDocument.builder() |
| 120 | + .putString("id", "allTypesBytes") |
| 121 | + .putNumber("sort", 2) |
| 122 | + .build()); |
| 123 | + |
| 124 | + assertThat(retrievedItem.toJson(), is("{\"putBytesSet\":[\"MQ==\",\"Mg==\"],\"putBytes\":\"MQ==\",\"putBytesList\":[\"MQ==\",\"Mg==\"],\"id\":\"allTypesBytes\",\"sort\":2,\"bytesMap\":{\"key1\":\"MQ==\",\"key2\":\"Mg==\"},\"put\":\"MQ==\"}" |
| 125 | + )); |
| 126 | + } |
| 127 | +} |
0 commit comments