30
30
import com .amazonaws .services .dynamodbv2 .document .Item ;
31
31
import com .amazonaws .services .dynamodbv2 .document .PrimaryKey ;
32
32
import 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 ;
33
36
import com .amazonaws .services .dynamodbv2 .local .embedded .DynamoDBEmbedded ;
34
37
import com .amazonaws .services .dynamodbv2 .model .AttributeDefinition ;
38
+ import com .amazonaws .services .dynamodbv2 .model .ConditionalCheckFailedException ;
35
39
import com .amazonaws .services .dynamodbv2 .model .CreateTableRequest ;
36
40
import com .amazonaws .services .dynamodbv2 .model .KeySchemaElement ;
37
41
import com .amazonaws .services .dynamodbv2 .model .KeyType ;
38
42
import com .amazonaws .services .dynamodbv2 .model .ProvisionedThroughput ;
43
+ import com .amazonaws .services .dynamodbv2 .model .ResourceNotFoundException ;
44
+ import com .amazonaws .services .dynamodbv2 .model .ReturnValue ;
39
45
import com .amazonaws .services .dynamodbv2 .model .ScalarAttributeType ;
40
46
import com .amazonaws .services .dynamodbv2 .xspec .ExpressionSpecBuilder ;
41
47
import com .amazonaws .services .dynamodbv2 .xspec .UpdateItemExpressionSpec ;
44
50
import com .google .common .collect .ImmutableMap ;
45
51
import com .google .common .collect .ImmutableSet ;
46
52
47
- public class JsonPatchToExpressionSpecBuilderReplaceIT {
53
+ public class JsonPatchToXSpecReplace {
48
54
49
55
private static final String KEY_ATTRIBUTE_NAME = "key" ;
50
56
@@ -60,6 +66,11 @@ public class JsonPatchToExpressionSpecBuilderReplaceIT {
60
66
@ BeforeTest
61
67
public void setUp () throws Exception {
62
68
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
+ }
63
74
amazonDynamoDB .createTable (new CreateTableRequest ()
64
75
.withTableName (TABLE_NAME )
65
76
.withProvisionedThroughput (new ProvisionedThroughput (1L , 1L ))
@@ -72,12 +83,12 @@ public void setUp() throws Exception {
72
83
table = new Table (amazonDynamoDB , TABLE_NAME );
73
84
}
74
85
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 {
80
88
// setup
89
+ table .putItem (Item .fromMap (ImmutableMap .<String , Object > builder ()
90
+ .put (KEY_ATTRIBUTE_NAME , VALUE )
91
+ .build ()));
81
92
String patchExpression = "[ { \" op\" : \" replace\" , \" path\" : \" /a\" , \" value\" : 1 } ]" ;
82
93
JsonNode jsonNode = JsonLoader .fromString (patchExpression );
83
94
JsonPatch jsonPatch = JsonPatch .fromJson (jsonNode );
@@ -86,15 +97,40 @@ public void test_replace_singlePath_number() throws Exception {
86
97
UpdateItemExpressionSpec spec = builder .buildForUpdate ();
87
98
table .updateItem (KEY_ATTRIBUTE_NAME , VALUE , spec );
88
99
// 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
89
125
Item item = table .getItem (PK );
90
126
Assert .assertTrue (item .hasAttribute ("key" ));
91
127
Assert .assertEquals (item .getString ("key" ), "keyValue" );
92
128
Assert .assertTrue (item .hasAttribute ("a" ));
93
129
Assert .assertEquals (item .getNumber ("a" ).longValue (), 1L );
94
130
}
95
131
96
- @ Test
97
- public void test_replace_nestedPath_string () throws Exception {
132
+ @ Test ( expectedExceptions = ConditionalCheckFailedException . class )
133
+ public void testReplaceNestedPathString () throws Exception {
98
134
// setup
99
135
table .putItem (Item .fromMap (ImmutableMap .<String , Object > builder ()
100
136
.put (KEY_ATTRIBUTE_NAME , VALUE )
@@ -108,15 +144,6 @@ public void test_replace_nestedPath_string() throws Exception {
108
144
ExpressionSpecBuilder builder = jsonPatch .get ();
109
145
UpdateItemExpressionSpec spec = builder .buildForUpdate ();
110
146
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" );
120
147
}
121
148
122
149
@ Test
@@ -162,26 +189,6 @@ public void test_replace_property_toScalar_string() throws Exception {
162
189
table .updateItem (KEY_ATTRIBUTE_NAME , VALUE , spec );
163
190
}
164
191
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
-
185
192
@ Test
186
193
public void test_replace_singlePath_stringSet () throws Exception {
187
194
// setup
0 commit comments