diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a53e182..bfd281ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/com/contentstack/sdk/AssetLibrary.java b/src/main/java/com/contentstack/sdk/AssetLibrary.java index 3f54c94a..07512661 100644 --- a/src/main/java/com/contentstack/sdk/AssetLibrary.java +++ b/src/main/java/com/contentstack/sdk/AssetLibrary.java @@ -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 + * + *
+ *
+ * Example :
+ * + *
+     *         Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
+     *         AssetLibrary assetLibObject = stack.assetlibrary();
+     *         assetLibObject.addParam();
+     *         
+ */ + 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 + * + *
+ *
+ * Example :
+ * + *
+     *         Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
+     *         AssetLibrary assetLibObject = stack.assetlibrary();
+     *         assetLibObject.removeParam(paramKey);
+     *         
+ */ + 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. + *

+ * Note: The skip parameter can be used for pagination, + * "skip" specifies the number of objects to skip in the response.
+ * + *
+ *
+ * Example :
+ * + *

+     *          Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
+     *          AssetLibrary assetLibObject = stack.assetlibrary.skip(4);
+ *
+ */ + + 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. + *

+ * Note: The limit parameter can be used for pagination, " + * limit" specifies the number of objects to limit to in the response.
+ * + *
+ *
+ * Example :
+ * + *

+     *          Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
+     *           AssetLibrary assetLibObject = stack.assetlibrary.limit(4);
+ *
+ */ + + public AssetLibrary limit (@NotNull int number) { + urlQueries.put("limit", number); + return this; + } + /** * Fetch all. * @@ -180,6 +275,10 @@ public void getResultObject(List objects, JSONObject jsonObject, boolean List 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; @@ -193,7 +292,10 @@ public void getResultObject(List 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); diff --git a/src/main/java/com/contentstack/sdk/AssetsModel.java b/src/main/java/com/contentstack/sdk/AssetsModel.java index 4f57833b..13030019 100644 --- a/src/main/java/com/contentstack/sdk/AssetsModel.java +++ b/src/main/java/com/contentstack/sdk/AssetsModel.java @@ -2,10 +2,10 @@ import java.util.ArrayList; import java.util.List; + import org.json.JSONArray; import org.json.JSONObject; - /** * The type Assets model. */ diff --git a/src/test/java/com/contentstack/sdk/TestAssetLibrary.java b/src/test/java/com/contentstack/sdk/TestAssetLibrary.java index 1238e981..4b99877b 100644 --- a/src/test/java/com/contentstack/sdk/TestAssetLibrary.java +++ b/src/test/java/com/contentstack/sdk/TestAssetLibrary.java @@ -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) @@ -24,15 +23,15 @@ void testNewAssetLibrary() { public void onCompletion(ResponseType responseType, List 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..."); } }); @@ -107,4 +106,60 @@ public void onCompletion(ResponseType responseType, List 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 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 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 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 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]); + } + }); + } + } + }