|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import org.json.JSONArray; |
| 4 | +import org.json.JSONObject; |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.robolectric.RobolectricTestRunner; |
| 9 | +import org.robolectric.annotation.Config; |
| 10 | + |
| 11 | +import static org.junit.Assert.*; |
| 12 | + |
| 13 | +/** |
| 14 | + * Comprehensive unit tests for EntriesModel class |
| 15 | + * Based on Java SDK test patterns |
| 16 | + */ |
| 17 | +@RunWith(RobolectricTestRunner.class) |
| 18 | +@Config(sdk = 28) |
| 19 | +public class TestEntriesModel { |
| 20 | + |
| 21 | + private JSONObject testEntriesJson; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setUp() throws Exception { |
| 25 | + testEntriesJson = new JSONObject(); |
| 26 | + |
| 27 | + JSONArray entriesArray = new JSONArray(); |
| 28 | + for (int i = 1; i <= 5; i++) { |
| 29 | + JSONObject entry = new JSONObject(); |
| 30 | + entry.put("uid", "entry_" + i); |
| 31 | + entry.put("title", "Entry " + i); |
| 32 | + entry.put("url", "/entry-" + i); |
| 33 | + entry.put("locale", "en-us"); |
| 34 | + entriesArray.put(entry); |
| 35 | + } |
| 36 | + |
| 37 | + testEntriesJson.put("entries", entriesArray); |
| 38 | + } |
| 39 | + |
| 40 | + // ==================== BASIC CONSTRUCTOR TESTS ==================== |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testEntriesModelConstructor() { |
| 44 | + EntriesModel model = new EntriesModel(testEntriesJson, "entries", false); |
| 45 | + assertNotNull("EntriesModel should not be null", model); |
| 46 | + assertNotNull("Object list should not be null", model.objectList); |
| 47 | + assertEquals(5, model.objectList.size()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testEntriesModelFromCache() throws Exception { |
| 52 | + JSONObject cacheJson = new JSONObject(); |
| 53 | + cacheJson.put("response", testEntriesJson); |
| 54 | + |
| 55 | + EntriesModel model = new EntriesModel(cacheJson, "entries", true); |
| 56 | + assertNotNull("EntriesModel should not be null", model); |
| 57 | + assertNotNull("Object list should not be null", model.objectList); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testEntriesModelNotFromCache() { |
| 62 | + EntriesModel model = new EntriesModel(testEntriesJson, "entries", false); |
| 63 | + assertNotNull("EntriesModel should not be null", model); |
| 64 | + assertEquals("entries", model.formName); |
| 65 | + } |
| 66 | + |
| 67 | + // ==================== ENTRY PARSING TESTS ==================== |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testEntriesModelParsesEntries() { |
| 71 | + EntriesModel model = new EntriesModel(testEntriesJson, "entries", false); |
| 72 | + |
| 73 | + assertNotNull("Object list should not be null", model.objectList); |
| 74 | + assertEquals(5, model.objectList.size()); |
| 75 | + |
| 76 | + // Verify first entry |
| 77 | + EntryModel firstEntry = (EntryModel) model.objectList.get(0); |
| 78 | + assertEquals("entry_1", firstEntry.entryUid); |
| 79 | + assertEquals("Entry 1", firstEntry.title); |
| 80 | + assertEquals("/entry-1", firstEntry.url); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testEntriesModelWithSingleEntry() throws Exception { |
| 85 | + JSONObject json = new JSONObject(); |
| 86 | + JSONArray entriesArray = new JSONArray(); |
| 87 | + |
| 88 | + JSONObject entry = new JSONObject(); |
| 89 | + entry.put("uid", "single_entry"); |
| 90 | + entry.put("title", "Single Entry"); |
| 91 | + entriesArray.put(entry); |
| 92 | + |
| 93 | + json.put("entries", entriesArray); |
| 94 | + |
| 95 | + EntriesModel model = new EntriesModel(json, "entries", false); |
| 96 | + |
| 97 | + assertNotNull("EntriesModel should not be null", model); |
| 98 | + assertEquals(1, model.objectList.size()); |
| 99 | + |
| 100 | + EntryModel entryModel = (EntryModel) model.objectList.get(0); |
| 101 | + assertEquals("single_entry", entryModel.entryUid); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testEntriesModelWithEmptyArray() throws Exception { |
| 106 | + JSONObject json = new JSONObject(); |
| 107 | + json.put("entries", new JSONArray()); |
| 108 | + |
| 109 | + EntriesModel model = new EntriesModel(json, "entries", false); |
| 110 | + |
| 111 | + assertNotNull("EntriesModel should not be null", model); |
| 112 | + assertNotNull("Object list should not be null", model.objectList); |
| 113 | + assertEquals(0, model.objectList.size()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testEntriesModelWithNullEntries() { |
| 118 | + JSONObject json = new JSONObject(); |
| 119 | + // No "entries" field |
| 120 | + |
| 121 | + EntriesModel model = new EntriesModel(json, "entries", false); |
| 122 | + |
| 123 | + assertNotNull("EntriesModel should not be null", model); |
| 124 | + assertNotNull("Object list should not be null", model.objectList); |
| 125 | + assertEquals(0, model.objectList.size()); |
| 126 | + } |
| 127 | + |
| 128 | + // ==================== FORM NAME TESTS ==================== |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testEntriesModelFormName() { |
| 132 | + EntriesModel model = new EntriesModel(testEntriesJson, "entries", false); |
| 133 | + assertEquals("entries", model.formName); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testEntriesModelWithCustomFormName() { |
| 138 | + EntriesModel model = new EntriesModel(testEntriesJson, "custom_entries", false); |
| 139 | + assertEquals("custom_entries", model.formName); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testEntriesModelWithNullFormName() { |
| 144 | + EntriesModel model = new EntriesModel(testEntriesJson, null, false); |
| 145 | + assertNotNull("EntriesModel should not be null", model); |
| 146 | + assertNull(model.formName); |
| 147 | + } |
| 148 | + |
| 149 | + // ==================== COMPLEX DATA TESTS ==================== |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testEntriesModelWithComplexEntries() throws Exception { |
| 153 | + JSONObject json = new JSONObject(); |
| 154 | + JSONArray entriesArray = new JSONArray(); |
| 155 | + |
| 156 | + for (int i = 1; i <= 3; i++) { |
| 157 | + JSONObject entry = new JSONObject(); |
| 158 | + entry.put("uid", "complex_entry_" + i); |
| 159 | + entry.put("title", "Complex Entry " + i); |
| 160 | + entry.put("url", "/complex-" + i); |
| 161 | + entry.put("locale", "en-us"); |
| 162 | + |
| 163 | + // Add tags |
| 164 | + JSONArray tags = new JSONArray(); |
| 165 | + tags.put("tag1"); |
| 166 | + tags.put("tag2"); |
| 167 | + entry.put("tags", tags); |
| 168 | + |
| 169 | + // Add metadata |
| 170 | + JSONObject metadata = new JSONObject(); |
| 171 | + metadata.put("uid", "complex_entry_" + i); |
| 172 | + entry.put("_metadata", metadata); |
| 173 | + |
| 174 | + entriesArray.put(entry); |
| 175 | + } |
| 176 | + |
| 177 | + json.put("entries", entriesArray); |
| 178 | + |
| 179 | + EntriesModel model = new EntriesModel(json, "entries", false); |
| 180 | + |
| 181 | + assertNotNull("EntriesModel should not be null", model); |
| 182 | + assertEquals(3, model.objectList.size()); |
| 183 | + |
| 184 | + // Verify entries were parsed with complex data |
| 185 | + for (int i = 0; i < 3; i++) { |
| 186 | + EntryModel entry = (EntryModel) model.objectList.get(i); |
| 187 | + assertNotNull(entry); |
| 188 | + assertNotNull(entry.entryUid); |
| 189 | + assertNotNull(entry.title); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testEntriesModelWithMixedValidAndInvalidEntries() throws Exception { |
| 195 | + JSONObject json = new JSONObject(); |
| 196 | + JSONArray entriesArray = new JSONArray(); |
| 197 | + |
| 198 | + // Add valid entry |
| 199 | + JSONObject validEntry = new JSONObject(); |
| 200 | + validEntry.put("uid", "valid_entry"); |
| 201 | + validEntry.put("title", "Valid Entry"); |
| 202 | + entriesArray.put(validEntry); |
| 203 | + |
| 204 | + // Add invalid entry (string instead of JSONObject) |
| 205 | + entriesArray.put("invalid_entry"); |
| 206 | + |
| 207 | + // Add another valid entry |
| 208 | + JSONObject validEntry2 = new JSONObject(); |
| 209 | + validEntry2.put("uid", "valid_entry2"); |
| 210 | + validEntry2.put("title", "Valid Entry 2"); |
| 211 | + entriesArray.put(validEntry2); |
| 212 | + |
| 213 | + json.put("entries", entriesArray); |
| 214 | + |
| 215 | + EntriesModel model = new EntriesModel(json, "entries", false); |
| 216 | + |
| 217 | + assertNotNull("EntriesModel should not be null", model); |
| 218 | + // Should only process valid JSONObject entries |
| 219 | + assertEquals(2, model.objectList.size()); |
| 220 | + } |
| 221 | + |
| 222 | + // ==================== EDGE CASES ==================== |
| 223 | + |
| 224 | + @Test |
| 225 | + public void testEntriesModelWithEmptyJson() { |
| 226 | + JSONObject emptyJson = new JSONObject(); |
| 227 | + EntriesModel model = new EntriesModel(emptyJson, "entries", false); |
| 228 | + assertNotNull("EntriesModel should not be null", model); |
| 229 | + assertNotNull("Object list should not be null", model.objectList); |
| 230 | + assertEquals(0, model.objectList.size()); |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + public void testEntriesModelWithLargeArray() throws Exception { |
| 235 | + JSONObject json = new JSONObject(); |
| 236 | + JSONArray largeArray = new JSONArray(); |
| 237 | + |
| 238 | + for (int i = 0; i < 100; i++) { |
| 239 | + JSONObject entry = new JSONObject(); |
| 240 | + entry.put("uid", "entry_" + i); |
| 241 | + entry.put("title", "Entry " + i); |
| 242 | + largeArray.put(entry); |
| 243 | + } |
| 244 | + |
| 245 | + json.put("entries", largeArray); |
| 246 | + |
| 247 | + EntriesModel model = new EntriesModel(json, "entries", false); |
| 248 | + |
| 249 | + assertNotNull("EntriesModel should not be null", model); |
| 250 | + assertEquals(100, model.objectList.size()); |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + public void testEntriesModelWithSpecialCharacters() throws Exception { |
| 255 | + JSONObject json = new JSONObject(); |
| 256 | + JSONArray entriesArray = new JSONArray(); |
| 257 | + |
| 258 | + JSONObject entry = new JSONObject(); |
| 259 | + entry.put("uid", "special_entry"); |
| 260 | + entry.put("title", "Entry with special chars: äöü ñ 中文 日本語"); |
| 261 | + entry.put("url", "/entry-special"); |
| 262 | + entriesArray.put(entry); |
| 263 | + |
| 264 | + json.put("entries", entriesArray); |
| 265 | + |
| 266 | + EntriesModel model = new EntriesModel(json, "entries", false); |
| 267 | + |
| 268 | + assertNotNull("EntriesModel should not be null", model); |
| 269 | + assertEquals(1, model.objectList.size()); |
| 270 | + |
| 271 | + EntryModel entryModel = (EntryModel) model.objectList.get(0); |
| 272 | + assertEquals("Entry with special chars: äöü ñ 中文 日本語", entryModel.title); |
| 273 | + } |
| 274 | + |
| 275 | + // ==================== JSON OBJECT FIELD TESTS ==================== |
| 276 | + |
| 277 | + @Test |
| 278 | + public void testEntriesModelJsonObjectField() { |
| 279 | + EntriesModel model = new EntriesModel(testEntriesJson, "entries", false); |
| 280 | + assertNotNull("JSON object should not be null", model.jsonObject); |
| 281 | + assertTrue(model.jsonObject.has("entries")); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + public void testEntriesModelJsonObjectFieldWithCache() throws Exception { |
| 286 | + JSONObject cacheJson = new JSONObject(); |
| 287 | + cacheJson.put("response", testEntriesJson); |
| 288 | + |
| 289 | + EntriesModel model = new EntriesModel(cacheJson, "entries", true); |
| 290 | + assertNotNull("EntriesModel should not be null", model); |
| 291 | + assertNotNull("JSON object should not be null", model.jsonObject); |
| 292 | + } |
| 293 | + |
| 294 | + // ==================== COMBINED SCENARIOS ==================== |
| 295 | + |
| 296 | + @Test |
| 297 | + public void testEntriesModelFromCacheWithComplexData() throws Exception { |
| 298 | + JSONObject cacheJson = new JSONObject(); |
| 299 | + JSONObject response = new JSONObject(); |
| 300 | + JSONArray entriesArray = new JSONArray(); |
| 301 | + |
| 302 | + for (int i = 1; i <= 10; i++) { |
| 303 | + JSONObject entry = new JSONObject(); |
| 304 | + entry.put("uid", "cached_entry_" + i); |
| 305 | + entry.put("title", "Cached Entry " + i); |
| 306 | + entry.put("url", "/cached-" + i); |
| 307 | + entriesArray.put(entry); |
| 308 | + } |
| 309 | + |
| 310 | + response.put("entries", entriesArray); |
| 311 | + cacheJson.put("response", response); |
| 312 | + |
| 313 | + EntriesModel model = new EntriesModel(cacheJson, "entries", true); |
| 314 | + |
| 315 | + assertNotNull("EntriesModel should not be null", model); |
| 316 | + assertNotNull("Object list should not be null", model.objectList); |
| 317 | + assertEquals(10, model.objectList.size()); |
| 318 | + } |
| 319 | +} |
| 320 | + |
0 commit comments