Skip to content

Commit e6a6f7e

Browse files
committed
Adds more test cases in JSONDocumentTest.java
1 parent 3e3e6cf commit e6a6f7e

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

document-store/src/main/java/org/hypertrace/core/documentstore/JSONDocument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ public boolean equals(Object obj) {
7575
}
7676

7777
JSONDocument other = (JSONDocument) obj;
78-
return Objects.equals(node, other.node);
78+
return Objects.equals(node, other.node) && documentType == other.documentType;
7979
}
8080
}

document-store/src/test/java/org/hypertrace/core/documentstore/JSONDocumentTest.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package org.hypertrace.core.documentstore;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import java.util.Map;
46
import org.junit.jupiter.api.Assertions;
57
import org.junit.jupiter.api.Test;
68

79
public class JSONDocumentTest {
810

11+
private static final ObjectMapper mapper = new ObjectMapper();
12+
913
@Test
1014
public void testJSONDocument() throws Exception {
1115
Map<String, String> data = Map.of("key1", "value1", "key2", "value2");
@@ -14,4 +18,91 @@ public void testJSONDocument() throws Exception {
1418
Assertions.assertEquals(document1, document2);
1519
Assertions.assertEquals(document1.toJson(), document2.toJson());
1620
}
21+
22+
@Test
23+
public void testJSONDocumentWithDefaultDocumentType() throws Exception {
24+
Map<String, String> data = Map.of("key1", "value1", "key2", "value2");
25+
JSONDocument document = new JSONDocument(data);
26+
Assertions.assertEquals(DocumentType.NESTED, document.getDocumentType());
27+
}
28+
29+
@Test
30+
public void testJSONDocumentWithExplicitDocumentType() throws Exception {
31+
Map<String, String> data = Map.of("key1", "value1", "key2", "value2");
32+
33+
JSONDocument nestedDocument = new JSONDocument(data, DocumentType.NESTED);
34+
Assertions.assertEquals(DocumentType.NESTED, nestedDocument.getDocumentType());
35+
36+
JSONDocument flatDocument = new JSONDocument(data, DocumentType.FLAT);
37+
Assertions.assertEquals(DocumentType.FLAT, flatDocument.getDocumentType());
38+
}
39+
40+
@Test
41+
public void testJSONDocumentConstructorsWithDocumentType() throws Exception {
42+
String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
43+
JsonNode node = mapper.readTree(json);
44+
Map<String, String> data = Map.of("key1", "value1", "key2", "value2");
45+
46+
// Test string constructor with DocumentType
47+
JSONDocument stringDoc = new JSONDocument(json, DocumentType.FLAT);
48+
Assertions.assertEquals(DocumentType.FLAT, stringDoc.getDocumentType());
49+
50+
// Test object constructor with DocumentType
51+
JSONDocument objectDoc = new JSONDocument(data, DocumentType.FLAT);
52+
Assertions.assertEquals(DocumentType.FLAT, objectDoc.getDocumentType());
53+
54+
// Test JsonNode constructor with DocumentType
55+
JSONDocument nodeDoc = new JSONDocument(node, DocumentType.FLAT);
56+
Assertions.assertEquals(DocumentType.FLAT, nodeDoc.getDocumentType());
57+
}
58+
59+
@Test
60+
public void testEqualsWithSameContentDifferentDocumentType() throws Exception {
61+
Map<String, String> data = Map.of("key1", "value1", "key2", "value2");
62+
63+
JSONDocument nestedDoc = new JSONDocument(data, DocumentType.NESTED);
64+
JSONDocument flatDoc = new JSONDocument(data, DocumentType.FLAT);
65+
66+
// Current implementation only compares JsonNode, not DocumentType
67+
// This test documents the current behavior - documents with same content but different types are equal
68+
Assertions.assertNotEquals(nestedDoc, flatDoc);
69+
Assertions.assertEquals(nestedDoc.toJson(), flatDoc.toJson());
70+
}
71+
72+
@Test
73+
public void testEqualsWithSameContentSameDocumentType() throws Exception {
74+
Map<String, String> data = Map.of("key1", "value1", "key2", "value2");
75+
76+
JSONDocument doc1 = new JSONDocument(data, DocumentType.FLAT);
77+
JSONDocument doc2 = new JSONDocument(data, DocumentType.FLAT);
78+
79+
Assertions.assertEquals(doc1, doc2);
80+
Assertions.assertEquals(doc1.getDocumentType(), doc2.getDocumentType());
81+
}
82+
83+
@Test
84+
public void testErrorDocument() throws Exception {
85+
String errorMessage = "Test error message";
86+
JSONDocument errorDoc = JSONDocument.errorDocument(errorMessage);
87+
88+
// Verify default document type
89+
Assertions.assertEquals(DocumentType.NESTED, errorDoc.getDocumentType());
90+
91+
// Verify error message is in the JSON
92+
String expectedJson = "{\"errorMessage\":\"" + errorMessage + "\"}";
93+
Assertions.assertEquals(expectedJson, errorDoc.toJson());
94+
95+
// Test error document equality
96+
JSONDocument anotherErrorDoc = JSONDocument.errorDocument(errorMessage);
97+
Assertions.assertEquals(errorDoc, anotherErrorDoc);
98+
}
99+
100+
@Test
101+
public void testToStringMethod() throws Exception {
102+
Map<String, String> data = Map.of("key1", "value1");
103+
JSONDocument document = new JSONDocument(data, DocumentType.FLAT);
104+
105+
// toString should return the same as toJson
106+
Assertions.assertEquals(document.toJson(), document.toString());
107+
}
17108
}

0 commit comments

Comments
 (0)