Skip to content

Commit

Permalink
feat: fetch asset by query implementation and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
reeshika-h committed Aug 12, 2024
1 parent 800de11 commit 5cced41
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import org.junit.runners.MethodSorters;

import java.util.List;
import java.util.concurrent.CountDownLatch;

import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;

import androidx.test.InstrumentationRegistry;
import androidx.test.core.app.ApplicationProvider;
Expand All @@ -22,6 +24,8 @@ public class AssetTestCase {
private final String TAG = AssetTestCase.class.getSimpleName();
private static String assetUid = BuildConfig.assetUID;
private static Stack stack;
private static CountDownLatch latch;


@BeforeClass
public static void oneTimeSetUp() throws Exception {
Expand Down Expand Up @@ -191,4 +195,75 @@ public void test_GCP_NA() throws Exception {
stack = Contentstack.stack(appContext, DEFAULT_API_KEY, DEFAULT_DELIVERY_TOKEN, DEFAULT_ENV, config);
}

@Test
public void test_I_fetch_asset_by_title() {
final AssetLibrary assetLibrary = stack.assetLibrary().where("title", "iot-icon.png");
assetLibrary.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
if (error == null) {
for (Asset asset : assets) {
Log.d("RESULT:", "resp" + asset.json);
}
}
}
});
}

@Test
public void test_J_fetch_asset_by_tags() {
final AssetLibrary assetLibrary = stack.assetLibrary().where("tags","tag1");
assetLibrary.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
if (error == null) {
for( Asset asset : assets){
Log.d("RESULT:", "resp" + asset.json);
}
assertTrue(assets.size()>0);
}
}
});
}

@Test
public void test_K_fetch_asset_by_description() {
final AssetLibrary assetLibrary= stack.assetLibrary().where("description","Page1");
assetLibrary.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
for(Asset asset : assets){
Log.d("RESULT:", "resp" + asset.toJSON());
}
assertTrue(assets.size()>0);
}
});
}

@Test
public void test_L_fetch_asset_invalid() {
final AssetLibrary assetLibrary = stack.assetLibrary().where("title",null);
assetLibrary.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
Log.e("RESULT:", "ERROR:"+ error.errorMessage);
}
});

}

@Test
public void test_M_fetch_asset_empty_title() {
final AssetLibrary assetLibrary = stack.assetLibrary().where("title","");
assetLibrary.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
for(Asset asset : assets){
Log.d("RESULT:", "resp: " + asset.toJSON());
}
assertEquals(0, assets.size());
}
});
}

}
15 changes: 15 additions & 0 deletions contentstack/src/main/java/com/contentstack/sdk/AssetLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,5 +452,20 @@ public AssetLibrary includeMetadata() {
}
return this;
}
public AssetLibrary where(String key, String value) {
if (value != null) {
try {
JSONObject queryParams = new JSONObject();
queryParams.put(key, value);
urlQueries.put("query", queryParams);
} catch (JSONException e) {
Log.e(TAG, "JSON error: " + e.getLocalizedMessage());
}
} else {
Log.e(TAG, "Value for key '" + key + "' is null. Skipping addition to query.");
}
return this;
}


}

0 comments on commit 5cced41

Please sign in to comment.