Skip to content

Commit b82f8ff

Browse files
Add unit tests for AssetModel constructor with isArray=false and tags support
1 parent 0f43c93 commit b82f8ff

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

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

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,78 @@ void testConstructorWithIsArrayTrue() {
3232
assertEquals("https://cdn.example.com/test_image.jpg", model.uploadUrl);
3333
}
3434

35-
/**
36-
* Note: Testing isArray=false is challenging because the constructor expects
37-
* response.get("asset") to return a LinkedHashMap, but when you put a LinkedHashMap
38-
* into a JSONObject, the org.json library converts it to a JSONObject internally.
39-
* This scenario is typically exercised in integration tests with actual network responses.
40-
*/
35+
@Test
36+
void testConstructorWithIsArrayFalse() throws Exception {
37+
// When isArray=false, the constructor expects response.get("asset") to return a LinkedHashMap
38+
// We use reflection to bypass org.json's automatic conversion of LinkedHashMap to JSONObject
39+
40+
LinkedHashMap<String, Object> assetMap = new LinkedHashMap<>();
41+
assetMap.put("uid", "asset_uid_456");
42+
assetMap.put("content_type", "application/pdf");
43+
assetMap.put("file_size", "1024000");
44+
assetMap.put("filename", "document.pdf");
45+
assetMap.put("url", "https://cdn.example.com/document.pdf");
46+
47+
JSONObject response = new JSONObject();
48+
49+
// Use reflection to inject the LinkedHashMap directly into the JSONObject's internal map
50+
java.lang.reflect.Field mapField = JSONObject.class.getDeclaredField("map");
51+
mapField.setAccessible(true);
52+
@SuppressWarnings("unchecked")
53+
java.util.Map<String, Object> internalMap = (java.util.Map<String, Object>) mapField.get(response);
54+
55+
// Put the LinkedHashMap directly, bypassing put() method
56+
internalMap.put("asset", assetMap);
57+
58+
// Now create AssetModel with isArray=false
59+
AssetModel model = new AssetModel(response, false);
60+
61+
assertNotNull(model);
62+
assertEquals("asset_uid_456", model.uploadedUid);
63+
assertEquals("application/pdf", model.contentType);
64+
assertEquals("1024000", model.fileSize);
65+
assertEquals("document.pdf", model.fileName);
66+
assertEquals("https://cdn.example.com/document.pdf", model.uploadUrl);
67+
}
68+
69+
@Test
70+
void testConstructorWithIsArrayFalseWithTags() throws Exception {
71+
// Test isArray=false path with tags
72+
73+
LinkedHashMap<String, Object> assetMap = new LinkedHashMap<>();
74+
assetMap.put("uid", "asset_with_tags");
75+
assetMap.put("filename", "tagged_file.jpg");
76+
77+
JSONArray tags = new JSONArray();
78+
tags.put("tag1");
79+
tags.put("tag2");
80+
tags.put("tag3");
81+
assetMap.put("tags", tags);
82+
83+
JSONObject response = new JSONObject();
84+
response.put("count", 5);
85+
response.put("objects", 10);
86+
87+
// Use reflection to inject LinkedHashMap
88+
java.lang.reflect.Field mapField = JSONObject.class.getDeclaredField("map");
89+
mapField.setAccessible(true);
90+
@SuppressWarnings("unchecked")
91+
java.util.Map<String, Object> internalMap = (java.util.Map<String, Object>) mapField.get(response);
92+
internalMap.put("asset", assetMap);
93+
94+
AssetModel model = new AssetModel(response, false);
95+
96+
assertNotNull(model);
97+
assertEquals("asset_with_tags", model.uploadedUid);
98+
assertEquals("tagged_file.jpg", model.fileName);
99+
assertEquals(5, model.count);
100+
assertEquals(10, model.totalCount);
101+
assertNotNull(model.tags);
102+
assertEquals(3, model.tags.length);
103+
assertEquals("tag1", model.tags[0]);
104+
assertEquals("tag2", model.tags[1]);
105+
assertEquals("tag3", model.tags[2]);
106+
}
41107

42108
@Test
43109
void testConstructorWithTags() {

0 commit comments

Comments
 (0)