Skip to content

Commit

Permalink
Merge pull request #71 from contentstack/feat/DX-692
Browse files Browse the repository at this point in the history
feat: added integration tests
  • Loading branch information
reeshika-h authored Oct 16, 2024
2 parents 5fe94a8 + 66697e2 commit b084b20
Show file tree
Hide file tree
Showing 9 changed files with 697 additions and 1,053 deletions.
2 changes: 2 additions & 0 deletions contentstack/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:core:1.5.0'
testImplementation 'org.robolectric:robolectric:4.6.1'

androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
Expand All @@ -26,7 +27,6 @@ public class AssetTestCase {
private static Stack stack;
private static CountDownLatch latch;


@BeforeClass
public static void oneTimeSetUp() throws Exception {
Context appContext = ApplicationProvider.getApplicationContext();
Expand All @@ -39,134 +39,87 @@ public static void oneTimeSetUp() throws Exception {
stack = Contentstack.stack(appContext, DEFAULT_API_KEY, DEFAULT_DELIVERY_TOKEN, DEFAULT_ENV, config);
}


@Test()
public void test_A_getAllAssetsToSetAssetUID() {
final AssetLibrary assetLibrary = stack.assetLibrary();
assetLibrary.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
if (error == null) {
Log.d(TAG, "response: " + assets.get(0).getAssetUid());
assetUid = assets.get(0).getAssetUid();
Log.e(assetUid, assetUid);
}
}
});

}

@Test
public void test_B_VerifyAssetUID() {
public void test_B_VerifyAssetUID() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Asset asset = stack.asset(assetUid);
asset.fetch(new FetchResultCallback() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
if (error == null) {
// Success Block.
Log.d(TAG, "response: " + asset.getAssetUid());
assertEquals(assetUid, asset.getAssetUid());
}
// Success Block.
Log.d(TAG, "response: " + asset.getAssetUid());
assertEquals(assetUid, asset.getAssetUid());
// Unlock the latch to allow the test to proceed
latch.countDown();
}
});
latch.await(5, TimeUnit.SECONDS);
assertEquals("Query was not completed in time", 0, latch.getCount());
}

@Test
public void test_C_Asset_fetch() {
public void test_C_Asset_fetch() throws Exception {
Config config = new Config();
Context appContext = ApplicationProvider.getApplicationContext();
stack = Contentstack.stack(appContext, BuildConfig.APIKey, BuildConfig.deliveryToken, BuildConfig.environment, config);
final CountDownLatch latch = new CountDownLatch(1);
final Asset asset = stack.asset(assetUid);
asset.fetch(new FetchResultCallback() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
if (error == null) {
assertEquals(BuildConfig.assetUID, asset.getAssetUid());
assertEquals("image/jpeg", asset.getFileType());
assertEquals("phoenix2.jpg", asset.getFileName());
assertEquals("482141", asset.getFileSize());
} else {
assertEquals(105, error.getErrorCode());
}
}
});
}

@Test
public void test_D_AssetLibrary_fetch() {
final AssetLibrary assetLibrary = stack.assetLibrary();
assetLibrary.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
if (error == null) {
assets.forEach(asset -> {
Log.d(TAG, "----Test--Asset-D--Success----" + asset.toJSON());
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getFileType());
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getCreatedBy());
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getUpdatedBy());
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getFileName());
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getFileSize());
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getAssetUid());
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getUrl());
});
}
assertEquals(BuildConfig.assetUID, asset.getAssetUid());
assertEquals("image/jpeg", asset.getFileType());
assertEquals("image1.jpg", asset.getFileName());
latch.countDown();
}
});
latch.await(5, TimeUnit.SECONDS);
}

@Test
public void test_E_AssetLibrary_includeCount_fetch() {
public void test_E_AssetLibrary_includeCount_fetch() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final AssetLibrary assetLibrary = stack.assetLibrary();
assetLibrary.includeCount();
assetLibrary.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
if (error == null) {
assertEquals(16, assetLibrary.getCount());
}
}
});
}

@Test
public void test_F_AssetLibrary_includeRelativeUrl_fetch() {
final AssetLibrary assetLibrary = stack.assetLibrary();
assetLibrary.includeRelativeUrl();
assetLibrary.fetchAll(new FetchAssetsCallback() {
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
if (error == null) {
assertTrue(assets.get(0).getUrl().contains("phoenix2.jpg"));
}
assertEquals(5, assetLibrary.getCount());
latch.countDown();
}
});
latch.await(5, TimeUnit.SECONDS);
}

@Test
public void test_G_Include_Dimension() {
public void test_G_Include_Dimension() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Asset asset = stack.asset(assetUid);
asset.includeDimension();
asset.fetch(new FetchResultCallback() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
if (error == null) {
Log.d(TAG, asset.getAssetUid());
assertEquals(assetUid, asset.getAssetUid());
}
assertEquals(assetUid, asset.getAssetUid());
latch.countDown();
}
});
latch.await(5, TimeUnit.SECONDS);
}


@Test
public void test_H_include_fallback() {
public void test_H_include_fallback() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Asset asset = stack.asset(assetUid);
asset.includeFallback();
asset.fetch(new FetchResultCallback() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
if (error == null) {
Log.d(TAG, asset.getAssetUid());
assertEquals(assetUid, asset.getAssetUid());
}
assertEquals(assetUid, asset.getAssetUid());
latch.countDown();
}
});
latch.await(5, TimeUnit.SECONDS);
}

@Test
Expand Down Expand Up @@ -196,74 +149,17 @@ public void test_GCP_NA() throws Exception {
}

@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() {
public void test_M_fetch_asset_empty_title() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
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());
latch.countDown();
}
});
latch.await(5, TimeUnit.SECONDS);
}

}
Loading

0 comments on commit b084b20

Please sign in to comment.