Skip to content

Commit b084b20

Browse files
authored
Merge pull request #71 from contentstack/feat/DX-692
feat: added integration tests
2 parents 5fe94a8 + 66697e2 commit b084b20

File tree

9 files changed

+697
-1053
lines changed

9 files changed

+697
-1053
lines changed

contentstack/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ dependencies {
161161
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'
162162
testImplementation 'junit:junit:4.13.2'
163163
androidTestImplementation 'androidx.test:core:1.5.0'
164+
testImplementation 'org.robolectric:robolectric:4.6.1'
165+
164166
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
165167
exclude group: 'com.android.support', module: 'support-annotations'
166168
})

contentstack/src/androidTest/java/com/contentstack/sdk/AssetTestCase.java

Lines changed: 39 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.util.List;
1212
import java.util.concurrent.CountDownLatch;
13+
import java.util.concurrent.TimeUnit;
1314

1415
import static junit.framework.Assert.assertTrue;
1516
import static junit.framework.TestCase.assertEquals;
@@ -26,7 +27,6 @@ public class AssetTestCase {
2627
private static Stack stack;
2728
private static CountDownLatch latch;
2829

29-
3030
@BeforeClass
3131
public static void oneTimeSetUp() throws Exception {
3232
Context appContext = ApplicationProvider.getApplicationContext();
@@ -39,134 +39,87 @@ public static void oneTimeSetUp() throws Exception {
3939
stack = Contentstack.stack(appContext, DEFAULT_API_KEY, DEFAULT_DELIVERY_TOKEN, DEFAULT_ENV, config);
4040
}
4141

42-
43-
@Test()
44-
public void test_A_getAllAssetsToSetAssetUID() {
45-
final AssetLibrary assetLibrary = stack.assetLibrary();
46-
assetLibrary.fetchAll(new FetchAssetsCallback() {
47-
@Override
48-
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
49-
if (error == null) {
50-
Log.d(TAG, "response: " + assets.get(0).getAssetUid());
51-
assetUid = assets.get(0).getAssetUid();
52-
Log.e(assetUid, assetUid);
53-
}
54-
}
55-
});
56-
57-
}
58-
5942
@Test
60-
public void test_B_VerifyAssetUID() {
43+
public void test_B_VerifyAssetUID() throws InterruptedException {
44+
final CountDownLatch latch = new CountDownLatch(1);
6145
final Asset asset = stack.asset(assetUid);
6246
asset.fetch(new FetchResultCallback() {
6347
@Override
6448
public void onCompletion(ResponseType responseType, Error error) {
65-
if (error == null) {
66-
// Success Block.
67-
Log.d(TAG, "response: " + asset.getAssetUid());
68-
assertEquals(assetUid, asset.getAssetUid());
69-
}
49+
// Success Block.
50+
Log.d(TAG, "response: " + asset.getAssetUid());
51+
assertEquals(assetUid, asset.getAssetUid());
52+
// Unlock the latch to allow the test to proceed
53+
latch.countDown();
7054
}
7155
});
56+
latch.await(5, TimeUnit.SECONDS);
57+
assertEquals("Query was not completed in time", 0, latch.getCount());
7258
}
7359

7460
@Test
75-
public void test_C_Asset_fetch() {
61+
public void test_C_Asset_fetch() throws Exception {
62+
Config config = new Config();
63+
Context appContext = ApplicationProvider.getApplicationContext();
64+
stack = Contentstack.stack(appContext, BuildConfig.APIKey, BuildConfig.deliveryToken, BuildConfig.environment, config);
65+
final CountDownLatch latch = new CountDownLatch(1);
7666
final Asset asset = stack.asset(assetUid);
7767
asset.fetch(new FetchResultCallback() {
7868
@Override
7969
public void onCompletion(ResponseType responseType, Error error) {
80-
if (error == null) {
81-
assertEquals(BuildConfig.assetUID, asset.getAssetUid());
82-
assertEquals("image/jpeg", asset.getFileType());
83-
assertEquals("phoenix2.jpg", asset.getFileName());
84-
assertEquals("482141", asset.getFileSize());
85-
} else {
86-
assertEquals(105, error.getErrorCode());
87-
}
88-
}
89-
});
90-
}
91-
92-
@Test
93-
public void test_D_AssetLibrary_fetch() {
94-
final AssetLibrary assetLibrary = stack.assetLibrary();
95-
assetLibrary.fetchAll(new FetchAssetsCallback() {
96-
@Override
97-
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
98-
if (error == null) {
99-
assets.forEach(asset -> {
100-
Log.d(TAG, "----Test--Asset-D--Success----" + asset.toJSON());
101-
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getFileType());
102-
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getCreatedBy());
103-
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getUpdatedBy());
104-
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getFileName());
105-
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getFileSize());
106-
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getAssetUid());
107-
Log.d(TAG, "----Test--Asset-D--Success----" + asset.getUrl());
108-
});
109-
}
70+
assertEquals(BuildConfig.assetUID, asset.getAssetUid());
71+
assertEquals("image/jpeg", asset.getFileType());
72+
assertEquals("image1.jpg", asset.getFileName());
73+
latch.countDown();
11074
}
11175
});
76+
latch.await(5, TimeUnit.SECONDS);
11277
}
11378

