Skip to content

Commit 9d39ac5

Browse files
Add test for handling single JSONObject under "items" in SyncStack
1 parent 8beaa58 commit 9d39ac5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/test/java/com/contentstack/sdk/TestSyncStack.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,39 @@ void testSetJSON_JSONInjection() {
8181
assertEquals("<script>alert('Hacked');</script>", items.get(0).optString("title"));
8282
}
8383

84+
/**
85+
* ✅ Should treat a lone JSONObject under "items" the same as a one‑element
86+
* array.
87+
*/
88+
@Test
89+
void testSetJSON_handlesSingleItemObject() {
90+
JSONObject input = new JSONObject()
91+
.put("items", new JSONObject()
92+
.put("title", "Single Entry")
93+
.put("uid", "entry123")
94+
.put("content_type", "blog"))
95+
.put("skip", 0)
96+
.put("total_count", 1)
97+
.put("limit", 10)
98+
.put("sync_token", "token123");
99+
100+
syncStack.setJSON(input);
101+
List<JSONObject> items = syncStack.getItems();
102+
103+
assertNotNull(items, "Items list should be initialised");
104+
assertEquals(1, items.size(), "Exactly one item expected");
105+
106+
JSONObject item = items.get(0);
107+
assertEquals("Single Entry", item.optString("title"));
108+
assertEquals("entry123", item.optString("uid"));
109+
assertEquals("blog", item.optString("content_type"));
110+
111+
assertEquals(0, syncStack.getSkip());
112+
assertEquals(1, syncStack.getCount());
113+
assertEquals(10, syncStack.getLimit());
114+
assertEquals("token123", syncStack.getSyncToken());
115+
}
116+
84117
/**
85118
* ✅ Test: Invalid `items` field (should not crash)
86119
*/

0 commit comments

Comments
 (0)