|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import org.json.JSONArray; |
| 4 | +import org.json.JSONObject; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.util.LinkedHashMap; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.*; |
| 10 | + |
| 11 | +/** |
| 12 | + * Comprehensive unit tests for AssetModel class. |
| 13 | + */ |
| 14 | +public class TestAssetModel { |
| 15 | + |
| 16 | + @Test |
| 17 | + void testConstructorWithIsArrayTrue() { |
| 18 | + JSONObject response = new JSONObject(); |
| 19 | + response.put("uid", "asset_uid_123"); |
| 20 | + response.put("content_type", "image/jpeg"); |
| 21 | + response.put("file_size", "2048576"); |
| 22 | + response.put("filename", "test_image.jpg"); |
| 23 | + response.put("url", "https://cdn.example.com/test_image.jpg"); |
| 24 | + |
| 25 | + AssetModel model = new AssetModel(response, true); |
| 26 | + |
| 27 | + assertNotNull(model); |
| 28 | + assertEquals("asset_uid_123", model.uploadedUid); |
| 29 | + assertEquals("image/jpeg", model.contentType); |
| 30 | + assertEquals("2048576", model.fileSize); |
| 31 | + assertEquals("test_image.jpg", model.fileName); |
| 32 | + assertEquals("https://cdn.example.com/test_image.jpg", model.uploadUrl); |
| 33 | + } |
| 34 | + |
| 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 | + */ |
| 41 | + |
| 42 | + @Test |
| 43 | + void testConstructorWithTags() { |
| 44 | + JSONObject response = new JSONObject(); |
| 45 | + response.put("uid", "asset_with_tags"); |
| 46 | + response.put("filename", "tagged_asset.jpg"); |
| 47 | + |
| 48 | + JSONArray tags = new JSONArray(); |
| 49 | + tags.put("production"); |
| 50 | + tags.put("featured"); |
| 51 | + tags.put("banner"); |
| 52 | + response.put("tags", tags); |
| 53 | + |
| 54 | + AssetModel model = new AssetModel(response, true); |
| 55 | + |
| 56 | + assertNotNull(model); |
| 57 | + assertNotNull(model.tags); |
| 58 | + assertEquals(3, model.tags.length); |
| 59 | + assertEquals("production", model.tags[0]); |
| 60 | + assertEquals("featured", model.tags[1]); |
| 61 | + assertEquals("banner", model.tags[2]); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testConstructorWithEmptyTags() { |
| 66 | + JSONObject response = new JSONObject(); |
| 67 | + response.put("uid", "asset_empty_tags"); |
| 68 | + response.put("filename", "test.jpg"); |
| 69 | + response.put("tags", new JSONArray()); |
| 70 | + |
| 71 | + AssetModel model = new AssetModel(response, true); |
| 72 | + |
| 73 | + assertNotNull(model); |
| 74 | + // Empty tags array shouldn't set the tags field |
| 75 | + assertNull(model.tags); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testConstructorWithCount() { |
| 80 | + JSONObject response = new JSONObject(); |
| 81 | + response.put("uid", "asset_with_count"); |
| 82 | + response.put("filename", "test.jpg"); |
| 83 | + response.put("count", 42); |
| 84 | + |
| 85 | + AssetModel model = new AssetModel(response, true); |
| 86 | + |
| 87 | + assertNotNull(model); |
| 88 | + assertEquals(42, model.count); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void testConstructorWithObjects() { |
| 93 | + JSONObject response = new JSONObject(); |
| 94 | + response.put("uid", "asset_with_objects"); |
| 95 | + response.put("filename", "test.jpg"); |
| 96 | + response.put("objects", 100); |
| 97 | + |
| 98 | + AssetModel model = new AssetModel(response, true); |
| 99 | + |
| 100 | + assertNotNull(model); |
| 101 | + assertEquals(100, model.totalCount); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testConstructorWithCountAndObjects() { |
| 106 | + JSONObject response = new JSONObject(); |
| 107 | + response.put("uid", "asset_full"); |
| 108 | + response.put("filename", "complete.jpg"); |
| 109 | + response.put("count", 25); |
| 110 | + response.put("objects", 150); |
| 111 | + |
| 112 | + AssetModel model = new AssetModel(response, true); |
| 113 | + |
| 114 | + assertNotNull(model); |
| 115 | + assertEquals(25, model.count); |
| 116 | + assertEquals(150, model.totalCount); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testConstructorWithAllFields() { |
| 121 | + JSONObject response = new JSONObject(); |
| 122 | + response.put("uid", "complete_asset"); |
| 123 | + response.put("content_type", "video/mp4"); |
| 124 | + response.put("file_size", "10485760"); |
| 125 | + response.put("filename", "video.mp4"); |
| 126 | + response.put("url", "https://cdn.example.com/video.mp4"); |
| 127 | + response.put("count", 1); |
| 128 | + response.put("objects", 1); |
| 129 | + |
| 130 | + JSONArray tags = new JSONArray(); |
| 131 | + tags.put("video"); |
| 132 | + tags.put("tutorial"); |
| 133 | + response.put("tags", tags); |
| 134 | + |
| 135 | + AssetModel model = new AssetModel(response, true); |
| 136 | + |
| 137 | + assertNotNull(model); |
| 138 | + assertEquals("complete_asset", model.uploadedUid); |
| 139 | + assertEquals("video/mp4", model.contentType); |
| 140 | + assertEquals("10485760", model.fileSize); |
| 141 | + assertEquals("video.mp4", model.fileName); |
| 142 | + assertEquals("https://cdn.example.com/video.mp4", model.uploadUrl); |
| 143 | + assertEquals(1, model.count); |
| 144 | + assertEquals(1, model.totalCount); |
| 145 | + assertNotNull(model.tags); |
| 146 | + assertEquals(2, model.tags.length); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void testConstructorWithMinimalData() { |
| 151 | + JSONObject response = new JSONObject(); |
| 152 | + response.put("uid", "minimal_asset"); |
| 153 | + |
| 154 | + AssetModel model = new AssetModel(response, true); |
| 155 | + |
| 156 | + assertNotNull(model); |
| 157 | + assertEquals("minimal_asset", model.uploadedUid); |
| 158 | + assertNull(model.contentType); |
| 159 | + assertNull(model.fileSize); |
| 160 | + assertNull(model.fileName); |
| 161 | + assertNull(model.uploadUrl); |
| 162 | + assertNull(model.tags); |
| 163 | + assertEquals(0, model.count); |
| 164 | + assertEquals(0, model.totalCount); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void testConstructorWithNonJSONArrayTags() { |
| 169 | + JSONObject response = new JSONObject(); |
| 170 | + response.put("uid", "asset_string_tags"); |
| 171 | + response.put("filename", "test.jpg"); |
| 172 | + response.put("tags", "not_an_array"); // String instead of JSONArray |
| 173 | + |
| 174 | + AssetModel model = new AssetModel(response, true); |
| 175 | + |
| 176 | + assertNotNull(model); |
| 177 | + // tags should not be extracted since it's not a JSONArray |
| 178 | + assertNull(model.tags); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void testConstructorWithEmptyResponse() { |
| 183 | + JSONObject response = new JSONObject(); |
| 184 | + AssetModel model = new AssetModel(response, true); |
| 185 | + |
| 186 | + assertNotNull(model); |
| 187 | + assertNull(model.uploadedUid); |
| 188 | + assertNull(model.contentType); |
| 189 | + assertNull(model.fileSize); |
| 190 | + assertNull(model.fileName); |
| 191 | + assertNull(model.uploadUrl); |
| 192 | + assertNull(model.tags); |
| 193 | + assertNotNull(model.json); |
| 194 | + assertEquals(0, model.count); |
| 195 | + assertEquals(0, model.totalCount); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + void testFieldAccess() { |
| 200 | + JSONObject response = new JSONObject(); |
| 201 | + response.put("uid", "initial_uid"); |
| 202 | + AssetModel model = new AssetModel(response, true); |
| 203 | + |
| 204 | + // Modify fields directly (package-private access) |
| 205 | + model.uploadedUid = "test_uid"; |
| 206 | + model.contentType = "image/png"; |
| 207 | + model.fileSize = "1024"; |
| 208 | + model.fileName = "test.png"; |
| 209 | + model.uploadUrl = "https://example.com/test.png"; |
| 210 | + model.count = 5; |
| 211 | + model.totalCount = 10; |
| 212 | + |
| 213 | + String[] testTags = {"tag1", "tag2"}; |
| 214 | + model.tags = testTags; |
| 215 | + |
| 216 | + JSONObject testJson = new JSONObject(); |
| 217 | + testJson.put("key", "value"); |
| 218 | + model.json = testJson; |
| 219 | + |
| 220 | + // Verify fields (package-private access) |
| 221 | + assertEquals("test_uid", model.uploadedUid); |
| 222 | + assertEquals("image/png", model.contentType); |
| 223 | + assertEquals("1024", model.fileSize); |
| 224 | + assertEquals("test.png", model.fileName); |
| 225 | + assertEquals("https://example.com/test.png", model.uploadUrl); |
| 226 | + assertEquals(5, model.count); |
| 227 | + assertEquals(10, model.totalCount); |
| 228 | + assertArrayEquals(testTags, model.tags); |
| 229 | + assertEquals(testJson, model.json); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + void testExtractTagsWithSingleTag() { |
| 234 | + JSONObject response = new JSONObject(); |
| 235 | + response.put("uid", "single_tag_asset"); |
| 236 | + |
| 237 | + JSONArray tags = new JSONArray(); |
| 238 | + tags.put("single"); |
| 239 | + response.put("tags", tags); |
| 240 | + |
| 241 | + AssetModel model = new AssetModel(response, true); |
| 242 | + |
| 243 | + assertNotNull(model.tags); |
| 244 | + assertEquals(1, model.tags.length); |
| 245 | + assertEquals("single", model.tags[0]); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + void testExtractTagsWithManyTags() { |
| 250 | + JSONObject response = new JSONObject(); |
| 251 | + response.put("uid", "many_tags_asset"); |
| 252 | + |
| 253 | + JSONArray tags = new JSONArray(); |
| 254 | + for (int i = 0; i < 10; i++) { |
| 255 | + tags.put("tag" + i); |
| 256 | + } |
| 257 | + response.put("tags", tags); |
| 258 | + |
| 259 | + AssetModel model = new AssetModel(response, true); |
| 260 | + |
| 261 | + assertNotNull(model.tags); |
| 262 | + assertEquals(10, model.tags.length); |
| 263 | + for (int i = 0; i < 10; i++) { |
| 264 | + assertEquals("tag" + i, model.tags[i]); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | +} |
| 269 | + |
0 commit comments