|
4 | 4 | import org.json.JSONObject; |
5 | 5 | import org.junit.jupiter.api.Test; |
6 | 6 |
|
| 7 | +import java.lang.reflect.Field; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.LinkedHashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
7 | 12 | import static org.junit.jupiter.api.Assertions.*; |
8 | 13 |
|
9 | 14 | /** |
@@ -78,4 +83,358 @@ void testMultipleSetJSONCalls() { |
78 | 83 | // Should not throw exception |
79 | 84 | assertNotNull(model); |
80 | 85 | } |
| 86 | + |
| 87 | + // ========== SINGLE GLOBAL FIELD TESTS (global_field key) ========== |
| 88 | + |
| 89 | + @Test |
| 90 | + void testSetJSONWithSingleGlobalField() throws Exception { |
| 91 | + LinkedHashMap<String, Object> globalFieldMap = new LinkedHashMap<>(); |
| 92 | + globalFieldMap.put("uid", "seo_metadata"); |
| 93 | + globalFieldMap.put("title", "SEO Metadata"); |
| 94 | + globalFieldMap.put("description", "SEO related fields"); |
| 95 | + |
| 96 | + JSONObject response = new JSONObject(); |
| 97 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 98 | + mapField.setAccessible(true); |
| 99 | + @SuppressWarnings("unchecked") |
| 100 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 101 | + internalMap.put("global_field", globalFieldMap); |
| 102 | + |
| 103 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 104 | + model.setJSON(response); |
| 105 | + |
| 106 | + assertNotNull(model.getResponse()); |
| 107 | + assertTrue(model.getResponse() instanceof JSONObject); |
| 108 | + |
| 109 | + JSONObject responseObj = (JSONObject) model.getResponse(); |
| 110 | + assertEquals("seo_metadata", responseObj.opt("uid")); |
| 111 | + assertEquals("SEO Metadata", responseObj.opt("title")); |
| 112 | + assertEquals("SEO related fields", responseObj.opt("description")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testSetJSONWithSingleGlobalFieldMinimal() throws Exception { |
| 117 | + LinkedHashMap<String, Object> globalFieldMap = new LinkedHashMap<>(); |
| 118 | + globalFieldMap.put("uid", "minimal_gf"); |
| 119 | + |
| 120 | + JSONObject response = new JSONObject(); |
| 121 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 122 | + mapField.setAccessible(true); |
| 123 | + @SuppressWarnings("unchecked") |
| 124 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 125 | + internalMap.put("global_field", globalFieldMap); |
| 126 | + |
| 127 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 128 | + model.setJSON(response); |
| 129 | + |
| 130 | + assertNotNull(model.getResponse()); |
| 131 | + assertTrue(model.getResponse() instanceof JSONObject); |
| 132 | + |
| 133 | + JSONObject responseObj = (JSONObject) model.getResponse(); |
| 134 | + assertEquals("minimal_gf", responseObj.opt("uid")); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void testSetJSONWithSingleGlobalFieldEmptyMap() throws Exception { |
| 139 | + LinkedHashMap<String, Object> globalFieldMap = new LinkedHashMap<>(); |
| 140 | + |
| 141 | + JSONObject response = new JSONObject(); |
| 142 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 143 | + mapField.setAccessible(true); |
| 144 | + @SuppressWarnings("unchecked") |
| 145 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 146 | + internalMap.put("global_field", globalFieldMap); |
| 147 | + |
| 148 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 149 | + model.setJSON(response); |
| 150 | + |
| 151 | + assertNotNull(model.getResponse()); |
| 152 | + assertTrue(model.getResponse() instanceof JSONObject); |
| 153 | + } |
| 154 | + |
| 155 | + // ========== MULTIPLE GLOBAL FIELDS TESTS (global_fields key) ========== |
| 156 | + |
| 157 | + @Test |
| 158 | + void testSetJSONWithMultipleGlobalFields() throws Exception { |
| 159 | + LinkedHashMap<String, Object> gf1 = new LinkedHashMap<>(); |
| 160 | + gf1.put("uid", "seo_metadata"); |
| 161 | + gf1.put("title", "SEO Metadata"); |
| 162 | + |
| 163 | + LinkedHashMap<String, Object> gf2 = new LinkedHashMap<>(); |
| 164 | + gf2.put("uid", "author_info"); |
| 165 | + gf2.put("title", "Author Information"); |
| 166 | + |
| 167 | + ArrayList<LinkedHashMap<?, ?>> globalFieldsList = new ArrayList<>(); |
| 168 | + globalFieldsList.add(gf1); |
| 169 | + globalFieldsList.add(gf2); |
| 170 | + |
| 171 | + JSONObject response = new JSONObject(); |
| 172 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 173 | + mapField.setAccessible(true); |
| 174 | + @SuppressWarnings("unchecked") |
| 175 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 176 | + internalMap.put("global_fields", globalFieldsList); |
| 177 | + |
| 178 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 179 | + model.setJSON(response); |
| 180 | + |
| 181 | + assertNotNull(model.getResponse()); |
| 182 | + assertTrue(model.getResponse() instanceof JSONArray); |
| 183 | + |
| 184 | + JSONArray responseArray = (JSONArray) model.getResponse(); |
| 185 | + assertEquals(2, responseArray.length()); |
| 186 | + |
| 187 | + JSONObject firstGF = (JSONObject) responseArray.get(0); |
| 188 | + assertEquals("seo_metadata", firstGF.opt("uid")); |
| 189 | + assertEquals("SEO Metadata", firstGF.opt("title")); |
| 190 | + |
| 191 | + JSONObject secondGF = (JSONObject) responseArray.get(1); |
| 192 | + assertEquals("author_info", secondGF.opt("uid")); |
| 193 | + assertEquals("Author Information", secondGF.opt("title")); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void testSetJSONWithSingleGlobalFieldInList() throws Exception { |
| 198 | + LinkedHashMap<String, Object> gf1 = new LinkedHashMap<>(); |
| 199 | + gf1.put("uid", "single_gf"); |
| 200 | + gf1.put("title", "Single Global Field"); |
| 201 | + |
| 202 | + ArrayList<LinkedHashMap<?, ?>> globalFieldsList = new ArrayList<>(); |
| 203 | + globalFieldsList.add(gf1); |
| 204 | + |
| 205 | + JSONObject response = new JSONObject(); |
| 206 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 207 | + mapField.setAccessible(true); |
| 208 | + @SuppressWarnings("unchecked") |
| 209 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 210 | + internalMap.put("global_fields", globalFieldsList); |
| 211 | + |
| 212 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 213 | + model.setJSON(response); |
| 214 | + |
| 215 | + assertNotNull(model.getResponse()); |
| 216 | + assertTrue(model.getResponse() instanceof JSONArray); |
| 217 | + |
| 218 | + JSONArray responseArray = (JSONArray) model.getResponse(); |
| 219 | + assertEquals(1, responseArray.length()); |
| 220 | + |
| 221 | + JSONObject firstGF = (JSONObject) responseArray.get(0); |
| 222 | + assertEquals("single_gf", firstGF.opt("uid")); |
| 223 | + assertEquals("Single Global Field", firstGF.opt("title")); |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + void testSetJSONWithEmptyGlobalFieldsList() throws Exception { |
| 228 | + ArrayList<LinkedHashMap<?, ?>> globalFieldsList = new ArrayList<>(); |
| 229 | + |
| 230 | + JSONObject response = new JSONObject(); |
| 231 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 232 | + mapField.setAccessible(true); |
| 233 | + @SuppressWarnings("unchecked") |
| 234 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 235 | + internalMap.put("global_fields", globalFieldsList); |
| 236 | + |
| 237 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 238 | + model.setJSON(response); |
| 239 | + |
| 240 | + assertNotNull(model.getResponse()); |
| 241 | + assertTrue(model.getResponse() instanceof JSONArray); |
| 242 | + |
| 243 | + JSONArray responseArray = (JSONArray) model.getResponse(); |
| 244 | + assertEquals(0, responseArray.length()); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + void testSetJSONWithManyGlobalFields() throws Exception { |
| 249 | + ArrayList<LinkedHashMap<?, ?>> globalFieldsList = new ArrayList<>(); |
| 250 | + |
| 251 | + for (int i = 0; i < 5; i++) { |
| 252 | + LinkedHashMap<String, Object> gf = new LinkedHashMap<>(); |
| 253 | + gf.put("uid", "global_field_" + i); |
| 254 | + gf.put("title", "Global Field " + i); |
| 255 | + globalFieldsList.add(gf); |
| 256 | + } |
| 257 | + |
| 258 | + JSONObject response = new JSONObject(); |
| 259 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 260 | + mapField.setAccessible(true); |
| 261 | + @SuppressWarnings("unchecked") |
| 262 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 263 | + internalMap.put("global_fields", globalFieldsList); |
| 264 | + |
| 265 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 266 | + model.setJSON(response); |
| 267 | + |
| 268 | + assertNotNull(model.getResponse()); |
| 269 | + assertTrue(model.getResponse() instanceof JSONArray); |
| 270 | + |
| 271 | + JSONArray responseArray = (JSONArray) model.getResponse(); |
| 272 | + assertEquals(5, responseArray.length()); |
| 273 | + |
| 274 | + for (int i = 0; i < 5; i++) { |
| 275 | + JSONObject gf = (JSONObject) responseArray.get(i); |
| 276 | + assertEquals("global_field_" + i, gf.opt("uid")); |
| 277 | + assertEquals("Global Field " + i, gf.opt("title")); |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + // ========== GET RESULT ARRAY TESTS ========== |
| 282 | + |
| 283 | + @Test |
| 284 | + void testGetResultArrayWithMultipleGlobalFields() throws Exception { |
| 285 | + LinkedHashMap<String, Object> gf1 = new LinkedHashMap<>(); |
| 286 | + gf1.put("uid", "gf_1"); |
| 287 | + |
| 288 | + LinkedHashMap<String, Object> gf2 = new LinkedHashMap<>(); |
| 289 | + gf2.put("uid", "gf_2"); |
| 290 | + |
| 291 | + ArrayList<LinkedHashMap<?, ?>> globalFieldsList = new ArrayList<>(); |
| 292 | + globalFieldsList.add(gf1); |
| 293 | + globalFieldsList.add(gf2); |
| 294 | + |
| 295 | + JSONObject response = new JSONObject(); |
| 296 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 297 | + mapField.setAccessible(true); |
| 298 | + @SuppressWarnings("unchecked") |
| 299 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 300 | + internalMap.put("global_fields", globalFieldsList); |
| 301 | + |
| 302 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 303 | + model.setJSON(response); |
| 304 | + |
| 305 | + JSONArray resultArray = model.getResultArray(); |
| 306 | + assertNotNull(resultArray); |
| 307 | + assertEquals(2, resultArray.length()); |
| 308 | + |
| 309 | + JSONObject firstGF = (JSONObject) resultArray.get(0); |
| 310 | + assertEquals("gf_1", firstGF.opt("uid")); |
| 311 | + } |
| 312 | + |
| 313 | + @Test |
| 314 | + void testGetResultArrayWithEmptyList() throws Exception { |
| 315 | + ArrayList<LinkedHashMap<?, ?>> globalFieldsList = new ArrayList<>(); |
| 316 | + |
| 317 | + JSONObject response = new JSONObject(); |
| 318 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 319 | + mapField.setAccessible(true); |
| 320 | + @SuppressWarnings("unchecked") |
| 321 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 322 | + internalMap.put("global_fields", globalFieldsList); |
| 323 | + |
| 324 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 325 | + model.setJSON(response); |
| 326 | + |
| 327 | + JSONArray resultArray = model.getResultArray(); |
| 328 | + assertNotNull(resultArray); |
| 329 | + assertEquals(0, resultArray.length()); |
| 330 | + } |
| 331 | + |
| 332 | + // ========== EDGE CASE TESTS ========== |
| 333 | + |
| 334 | + @Test |
| 335 | + void testSetJSONWithBothSingleAndMultiple() throws Exception { |
| 336 | + // Test when both keys are present - should process both |
| 337 | + LinkedHashMap<String, Object> singleGF = new LinkedHashMap<>(); |
| 338 | + singleGF.put("uid", "single"); |
| 339 | + |
| 340 | + LinkedHashMap<String, Object> multiGF = new LinkedHashMap<>(); |
| 341 | + multiGF.put("uid", "multi"); |
| 342 | + |
| 343 | + ArrayList<LinkedHashMap<?, ?>> globalFieldsList = new ArrayList<>(); |
| 344 | + globalFieldsList.add(multiGF); |
| 345 | + |
| 346 | + JSONObject response = new JSONObject(); |
| 347 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 348 | + mapField.setAccessible(true); |
| 349 | + @SuppressWarnings("unchecked") |
| 350 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 351 | + internalMap.put("global_field", singleGF); |
| 352 | + internalMap.put("global_fields", globalFieldsList); |
| 353 | + |
| 354 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 355 | + model.setJSON(response); |
| 356 | + |
| 357 | + // When both are present, global_fields overwrites the response |
| 358 | + assertNotNull(model.getResponse()); |
| 359 | + assertTrue(model.getResponse() instanceof JSONArray); |
| 360 | + } |
| 361 | + |
| 362 | + @Test |
| 363 | + void testSetJSONWithComplexGlobalField() throws Exception { |
| 364 | + LinkedHashMap<String, Object> globalFieldMap = new LinkedHashMap<>(); |
| 365 | + globalFieldMap.put("uid", "complex_gf"); |
| 366 | + globalFieldMap.put("title", "Complex Global Field"); |
| 367 | + globalFieldMap.put("schema", new LinkedHashMap<>()); |
| 368 | + globalFieldMap.put("version", 2); |
| 369 | + |
| 370 | + JSONObject response = new JSONObject(); |
| 371 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 372 | + mapField.setAccessible(true); |
| 373 | + @SuppressWarnings("unchecked") |
| 374 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 375 | + internalMap.put("global_field", globalFieldMap); |
| 376 | + |
| 377 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 378 | + model.setJSON(response); |
| 379 | + |
| 380 | + assertNotNull(model.getResponse()); |
| 381 | + assertTrue(model.getResponse() instanceof JSONObject); |
| 382 | + |
| 383 | + JSONObject responseObj = (JSONObject) model.getResponse(); |
| 384 | + assertEquals("complex_gf", responseObj.opt("uid")); |
| 385 | + assertEquals(2, responseObj.opt("version")); |
| 386 | + } |
| 387 | + |
| 388 | + @Test |
| 389 | + void testSetJSONWithNullGlobalFieldValue() throws Exception { |
| 390 | + JSONObject response = new JSONObject(); |
| 391 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 392 | + mapField.setAccessible(true); |
| 393 | + @SuppressWarnings("unchecked") |
| 394 | + Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response); |
| 395 | + internalMap.put("global_field", null); |
| 396 | + |
| 397 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 398 | + model.setJSON(response); |
| 399 | + |
| 400 | + // Should not crash, response should remain null |
| 401 | + assertNull(model.getResponse()); |
| 402 | + } |
| 403 | + |
| 404 | + @Test |
| 405 | + void testSetJSONMultipleTimes() throws Exception { |
| 406 | + GlobalFieldsModel model = new GlobalFieldsModel(); |
| 407 | + |
| 408 | + // First call with single global field |
| 409 | + LinkedHashMap<String, Object> singleGF = new LinkedHashMap<>(); |
| 410 | + singleGF.put("uid", "first"); |
| 411 | + |
| 412 | + JSONObject response1 = new JSONObject(); |
| 413 | + Field mapField = JSONObject.class.getDeclaredField("map"); |
| 414 | + mapField.setAccessible(true); |
| 415 | + @SuppressWarnings("unchecked") |
| 416 | + Map<String, Object> internalMap1 = (Map<String, Object>) mapField.get(response1); |
| 417 | + internalMap1.put("global_field", singleGF); |
| 418 | + |
| 419 | + model.setJSON(response1); |
| 420 | + assertNotNull(model.getResponse()); |
| 421 | + assertTrue(model.getResponse() instanceof JSONObject); |
| 422 | + |
| 423 | + // Second call with multiple global fields |
| 424 | + LinkedHashMap<String, Object> multiGF = new LinkedHashMap<>(); |
| 425 | + multiGF.put("uid", "second"); |
| 426 | + |
| 427 | + ArrayList<LinkedHashMap<?, ?>> globalFieldsList = new ArrayList<>(); |
| 428 | + globalFieldsList.add(multiGF); |
| 429 | + |
| 430 | + JSONObject response2 = new JSONObject(); |
| 431 | + @SuppressWarnings("unchecked") |
| 432 | + Map<String, Object> internalMap2 = (Map<String, Object>) mapField.get(response2); |
| 433 | + internalMap2.put("global_fields", globalFieldsList); |
| 434 | + |
| 435 | + model.setJSON(response2); |
| 436 | + assertNotNull(model.getResponse()); |
| 437 | + assertTrue(model.getResponse() instanceof JSONArray); |
| 438 | + } |
| 439 | + |
81 | 440 | } |
0 commit comments