Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added skip limit methods for fetching assets #165

Merged
merged 7 commits into from
Feb 25, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added testcases and fixes for arraylist to jsonarray conversion
reeshika-h committed Feb 11, 2025
commit 3bf01cf80f42d1e4c8b04bbbd2f20662bf273a03
9 changes: 8 additions & 1 deletion src/main/java/com/contentstack/sdk/AssetLibrary.java
Original file line number Diff line number Diff line change
@@ -259,6 +259,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean

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

// if (objects == null || objects.isEmpty()) {
// System.out.println("Objects list is null or empty");
// }

if (objects != null && !objects.isEmpty()) {
for (Object object : objects) {
AssetModel model = (AssetModel) object;
@@ -272,7 +276,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
asset.setTags(model.tags);
assets.add(asset);
}
}
}
// else {
// System.out.println("Object is not an instance of AssetModel");
// }

if (callback != null) {
callback.onRequestFinish(ResponseType.NETWORK, assets);
27 changes: 19 additions & 8 deletions src/main/java/com/contentstack/sdk/AssetsModel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.contentstack.sdk;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

/**
* The type Assets model.
*/
@@ -19,13 +19,24 @@ class AssetsModel {
* @param response the response
*/
public AssetsModel(JSONObject response) {
JSONArray listResponse = response != null && response.has("assets") ? response.optJSONArray("assets") : null;
if (listResponse != null) {
listResponse.forEach(model -> {
Object listResponse = response != null && response.has("assets") ? response.opt("assets") : null;
if (listResponse instanceof JSONArray) {
// Handle traditional JSONArray
populateObjectsFromJSONArray((JSONArray) listResponse);
} else if (listResponse instanceof List) {
// Convert ArrayList to JSONArray
JSONArray jsonArray = new JSONArray((List<?>) listResponse);
populateObjectsFromJSONArray(jsonArray);
}
}

private void populateObjectsFromJSONArray(JSONArray jsonArray) {
jsonArray.forEach(model -> {
if (model instanceof JSONObject) {
JSONObject modelObj = (JSONObject) model;
AssetModel newModel = new AssetModel(modelObj, true);
objects.add(newModel);
});
}
}
});
}
}
8 changes: 4 additions & 4 deletions src/test/java/com/contentstack/sdk/TestAsset.java
Original file line number Diff line number Diff line change
@@ -38,10 +38,10 @@ public void onCompletion(ResponseType responseType, List<Asset> assets, Error er
Asset model = assets.get(0);
assetUid = model.getAssetUid();
Assertions.assertTrue(model.getAssetUid().startsWith("blt"));
Assertions.assertEquals("image/png", model.getFileType());
Assertions.assertEquals("13006", model.getFileSize());
Assertions.assertEquals("iot-icon.png", model.getFileName());
Assertions.assertTrue(model.getUrl().endsWith("iot-icon.png"));
Assertions.assertEquals("image/jpeg", model.getFileType());
Assertions.assertEquals("12668", model.getFileSize());
Assertions.assertEquals("Jane_Austen_Headshot.jpg", model.getFileName());
Assertions.assertTrue(model.getUrl().endsWith("Jane_Austen_Headshot.jpg"));
Assertions.assertTrue(model.toJSON().has("created_at"));
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
Assertions.assertEquals("gregory", model.getUpdateAt().getCalendarType());
64 changes: 60 additions & 4 deletions src/test/java/com/contentstack/sdk/TestAssetLibrary.java
Original file line number Diff line number Diff line change
@@ -24,10 +24,10 @@ void testNewAssetLibrary() {
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
Asset model = assets.get(0);
Assertions.assertTrue(model.getAssetUid().startsWith("blt"));
assertEquals("image/png", model.getFileType());
assertEquals("13006", model.getFileSize());
assertEquals("iot-icon.png", model.getFileName());
Assertions.assertTrue(model.getUrl().endsWith("iot-icon.png"));
assertEquals("image/jpeg", model.getFileType());
assertEquals("12668", model.getFileSize());
assertEquals("Jane_Austen_Headshot.jpg", model.getFileName());
Assertions.assertTrue(model.getUrl().endsWith("Jane_Austen_Headshot.jpg"));
Assertions.assertTrue(model.toJSON().has("created_at"));
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
assertEquals("gregory", model.getUpdateAt().getCalendarType());
@@ -107,4 +107,60 @@ public void onCompletion(ResponseType responseType, List<Asset> assets, Error er
}
});
}

@Test
void testFetchFirst10Assets() throws IllegalAccessException {
AssetLibrary assetLibrary = stack.assetLibrary();
assetLibrary.skip(0).limit(10).fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
Assertions.assertNotNull(assets, "Assets list should not be null");
Assertions.assertTrue(assets.size() <= 10, "Assets fetched should not exceed the limit");
}
});
}

@Test
void testFetchAssetsWithSkip() throws IllegalAccessException {
AssetLibrary assetLibrary = stack.assetLibrary();
assetLibrary.skip(10).limit(10).fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
Assertions.assertNotNull(assets, "Assets list should not be null");
Assertions.assertTrue(assets.size() <= 10, "Assets fetched should not exceed the limit");
}
});
}

@Test
void testFetchBeyondAvailableAssets() throws IllegalAccessException {
AssetLibrary assetLibrary = stack.assetLibrary();
assetLibrary.skip(5000).limit(10).fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
Assertions.assertNotNull(assets, "Assets list should not be null");
Assertions.assertEquals(0, assets.size(), "No assets should be fetched when skip exceeds available assets");
}
});
}

@Test
void testFetchAllAssetsInBatches() throws IllegalAccessException {
AssetLibrary assetLibrary = stack.assetLibrary();
int limit = 50;
int totalAssetsFetched[] = {0};

for (int skip = 0; skip < 150; skip += limit) {
assetLibrary.skip(skip).limit(limit).fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
totalAssetsFetched[0] += assets.size();
Assertions.assertNotNull(assets, "Assets list should not be null");
Assertions.assertTrue(assets.size() <= limit, "Assets fetched should not exceed the limit");
Assertions.assertEquals(7, totalAssetsFetched[0]);
}
});
}
}

}