|
| 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.document; |
| 17 | + |
| 18 | +import static software.amazon.awssdk.enhanced.dynamodb.document.DefaultEnhancedDocumentTest.ARRAY_AND_MAP_IN_JSON; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static software.amazon.awssdk.enhanced.dynamodb.document.DefaultEnhancedDocumentTest.NUMBER_STRING_ARRAY; |
| 21 | +import static software.amazon.awssdk.enhanced.dynamodb.document.DefaultEnhancedDocumentTest.STRINGS_ARRAY; |
| 22 | + |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import java.util.stream.Stream; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.Arguments; |
| 32 | +import org.junit.jupiter.params.provider.MethodSource; |
| 33 | +import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; |
| 34 | + |
| 35 | +public class EnhancedDocumentTest { |
| 36 | + |
| 37 | + static String INNER_JSON = "{\"1\": [\"a\", \"b\", \"c\"],\"2\": 1}"; |
| 38 | + |
| 39 | + private static Stream<Arguments> documentsCreatedFromStaticMethods() { |
| 40 | + Map<String, Object> map = getStringObjectMap(); |
| 41 | + return Stream.of( |
| 42 | + Arguments.of(EnhancedDocument.fromJson(ARRAY_AND_MAP_IN_JSON)), |
| 43 | + Arguments.of(EnhancedDocument.fromMap(map))); |
| 44 | + } |
| 45 | + |
| 46 | + private static Map<String, Object> getStringObjectMap() { |
| 47 | + Map<String, Object> map = new LinkedHashMap<>(); |
| 48 | + map.put("numberKey", 1); |
| 49 | + map.put("numberList", Arrays.asList(1, 2, 3)); |
| 50 | + Map<String, Object> innerMap = new LinkedHashMap<>(); |
| 51 | + map.put("mapKey", innerMap); |
| 52 | + |
| 53 | + innerMap.put("1", Arrays.asList(STRINGS_ARRAY)); |
| 54 | + innerMap.put("2", 1); |
| 55 | + return map; |
| 56 | + } |
| 57 | + |
| 58 | + @ParameterizedTest |
| 59 | + @MethodSource("documentsCreatedFromStaticMethods") |
| 60 | + void createFromJson(EnhancedDocument enhancedDocument) { |
| 61 | + assertThat(enhancedDocument.toJson()).isEqualTo(ARRAY_AND_MAP_IN_JSON); |
| 62 | + |
| 63 | + enhancedDocument.getJson("mapKey").equals(INNER_JSON); |
| 64 | + |
| 65 | + assertThat(enhancedDocument.getSdkNumber("numberKey").intValue()).isEqualTo(1); |
| 66 | + |
| 67 | + assertThat(enhancedDocument.getList("numberList") |
| 68 | + .stream() |
| 69 | + .map( o ->Integer.parseInt(o.toString()) ) |
| 70 | + .collect(Collectors.toList())) |
| 71 | + .isEqualTo(Arrays.stream(NUMBER_STRING_ARRAY) |
| 72 | + .map(s -> Integer.parseInt(s)) |
| 73 | + .collect(Collectors.toList())); |
| 74 | + |
| 75 | + assertThat(enhancedDocument.getList("numberList", EnhancedType.of(String.class))) |
| 76 | + .isEqualTo(Arrays.asList(NUMBER_STRING_ARRAY)); |
| 77 | + |
| 78 | + |
| 79 | + assertThat(enhancedDocument.getMapAsDocument("mapKey").toJson()) |
| 80 | + .isEqualTo(EnhancedDocument.fromJson(INNER_JSON).toJson()); |
| 81 | + |
| 82 | + // This is same as V1, where the Json List of String is identified as List of Strings rather than set of string |
| 83 | + assertThat(enhancedDocument.getMapAsDocument("mapKey").getList("1")).isEqualTo(Arrays.asList(STRINGS_ARRAY)); |
| 84 | + assertThat(enhancedDocument.getMapAsDocument("mapKey").getStringSet("1")).isNull(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void nullArgsInStaticConstructor(){ |
| 89 | + assertThat(EnhancedDocument.fromMap(null)).isNull(); |
| 90 | + assertThat(EnhancedDocument.fromJson(null)).isNull(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void accessingStringSetFromBuilderMethods(){ |
| 95 | + |
| 96 | + Set<String> stringSet = Stream.of(STRINGS_ARRAY).collect(Collectors.toSet()); |
| 97 | + EnhancedDocument enhancedDocument = EnhancedDocument.builder() |
| 98 | + .addStringSet("stringSet", stringSet) |
| 99 | + .build(); |
| 100 | + |
| 101 | + assertThat(enhancedDocument.getStringSet("stringSet")).isEqualTo(stringSet); |
| 102 | + assertThat(enhancedDocument.getList("stringSet")).isNull(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void toBuilderOverwritingOldJson(){ |
| 107 | + EnhancedDocument document = EnhancedDocument.fromJson(ARRAY_AND_MAP_IN_JSON); |
| 108 | + assertThat(document.toJson()).isEqualTo(ARRAY_AND_MAP_IN_JSON); |
| 109 | + EnhancedDocument fromBuilder = document.toBuilder().json(INNER_JSON).build(); |
| 110 | + assertThat(fromBuilder.toJson()).isEqualTo(INNER_JSON); |
| 111 | + } |
| 112 | +} |
0 commit comments