|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import org.json.JSONObject; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import java.lang.reflect.Field; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.LinkedHashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.*; |
| 12 | + |
| 13 | +/** |
| 14 | + * Unit tests for EntriesModel class |
| 15 | + */ |
| 16 | +class TestEntriesModel { |
| 17 | + |
| 18 | + @Test |
| 19 | + void testConstructorWithArrayListEntries() throws Exception { |
| 20 | + // Create entry data as LinkedHashMap |
| 21 | + LinkedHashMap<String, Object> entry1 = new LinkedHashMap<>(); |
| 22 | + entry1.put("uid", "entry1_uid"); |
| 23 | + entry1.put("title", "Entry 1 Title"); |
| 24 | + entry1.put("url", "/entry1"); |
| 25 | + |
| 26 | + LinkedHashMap<String, Object> entry2 = new LinkedHashMap<>(); |
| 27 | + entry2.put("uid", "entry2_uid"); |
| 28 | + entry2.put("title", "Entry 2 Title"); |
| 29 | + entry2.put("url", "/entry2"); |
| 30 | + |
| 31 | + // Create ArrayList of LinkedHashMap entries |
| 32 | + ArrayList<LinkedHashMap<String, Object>> entriesList = new ArrayList<>(); |
| 33 | + entriesList.add(entry1); |
| 34 | + entriesList.add(entry2); |
| 35 | + |
| 36 | + // Create JSONObject and inject ArrayList using reflection |
| 37 | + JSONObject response = new JSONObject(); |
| 38 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 39 | + mapField.setAccessible(true); |
| 40 | + @SuppressWarnings("unchecked") |
| 41 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 42 | + internalMap.put("entries", entriesList); |
| 43 | + |
| 44 | + // Create EntriesModel |
| 45 | + EntriesModel model = new EntriesModel(response); |
| 46 | + |
| 47 | + // Verify |
| 48 | + assertNotNull(model); |
| 49 | + assertNotNull(model.objectList); |
| 50 | + assertEquals(2, model.objectList.size()); |
| 51 | + |
| 52 | + // Verify first entry |
| 53 | + EntryModel firstEntry = (EntryModel) model.objectList.get(0); |
| 54 | + assertEquals("entry1_uid", firstEntry.uid); |
| 55 | + assertEquals("Entry 1 Title", firstEntry.title); |
| 56 | + assertEquals("/entry1", firstEntry.url); |
| 57 | + |
| 58 | + // Verify second entry |
| 59 | + EntryModel secondEntry = (EntryModel) model.objectList.get(1); |
| 60 | + assertEquals("entry2_uid", secondEntry.uid); |
| 61 | + assertEquals("Entry 2 Title", secondEntry.title); |
| 62 | + assertEquals("/entry2", secondEntry.url); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testConstructorWithSingleEntry() throws Exception { |
| 67 | + // Create single entry as LinkedHashMap |
| 68 | + LinkedHashMap<String, Object> entry = new LinkedHashMap<>(); |
| 69 | + entry.put("uid", "single_entry_uid"); |
| 70 | + entry.put("title", "Single Entry"); |
| 71 | + entry.put("url", "/single-entry"); |
| 72 | + entry.put("locale", "en-us"); |
| 73 | + |
| 74 | + // Create ArrayList with single entry |
| 75 | + ArrayList<LinkedHashMap<String, Object>> entriesList = new ArrayList<>(); |
| 76 | + entriesList.add(entry); |
| 77 | + |
| 78 | + // Create JSONObject and inject ArrayList using reflection |
| 79 | + JSONObject response = new JSONObject(); |
| 80 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 81 | + mapField.setAccessible(true); |
| 82 | + @SuppressWarnings("unchecked") |
| 83 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 84 | + internalMap.put("entries", entriesList); |
| 85 | + |
| 86 | + // Create EntriesModel |
| 87 | + EntriesModel model = new EntriesModel(response); |
| 88 | + |
| 89 | + // Verify |
| 90 | + assertNotNull(model); |
| 91 | + assertNotNull(model.objectList); |
| 92 | + assertEquals(1, model.objectList.size()); |
| 93 | + |
| 94 | + EntryModel entryModel = (EntryModel) model.objectList.get(0); |
| 95 | + assertEquals("single_entry_uid", entryModel.uid); |
| 96 | + assertEquals("Single Entry", entryModel.title); |
| 97 | + assertEquals("/single-entry", entryModel.url); |
| 98 | + assertEquals("en-us", entryModel.language); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testConstructorWithEmptyEntriesList() throws Exception { |
| 103 | + // Create empty ArrayList |
| 104 | + ArrayList<LinkedHashMap<String, Object>> entriesList = new ArrayList<>(); |
| 105 | + |
| 106 | + // Create JSONObject and inject empty ArrayList using reflection |
| 107 | + JSONObject response = new JSONObject(); |
| 108 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 109 | + mapField.setAccessible(true); |
| 110 | + @SuppressWarnings("unchecked") |
| 111 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 112 | + internalMap.put("entries", entriesList); |
| 113 | + |
| 114 | + // Create EntriesModel |
| 115 | + EntriesModel model = new EntriesModel(response); |
| 116 | + |
| 117 | + // Verify - should have empty objectList |
| 118 | + assertNotNull(model); |
| 119 | + assertNotNull(model.objectList); |
| 120 | + assertEquals(0, model.objectList.size()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testConstructorWithNullEntries() { |
| 125 | + // Create JSONObject without "entries" key |
| 126 | + JSONObject response = new JSONObject(); |
| 127 | + |
| 128 | + // Create EntriesModel |
| 129 | + EntriesModel model = new EntriesModel(response); |
| 130 | + |
| 131 | + // Verify - should have empty objectList |
| 132 | + assertNotNull(model); |
| 133 | + assertNotNull(model.objectList); |
| 134 | + assertEquals(0, model.objectList.size()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void testConstructorWithNonArrayListEntries() { |
| 139 | + // Create JSONObject with entries as a string (invalid type) |
| 140 | + JSONObject response = new JSONObject(); |
| 141 | + response.put("entries", "not_an_arraylist"); |
| 142 | + |
| 143 | + // Create EntriesModel - should handle gracefully |
| 144 | + EntriesModel model = new EntriesModel(response); |
| 145 | + |
| 146 | + // Verify - should have empty objectList |
| 147 | + assertNotNull(model); |
| 148 | + assertNotNull(model.objectList); |
| 149 | + assertEquals(0, model.objectList.size()); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void testConstructorWithComplexEntryData() throws Exception { |
| 154 | + // Create entry with nested data |
| 155 | + LinkedHashMap<String, Object> entry = new LinkedHashMap<>(); |
| 156 | + entry.put("uid", "complex_entry_uid"); |
| 157 | + entry.put("title", "Complex Entry"); |
| 158 | + entry.put("url", "/complex-entry"); |
| 159 | + entry.put("locale", "en-us"); |
| 160 | + entry.put("description", "Complex entry description"); |
| 161 | + entry.put("_version", 2); |
| 162 | + |
| 163 | + // Create ArrayList with entry |
| 164 | + ArrayList<LinkedHashMap<String, Object>> entriesList = new ArrayList<>(); |
| 165 | + entriesList.add(entry); |
| 166 | + |
| 167 | + // Create JSONObject and inject ArrayList using reflection |
| 168 | + JSONObject response = new JSONObject(); |
| 169 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 170 | + mapField.setAccessible(true); |
| 171 | + @SuppressWarnings("unchecked") |
| 172 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 173 | + internalMap.put("entries", entriesList); |
| 174 | + |
| 175 | + // Create EntriesModel |
| 176 | + EntriesModel model = new EntriesModel(response); |
| 177 | + |
| 178 | + // Verify |
| 179 | + assertNotNull(model); |
| 180 | + assertNotNull(model.objectList); |
| 181 | + assertEquals(1, model.objectList.size()); |
| 182 | + |
| 183 | + EntryModel entryModel = (EntryModel) model.objectList.get(0); |
| 184 | + assertEquals("complex_entry_uid", entryModel.uid); |
| 185 | + assertEquals("Complex Entry", entryModel.title); |
| 186 | + assertEquals("/complex-entry", entryModel.url); |
| 187 | + assertEquals("en-us", entryModel.language); |
| 188 | + assertEquals("Complex entry description", entryModel.description); |
| 189 | + assertEquals(2, entryModel.version); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + void testConstructorWithMultipleEntries() throws Exception { |
| 194 | + // Create multiple entries |
| 195 | + ArrayList<LinkedHashMap<String, Object>> entriesList = new ArrayList<>(); |
| 196 | + |
| 197 | + for (int i = 1; i <= 5; i++) { |
| 198 | + LinkedHashMap<String, Object> entry = new LinkedHashMap<>(); |
| 199 | + entry.put("uid", "entry_" + i); |
| 200 | + entry.put("title", "Entry " + i); |
| 201 | + entry.put("url", "/entry-" + i); |
| 202 | + entriesList.add(entry); |
| 203 | + } |
| 204 | + |
| 205 | + // Create JSONObject and inject ArrayList using reflection |
| 206 | + JSONObject response = new JSONObject(); |
| 207 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 208 | + mapField.setAccessible(true); |
| 209 | + @SuppressWarnings("unchecked") |
| 210 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 211 | + internalMap.put("entries", entriesList); |
| 212 | + |
| 213 | + // Create EntriesModel |
| 214 | + EntriesModel model = new EntriesModel(response); |
| 215 | + |
| 216 | + // Verify |
| 217 | + assertNotNull(model); |
| 218 | + assertNotNull(model.objectList); |
| 219 | + assertEquals(5, model.objectList.size()); |
| 220 | + |
| 221 | + // Verify each entry |
| 222 | + for (int i = 0; i < 5; i++) { |
| 223 | + EntryModel entryModel = (EntryModel) model.objectList.get(i); |
| 224 | + assertEquals("entry_" + (i + 1), entryModel.uid); |
| 225 | + assertEquals("Entry " + (i + 1), entryModel.title); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + @Test |
| 230 | + void testConstructorWithMixedValidAndInvalidEntries() throws Exception { |
| 231 | + // Create ArrayList with mixed types |
| 232 | + ArrayList<Object> entriesList = new ArrayList<>(); |
| 233 | + |
| 234 | + // Add valid LinkedHashMap entry |
| 235 | + LinkedHashMap<String, Object> validEntry = new LinkedHashMap<>(); |
| 236 | + validEntry.put("uid", "valid_entry"); |
| 237 | + validEntry.put("title", "Valid Entry"); |
| 238 | + entriesList.add(validEntry); |
| 239 | + |
| 240 | + // Add invalid entry (String instead of LinkedHashMap) |
| 241 | + entriesList.add("invalid_entry"); |
| 242 | + |
| 243 | + // Create JSONObject and inject ArrayList using reflection |
| 244 | + JSONObject response = new JSONObject(); |
| 245 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 246 | + mapField.setAccessible(true); |
| 247 | + @SuppressWarnings("unchecked") |
| 248 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 249 | + internalMap.put("entries", entriesList); |
| 250 | + |
| 251 | + // Create EntriesModel - should only process valid entry |
| 252 | + EntriesModel model = new EntriesModel(response); |
| 253 | + |
| 254 | + // Verify - should have only 1 entry (the valid one) |
| 255 | + assertNotNull(model); |
| 256 | + assertNotNull(model.objectList); |
| 257 | + assertEquals(1, model.objectList.size()); |
| 258 | + |
| 259 | + EntryModel entryModel = (EntryModel) model.objectList.get(0); |
| 260 | + assertEquals("valid_entry", entryModel.uid); |
| 261 | + assertEquals("Valid Entry", entryModel.title); |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + void testConstructorWithExceptionHandling() { |
| 266 | + // Create a malformed JSONObject that might cause exceptions |
| 267 | + JSONObject response = new JSONObject(); |
| 268 | + response.put("entries", new Object()); // Invalid type |
| 269 | + |
| 270 | + // Should not throw exception, should handle gracefully |
| 271 | + assertDoesNotThrow(() -> { |
| 272 | + EntriesModel model = new EntriesModel(response); |
| 273 | + assertNotNull(model); |
| 274 | + assertNotNull(model.objectList); |
| 275 | + }); |
| 276 | + } |
| 277 | + |
| 278 | + @Test |
| 279 | + void testJsonObjectField() throws Exception { |
| 280 | + // Create entry data |
| 281 | + LinkedHashMap<String, Object> entry = new LinkedHashMap<>(); |
| 282 | + entry.put("uid", "test_entry"); |
| 283 | + entry.put("title", "Test Entry"); |
| 284 | + |
| 285 | + ArrayList<LinkedHashMap<String, Object>> entriesList = new ArrayList<>(); |
| 286 | + entriesList.add(entry); |
| 287 | + |
| 288 | + // Create JSONObject and inject ArrayList using reflection |
| 289 | + JSONObject response = new JSONObject(); |
| 290 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 291 | + mapField.setAccessible(true); |
| 292 | + @SuppressWarnings("unchecked") |
| 293 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 294 | + internalMap.put("entries", entriesList); |
| 295 | + |
| 296 | + // Create EntriesModel |
| 297 | + EntriesModel model = new EntriesModel(response); |
| 298 | + |
| 299 | + // Verify jsonObject field is set |
| 300 | + assertNotNull(model.jsonObject); |
| 301 | + assertTrue(model.jsonObject.has("entries")); |
| 302 | + } |
| 303 | + |
| 304 | + @Test |
| 305 | + void testConstructorWithEntryContainingAllFields() throws Exception { |
| 306 | + // Create comprehensive entry with all possible fields |
| 307 | + LinkedHashMap<String, Object> entry = new LinkedHashMap<>(); |
| 308 | + entry.put("uid", "comprehensive_entry"); |
| 309 | + entry.put("title", "Comprehensive Entry"); |
| 310 | + entry.put("url", "/comprehensive"); |
| 311 | + entry.put("locale", "en-us"); |
| 312 | + entry.put("_version", 1); |
| 313 | + entry.put("created_at", "2024-01-01T00:00:00.000Z"); |
| 314 | + entry.put("updated_at", "2024-01-02T00:00:00.000Z"); |
| 315 | + |
| 316 | + ArrayList<LinkedHashMap<String, Object>> entriesList = new ArrayList<>(); |
| 317 | + entriesList.add(entry); |
| 318 | + |
| 319 | + // Create JSONObject and inject ArrayList using reflection |
| 320 | + JSONObject response = new JSONObject(); |
| 321 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 322 | + mapField.setAccessible(true); |
| 323 | + @SuppressWarnings("unchecked") |
| 324 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 325 | + internalMap.put("entries", entriesList); |
| 326 | + |
| 327 | + // Create EntriesModel |
| 328 | + EntriesModel model = new EntriesModel(response); |
| 329 | + |
| 330 | + // Verify |
| 331 | + assertNotNull(model); |
| 332 | + assertNotNull(model.objectList); |
| 333 | + assertEquals(1, model.objectList.size()); |
| 334 | + |
| 335 | + EntryModel entryModel = (EntryModel) model.objectList.get(0); |
| 336 | + assertEquals("comprehensive_entry", entryModel.uid); |
| 337 | + assertEquals("Comprehensive Entry", entryModel.title); |
| 338 | + assertEquals("/comprehensive", entryModel.url); |
| 339 | + assertEquals("en-us", entryModel.language); |
| 340 | + } |
| 341 | +} |
| 342 | + |
0 commit comments