Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
88 changes: 87 additions & 1 deletion src/main/java/com/contentstack/sdk/AssetLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,85 @@ public int getCount() {
return count;
}

/**
* Add param assetlibrary.
*
* @param paramKey the param key
* @param paramValue the param value
* @return the assetlibrary
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary();
* assetLibObject.addParam();
* </pre>
*/
public AssetLibrary addParam(@NotNull String paramKey, @NotNull Object paramValue) {
urlQueries.put(paramKey, paramValue);
return this;
}

public AssetLibrary removeParam(@NotNull String paramKey){
if(urlQueries.has(paramKey)){
urlQueries.remove(paramKey);
}
return this;
}



/**
* The number of objects to skip before returning any.
*
* @param number No of objects to skip from returned objects
* @return {@link Query} object, so you can chain this call.
* <p>
* <b> Note: </b> The skip parameter can be used for pagination,
* &#34;skip&#34; specifies the number of objects to skip in the response. <br>
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary.skip(4);<br>
* </pre>
*/

public AssetLibrary skip (@NotNull int number) {
urlQueries.put("skip",number);
return this;
}

/**
* A limit on the number of objects to return.
*
* @param number No of objects to limit.
* @return {@link Query} object, so you can chain this call.
* <p>
* <b> Note:</b> The limit parameter can be used for pagination, &#34;
* limit&#34; specifies the number of objects to limit to in the response. <br>
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary.limit(4);<br>
* </pre>
*/

public AssetLibrary limit (@NotNull int number) {
urlQueries.put("limit", number);
return this;
}

/**
* Fetch all.
*
Expand Down Expand Up @@ -180,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;
Expand All @@ -193,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);
Expand Down
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.
*/
Expand All @@ -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
Expand Up @@ -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());
Expand Down
64 changes: 60 additions & 4 deletions src/test/java/com/contentstack/sdk/TestAssetLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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]);
}
});
}
}

}
Loading