1010
1111import java .util .List ;
1212import java .util .concurrent .CountDownLatch ;
13+ import java .util .concurrent .TimeUnit ;
1314
1415import static junit .framework .Assert .assertTrue ;
1516import 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 ();
@@ -40,94 +40,86 @@ public static void oneTimeSetUp() throws Exception {
4040 }
4141
4242 @ Test
43- public void test_B_VerifyAssetUID () {
43+ public void test_B_VerifyAssetUID () throws InterruptedException {
44+ final CountDownLatch latch = new CountDownLatch (1 );
4445 final Asset asset = stack .asset (assetUid );
4546 asset .fetch (new FetchResultCallback () {
4647 @ Override
4748 public void onCompletion (ResponseType responseType , Error error ) {
48- if ( error == null ) {
49- // Success Block.
50- Log . d ( TAG , "response: " + asset .getAssetUid ());
51- assertEquals ( assetUid , asset . getAssetUid ());
52- }
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 ();
5354 }
5455 });
56+ latch .await (5 , TimeUnit .SECONDS );
57+ assertEquals ("Query was not completed in time" , 0 , latch .getCount ());
5558 }
5659
5760 @ Test
58- 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 );
5966 final Asset asset = stack .asset (assetUid );
6067 asset .fetch (new FetchResultCallback () {
6168 @ Override
6269 public void onCompletion (ResponseType responseType , Error error ) {
63- if (error == null ) {
64- assertEquals (BuildConfig .assetUID , asset .getAssetUid ());
65- assertEquals ("image/jpeg" , asset .getFileType ());
66- assertEquals ("phoenix2.jpg" , asset .getFileName ());
67- assertEquals ("482141" , asset .getFileSize ());
68- } else {
69- assertEquals (105 , error .getErrorCode ());
70- }
70+ assertEquals (BuildConfig .assetUID , asset .getAssetUid ());
71+ assertEquals ("image/jpeg" , asset .getFileType ());
72+ assertEquals ("image1.jpg" , asset .getFileName ());
73+ latch .countDown ();
7174 }
7275 });
76+ latch .await (5 , TimeUnit .SECONDS );
7377 }
7478
7579 @ Test
76- public void test_E_AssetLibrary_includeCount_fetch () {
80+ public void test_E_AssetLibrary_includeCount_fetch () throws InterruptedException {
81+ final CountDownLatch latch = new CountDownLatch (1 );
7782 final AssetLibrary assetLibrary = stack .assetLibrary ();
7883 assetLibrary .includeCount ();
7984 assetLibrary .fetchAll (new FetchAssetsCallback () {
8085 @ Override
8186 public void onCompletion (ResponseType responseType , List <Asset > assets , Error error ) {
82- if (error == null ) {
83- assertEquals (16 , assetLibrary .getCount ());
84- }
85- }
86- });
87- }
88-
89- @ Test
90- public void test_F_AssetLibrary_includeRelativeUrl_fetch () {
91- final AssetLibrary assetLibrary = stack .assetLibrary ();
92- assetLibrary .includeRelativeUrl ();
93- assetLibrary .fetchAll (new FetchAssetsCallback () {
94- public void onCompletion (ResponseType responseType , List <Asset > assets , Error error ) {
95- if (error == null ) {
96- assertTrue (assets .get (0 ).getUrl ().contains ("phoenix2.jpg" ));
97- }
87+ assertEquals (5 , assetLibrary .getCount ());
88+ latch .countDown ();
9889 }
9990 });
91+ latch .await (5 , TimeUnit .SECONDS );
10092 }
10193
10294 @ Test
103- public void test_G_Include_Dimension () {
95+ public void test_G_Include_Dimension () throws InterruptedException {
96+ final CountDownLatch latch = new CountDownLatch (1 );
10497 final Asset asset = stack .asset (assetUid );
10598 asset .includeDimension ();
10699 asset .fetch (new FetchResultCallback () {
107100 @ Override
108101 public void onCompletion (ResponseType responseType , Error error ) {
109- if (error == null ) {
110- Log .d (TAG , asset .getAssetUid ());
111- assertEquals (assetUid , asset .getAssetUid ());
112- }
102+ assertEquals (assetUid , asset .getAssetUid ());
103+ latch .countDown ();
113104 }
114105 });
106+ latch .await (5 , TimeUnit .SECONDS );
115107 }
116108
117109
118110 @ Test
119- public void test_H_include_fallback () {
111+ public void test_H_include_fallback () throws InterruptedException {
112+ final CountDownLatch latch = new CountDownLatch (1 );
120113 final Asset asset = stack .asset (assetUid );
121114 asset .includeFallback ();
122115 asset .fetch (new FetchResultCallback () {
123116 @ Override
124117 public void onCompletion (ResponseType responseType , Error error ) {
125- if (error == null ) {
126- Log .d (TAG , asset .getAssetUid ());
127- assertEquals (assetUid , asset .getAssetUid ());
128- }
118+ assertEquals (assetUid , asset .getAssetUid ());
119+ latch .countDown ();
129120 }
130121 });
122+ latch .await (5 , TimeUnit .SECONDS );
131123 }
132124
133125 @ Test
@@ -157,47 +149,17 @@ public void test_GCP_NA() throws Exception {
157149 }
158150
159151 @ Test
160- public void test_J_fetch_asset_by_tags () {
161- final AssetLibrary assetLibrary = stack .assetLibrary ().where ("tags" ,"tag1" );
162- assetLibrary .fetchAll (new FetchAssetsCallback () {
163- @ Override
164- public void onCompletion (ResponseType responseType , List <Asset > assets , Error error ) {
165- if (error == null ) {
166- for ( Asset asset : assets ){
167- Log .d ("RESULT:" , "resp" + asset .json );
168- }
169- assertTrue (assets .size ()>0 );
170- }
171- }
172- });
173- }
174-
175- @ Test
176- public void test_K_fetch_asset_by_description () {
177- final AssetLibrary assetLibrary = stack .assetLibrary ().where ("description" ,"Page1" );
178- assetLibrary .fetchAll (new FetchAssetsCallback () {
179- @ Override
180- public void onCompletion (ResponseType responseType , List <Asset > assets , Error error ) {
181- for (Asset asset : assets ){
182- Log .d ("RESULT:" , "resp" + asset .toJSON ());
183- }
184- assertTrue (assets .size ()>0 );
185- }
186- });
187- }
188-
189- @ Test
190- 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 );
191154 final AssetLibrary assetLibrary = stack .assetLibrary ().where ("title" ,"" );
192155 assetLibrary .fetchAll (new FetchAssetsCallback () {
193156 @ Override
194157 public void onCompletion (ResponseType responseType , List <Asset > assets , Error error ) {
195- for (Asset asset : assets ){
196- Log .d ("RESULT:" , "resp: " + asset .toJSON ());
197- }
198158 assertEquals (0 , assets .size ());
159+ latch .countDown ();
199160 }
200161 });
162+ latch .await (5 , TimeUnit .SECONDS );
201163 }
202164
203165}
0 commit comments