|
| 1 | +package org.baeldung.jackson.node; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.containsString; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertFalse; |
| 6 | +import static org.junit.Assert.assertThat; |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | + |
| 9 | +import java.io.FileReader; |
| 10 | +import java.io.FileWriter; |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Paths; |
| 14 | + |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 18 | +import com.fasterxml.jackson.databind.JsonNode; |
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | + |
| 22 | +public class JacksonNodeOperationTest { |
| 23 | + private static ObjectMapper mapper = new ObjectMapper(); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void giveAnObject_whenConvertingIntoANode_thenCorrect() { |
| 27 | + MyBean fromValue = new MyBean(2016, "baeldung.com"); |
| 28 | + |
| 29 | + JsonNode node = mapper.valueToTree(fromValue); |
| 30 | + |
| 31 | + assertEquals(2016, node.get("id").intValue()); |
| 32 | + assertEquals("baeldung.com", node.get("name").textValue()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenANode_whenWritingOutAsAJsonString_thenCorrect() throws IOException { |
| 37 | + final String pathToTestFile = "node_to_json_test.json"; |
| 38 | + char[] characterBuffer = new char[50]; |
| 39 | + |
| 40 | + JsonNode node = mapper.createObjectNode(); |
| 41 | + ((ObjectNode) node).put("id", 2016); |
| 42 | + ((ObjectNode) node).put("name", "baeldung.com"); |
| 43 | + |
| 44 | + try (FileWriter outputStream = new FileWriter(pathToTestFile)) { |
| 45 | + mapper.writeValue(outputStream, node); |
| 46 | + } |
| 47 | + |
| 48 | + try (FileReader inputStreamForAssertion = new FileReader(pathToTestFile)) { |
| 49 | + inputStreamForAssertion.read(characterBuffer); |
| 50 | + } |
| 51 | + String textContentOfTestFile = new String(characterBuffer); |
| 52 | + |
| 53 | + assertThat(textContentOfTestFile, containsString("2016")); |
| 54 | + assertThat(textContentOfTestFile, containsString("baeldung.com")); |
| 55 | + |
| 56 | + Files.delete(Paths.get(pathToTestFile)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenANode_whenConvertingIntoAnObject_thenCorrect() throws JsonProcessingException { |
| 61 | + JsonNode node = mapper.createObjectNode(); |
| 62 | + ((ObjectNode) node).put("id", 2016); |
| 63 | + ((ObjectNode) node).put("name", "baeldung.com"); |
| 64 | + |
| 65 | + MyBean toValue = mapper.treeToValue(node, MyBean.class); |
| 66 | + |
| 67 | + assertEquals(2016, toValue.getId()); |
| 68 | + assertEquals("baeldung.com", toValue.getName()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void givenANode_whenAddingIntoATree_thenCorrect() throws IOException { |
| 73 | + JsonNode rootNode = ExampleStructure.getExampleRoot(); |
| 74 | + ObjectNode addedNode = ((ObjectNode) rootNode).putObject("address"); |
| 75 | + addedNode.put("city", "Seattle").put("state", "Washington").put("country", "United States"); |
| 76 | + |
| 77 | + assertFalse(rootNode.path("address").isMissingNode()); |
| 78 | + assertEquals("Seattle", rootNode.path("address").path("city").textValue()); |
| 79 | + assertEquals("Washington", rootNode.path("address").path("state").textValue()); |
| 80 | + assertEquals("United States", rootNode.path("address").path("country").textValue()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void givenANode_whenModifyingIt_thenCorrect() throws IOException { |
| 85 | + String newString = "{\"nick\": \"cowtowncoder\"}"; |
| 86 | + JsonNode newNode = mapper.readTree(newString); |
| 87 | + |
| 88 | + JsonNode rootNode = ExampleStructure.getExampleRoot(); |
| 89 | + ((ObjectNode) rootNode).set("name", newNode); |
| 90 | + |
| 91 | + assertFalse(rootNode.path("name").path("nick").isMissingNode()); |
| 92 | + assertEquals("cowtowncoder", rootNode.path("name").path("nick").textValue()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void givenANode_whenRemovingFromATree_thenCorrect() throws IOException { |
| 97 | + JsonNode rootNode = ExampleStructure.getExampleRoot(); |
| 98 | + ((ObjectNode) rootNode).remove("company"); |
| 99 | + |
| 100 | + assertTrue(rootNode.path("company").isMissingNode()); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments