List of usage examples for com.amazonaws.services.dynamodbv2.document Item fromMap
public static Item fromMap(Map<String, Object> attributes)
Item
from the given map. From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java
License:LGPL
@Test public void test_remove_singlePath() throws Exception { // setup/*from ww w .j a va 2s . co m*/ table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE) .put("a", ImmutableMap.of("a", 2, "b", true)).build())); // setup String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/a\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder actual = jsonPatch.get(); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertFalse(item.hasAttribute("a")); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java
License:LGPL
@Test public void test_remove_nestedPath() throws Exception { // setup/* w w w.ja va2 s. c o m*/ table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE) .put("a", ImmutableMap.of("a", 2, "b", true)).build())); // setup String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/a/a\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder actual = jsonPatch.get(); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertTrue(item.getRawMap("a").containsKey("b")); Assert.assertEquals(item.getRawMap("a").get("b"), true); Assert.assertFalse(item.getRawMap("a").containsKey("a")); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java
License:LGPL
@Test public void test_remove_absentPath() throws Exception { // setup/*from w w w . j ava2 s . c om*/ table.putItem(Item.fromMap( ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).put("a", "b").build())); // setup String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/c\" } ]"; // $.c does not exist in target JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder actual = jsonPatch.get(); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertEquals(item.getString("a"), "b"); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java
License:LGPL
@Test(expectedExceptions = AmazonServiceException.class) public void test_remove_absentObjectPath() throws Exception { // setup/*from w w w . j av a 2 s. com*/ table.putItem(Item.fromMap( ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).put("a", "b").build())); // setup String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/c/d\" } ]"; // $.c does not exist in target JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder actual = jsonPatch.get(); UpdateItemExpressionSpec actualSpec = actual.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java
License:LGPL
@Test(expectedExceptions = ConditionalCheckFailedException.class) public void testReplaceSinglePathNumberNonextant() throws Exception { // setup//from www . j a v a 2 s. com table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).build())); String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": 1 } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder builder = jsonPatch.get(); UpdateItemExpressionSpec spec = builder.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec); // verify table.getItem(PK); //throw }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java
License:LGPL
@Test public void testReplaceSinglePathNumberExtant() throws Exception { // setup/*from w ww.j av a 2 s . c o m*/ table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE) .put("a", "peekaboo").build())); String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": 1 } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder builder = jsonPatch.get(); UpdateItemExpressionSpec spec = builder.buildForUpdate(); UpdateItemOutcome out = table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY_ATTRIBUTE_NAME, VALUE) .withExpressionSpec(spec).withReturnValues(ReturnValue.ALL_OLD)); Item oldItem = Item.fromMap(InternalUtils.toSimpleMapValue(out.getUpdateItemResult().getAttributes())); Assert.assertTrue(oldItem.hasAttribute("a")); Assert.assertEquals(oldItem.getString("a"), "peekaboo"); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertEquals(item.getNumber("a").longValue(), 1L); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java
License:LGPL
@Test(expectedExceptions = ConditionalCheckFailedException.class) public void testReplaceNestedPathString() throws Exception { // setup/*from w ww. ja va2 s . c o m*/ table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE) .put("a", ImmutableMap.of("a", 1L)).build())); String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a/b\", \"value\": \"foo\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder builder = jsonPatch.get(); UpdateItemExpressionSpec spec = builder.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java
License:LGPL
@Test public void test_replace_existingNestedPath_string() throws Exception { // setup//from www . ja v a 2 s .c om table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE) .put("a", ImmutableMap.of("a", 2L, "b", true)).build())); String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a/b\", \"value\": \"bar\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder builder = jsonPatch.get(); UpdateItemExpressionSpec spec = builder.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertTrue(item.getRawMap("a").containsKey("a")); Assert.assertEquals(((BigDecimal) item.getRawMap("a").get("a")).longValue(), 2L); Assert.assertTrue(item.getRawMap("a").containsKey("b")); Assert.assertEquals(item.getRawMap("a").get("b"), "bar"); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java
License:LGPL
@Test(expectedExceptions = AmazonServiceException.class) public void test_replace_property_toScalar_string() throws Exception { // setup//from w w w. j a va 2 s. c o m table.putItem(Item.fromMap( ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).put("a", 1L).build())); String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a/b\", \"value\": \"bar\" } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder builder = jsonPatch.get(); UpdateItemExpressionSpec spec = builder.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec); }
From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java
License:LGPL
@Test public void test_replace_singlePath_stringSet() throws Exception { // setup/*from w ww . ja v a 2s.com*/ table.putItem(Item.fromMap( ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).put("a", 1L).build())); String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": [\"foo\",\"bar\"] } ]"; JsonNode jsonNode = JsonLoader.fromString(patchExpression); JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode); // exercise ExpressionSpecBuilder builder = jsonPatch.get(); UpdateItemExpressionSpec spec = builder.buildForUpdate(); table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec); // verify Item item = table.getItem(PK); Assert.assertTrue(item.hasAttribute("key")); Assert.assertEquals(item.getString("key"), "keyValue"); Assert.assertTrue(item.hasAttribute("a")); Assert.assertTrue(item.getList("a").contains("foo")); Assert.assertTrue(item.getList("a").contains("bar")); }