diff --git a/json-modules/json-conversion/src/test/java/com/baeldung/jsonnodetojsonobject/JsonNodeToJsonObjectUnitTest.java b/json-modules/json-conversion/src/test/java/com/baeldung/jsonnodetojsonobject/JsonNodeToJsonObjectUnitTest.java new file mode 100644 index 0000000000..38d097e5b8 --- /dev/null +++ b/json-modules/json-conversion/src/test/java/com/baeldung/jsonnodetojsonobject/JsonNodeToJsonObjectUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.jsonnodetojsonobject; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.JsonNode; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class JsonNodeToJsonObjectUnitTest { + + public static String jsonString = "{\"name\": \"John\", \"gender\": \"male\", \"company\": \"Baeldung\", \"isEmployee\": true, \"age\": 30}"; + + @Test + public void givenJsonNode_whenConvertingToObjectNode_thenVerifyFieldsIntegrity() throws JsonProcessingException { + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonString); + ObjectNode objectNode = objectMapper.createObjectNode().setAll((ObjectNode) jsonNode); + + assertEquals("John", objectNode.get("name").asText()); + assertEquals("male", objectNode.get("gender").asText()); + assertEquals("Baeldung", objectNode.get("company").asText()); + assertTrue(objectNode.get("isEmployee").asBoolean()); + assertEquals(30, objectNode.get("age").asInt()); + } +}