PartitionNodeFunction implements Function<JsonNode, JsonNode> { @Override public JsonNode apply(JsonNode node) { return node.at(pointer); } PartitionNodeFunction(Path path); @Override JsonNode apply(JsonNode node);  }
@Test public void valueNode() throws JsonProcessingException, IOException { String json = "{ \"a\": { \"b\": { \"c\": 1 } } }"; JsonNode node = new ObjectMapper().readTree(json); underTest = new PartitionNodeFunction(KeyPathParser.parse("$.a.b.c")); JsonNode result = underTest.apply(node); assertThat(result, is(JsonNodeFactory.instance.numberNode(1))); }
@Test public void objectNode() throws JsonProcessingException, IOException { String json = "{ \"a\": { \"b\": { \"c\": 1 } } }"; JsonNode node = new ObjectMapper().readTree(json); underTest = new PartitionNodeFunction(KeyPathParser.parse("$.a.b")); JsonNode result = underTest.apply(node); assertThat(result, is(JsonNodeFactory.instance.objectNode().put("c", 1))); }
@Test public void missingNode() throws JsonProcessingException, IOException { String json = "{ \"a\": { \"b\": {} } }"; JsonNode node = new ObjectMapper().readTree(json); underTest = new PartitionNodeFunction(KeyPathParser.parse("$.a.b.c")); JsonNode result = underTest.apply(node); assertThat(result, is(MissingNode.getInstance())); }
@Test public void nullNode() throws JsonProcessingException, IOException { String json = "{ \"a\": { \"b\": { \"c\": null } } }"; JsonNode node = new ObjectMapper().readTree(json); underTest = new PartitionNodeFunction(KeyPathParser.parse("$.a.b.c")); JsonNode result = underTest.apply(node); assertThat(result, is(NullNode.getInstance())); }