Skip to content

Commit 9d43c94

Browse files
Add comprehensive unit tests for ContentTypesModel class
1 parent aa4b7c2 commit 9d43c94

File tree

1 file changed

+356
-0
lines changed

1 file changed

+356
-0
lines changed

src/test/java/com/contentstack/sdk/TestContentTypesModel.java

Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import org.json.JSONObject;
55
import org.junit.jupiter.api.Test;
66

7+
import java.lang.reflect.Field;
8+
import java.util.ArrayList;
9+
import java.util.LinkedHashMap;
10+
import java.util.Map;
11+
712
import static org.junit.jupiter.api.Assertions.*;
813

914
/**
@@ -84,4 +89,355 @@ void testMultipleSetJSONCalls() {
8489
// Should not throw exception
8590
assertNotNull(model);
8691
}
92+
93+
// ========== SINGLE CONTENT TYPE (LinkedHashMap) TESTS ==========
94+
95+
@Test
96+
void testSetJSONWithSingleContentType() throws Exception {
97+
// Test the instanceof LinkedHashMap path
98+
// We use reflection to inject LinkedHashMap directly
99+
100+
LinkedHashMap<String, Object> contentTypeMap = new LinkedHashMap<>();
101+
contentTypeMap.put("uid", "blog_post");
102+
contentTypeMap.put("title", "Blog Post");
103+
contentTypeMap.put("description", "A blog post content type");
104+
105+
JSONObject response = new JSONObject();
106+
107+
// Use reflection to inject the LinkedHashMap directly
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("content_type", contentTypeMap);
113+
114+
ContentTypesModel model = new ContentTypesModel();
115+
model.setJSON(response);
116+
117+
// Verify the response was set
118+
assertNotNull(model.getResponse());
119+
assertTrue(model.getResponse() instanceof JSONObject);
120+
121+
JSONObject responseObj = (JSONObject) model.getResponse();
122+
assertEquals("blog_post", responseObj.opt("uid"));
123+
assertEquals("Blog Post", responseObj.opt("title"));
124+
assertEquals("A blog post content type", responseObj.opt("description"));
125+
}
126+
127+
@Test
128+
void testSetJSONWithSingleContentTypeMinimal() throws Exception {
129+
LinkedHashMap<String, Object> contentTypeMap = new LinkedHashMap<>();
130+
contentTypeMap.put("uid", "minimal_ct");
131+
132+
JSONObject response = new JSONObject();
133+
134+
Field mapField = JSONObject.class.getDeclaredField("map");
135+
mapField.setAccessible(true);
136+
@SuppressWarnings("unchecked")
137+
Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response);
138+
internalMap.put("content_type", contentTypeMap);
139+
140+
ContentTypesModel model = new ContentTypesModel();
141+
model.setJSON(response);
142+
143+
assertNotNull(model.getResponse());
144+
assertTrue(model.getResponse() instanceof JSONObject);
145+
}
146+
147+
@Test
148+
void testSetJSONWithSingleContentTypeWithSchema() throws Exception {
149+
LinkedHashMap<String, Object> contentTypeMap = new LinkedHashMap<>();
150+
contentTypeMap.put("uid", "complex_ct");
151+
contentTypeMap.put("title", "Complex Content Type");
152+
153+
// Add schema
154+
ArrayList<Object> schemaList = new ArrayList<>();
155+
LinkedHashMap<String, Object> field1 = new LinkedHashMap<>();
156+
field1.put("uid", "title");
157+
field1.put("data_type", "text");
158+
schemaList.add(field1);
159+
contentTypeMap.put("schema", schemaList);
160+
161+
JSONObject response = new JSONObject();
162+
163+
Field mapField = JSONObject.class.getDeclaredField("map");
164+
mapField.setAccessible(true);
165+
@SuppressWarnings("unchecked")
166+
Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response);
167+
internalMap.put("content_type", contentTypeMap);
168+
169+
ContentTypesModel model = new ContentTypesModel();
170+
model.setJSON(response);
171+
172+
assertNotNull(model.getResponse());
173+
JSONObject responseObj = (JSONObject) model.getResponse();
174+
assertEquals("complex_ct", responseObj.opt("uid"));
175+
}
176+
177+
// ========== MULTIPLE CONTENT TYPES (ArrayList) TESTS ==========
178+
179+
@Test
180+
void testSetJSONWithMultipleContentTypes() throws Exception {
181+
// Test the instanceof ArrayList path
182+
183+
LinkedHashMap<String, Object> ct1 = new LinkedHashMap<>();
184+
ct1.put("uid", "blog_post");
185+
ct1.put("title", "Blog Post");
186+
187+
LinkedHashMap<String, Object> ct2 = new LinkedHashMap<>();
188+
ct2.put("uid", "page");
189+
ct2.put("title", "Page");
190+
191+
LinkedHashMap<String, Object> ct3 = new LinkedHashMap<>();
192+
ct3.put("uid", "product");
193+
ct3.put("title", "Product");
194+
195+
ArrayList<LinkedHashMap<String, Object>> contentTypesList = new ArrayList<>();
196+
contentTypesList.add(ct1);
197+
contentTypesList.add(ct2);
198+
contentTypesList.add(ct3);
199+
200+
JSONObject response = new JSONObject();
201+
202+
// Use reflection to inject the ArrayList directly
203+
Field mapField = JSONObject.class.getDeclaredField("map");
204+
mapField.setAccessible(true);
205+
@SuppressWarnings("unchecked")
206+
Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response);
207+
internalMap.put("content_types", contentTypesList);
208+
209+
ContentTypesModel model = new ContentTypesModel();
210+
model.setJSON(response);
211+
212+
// Verify the response was set as JSONArray
213+
assertNotNull(model.getResponse());
214+
assertTrue(model.getResponse() instanceof JSONArray);
215+
216+
JSONArray responseArray = (JSONArray) model.getResponse();
217+
assertEquals(3, responseArray.length());
218+
219+
// Verify the responseJSONArray was also set
220+
assertNotNull(model.getResultArray());
221+
assertEquals(3, model.getResultArray().length());
222+
223+
// Verify content of first content type
224+
JSONObject firstCT = responseArray.getJSONObject(0);
225+
assertEquals("blog_post", firstCT.opt("uid"));
226+
assertEquals("Blog Post", firstCT.opt("title"));
227+
}
228+
229+
@Test
230+
void testSetJSONWithEmptyContentTypesList() throws Exception {
231+
ArrayList<LinkedHashMap<String, Object>> emptyList = new ArrayList<>();
232+
233+
JSONObject response = new JSONObject();
234+
235+
Field mapField = JSONObject.class.getDeclaredField("map");
236+
mapField.setAccessible(true);
237+
@SuppressWarnings("unchecked")
238+
Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response);
239+
internalMap.put("content_types", emptyList);
240+
241+
ContentTypesModel model = new ContentTypesModel();
242+
model.setJSON(response);
243+
244+
// Empty list should still create an empty JSONArray
245+
assertNotNull(model.getResponse());
246+
assertTrue(model.getResponse() instanceof JSONArray);
247+
248+
JSONArray responseArray = (JSONArray) model.getResponse();
249+
assertEquals(0, responseArray.length());
250+
}
251+
252+
@Test
253+
void testSetJSONWithSingleItemContentTypesList() throws Exception {
254+
LinkedHashMap<String, Object> ct = new LinkedHashMap<>();
255+
ct.put("uid", "single_ct");
256+
ct.put("title", "Single Content Type");
257+
258+
ArrayList<LinkedHashMap<String, Object>> singleItemList = new ArrayList<>();
259+
singleItemList.add(ct);
260+
261+
JSONObject response = new JSONObject();
262+
263+
Field mapField = JSONObject.class.getDeclaredField("map");
264+
mapField.setAccessible(true);
265+
@SuppressWarnings("unchecked")
266+
Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response);
267+
internalMap.put("content_types", singleItemList);
268+
269+
ContentTypesModel model = new ContentTypesModel();
270+
model.setJSON(response);
271+
272+
assertNotNull(model.getResponse());
273+
assertTrue(model.getResponse() instanceof JSONArray);
274+
275+
JSONArray responseArray = (JSONArray) model.getResponse();
276+
assertEquals(1, responseArray.length());
277+
278+
JSONObject firstCT = responseArray.getJSONObject(0);
279+
assertEquals("single_ct", firstCT.opt("uid"));
280+
}
281+
282+
// ========== SET CONTENT TYPE DATA TESTS ==========
283+
284+
@Test
285+
void testSetContentTypeDataWithJSONObject() throws Exception {
286+
// Create a ContentTypesModel with single content type
287+
LinkedHashMap<String, Object> contentTypeMap = new LinkedHashMap<>();
288+
contentTypeMap.put("uid", "test_ct");
289+
contentTypeMap.put("title", "Test Content Type");
290+
contentTypeMap.put("description", "Test description");
291+
292+
JSONObject response = new JSONObject();
293+
294+
Field mapField = JSONObject.class.getDeclaredField("map");
295+
mapField.setAccessible(true);
296+
@SuppressWarnings("unchecked")
297+
Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response);
298+
internalMap.put("content_type", contentTypeMap);
299+
300+
ContentTypesModel model = new ContentTypesModel();
301+
model.setJSON(response);
302+
303+
// Create a ContentType to receive the data
304+
Stack stack = Contentstack.stack("test_key", "test_token", "test_env");
305+
ContentType contentType = new ContentType("test_ct");
306+
contentType.stackInstance = stack;
307+
contentType.headers = new LinkedHashMap<>();
308+
309+
// Call setContentTypeData
310+
model.setContentTypeData(contentType);
311+
312+
// Verify the data was set on the ContentType
313+
assertEquals("test_ct", contentType.uid);
314+
assertEquals("Test Content Type", contentType.title);
315+
assertEquals("Test description", contentType.description);
316+
}
317+
318+
@Test
319+
void testSetContentTypeDataWithNullResponse() {
320+
ContentTypesModel model = new ContentTypesModel();
321+
// response is null by default
322+
323+
ContentType contentType = new ContentType("test_ct");
324+
325+
// Should not throw exception
326+
assertDoesNotThrow(() -> model.setContentTypeData(contentType));
327+
328+
// ContentType fields should remain null
329+
assertNull(contentType.title);
330+
assertNull(contentType.uid);
331+
}
332+
333+
@Test
334+
void testSetContentTypeDataWithJSONArray() throws Exception {
335+
// Create a ContentTypesModel with multiple content types
336+
LinkedHashMap<String, Object> ct1 = new LinkedHashMap<>();
337+
ct1.put("uid", "ct1");
338+
ct1.put("title", "CT 1");
339+
340+
ArrayList<LinkedHashMap<String, Object>> contentTypesList = new ArrayList<>();
341+
contentTypesList.add(ct1);
342+
343+
JSONObject response = new JSONObject();
344+
345+
Field mapField = JSONObject.class.getDeclaredField("map");
346+
mapField.setAccessible(true);
347+
@SuppressWarnings("unchecked")
348+
Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response);
349+
internalMap.put("content_types", contentTypesList);
350+
351+
ContentTypesModel model = new ContentTypesModel();
352+
model.setJSON(response);
353+
354+
// response is JSONArray, not JSONObject
355+
assertTrue(model.getResponse() instanceof JSONArray);
356+
357+
ContentType contentType = new ContentType("test_ct");
358+
359+
// Should not throw exception (but won't set data since response is array)
360+
assertDoesNotThrow(() -> model.setContentTypeData(contentType));
361+
}
362+
363+
@Test
364+
void testSetContentTypeDataWithCompleteData() throws Exception {
365+
LinkedHashMap<String, Object> contentTypeMap = new LinkedHashMap<>();
366+
contentTypeMap.put("uid", "complete_ct");
367+
contentTypeMap.put("title", "Complete Content Type");
368+
contentTypeMap.put("description", "Complete description");
369+
370+
// Add schema
371+
ArrayList<Object> schemaList = new ArrayList<>();
372+
LinkedHashMap<String, Object> field = new LinkedHashMap<>();
373+
field.put("uid", "title_field");
374+
field.put("data_type", "text");
375+
schemaList.add(field);
376+
contentTypeMap.put("schema", schemaList);
377+
378+
JSONObject response = new JSONObject();
379+
380+
Field mapField = JSONObject.class.getDeclaredField("map");
381+
mapField.setAccessible(true);
382+
@SuppressWarnings("unchecked")
383+
Map<String, Object> internalMap = (Map<String, Object>) mapField.get(response);
384+
internalMap.put("content_type", contentTypeMap);
385+
386+
ContentTypesModel model = new ContentTypesModel();
387+
model.setJSON(response);
388+
389+
Stack stack = Contentstack.stack("test_key", "test_token", "test_env");
390+
ContentType contentType = new ContentType("complete_ct");
391+
contentType.stackInstance = stack;
392+
contentType.headers = new LinkedHashMap<>();
393+
394+
model.setContentTypeData(contentType);
395+
396+
assertEquals("complete_ct", contentType.uid);
397+
assertEquals("Complete Content Type", contentType.title);
398+
assertEquals("Complete description", contentType.description);
399+
assertNotNull(contentType.schema);
400+
}
401+
402+
@Test
403+
void testSetContentTypeDataMultipleTimes() throws Exception {
404+
LinkedHashMap<String, Object> ct1Map = new LinkedHashMap<>();
405+
ct1Map.put("uid", "ct1");
406+
ct1Map.put("title", "Content Type 1");
407+
408+
JSONObject response1 = new JSONObject();
409+
Field mapField = JSONObject.class.getDeclaredField("map");
410+
mapField.setAccessible(true);
411+
@SuppressWarnings("unchecked")
412+
Map<String, Object> internalMap1 = (Map<String, Object>) mapField.get(response1);
413+
internalMap1.put("content_type", ct1Map);
414+
415+
ContentTypesModel model = new ContentTypesModel();
416+
model.setJSON(response1);
417+
418+
Stack stack = Contentstack.stack("test_key", "test_token", "test_env");
419+
ContentType contentType = new ContentType("test");
420+
contentType.stackInstance = stack;
421+
contentType.headers = new LinkedHashMap<>();
422+
423+
model.setContentTypeData(contentType);
424+
assertEquals("ct1", contentType.uid);
425+
426+
// Set again with different data
427+
LinkedHashMap<String, Object> ct2Map = new LinkedHashMap<>();
428+
ct2Map.put("uid", "ct2");
429+
ct2Map.put("title", "Content Type 2");
430+
431+
JSONObject response2 = new JSONObject();
432+
@SuppressWarnings("unchecked")
433+
Map<String, Object> internalMap2 = (Map<String, Object>) mapField.get(response2);
434+
internalMap2.put("content_type", ct2Map);
435+
436+
model.setJSON(response2);
437+
model.setContentTypeData(contentType);
438+
439+
// Should be updated to ct2
440+
assertEquals("ct2", contentType.uid);
441+
assertEquals("Content Type 2", contentType.title);
442+
}
87443
}

0 commit comments

Comments
 (0)