Skip to content

Commit fc88544

Browse files
authored
Functional Test Cases for Document DDB API and Surface API Review 2 comments (#3843)
* Functional Test Cases for Document DDB API and Surface API Review 2 comments * Removed extra newlines from test cases * Handled Review comments * Removed primitive boolean getter and replaced with Boolean getter * Spotbug issue fixed and using StringUtils * StringUtils corrected the right package * Sonar quebe test bug fixed
1 parent 5e15af7 commit fc88544

File tree

13 files changed

+3436
-197
lines changed

13 files changed

+3436
-197
lines changed

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/document/EnhancedDocument.java

Lines changed: 81 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
* {@snippet :
5252
* // CustomAttributeConverterProvider.create() is an example for some Custom converter provider
5353
* EnhancedDocument enhancedDocumentWithCustomConverter = EnhancedDocument.builder().attributeConverterProviders
54-
* (CustomAttributeConverterProvider.create(), AttributeConverterProvide.defaultProvider())
55-
* .putWithType("customObject", customObject, EnhancedType.of(CustomClass.class))
54+
* (CustomAttributeConverterProvider.create(), AttributeConverterProvide.defaultProvider()
55+
* .put("customObject", customObject, EnhancedType.of(CustomClass.class))
5656
* .build();
5757
*}
5858
* <p>Enhanced Document can be created with Json as input using Static factory method.In this case it used
@@ -143,7 +143,7 @@ static Builder builder() {
143143
* <p>
144144
* <b>Retrieving String Type for a document</b>
145145
* {@snippet :
146-
* Custom resultCustom = document.get("key", EnhancedType.of(Custom.class));
146+
* String resultCustom = document.get("key", EnhancedType.of(String.class));
147147
* }
148148
* <b>Retrieving Custom Type for which Convertor Provider was defined while creating the document</b>
149149
* {@snippet :
@@ -166,6 +166,31 @@ static Builder builder() {
166166
*/
167167
<T> T get(String attributeName, EnhancedType<T> type);
168168

169+
/**
170+
* Returns the value of the specified attribute in the current document as a specified class type; or null if the
171+
* attribute either doesn't exist or the attribute value is null.
172+
* <p>
173+
* <b>Retrieving String Type for a document</b>
174+
* {@snippet :
175+
* String resultCustom = document.get("key", String.class);
176+
* }
177+
* <b>Retrieving Custom Type for which Convertor Provider was defined while creating the document</b>
178+
* {@snippet :
179+
* Custom resultCustom = document.get("key", Custom.class);
180+
* }
181+
* <p>
182+
* Note :
183+
* This API should not be used to retrieve values of List and Map types.
184+
* Instead, getList and getMap APIs should be used to retrieve attributes of type List and Map, respectively.
185+
* </p>
186+
* @param attributeName Name of the attribute.
187+
* @param clazz Class type of value.
188+
* @param <T> The type of the attribute value.
189+
* @return Attribute value of type T
190+
* }
191+
*/
192+
<T> T get(String attributeName, Class<T> clazz);
193+
169194
/**
170195
* Gets the String value of specified attribute in the document.
171196
*
@@ -212,8 +237,8 @@ static Builder builder() {
212237
/**
213238
* Gets the Set of String values of the given attribute in the current document.
214239
* @param attributeName Name of the attribute.
215-
* @return value of the specified attribute in the current document as a set of SdkBytes; or null if the attribute either
216-
* doesn't exist or the attribute value is null.
240+
* @return value of the specified attribute in the current document as a set of SdkBytes;
241+
* or null if the attribute doesn't exist.
217242
*/
218243
Set<SdkBytes> getBytesSet(String attributeName);
219244

@@ -223,8 +248,8 @@ static Builder builder() {
223248
* @param attributeName Name of the attribute.
224249
* @param type {@link EnhancedType} of Type T.
225250
* @param <T> Type T of List elements
226-
* @return value of the specified attribute in the current document as a list of type T; or null if the
227-
* attribute either doesn't exist or the attribute value is null.
251+
* @return value of the specified attribute in the current document as a list of type T,
252+
* or null if the attribute does not exist.
228253
*/
229254

230255
<T> List<T> getList(String attributeName, EnhancedType<T> type);
@@ -259,10 +284,13 @@ static Builder builder() {
259284
* Gets the {@link Boolean} value for the specified attribute.
260285
*
261286
* @param attributeName Name of the attribute.
262-
* @return value of the specified attribute in the current document as a non-null Boolean.
287+
* @return value of the specified attribute in the current document as a Boolean representation; or null if the attribute
288+
* either doesn't exist or the attribute value is null.
263289
* @throws RuntimeException
264-
* if either the attribute doesn't exist or if the attribute
265-
* value cannot be converted into a boolean value.
290+
* if the attribute value cannot be converted to a Boolean representation.
291+
* Note that the Boolean representation of 0 and 1 in Numbers and "0" and "1" in Strings is false and true,
292+
* respectively.
293+
*
266294
*/
267295
Boolean getBoolean(String attributeName);
268296

@@ -276,7 +304,7 @@ static Builder builder() {
276304
* @param attributeName Name of the attribute.
277305
* @return value of the specified attribute in the current document as a List of {@link AttributeValue}
278306
*/
279-
List<AttributeValue> getUnknownTypeList(String attributeName);
307+
List<AttributeValue> getListOfUnknownType(String attributeName);
280308

281309
/**
282310
* Retrieves a Map with String keys and corresponding AttributeValue objects as values for a specified attribute in a
@@ -287,7 +315,7 @@ static Builder builder() {
287315
* @param attributeName Name of the attribute.
288316
* @return value of the specified attribute in the current document as a {@link AttributeValue}
289317
*/
290-
Map<String, AttributeValue> getUnknownTypeMap(String attributeName);
318+
Map<String, AttributeValue> getMapOfUnknownType(String attributeName);
291319

292320
/**
293321
*
@@ -350,7 +378,7 @@ interface Builder {
350378
* @param value The boolean value that needs to be set.
351379
* @return Builder instance to construct a {@link EnhancedDocument}
352380
*/
353-
Builder putBoolean(String attributeName, Boolean value);
381+
Builder putBoolean(String attributeName, boolean value);
354382

355383
/**
356384
* Appends an attribute of name attributeName with a null value.
@@ -414,11 +442,33 @@ interface Builder {
414442
* provider.
415443
* Example:
416444
{@snippet :
417-
EnhancedDocument.builder().putWithType("customKey", customValue, EnhancedType.of(CustomClass.class));
418-
* }
445+
* EnhancedDocument.builder().put("customKey", customValue, EnhancedType.of(CustomClass.class));
446+
*}
419447
* Use {@link #putString(String, String)} or {@link #putNumber(String, Number)} for inserting simple value types of
420448
* attributes.
421-
* Use {@link #putList(String, List, EnhancedType)} or {@link #putMapOfType(String, Map, EnhancedType, EnhancedType)} for
449+
* Use {@link #putList(String, List, EnhancedType)} or {@link #putMap(String, Map, EnhancedType, EnhancedType)} for
450+
* inserting collections of attribute values.
451+
* Note that the attribute converter provider added to the DocumentBuilder must provide the converter for the class T
452+
* that is to be inserted.
453+
@param attributeName the name of the attribute to be added to the document.
454+
@param value the value to set.
455+
@param type the Enhanced type of the value to set.
456+
@return a builder instance to construct a {@link EnhancedDocument}.
457+
@param <T> the type of the value to set.
458+
*/
459+
<T> Builder put(String attributeName, T value, EnhancedType<T> type);
460+
461+
/**
462+
* Appends an attribute named {@code attributeName} with a value of Class type T.
463+
* Use this method to insert attribute values of custom types that have attribute converters defined in a converter
464+
* provider.
465+
* Example:
466+
{@snippet :
467+
* EnhancedDocument.builder().put("customKey", customValue, CustomClass.class);
468+
*}
469+
* Use {@link #putString(String, String)} or {@link #putNumber(String, Number)} for inserting simple value types of
470+
* attributes.
471+
* Use {@link #putList(String, List, EnhancedType)} or {@link #putMap(String, Map, EnhancedType, EnhancedType)} for
422472
* inserting collections of attribute values.
423473
* Note that the attribute converter provider added to the DocumentBuilder must provide the converter for the class T
424474
* that is to be inserted.
@@ -428,7 +478,7 @@ interface Builder {
428478
@return a builder instance to construct a {@link EnhancedDocument}.
429479
@param <T> the type of the value to set.
430480
*/
431-
<T> Builder putWithType(String attributeName, T value, EnhancedType<T> type);
481+
<T> Builder put(String attributeName, T value, Class<T> type);
432482

433483
/**
434484
* Appends an attribute with the specified name and a Map containing keys and values of {@link EnhancedType} K
@@ -437,14 +487,14 @@ interface Builder {
437487
* values.
438488
* <p>For example, to insert a map with String keys and Long values:
439489
* {@snippet :
440-
* EnhancedDocument.builder().putMapOfType("stringMap", mapWithStringKeyNumberValue, EnhancedType.of(String.class),
490+
* EnhancedDocument.builder().putMap("stringMap", mapWithStringKeyNumberValue, EnhancedType.of(String.class),
441491
* EnhancedType.of(String.class), EnhancedType.of(Long.class))
442-
* }
492+
*}
443493
* <p>For example, to insert a map of String Key and Custom Values:
444494
* {@snippet :
445-
* EnhancedDocument.builder().putMapOfType("customMap", mapWithStringKeyCustomValue, EnhancedType.of(String.class),
495+
* EnhancedDocument.builder().putMap("customMap", mapWithStringKeyCustomValue, EnhancedType.of(String.class),
446496
* EnhancedType.of(String.class), EnhancedType.of(Custom.class))
447-
* }
497+
*}
448498
* Note that the AttributeConverterProvider added to the DocumentBuilder should provide the converter for the classes
449499
* K and V that
450500
* are to be inserted.
@@ -454,7 +504,7 @@ interface Builder {
454504
* @param valueType Enhanced type of Value class.
455505
* @return Builder instance to construct a {@link EnhancedDocument}
456506
*/
457-
<K, V> Builder putMapOfType(String attributeName, Map<K, V> value, EnhancedType<K> keyType, EnhancedType<V> valueType);
507+
<K, V> Builder putMap(String attributeName, Map<K, V> value, EnhancedType<K> keyType, EnhancedType<V> valueType);
458508

459509
/**
460510
Appends an attribute to the document builder with the specified name and value of a JSON document in string format.
@@ -464,6 +514,15 @@ interface Builder {
464514
*/
465515
Builder putJson(String attributeName, String json);
466516

517+
518+
/**
519+
* Removes a previously appended attribute.
520+
* This can be used where a previously added attribute to the Builder is no longer needed.
521+
* @param attributeName The attribute that needs to be removed.
522+
* @return Builder instance to construct a {@link EnhancedDocument}
523+
*/
524+
Builder remove(String attributeName);
525+
467526
/**
468527
* Appends collection of attributeConverterProvider to the document builder. These
469528
* AttributeConverterProvider will be used to convert any given key to custom type T.

0 commit comments

Comments
 (0)