Skip to content

Commit 3bf01cf

Browse files
committed
added testcases and fixes for arraylist to jsonarray conversion
1 parent 74215ca commit 3bf01cf

File tree

4 files changed

+91
-17
lines changed

4 files changed

+91
-17
lines changed

src/main/java/com/contentstack/sdk/AssetLibrary.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
259259

260260
List<Asset> assets = new ArrayList<>();
261261

262+
// if (objects == null || objects.isEmpty()) {
263+
// System.out.println("Objects list is null or empty");
264+
// }
265+
262266
if (objects != null && !objects.isEmpty()) {
263267
for (Object object : objects) {
264268
AssetModel model = (AssetModel) object;
@@ -272,7 +276,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
272276
asset.setTags(model.tags);
273277
assets.add(asset);
274278
}
275-
}
279+
}
280+
// else {
281+
// System.out.println("Object is not an instance of AssetModel");
282+
// }
276283

277284
if (callback != null) {
278285
callback.onRequestFinish(ResponseType.NETWORK, assets);
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.contentstack.sdk;
22

3-
import org.json.JSONArray;
4-
import org.json.JSONObject;
5-
63
import java.util.ArrayList;
74
import java.util.List;
85

6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
99
/**
1010
* The type Assets model.
1111
*/
@@ -19,13 +19,24 @@ class AssetsModel {
1919
* @param response the response
2020
*/
2121
public AssetsModel(JSONObject response) {
22-
JSONArray listResponse = response != null && response.has("assets") ? response.optJSONArray("assets") : null;
23-
if (listResponse != null) {
24-
listResponse.forEach(model -> {
22+
Object listResponse = response != null && response.has("assets") ? response.opt("assets") : null;
23+
if (listResponse instanceof JSONArray) {
24+
// Handle traditional JSONArray
25+
populateObjectsFromJSONArray((JSONArray) listResponse);
26+
} else if (listResponse instanceof List) {
27+
// Convert ArrayList to JSONArray
28+
JSONArray jsonArray = new JSONArray((List<?>) listResponse);
29+
populateObjectsFromJSONArray(jsonArray);
30+
}
31+
}
32+
33+
private void populateObjectsFromJSONArray(JSONArray jsonArray) {
34+
jsonArray.forEach(model -> {
35+
if (model instanceof JSONObject) {
2536
JSONObject modelObj = (JSONObject) model;
2637
AssetModel newModel = new AssetModel(modelObj, true);
2738
objects.add(newModel);
28-
});
29-
}
39+
}
40+
});
3041
}
3142
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public void onCompletion(ResponseType responseType, List<Asset> assets, Error er
3838
Asset model = assets.get(0);
3939
assetUid = model.getAssetUid();
4040
Assertions.assertTrue(model.getAssetUid().startsWith("blt"));
41-
Assertions.assertEquals("image/png", model.getFileType());
42-
Assertions.assertEquals("13006", model.getFileSize());
43-
Assertions.assertEquals("iot-icon.png", model.getFileName());
44-
Assertions.assertTrue(model.getUrl().endsWith("iot-icon.png"));
41+
Assertions.assertEquals("image/jpeg", model.getFileType());
42+
Assertions.assertEquals("12668", model.getFileSize());
43+
Assertions.assertEquals("Jane_Austen_Headshot.jpg", model.getFileName());
44+
Assertions.assertTrue(model.getUrl().endsWith("Jane_Austen_Headshot.jpg"));
4545
Assertions.assertTrue(model.toJSON().has("created_at"));
4646
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
4747
Assertions.assertEquals("gregory", model.getUpdateAt().getCalendarType());

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

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ void testNewAssetLibrary() {
2424
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
2525
Asset model = assets.get(0);
2626
Assertions.assertTrue(model.getAssetUid().startsWith("blt"));
27-
assertEquals("image/png", model.getFileType());
28-
assertEquals("13006", model.getFileSize());
29-
assertEquals("iot-icon.png", model.getFileName());
30-
Assertions.assertTrue(model.getUrl().endsWith("iot-icon.png"));
27+
assertEquals("image/jpeg", model.getFileType());
28+
assertEquals("12668", model.getFileSize());
29+
assertEquals("Jane_Austen_Headshot.jpg", model.getFileName());
30+
Assertions.assertTrue(model.getUrl().endsWith("Jane_Austen_Headshot.jpg"));
3131
Assertions.assertTrue(model.toJSON().has("created_at"));
3232
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
3333
assertEquals("gregory", model.getUpdateAt().getCalendarType());
@@ -107,4 +107,60 @@ public void onCompletion(ResponseType responseType, List<Asset> assets, Error er
107107
}
108108
});
109109
}
110+
111+
@Test
112+
void testFetchFirst10Assets() throws IllegalAccessException {
113+
AssetLibrary assetLibrary = stack.assetLibrary();
114+
assetLibrary.skip(0).limit(10).fetchAll(new FetchAssetsCallback() {
115+
@Override
116+
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
117+
Assertions.assertNotNull(assets, "Assets list should not be null");
118+
Assertions.assertTrue(assets.size() <= 10, "Assets fetched should not exceed the limit");
119+
}
120+
});
121+
}
122+
123+
@Test
124+
void testFetchAssetsWithSkip() throws IllegalAccessException {
125+
AssetLibrary assetLibrary = stack.assetLibrary();
126+
assetLibrary.skip(10).limit(10).fetchAll(new FetchAssetsCallback() {
127+
@Override
128+
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
129+
Assertions.assertNotNull(assets, "Assets list should not be null");
130+
Assertions.assertTrue(assets.size() <= 10, "Assets fetched should not exceed the limit");
131+
}
132+
});
133+
}
134+
135+
@Test
136+
void testFetchBeyondAvailableAssets() throws IllegalAccessException {
137+
AssetLibrary assetLibrary = stack.assetLibrary();
138+
assetLibrary.skip(5000).limit(10).fetchAll(new FetchAssetsCallback() {
139+
@Override
140+
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
141+
Assertions.assertNotNull(assets, "Assets list should not be null");
142+
Assertions.assertEquals(0, assets.size(), "No assets should be fetched when skip exceeds available assets");
143+
}
144+
});
145+
}
146+
147+
@Test
148+
void testFetchAllAssetsInBatches() throws IllegalAccessException {
149+
AssetLibrary assetLibrary = stack.assetLibrary();
150+
int limit = 50;
151+
int totalAssetsFetched[] = {0};
152+
153+
for (int skip = 0; skip < 150; skip += limit) {
154+
assetLibrary.skip(skip).limit(limit).fetchAll(new FetchAssetsCallback() {
155+
@Override
156+
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
157+
totalAssetsFetched[0] += assets.size();
158+
Assertions.assertNotNull(assets, "Assets list should not be null");
159+
Assertions.assertTrue(assets.size() <= limit, "Assets fetched should not exceed the limit");
160+
Assertions.assertEquals(7, totalAssetsFetched[0]);
161+
}
162+
});
163+
}
164+
}
165+
110166
}

0 commit comments

Comments
 (0)