InPlaceMapOverrider implements Function<JsonNode, JsonNode> { @Override public JsonNode apply(JsonNode t) { properties.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEach(e -> { PathSegment target = lastPathComponent(t, e.getKey()); target.fillMissingParents(); if (target.getParent() == null) { throw new IllegalArgumentException("No parent node"); } target.getParent().writeChildValue(target.getIncomingPath(), e.getValue()); }); return t; } InPlaceMapOverrider(Map<String, String> properties); @Override JsonNode apply(JsonNode t);  }
@Test public void testApply_InPlace() { Map<String, String> props = Collections.singletonMap("a", "50"); InPlaceMapOverrider overrider = new InPlaceMapOverrider(props); JsonNode node = YamlReader.read("a: 5"); JsonNode overridden = overrider.apply(node); assertSame(node, overridden, "Overriding must happen in place"); }
@Test public void testApply() { Map<String, String> props = Collections.singletonMap("a", "50"); InPlaceMapOverrider overrider = new InPlaceMapOverrider(props); JsonNode node = YamlReader.read("a: 5"); overrider.apply(node); assertEquals(50, node.get("a").asInt()); }
@Test public void testApply_Nested() { Map<String, String> props = Collections.singletonMap("a.b", "50"); InPlaceMapOverrider overrider = new InPlaceMapOverrider(props); JsonNode node = YamlReader.read("a:\n b: 5"); overrider.apply(node); assertEquals(50, node.get("a").get("b").asInt()); }
@Test public void testApply_MissingRecreated() { Map<String, String> props = Collections.singletonMap("a.b", "50"); InPlaceMapOverrider overrider = new InPlaceMapOverrider(props); JsonNode node = YamlReader.read("a:"); overrider.apply(node); assertEquals(50, node.get("a").get("b").asInt()); }
@Test public void testApply_ObjectArray() { Map<String, String> props = Collections.singletonMap("a[1]", "50"); InPlaceMapOverrider overrider = new InPlaceMapOverrider(props); JsonNode node = YamlReader.read("a:\n" + " - 1\n" + " - 5\n" + " - 10"); overrider.apply(node); ArrayNode array = (ArrayNode) node.get("a"); assertEquals(3, array.size()); assertEquals(1, array.get(0).asInt()); assertEquals(50, array.get(1).asInt()); assertEquals(10, array.get(2).asInt()); }
@Test public void testApply_ObjectArray_PastEnd() { Map<String, String> props = Collections.singletonMap("a[2]", "50"); InPlaceMapOverrider overrider = new InPlaceMapOverrider(props); JsonNode node = YamlReader.read("a:\n" + " - 1\n" + " - 5"); overrider.apply(node); ArrayNode array = (ArrayNode) node.get("a"); assertEquals(3, array.size()); assertEquals(1, array.get(0).asInt()); assertEquals(5, array.get(1).asInt()); assertEquals(50, array.get(2).asInt()); }