20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
22
import java .util .UUID ;
23
+ import java .util .stream .Stream ;
23
24
import org .junit .jupiter .api .BeforeAll ;
24
25
import org .junit .jupiter .params .ParameterizedTest ;
25
26
import org .junit .jupiter .params .provider .Arguments ;
26
27
import org .junit .jupiter .params .provider .MethodSource ;
27
28
import org .springframework .lang .Nullable ;
28
29
import org .springframework .util .StringUtils ;
29
30
import software .amazon .awssdk .enhanced .dynamodb .*;
31
+ import software .amazon .awssdk .enhanced .dynamodb .model .*;
30
32
import software .amazon .awssdk .enhanced .dynamodb .model .PageIterable ;
31
33
import software .amazon .awssdk .enhanced .dynamodb .model .QueryConditional ;
32
34
import software .amazon .awssdk .enhanced .dynamodb .model .QueryEnhancedRequest ;
40
42
* @author Matej Nedic
41
43
* @author Arun Patra
42
44
* @author Artem Bilan
45
+ * @author Marcus Voltolim
43
46
*/
44
47
public class DynamoDbTemplateIntegrationTest implements LocalstackContainerTest {
45
48
@@ -51,7 +54,7 @@ public class DynamoDbTemplateIntegrationTest implements LocalstackContainerTest
51
54
private static final String nameOfGSPK = "gsPk" ;
52
55
53
56
@ BeforeAll
54
- public static void createTable () {
57
+ static void createTable () {
55
58
DynamoDbClient dynamoDbClient = LocalstackContainerTest .dynamoDbClient ();
56
59
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient .builder ().dynamoDbClient (dynamoDbClient ).build ();
57
60
@@ -96,12 +99,36 @@ void dynamoDbTemplate_saveUpdateAndRead_entitySuccessful(DynamoDbTable<PersonEnt
96
99
PersonEntity personEntity = new PersonEntity (UUID .randomUUID (), "foo" , "bar" );
97
100
dynamoDbTemplate .save (personEntity );
98
101
102
+ personEntity .setName (null );
99
103
personEntity .setLastName ("xxx" );
100
104
dynamoDbTemplate .update (personEntity );
101
105
102
- PersonEntity personEntity1 = dynamoDbTemplate
103
- .load (Key .builder ().partitionValue (personEntity .getUuid ().toString ()).build (), PersonEntity .class );
104
- assertThat (personEntity1 ).isEqualTo (personEntity );
106
+ Key key = Key .builder ().partitionValue (personEntity .getUuid ().toString ()).build ();
107
+
108
+ assertThat (dynamoDbTemplate .load (key , PersonEntity .class ))
109
+ .extracting (PersonEntity ::getName , PersonEntity ::getLastName ).containsExactly (null , "xxx" );
110
+
111
+ // clean up
112
+ cleanUp (dynamoDbTable , personEntity .getUuid ());
113
+ }
114
+
115
+ @ ParameterizedTest
116
+ @ MethodSource ("argumentSource" )
117
+ void dynamoDbTemplate_saveUpdateIgnoreNullAndRead_entitySuccessful (DynamoDbTable <PersonEntity > dynamoDbTable ,
118
+ DynamoDbTemplate dynamoDbTemplate ) {
119
+ PersonEntity personEntity = new PersonEntity (UUID .randomUUID (), "foo" , "bar" );
120
+ dynamoDbTemplate .save (personEntity );
121
+
122
+ personEntity .setName (null );
123
+ personEntity .setLastName ("xxx" );
124
+ UpdateItemEnhancedRequest <PersonEntity > request = UpdateItemEnhancedRequest .builder (PersonEntity .class )
125
+ .item (personEntity ).ignoreNullsMode (IgnoreNullsMode .SCALAR_ONLY ).build ();
126
+ dynamoDbTemplate .update (request );
127
+
128
+ Key key = Key .builder ().partitionValue (personEntity .getUuid ().toString ()).build ();
129
+
130
+ assertThat (dynamoDbTemplate .load (key , PersonEntity .class ))
131
+ .extracting (PersonEntity ::getName , PersonEntity ::getLastName ).containsExactly ("foo" , "xxx" );
105
132
106
133
// clean up
107
134
cleanUp (dynamoDbTable , personEntity .getUuid ());
@@ -299,16 +326,16 @@ public static void cleanUp(DynamoDbTable<PersonEntity> dynamoDbTable, UUID uuid)
299
326
dynamoDbTable .deleteItem (Key .builder ().partitionValue (uuid .toString ()).build ());
300
327
}
301
328
302
- private static java . util . stream . Stream <Arguments > argumentSource () {
303
- return java . util . stream . Stream .of (Arguments .of (dynamoDbTable , dynamoDbTemplate ),
329
+ private static Stream <Arguments > argumentSource () {
330
+ return Stream .of (Arguments .of (dynamoDbTable , dynamoDbTemplate ),
304
331
Arguments .of (prefixedDynamoDbTable , prefixedDynamoDbTemplate ));
305
332
}
306
333
307
334
private static void describeAndCreateTable (DynamoDbClient dynamoDbClient , @ Nullable String tablePrefix ) {
308
- ArrayList <AttributeDefinition > attributeDefinitions = new ArrayList <AttributeDefinition >();
335
+ ArrayList <AttributeDefinition > attributeDefinitions = new ArrayList <>();
309
336
attributeDefinitions .add (AttributeDefinition .builder ().attributeName ("uuid" ).attributeType ("S" ).build ());
310
337
attributeDefinitions .add (AttributeDefinition .builder ().attributeName (nameOfGSPK ).attributeType ("S" ).build ());
311
- ArrayList <KeySchemaElement > tableKeySchema = new ArrayList <KeySchemaElement >();
338
+ ArrayList <KeySchemaElement > tableKeySchema = new ArrayList <>();
312
339
tableKeySchema .add (KeySchemaElement .builder ().attributeName ("uuid" ).keyType (KeyType .HASH ).build ());
313
340
List <KeySchemaElement > indexKeySchema = new ArrayList <>();
314
341
indexKeySchema .add (KeySchemaElement .builder ().attributeName (nameOfGSPK ).keyType (KeyType .HASH ).build ());
@@ -332,4 +359,5 @@ private static void describeAndCreateTable(DynamoDbClient dynamoDbClient, @Nulla
332
359
// table already exists, do nothing
333
360
}
334
361
}
362
+
335
363
}
0 commit comments