3030import com .amazonaws .services .dynamodbv2 .document .Item ;
3131import com .amazonaws .services .dynamodbv2 .document .PrimaryKey ;
3232import com .amazonaws .services .dynamodbv2 .document .Table ;
33+ import com .amazonaws .services .dynamodbv2 .document .UpdateItemOutcome ;
34+ import com .amazonaws .services .dynamodbv2 .document .internal .InternalUtils ;
35+ import com .amazonaws .services .dynamodbv2 .document .spec .UpdateItemSpec ;
3336import com .amazonaws .services .dynamodbv2 .local .embedded .DynamoDBEmbedded ;
3437import com .amazonaws .services .dynamodbv2 .model .AttributeDefinition ;
38+ import com .amazonaws .services .dynamodbv2 .model .ConditionalCheckFailedException ;
3539import com .amazonaws .services .dynamodbv2 .model .CreateTableRequest ;
3640import com .amazonaws .services .dynamodbv2 .model .KeySchemaElement ;
3741import com .amazonaws .services .dynamodbv2 .model .KeyType ;
3842import com .amazonaws .services .dynamodbv2 .model .ProvisionedThroughput ;
43+ import com .amazonaws .services .dynamodbv2 .model .ResourceNotFoundException ;
44+ import com .amazonaws .services .dynamodbv2 .model .ReturnValue ;
3945import com .amazonaws .services .dynamodbv2 .model .ScalarAttributeType ;
4046import com .amazonaws .services .dynamodbv2 .xspec .ExpressionSpecBuilder ;
4147import com .amazonaws .services .dynamodbv2 .xspec .UpdateItemExpressionSpec ;
4450import com .google .common .collect .ImmutableMap ;
4551import com .google .common .collect .ImmutableSet ;
4652
47- public class JsonPatchToExpressionSpecBuilderReplaceIT {
53+ public class JsonPatchToXSpecReplace {
4854
4955 private static final String KEY_ATTRIBUTE_NAME = "key" ;
5056
@@ -60,6 +66,11 @@ public class JsonPatchToExpressionSpecBuilderReplaceIT {
6066 @ BeforeTest
6167 public void setUp () throws Exception {
6268 AmazonDynamoDB amazonDynamoDB = DynamoDBEmbedded .create ().amazonDynamoDB ();
69+ try {
70+ amazonDynamoDB .deleteTable (TABLE_NAME );
71+ } catch (ResourceNotFoundException e ) {
72+ //do nothing because the first run will not have the table.
73+ }
6374 amazonDynamoDB .createTable (new CreateTableRequest ()
6475 .withTableName (TABLE_NAME )
6576 .withProvisionedThroughput (new ProvisionedThroughput (1L , 1L ))
@@ -72,12 +83,12 @@ public void setUp() throws Exception {
7283 table = new Table (amazonDynamoDB , TABLE_NAME );
7384 }
7485
75- /**
76- * try to update an item that doesnt exist. will create new item
77- */
78- @ Test
79- public void test_replace_singlePath_number () throws Exception {
86+ @ Test (expectedExceptions = ConditionalCheckFailedException .class )
87+ public void testReplaceSinglePathNumberNonextant () throws Exception {
8088 // setup
89+ table .putItem (Item .fromMap (ImmutableMap .<String , Object > builder ()
90+ .put (KEY_ATTRIBUTE_NAME , VALUE )
91+ .build ()));
8192 String patchExpression = "[ { \" op\" : \" replace\" , \" path\" : \" /a\" , \" value\" : 1 } ]" ;
8293 JsonNode jsonNode = JsonLoader .fromString (patchExpression );
8394 JsonPatch jsonPatch = JsonPatch .fromJson (jsonNode );
@@ -86,15 +97,40 @@ public void test_replace_singlePath_number() throws Exception {
8697 UpdateItemExpressionSpec spec = builder .buildForUpdate ();
8798 table .updateItem (KEY_ATTRIBUTE_NAME , VALUE , spec );
8899 // verify
100+ table .getItem (PK ); //throw
101+ }
102+
103+ @ Test
104+ public void testReplaceSinglePathNumberExtant () throws Exception {
105+ // setup
106+ table .putItem (Item .fromMap (ImmutableMap .<String , Object > builder ()
107+ .put (KEY_ATTRIBUTE_NAME , VALUE )
108+ .put ("a" , "peekaboo" )
109+ .build ()));
110+ String patchExpression = "[ { \" op\" : \" replace\" , \" path\" : \" /a\" , \" value\" : 1 } ]" ;
111+ JsonNode jsonNode = JsonLoader .fromString (patchExpression );
112+ JsonPatch jsonPatch = JsonPatch .fromJson (jsonNode );
113+ // exercise
114+ ExpressionSpecBuilder builder = jsonPatch .get ();
115+ UpdateItemExpressionSpec spec = builder .buildForUpdate ();
116+ UpdateItemOutcome out = table .updateItem (new UpdateItemSpec ()
117+ .withPrimaryKey (KEY_ATTRIBUTE_NAME , VALUE )
118+ .withExpressionSpec (spec )
119+ .withReturnValues (ReturnValue .ALL_OLD ));
120+
121+ Item oldItem = Item .fromMap (InternalUtils .toSimpleMapValue (out .getUpdateItemResult ().getAttributes ()));
122+ Assert .assertTrue (oldItem .hasAttribute ("a" ));
123+ Assert .assertEquals (oldItem .getString ("a" ), "peekaboo" );
124+ // verify
89125 Item item = table .getItem (PK );
90126 Assert .assertTrue (item .hasAttribute ("key" ));
91127 Assert .assertEquals (item .getString ("key" ), "keyValue" );
92128 Assert .assertTrue (item .hasAttribute ("a" ));
93129 Assert .assertEquals (item .getNumber ("a" ).longValue (), 1L );
94130 }
95131
96- @ Test
97- public void test_replace_nestedPath_string () throws Exception {
132+ @ Test ( expectedExceptions = ConditionalCheckFailedException . class )
133+ public void testReplaceNestedPathString () throws Exception {
98134 // setup
99135 table .putItem (Item .fromMap (ImmutableMap .<String , Object > builder ()
100136 .put (KEY_ATTRIBUTE_NAME , VALUE )
@@ -108,15 +144,6 @@ public void test_replace_nestedPath_string() throws Exception {
108144 ExpressionSpecBuilder builder = jsonPatch .get ();
109145 UpdateItemExpressionSpec spec = builder .buildForUpdate ();
110146 table .updateItem (KEY_ATTRIBUTE_NAME , VALUE , spec );
111- // verify
112- Item item = table .getItem (PK );
113- Assert .assertTrue (item .hasAttribute ("key" ));
114- Assert .assertEquals (item .getString ("key" ), "keyValue" );
115- Assert .assertTrue (item .hasAttribute ("a" ));
116- Assert .assertTrue (item .getRawMap ("a" ).containsKey ("a" ));
117- Assert .assertEquals (((BigDecimal ) item .getMap ("a" ).get ("a" )).longValue (), 1L );
118- Assert .assertTrue (item .getMap ("a" ).containsKey ("b" ));
119- Assert .assertEquals (item .getMap ("a" ).get ("b" ), "foo" );
120147 }
121148
122149 @ Test
@@ -162,26 +189,6 @@ public void test_replace_property_toScalar_string() throws Exception {
162189 table .updateItem (KEY_ATTRIBUTE_NAME , VALUE , spec );
163190 }
164191
165- @ Test
166- public void test_replace_singlePath_numberSet () throws Exception {
167- // setup
168- String patchExpression = "[ { \" op\" : \" replace\" , \" path\" : \" /a\" , \" value\" : [1,2] } ]" ;
169- JsonNode jsonNode = JsonLoader .fromString (patchExpression );
170- JsonPatch jsonPatch = JsonPatch .fromJson (jsonNode );
171- // exercise
172- ExpressionSpecBuilder builder = jsonPatch .get ();
173- UpdateItemExpressionSpec spec = builder .buildForUpdate ();
174- table .updateItem (KEY_ATTRIBUTE_NAME , VALUE , spec );
175- // verify
176- Item item = table .getItem (PK );
177- Assert .assertTrue (item .hasAttribute ("key" ));
178- Assert .assertEquals (item .getString ("key" ), "keyValue" );
179- Assert .assertTrue (item .hasAttribute ("a" ));
180- //number comparisons are failing so comment this out for now
181- Assert .assertTrue (item .getList ("a" ).contains (BigDecimal .valueOf (1L )));
182- Assert .assertTrue (item .getList ("a" ).contains (BigDecimal .valueOf (2L )));
183- }
184-
185192 @ Test
186193 public void test_replace_singlePath_stringSet () throws Exception {
187194 // setup
0 commit comments