Skip to content

Commit d2edb87

Browse files
committed
Add test coverage
1 parent 15a25be commit d2edb87

File tree

3 files changed

+183
-11
lines changed

3 files changed

+183
-11
lines changed

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/document/DocumentTableSchemaTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,4 @@ void validate_DocumentTableSchema_WithCustomIntegerAttributeProvider() {
237237
Assertions.assertThat(
238238
documentTableSchema.itemToMap(numberDocument, true)).isEqualTo(resultMap);
239239
}
240-
241-
@Test
242-
void validate_toJson_WithNonAsciiEmoji() {
243-
EnhancedDocument doc = EnhancedDocument.builder()
244-
.attributeConverterProviders(DefaultAttributeConverterProvider.create())
245-
.putString("emoji", "Hello 😀 World")
246-
.build();
247-
248-
String json = doc.toJson();
249-
assertThat(json).isEqualTo("{\"emoji\":\"Hello 😀 World\"}");
250-
}
251240
}

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/document/EnhancedDocumentTest.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,4 +496,122 @@ void error_when_usingClassGetPut_for_CollectionValues(){
496496
() -> EnhancedDocument.builder().build().get("listKey" , List.class))
497497
.withMessage("Values of type List are not supported by this API, please use the getList API instead");
498498
}
499+
500+
@Test
501+
void toJson_numberFormatting_veryLargeNumbers() {
502+
EnhancedDocument doc = EnhancedDocument.builder()
503+
.attributeConverterProviders(defaultProvider())
504+
.putNumber("longMax", Long.MAX_VALUE)
505+
.putNumber("longMin", Long.MIN_VALUE)
506+
.putNumber("doubleMax", Double.MAX_VALUE)
507+
.putNumber("scientific", 1.23e+100)
508+
.putNumber("manyDecimals", 1.123456789012345)
509+
.build();
510+
511+
String json = doc.toJson();
512+
assertThat(json).isNotNull();
513+
}
514+
515+
@Test
516+
void toJson_numberFormatting_trailingZeros() {
517+
EnhancedDocument doc = EnhancedDocument.builder()
518+
.attributeConverterProviders(defaultProvider())
519+
.putNumber("twoPointZero", 2.0)
520+
.putNumber("tenPointZeroZero", 10.00)
521+
.build();
522+
523+
String json = doc.toJson();
524+
assertThat(json).isNotNull();
525+
}
526+
527+
@Test
528+
void toJson_stringEscaping_allControlCharacters() {
529+
EnhancedDocument doc = EnhancedDocument.builder()
530+
.attributeConverterProviders(defaultProvider())
531+
.putString("allEscapes", "line1\nline2\ttab\"quote\\backslash\r\f")
532+
.build();
533+
534+
String json = doc.toJson();
535+
assertThat(json).contains("\\n").contains("\\t").contains("\\\"").contains("\\\\");
536+
}
537+
538+
@Test
539+
void toJson_stringEscaping_forwardSlash() {
540+
EnhancedDocument doc = EnhancedDocument.builder()
541+
.attributeConverterProviders(defaultProvider())
542+
.putString("slash", "path/to/resource")
543+
.build();
544+
545+
String json = doc.toJson();
546+
assertThat(json).isNotNull();
547+
}
548+
549+
@Test
550+
void toJson_emptyString() {
551+
EnhancedDocument doc = EnhancedDocument.builder()
552+
.attributeConverterProviders(defaultProvider())
553+
.putString("empty", "")
554+
.build();
555+
556+
String json = doc.toJson();
557+
assertThat(json).contains("\"empty\":\"\"");
558+
}
559+
560+
@Test
561+
void toJson_bytesEncoding_emptyBytes() {
562+
EnhancedDocument doc = EnhancedDocument.builder()
563+
.attributeConverterProviders(defaultProvider())
564+
.putBytes("empty", SdkBytes.fromByteArray(new byte[0]))
565+
.build();
566+
567+
String json = doc.toJson();
568+
assertThat(json).isNotNull();
569+
}
570+
571+
@Test
572+
void toJson_bytesEncoding_largeBytes() {
573+
byte[] largeArray = new byte[10000];
574+
Arrays.fill(largeArray, (byte) 65); // Fill with 'A'
575+
576+
EnhancedDocument doc = EnhancedDocument.builder()
577+
.attributeConverterProviders(defaultProvider())
578+
.putBytes("large", SdkBytes.fromByteArray(largeArray))
579+
.build();
580+
581+
String json = doc.toJson();
582+
assertThat(json).isNotNull();
583+
}
584+
585+
@Test
586+
void toJson_listWithAllNulls() {
587+
EnhancedDocument doc = EnhancedDocument.builder()
588+
.attributeConverterProviders(defaultProvider())
589+
.putJson("nullList", "[null,null,null]")
590+
.build();
591+
592+
String json = doc.toJson();
593+
assertThat(json).contains("[null,null,null]");
594+
}
595+
596+
@Test
597+
void toJson_mapWithSpecialCharKeys() {
598+
EnhancedDocument doc = EnhancedDocument.builder()
599+
.attributeConverterProviders(defaultProvider())
600+
.putJson("specialKeys", "{\"key with spaces\":1,\"key\\\"with\\\"quotes\":2,\"key/with/slash\":3}")
601+
.build();
602+
603+
String json = doc.toJson();
604+
assertThat(json).isNotNull();
605+
}
606+
607+
@Test
608+
void toJson_mapWithNullValues() {
609+
EnhancedDocument doc = EnhancedDocument.builder()
610+
.attributeConverterProviders(defaultProvider())
611+
.putJson("nullValues", "{\"key1\":null,\"key2\":\"value\",\"key3\":null}")
612+
.build();
613+
614+
String json = doc.toJson();
615+
assertThat(json).contains("null");
616+
}
499617
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.benchmark.enhanced.dynamodb;
17+
18+
import org.openjdk.jmh.annotations.Benchmark;
19+
import org.openjdk.jmh.annotations.BenchmarkMode;
20+
import org.openjdk.jmh.annotations.Fork;
21+
import org.openjdk.jmh.annotations.Measurement;
22+
import org.openjdk.jmh.annotations.Mode;
23+
import org.openjdk.jmh.annotations.OutputTimeUnit;
24+
import org.openjdk.jmh.annotations.Scope;
25+
import org.openjdk.jmh.annotations.Setup;
26+
import org.openjdk.jmh.annotations.State;
27+
import org.openjdk.jmh.annotations.Warmup;
28+
import software.amazon.awssdk.enhanced.dynamodb.document.EnhancedDocument;
29+
import com.amazonaws.services.dynamodbv2.document.Item;
30+
31+
import java.io.IOException;
32+
import java.nio.file.Files;
33+
import java.nio.file.Paths;
34+
import java.util.concurrent.TimeUnit;
35+
36+
@BenchmarkMode(Mode.SampleTime)
37+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
38+
@Warmup(iterations = 3, time = 2)
39+
@Measurement(iterations = 5, time = 2)
40+
@Fork(2)
41+
@State(Scope.Benchmark)
42+
public class JsonConversionBenchmark {
43+
44+
private String jsonString;
45+
private EnhancedDocument v2Document;
46+
private Item v1Item;
47+
48+
@Setup
49+
public void setup() throws IOException {
50+
jsonString = new String(Files
51+
.readAllBytes(Paths.get("src/main/java/software/amazon/awssdk/benchmark/enhanced/dynamodb/large_data.json")));
52+
v2Document = EnhancedDocument.fromJson(jsonString);
53+
v1Item = Item.fromJSON(jsonString);
54+
}
55+
56+
@Benchmark
57+
public String v1ToJson() {
58+
return v1Item.toJSON();
59+
}
60+
61+
@Benchmark
62+
public String v2ToJson() {
63+
return v2Document.toJson();
64+
}
65+
}

0 commit comments

Comments
 (0)