11479
@Test
115-
public void test_E_AssetLibrary_includeCount_fetch() {
80+
public void test_E_AssetLibrary_includeCount_fetch() throws InterruptedException {
81+
final CountDownLatch latch = new CountDownLatch(1);
11682
final AssetLibrary assetLibrary = stack.assetLibrary();
11783
assetLibrary.includeCount();
11884
assetLibrary.fetchAll(new FetchAssetsCallback() {
11985
@Override
12086
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
121-
if (error == null) {
122-
assertEquals(16, assetLibrary.getCount());
123-
}
124-
}
125-
});
126-
}
127-
128-
@Test
129-
public void test_F_AssetLibrary_includeRelativeUrl_fetch() {
130-
final AssetLibrary assetLibrary = stack.assetLibrary();
131-
assetLibrary.includeRelativeUrl();
132-
assetLibrary.fetchAll(new FetchAssetsCallback() {
133-
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
134-
if (error == null) {
135-
assertTrue(assets.get(0).getUrl().contains("phoenix2.jpg"));
136-
}
87+
assertEquals(5, assetLibrary.getCount());
88+
latch.countDown();
13789
}
13890
});
91+
latch.await(5, TimeUnit.SECONDS);
13992
}
14093

14194
@Test
142-
public void test_G_Include_Dimension() {
95+
public void test_G_Include_Dimension() throws InterruptedException {
96+
final CountDownLatch latch = new CountDownLatch(1);
14397
final Asset asset = stack.asset(assetUid);
14498
asset.includeDimension();
14599
asset.fetch(new FetchResultCallback() {
146100
@Override
147101
public void onCompletion(ResponseType responseType, Error error) {
148-
if (error == null) {
149-
Log.d(TAG, asset.getAssetUid());
150-
assertEquals(assetUid, asset.getAssetUid());
151-
}
102+
assertEquals(assetUid, asset.getAssetUid());
103+
latch.countDown();
152104
}
153105
});
106+
latch.await(5, TimeUnit.SECONDS);
154107
}
155108

156109

157110
@Test
158-
public void test_H_include_fallback() {
111+
public void test_H_include_fallback() throws InterruptedException {
112+
final CountDownLatch latch = new CountDownLatch(1);
159113
final Asset asset = stack.asset(assetUid);
160114
asset.includeFallback();
161115
asset.fetch(new FetchResultCallback() {
162116
@Override
163117
public void onCompletion(ResponseType responseType, Error error) {
164-
if (error == null) {
165-
Log.d(TAG, asset.getAssetUid());
166-
assertEquals(assetUid, asset.getAssetUid());
167-
}
118+
assertEquals(assetUid, asset.getAssetUid());
119+
latch.countDown();
168120
}
169121
});
122+
latch.await(5, TimeUnit.SECONDS);
170123
}
171124

172125
@Test
@@ -196,74 +149,17 @@ public void test_GCP_NA() throws Exception {
196149
}
197150

198151
@Test
199-
public void test_I_fetch_asset_by_title() {
200-
final AssetLibrary assetLibrary = stack.assetLibrary().where("title", "iot-icon.png");
201-
assetLibrary.fetchAll(new FetchAssetsCallback() {
202-
@Override
203-
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
204-
if (error == null) {
205-
for (Asset asset : assets) {
206-
Log.d("RESULT:", "resp" + asset.json);
207-
}
208-
}
209-
}
210-
});
211-
}
212-
213-
@Test
214-
public void test_J_fetch_asset_by_tags() {
215-
final AssetLibrary assetLibrary = stack.assetLibrary().where("tags","tag1");
216-
assetLibrary.fetchAll(new FetchAssetsCallback() {
217-
@Override
218-
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
219-
if (error == null) {
220-
for( Asset asset : assets){
221-
Log.d("RESULT:", "resp" + asset.json);
222-
}
223-
assertTrue(assets.size()>0);
224-
}
225-
}
226-
});
227-
}
228-
229-
@Test
230-
public void test_K_fetch_asset_by_description() {
231-
final AssetLibrary assetLibrary= stack.assetLibrary().where("description","Page1");
232-
assetLibrary.fetchAll(new FetchAssetsCallback() {
233-
@Override
234-
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
235-
for(Asset asset : assets){
236-
Log.d("RESULT:", "resp" + asset.toJSON());
237-
}
238-
assertTrue(assets.size()>0);
239-
}
240-
});
241-
}
242-
243-
@Test
244-
public void test_L_fetch_asset_invalid() {
245-
final AssetLibrary assetLibrary = stack.assetLibrary().where("title",null);
246-
assetLibrary.fetchAll(new FetchAssetsCallback() {
247-
@Override
248-
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
249-
Log.e("RESULT:", "ERROR:"+ error.errorMessage);
250-
}
251-
});
252-
253-
}
254-
255-
@Test
256-
public void test_M_fetch_asset_empty_title() {
152+
public void test_M_fetch_asset_empty_title() throws InterruptedException {
153+
final CountDownLatch latch = new CountDownLatch(1);
257154
final AssetLibrary assetLibrary = stack.assetLibrary().where("title","");
258155
assetLibrary.fetchAll(new FetchAssetsCallback() {
259156
@Override
260157
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
261-
for(Asset asset : assets){
262-
Log.d("RESULT:", "resp: " + asset.toJSON());
263-
}
264158
assertEquals(0, assets.size());
159+
latch.countDown();
265160
}
266161
});
162+
latch.await(5, TimeUnit.SECONDS);
267163
}
268164

269165
}

0 commit comments

Comments
 (0)