Skip to content

Added skip limit methods for fetching assets #165

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

Merged
merged 7 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## v2.0.3

### Date: 3-March-2025

- Added skip limit methods for Assets
- Resolved a bug

## v2.0.2

### Date: 5-December-2024
Expand Down
104 changes: 103 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,101 @@ 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;
}

/**
* Remove param key assetlibrary.
*
* @param paramKey the param key
* @return the assetlibrary
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary();
* assetLibObject.removeParam(paramKey);
* </pre>
*/
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 +275,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 +292,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
2 changes: 1 addition & 1 deletion src/main/java/com/contentstack/sdk/AssetsModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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

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


/**
* The type Assets model.
*/
Expand Down
67 changes: 61 additions & 6 deletions src/test/java/com/contentstack/sdk/TestAssetLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;
import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.assertEquals;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
Expand All @@ -24,15 +23,15 @@ 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.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.assertTrue(model.toJSON().has("created_at"));
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
assertEquals("gregory", model.getUpdateAt().getCalendarType());
Assertions.assertEquals("gregory", model.getUpdateAt().getCalendarType());
Assertions.assertTrue(model.getUpdatedBy().startsWith("blt"));
assertEquals("", model.getDeletedBy());
Assertions.assertEquals("", model.getDeletedBy());
logger.info("passed...");
}
});
Expand Down Expand Up @@ -107,4 +106,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(6, totalAssetsFetched[0]);
}
});
}
}

}
Loading