|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.JsonNode; |
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import java.io.IOException; |
5 | 6 | import java.util.Map; |
6 | 7 | import org.junit.jupiter.api.Assertions; |
7 | 8 | import org.junit.jupiter.api.Test; |
@@ -106,4 +107,36 @@ public void testToStringMethod() throws Exception { |
106 | 107 | // toString should return the same as toJson |
107 | 108 | Assertions.assertEquals(document.toJson(), document.toString()); |
108 | 109 | } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testJsonNodeConstructors() throws Exception { |
| 113 | + JsonNode node = mapper.createObjectNode().put("test", "value"); |
| 114 | + |
| 115 | + // Test JsonNode constructor without DocumentType |
| 116 | + JSONDocument doc1 = new JSONDocument(node); |
| 117 | + Assertions.assertEquals(DocumentType.NESTED, doc1.getDocumentType()); |
| 118 | + |
| 119 | + // Test JsonNode constructor with DocumentType |
| 120 | + JSONDocument doc2 = new JSONDocument(node, DocumentType.FLAT); |
| 121 | + Assertions.assertEquals(DocumentType.FLAT, doc2.getDocumentType()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testStringConstructorWithInvalidJson() { |
| 126 | + // Test invalid JSON handling |
| 127 | + Assertions.assertThrows( |
| 128 | + IOException.class, |
| 129 | + () -> new JSONDocument("invalid json {")); |
| 130 | + |
| 131 | + Assertions.assertThrows( |
| 132 | + IOException.class, |
| 133 | + () -> new JSONDocument("invalid json {", DocumentType.FLAT)); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testNullHandling() throws Exception { |
| 138 | + // Test null JsonNode |
| 139 | + JSONDocument nullNodeDoc = new JSONDocument((JsonNode) null); |
| 140 | + Assertions.assertNotNull(nullNodeDoc.toJson()); // Should handle gracefully |
| 141 | + } |
109 | 142 | } |
0 commit comments