|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import org.json.JSONArray; |
| 4 | +import org.json.JSONObject; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import static org.junit.jupiter.api.Assertions.*; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +public class TestSyncStack { |
| 11 | + private SyncStack syncStack; |
| 12 | + |
| 13 | + @BeforeEach |
| 14 | + void setUp() { |
| 15 | + syncStack = new SyncStack(); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * ✅ Test: Valid JSON with correct structure |
| 20 | + */ |
| 21 | + @Test |
| 22 | + void testSetJSON_WithValidData() { |
| 23 | + JSONObject validJson = new JSONObject() |
| 24 | + .put("items", new JSONArray() |
| 25 | + .put(new JSONObject().put("title", "Article 1")) |
| 26 | + .put(new JSONObject().put("title", "Article 2"))) |
| 27 | + .put("skip", 5) |
| 28 | + .put("total_count", 100) |
| 29 | + .put("limit", 20) |
| 30 | + .put("pagination_token", "validToken123") |
| 31 | + .put("sync_token", "sync123"); |
| 32 | + |
| 33 | + syncStack.setJSON(validJson); |
| 34 | + |
| 35 | + // Assertions |
| 36 | + assertEquals(5, syncStack.getSkip()); |
| 37 | + assertEquals(100, syncStack.getCount()); |
| 38 | + assertEquals(20, syncStack.getLimit()); |
| 39 | + assertEquals("validToken123", syncStack.getPaginationToken()); |
| 40 | + assertEquals("sync123", syncStack.getSyncToken()); |
| 41 | + |
| 42 | + List<JSONObject> items = syncStack.getItems(); |
| 43 | + assertNotNull(items); |
| 44 | + assertEquals(2, items.size()); |
| 45 | + assertEquals("Article 1", items.get(0).optString("title")); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * ✅ Test: Missing `items` should not cause a crash |
| 50 | + */ |
| 51 | + @Test |
| 52 | + void testSetJSON_MissingItems() { |
| 53 | + JSONObject jsonWithoutItems = new JSONObject() |
| 54 | + .put("skip", 5) |
| 55 | + .put("total_count", 50) |
| 56 | + .put("limit", 10); |
| 57 | + |
| 58 | + syncStack.setJSON(jsonWithoutItems); |
| 59 | + |
| 60 | + // Assertions |
| 61 | + assertEquals(5, syncStack.getSkip()); |
| 62 | + assertEquals(50, syncStack.getCount()); |
| 63 | + assertEquals(10, syncStack.getLimit()); |
| 64 | + assertTrue(syncStack.getItems().isEmpty()); // Should default to empty list |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * ✅ Test: Handling JSON Injection Attempt |
| 69 | + */ |
| 70 | + @Test |
| 71 | + void testSetJSON_JSONInjection() { |
| 72 | + JSONObject maliciousJson = new JSONObject() |
| 73 | + .put("items", new JSONArray() |
| 74 | + .put(new JSONObject().put("title", "<script>alert('Hacked');</script>"))); |
| 75 | + |
| 76 | + syncStack.setJSON(maliciousJson); |
| 77 | + |
| 78 | + List<JSONObject> items = syncStack.getItems(); |
| 79 | + assertNotNull(items); |
| 80 | + assertEquals(1, items.size()); |
| 81 | + assertEquals("<script>alert('Hacked');</script>", items.get(0).optString("title")); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * ✅ Test: Invalid `items` field (should not crash) |
| 86 | + */ |
| 87 | + @Test |
| 88 | + void testSetJSON_InvalidItemsType() { |
| 89 | + JSONObject invalidJson = new JSONObject() |
| 90 | + .put("items", "This is not a valid array") |
| 91 | + .put("skip", 10); |
| 92 | + |
| 93 | + assertDoesNotThrow(() -> syncStack.setJSON(invalidJson)); |
| 94 | + assertTrue(syncStack.getItems().isEmpty()); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * ✅ Test: Null `paginationToken` and `syncToken` are handled correctly |
| 99 | + */ |
| 100 | + @Test |
| 101 | + void testSetJSON_NullTokens() { |
| 102 | + JSONObject jsonWithNullTokens = new JSONObject() |
| 103 | + .put("pagination_token", JSONObject.NULL) |
| 104 | + .put("sync_token", JSONObject.NULL); |
| 105 | + |
| 106 | + syncStack.setJSON(jsonWithNullTokens); |
| 107 | + |
| 108 | + assertNull(syncStack.getPaginationToken()); |
| 109 | + assertNull(syncStack.getSyncToken()); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * ✅ Test: Invalid characters in `paginationToken` should be rejected |
| 114 | + */ |
| 115 | + @Test |
| 116 | + void testSetJSON_InvalidTokenCharacters() { |
| 117 | + JSONObject jsonWithInvalidTokens = new JSONObject() |
| 118 | + .put("pagination_token", "invalid!!@#") |
| 119 | + .put("sync_token", "<script>attack</script>"); |
| 120 | + |
| 121 | + syncStack.setJSON(jsonWithInvalidTokens); |
| 122 | + |
| 123 | + assertNull(syncStack.getPaginationToken()); // Should be sanitized |
| 124 | + assertNull(syncStack.getSyncToken()); // Should be sanitized |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * ✅ Test: Thread-Safety - Concurrent Modification of `syncItems` |
| 129 | + */ |
| 130 | + @Test |
| 131 | + void testSetJSON_ThreadSafety() throws InterruptedException { |
| 132 | + JSONObject jsonWithItems = new JSONObject() |
| 133 | + .put("items", new JSONArray() |
| 134 | + .put(new JSONObject().put("title", "Safe Entry"))); |
| 135 | + |
| 136 | + Thread thread1 = new Thread(() -> syncStack.setJSON(jsonWithItems)); |
| 137 | + Thread thread2 = new Thread(() -> syncStack.setJSON(jsonWithItems)); |
| 138 | + |
| 139 | + thread1.start(); |
| 140 | + thread2.start(); |
| 141 | + thread1.join(); |
| 142 | + thread2.join(); |
| 143 | + |
| 144 | + assertFalse(syncStack.getItems().isEmpty()); // No race conditions |
| 145 | + } |
| 146 | +} |
0 commit comments