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 ();
@@ -39,125 +39,93 @@ 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- }
70+ assertEquals (BuildConfig .assetUID , asset .getAssetUid ());
71+ assertEquals ("image/jpeg" , asset .getFileType ());
72+ assertEquals ("phoenix2.jpg" , asset .getFileName ());
73+ latch .countDown ();
8874 }
8975 });
76+ latch .await (5 , TimeUnit .SECONDS );
9077 }
9178
9279 @ Test
93- public void test_D_AssetLibrary_fetch () {
80+ public void test_E_AssetLibrary_includeCount_fetch () throws InterruptedException {
81+ final CountDownLatch latch = new CountDownLatch (1 );
9482 final AssetLibrary assetLibrary = stack .assetLibrary ();
83+ assetLibrary .includeCount ();
9584 assetLibrary .fetchAll (new FetchAssetsCallback () {
9685 @ Override
9786 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- }
87+ assertEquals (6 , assetLibrary .getCount ());
88+ latch .countDown ();
11089 }
11190 });
91+ latch .await (5 , TimeUnit .SECONDS );
11292 }
11393
11494 @ Test
115- public void test_E_AssetLibrary_includeCount_fetch () {
116- final AssetLibrary assetLibrary = stack .assetLibrary ();
117- assetLibrary .includeCount ();
118- assetLibrary .fetchAll (new FetchAssetsCallback () {
95+ public void test_G_Include_Dimension () throws InterruptedException {
96+ final CountDownLatch latch = new CountDownLatch (1 );
97+ final Asset asset = stack .asset (assetUid );
98+ asset .includeDimension ();
99+ asset .fetch (new FetchResultCallback () {
119100 @ Override
120- public void onCompletion (ResponseType responseType , List <Asset > assets , Error error ) {
121- if (error == null ) {
122- assertEquals (16 , assetLibrary .getCount ());
123- }
101+ public void onCompletion (ResponseType responseType , Error error ) {
102+ assertEquals (assetUid , asset .getAssetUid ());
103+ latch .countDown ();
124104 }
125105 });
106+ latch .await (5 , TimeUnit .SECONDS );
126107 }
127108
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- }
137- }
138- });
139- }
140109
141110 @ Test
142- public void test_G_Include_Dimension () {
111+ public void test_H_include_fallback () throws InterruptedException {
112+ final CountDownLatch latch = new CountDownLatch (1 );
143113 final Asset asset = stack .asset (assetUid );
144- asset .includeDimension ();
114+ asset .includeFallback ();
145115 asset .fetch (new FetchResultCallback () {
146116 @ Override
147117 public void onCompletion (ResponseType responseType , Error error ) {
148- if (error == null ) {
149- Log .d (TAG , asset .getAssetUid ());
150- assertEquals (assetUid , asset .getAssetUid ());
151- }
118+ assertEquals (assetUid , asset .getAssetUid ());
119+ latch .countDown ();
152120 }
153121 });
122+ latch .await (5 , TimeUnit .SECONDS );
154123 }
155124
156-
157125 @ Test
158- public void test_H_include_fallback () {
126+ public void test_include_branch () {
159127 final Asset asset = stack .asset (assetUid );
160- asset .includeFallback ();
128+ asset .includeBranch ();
161129 asset .fetch (new FetchResultCallback () {
162130 @ Override
163131 public void onCompletion (ResponseType responseType , Error error ) {
@@ -169,6 +137,7 @@ public void onCompletion(ResponseType responseType, Error error) {
169137 });
170138 }
171139
140+
172141 @ Test
173142 public void test_AZURE_NA () throws Exception {
174143 Config config = new Config ();
@@ -196,74 +165,17 @@ public void test_GCP_NA() throws Exception {
196165 }
197166
198167 @ 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 () {
168+ public void test_M_fetch_asset_empty_title () throws InterruptedException {
169+ final CountDownLatch latch = new CountDownLatch (1 );
257170 final AssetLibrary assetLibrary = stack .assetLibrary ().where ("title" ,"" );
258171 assetLibrary .fetchAll (new FetchAssetsCallback () {
259172 @ Override
260173 public void onCompletion (ResponseType responseType , List <Asset > assets , Error error ) {
261- for (Asset asset : assets ){
262- Log .d ("RESULT:" , "resp: " + asset .toJSON ());
263- }
264174 assertEquals (0 , assets .size ());
175+ latch .countDown ();
265176 }
266177 });
178+ latch .await (5 , TimeUnit .SECONDS );
267179 }
268180
269181}
0 commit comments