@Test public void array_foreach_modone() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson( "{ '$foreach' : { 'field7' : { 'field':'elemf1','op':'=','rvalue':'elvalue0_1'} , '$update' : {'$set': { 'elemf1':'test'}} } }"); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); Assert.assertEquals(4, jsonDoc.get(new Path("field7")).size()); Assert.assertEquals("test", jsonDoc.get(new Path("field7.0.elemf1")).asText()); }
@Test public void setArrayFieldTest() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson( "{'$set' : { 'field6.nf5.0':'50', 'field6.nf6.1':'blah', 'field7.0.elemf1':'test'}} "); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); Assert.assertEquals(50, jsonDoc.get(new Path("field6.nf5.0")).intValue()); Assert.assertEquals("blah", jsonDoc.get(new Path("field6.nf6.1")).asText()); Assert.assertEquals("test", jsonDoc.get(new Path("field7.0.elemf1")).asText()); }
@Test public void array_foreach_removeall() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson( "{ '$foreach' : { 'field7' : '$all', '$update' : '$remove' } }"); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); Assert.assertEquals(0, jsonDoc.get(new Path("field7")).size()); Assert.assertEquals(0, jsonDoc.get(new Path("field7#")).asInt()); }
@Test public void null_array_set() throws Exception { // Set field7 to null jsonDoc.modify(new Path("field7"), null, true); Assert.assertNull(jsonDoc.get(new Path("field7"))); UpdateExpression expr = EvalTestContext.updateExpressionFromJson("{ '$set' : { 'field7' : [] } }"); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); updater.update(jsonDoc, md.getFieldTreeRoot(), new Path()); Assert.assertEquals(0, jsonDoc.get(new Path("field7")).size()); }
@Test public void setSimpleFieldTest() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson( "[ {'$set' : { 'field1' : 'set1', 'field2':'set2', 'field5': 0, 'field6.nf1':'set6' } }, {'$add' : { 'field3':1 } } ] "); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); Assert.assertEquals("set1", jsonDoc.get(new Path("field1")).asText()); Assert.assertEquals("set2", jsonDoc.get(new Path("field2")).asText()); Assert.assertEquals(4, jsonDoc.get(new Path("field3")).asInt()); Assert.assertFalse(jsonDoc.get(new Path("field5")).asBoolean()); Assert.assertEquals("set6", jsonDoc.get(new Path("field6.nf1")).asText()); }
@Test public void refSet() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson( "{'$set' : { 'field6.nf5.0': { '$valueof' : 'field3' }, 'field7.0' : {}}}"); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); Assert.assertEquals( jsonDoc.get(new Path("field3")).intValue(), jsonDoc.get(new Path("field6.nf5.0")).intValue()); JsonNode node = jsonDoc.get(new Path("field7.0")); Assert.assertNotNull(node); Assert.assertEquals(0, node.size()); Assert.assertTrue(node instanceof ObjectNode); }
@Test public void unset() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson( "{'$unset' : [ 'field1', 'field6.nf2', 'field6.nf6.1','field7.1'] }"); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); Assert.assertNull(jsonDoc.get(new Path("field1"))); Assert.assertNull(jsonDoc.get(new Path("field6.nf2"))); Assert.assertEquals("three", jsonDoc.get(new Path("field6.nf6.1")).asText()); Assert.assertEquals(3, jsonDoc.get(new Path("field6.nf6#")).asInt()); Assert.assertEquals(3, jsonDoc.get(new Path("field6.nf6")).size()); Assert.assertEquals("elvalue2_1", jsonDoc.get(new Path("field7.1.elemf1")).asText()); Assert.assertEquals(3, jsonDoc.get(new Path("field7#")).asInt()); Assert.assertEquals(3, jsonDoc.get(new Path("field7")).size()); }
@Test public void array_insert() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson( "{ '$insert' : { 'field6.nf6.2' : [ 'five','six',{'$valueof':'field2' }] } }"); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); Assert.assertEquals("one", jsonDoc.get(new Path("field6.nf6.0")).asText()); Assert.assertEquals("two", jsonDoc.get(new Path("field6.nf6.1")).asText()); Assert.assertEquals("five", jsonDoc.get(new Path("field6.nf6.2")).asText()); Assert.assertEquals("six", jsonDoc.get(new Path("field6.nf6.3")).asText()); Assert.assertEquals("value2", jsonDoc.get(new Path("field6.nf6.4")).asText()); Assert.assertEquals("three", jsonDoc.get(new Path("field6.nf6.5")).asText()); Assert.assertEquals("four", jsonDoc.get(new Path("field6.nf6.6")).asText()); Assert.assertNull(jsonDoc.get(new Path("field6.ng6.7"))); Assert.assertEquals(7, jsonDoc.get(new Path("field6.nf6#")).asInt()); Assert.assertEquals(7, jsonDoc.get(new Path("field6.nf6")).size()); }
@Test public void array_foreach_append() throws Exception { jsonDoc = EvalTestContext.getDoc("./termsdata.json"); md = EvalTestContext.getMd("./termsmd.json"); UpdateExpression expr = EvalTestContext.updateExpressionFromJson( "{ '$foreach' : { 'termsVerbiage' : { 'field':'uid','op':'=','rvalue':1} ," + "'$update' : [ " + "{ '$insert': { 'termsVerbiageTranslation.0': {}}}," + "{ '$set': {'termsVerbiageTranslation.0.localeCode':'lg','termsVerbiageTranslation.0.localeText':'Lang' } }" + " ] }}"); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); System.out.println("before:" + JsonUtils.prettyPrint(jsonDoc.getRoot())); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); System.out.println("After:" + JsonUtils.prettyPrint(jsonDoc.getRoot())); Assert.assertEquals( 4, jsonDoc.get(new Path("termsVerbiage.0.termsVerbiageTranslation")).size()); Assert.assertEquals( "lg", jsonDoc.get(new Path("termsVerbiage.0.termsVerbiageTranslation.0.localeCode")).asText()); }