|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import org.json.JSONArray; |
| 4 | +import org.json.JSONObject; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | + |
| 9 | +import static org.junit.Assert.*; |
| 10 | + |
| 11 | +public class TestSyncStack { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void testSetJSON_withAllFields() throws Exception { |
| 15 | + // Build JSON with all properties |
| 16 | + JSONObject json = new JSONObject(); |
| 17 | + json.put("skip", 5); |
| 18 | + json.put("total_count", 100); |
| 19 | + json.put("limit", 20); |
| 20 | + json.put("pagination_token", "page_token_value"); |
| 21 | + json.put("sync_token", "sync_token_value"); |
| 22 | + |
| 23 | + JSONArray items = new JSONArray(); |
| 24 | + JSONObject item1 = new JSONObject(); |
| 25 | + item1.put("uid", "1"); |
| 26 | + items.put(item1); |
| 27 | + JSONObject item2 = new JSONObject(); |
| 28 | + item2.put("uid", "2"); |
| 29 | + items.put(item2); |
| 30 | + json.put("items", items); |
| 31 | + |
| 32 | + SyncStack syncStack = new SyncStack(); |
| 33 | + syncStack.setJSON(json); |
| 34 | + |
| 35 | + // URL should be set to empty string in setJSON |
| 36 | + assertEquals("", syncStack.getURL()); |
| 37 | + |
| 38 | + // verify basic numeric fields |
| 39 | + assertEquals(5, syncStack.getSkip()); |
| 40 | + assertEquals(100, syncStack.getCount()); |
| 41 | + assertEquals(20, syncStack.getLimit()); |
| 42 | + |
| 43 | + // verify tokens |
| 44 | + assertEquals("page_token_value", syncStack.getPaginationToken()); |
| 45 | + assertEquals("sync_token_value", syncStack.getSyncToken()); |
| 46 | + |
| 47 | + // verify items |
| 48 | + ArrayList<JSONObject> resultItems = syncStack.getItems(); |
| 49 | + assertNotNull(resultItems); |
| 50 | + assertEquals(2, resultItems.size()); |
| 51 | + assertEquals("1", resultItems.get(0).optString("uid")); |
| 52 | + assertEquals("2", resultItems.get(1).optString("uid")); |
| 53 | + |
| 54 | + // verify JSON response stored |
| 55 | + assertNotNull(syncStack.getJSONResponse()); |
| 56 | + assertEquals(json.toString(), syncStack.getJSONResponse().toString()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testSetJSON_onlyPaginationToken() throws Exception { |
| 61 | + JSONObject json = new JSONObject(); |
| 62 | + json.put("skip", 0); |
| 63 | + json.put("total_count", 10); |
| 64 | + json.put("limit", 10); |
| 65 | + json.put("pagination_token", "only_pagination"); |
| 66 | + |
| 67 | + SyncStack syncStack = new SyncStack(); |
| 68 | + syncStack.setJSON(json); |
| 69 | + |
| 70 | + assertEquals(0, syncStack.getSkip()); |
| 71 | + assertEquals(10, syncStack.getCount()); |
| 72 | + assertEquals(10, syncStack.getLimit()); |
| 73 | + assertEquals("only_pagination", syncStack.getPaginationToken()); |
| 74 | + // because has("sync_token") == false, sync_token should be null |
| 75 | + assertNull(syncStack.getSyncToken()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testSetJSON_onlySyncToken() throws Exception { |
| 80 | + JSONObject json = new JSONObject(); |
| 81 | + json.put("skip", 1); |
| 82 | + json.put("total_count", 5); |
| 83 | + json.put("limit", 5); |
| 84 | + json.put("sync_token", "only_sync"); |
| 85 | + |
| 86 | + SyncStack syncStack = new SyncStack(); |
| 87 | + syncStack.setJSON(json); |
| 88 | + |
| 89 | + assertEquals(1, syncStack.getSkip()); |
| 90 | + assertEquals(5, syncStack.getCount()); |
| 91 | + assertEquals(5, syncStack.getLimit()); |
| 92 | + |
| 93 | + // no pagination_token present |
| 94 | + assertNull(syncStack.getPaginationToken()); |
| 95 | + assertEquals("only_sync", syncStack.getSyncToken()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testSetJSON_nullDoesNothing() { |
| 100 | + SyncStack syncStack = new SyncStack(); |
| 101 | + // should simply not throw and not change fields |
| 102 | + syncStack.setJSON(null); |
| 103 | + |
| 104 | + // all getters should remain default (null / 0) |
| 105 | + assertNull(syncStack.getJSONResponse()); |
| 106 | + assertNull(syncStack.getURL()); |
| 107 | + assertEquals(0, syncStack.getSkip()); |
| 108 | + assertEquals(0, syncStack.getCount()); |
| 109 | + assertEquals(0, syncStack.getLimit()); |
| 110 | + assertNull(syncStack.getPaginationToken()); |
| 111 | + assertNull(syncStack.getSyncToken()); |
| 112 | + assertNull(syncStack.getItems()); |
| 113 | + } |
| 114 | +} |
0 commit comments