10
10
11
11
import java .util .List ;
12
12
import java .util .concurrent .CountDownLatch ;
13
+ import java .util .concurrent .TimeUnit ;
13
14
14
15
import static junit .framework .Assert .assertTrue ;
15
16
import static junit .framework .TestCase .assertEquals ;
@@ -26,7 +27,6 @@ public class AssetTestCase {
26
27
private static Stack stack ;
27
28
private static CountDownLatch latch ;
28
29
29
-
30
30
@ BeforeClass
31
31
public static void oneTimeSetUp () throws Exception {
32
32
Context appContext = ApplicationProvider .getApplicationContext ();
@@ -39,134 +39,87 @@ public static void oneTimeSetUp() throws Exception {
39
39
stack = Contentstack .stack (appContext , DEFAULT_API_KEY , DEFAULT_DELIVERY_TOKEN , DEFAULT_ENV , config );
40
40
}
41
41
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
-
59
42
@ Test
60
- public void test_B_VerifyAssetUID () {
43
+ public void test_B_VerifyAssetUID () throws InterruptedException {
44
+ final CountDownLatch latch = new CountDownLatch (1 );
61
45
final Asset asset = stack .asset (assetUid );
62
46
asset .fetch (new FetchResultCallback () {
63
47
@ Override
64
48
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 ();
70
54
}
71
55
});
56
+ latch .await (5 , TimeUnit .SECONDS );
57
+ assertEquals ("Query was not completed in time" , 0 , latch .getCount ());
72
58
}
73
59
74
60
@ 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 );
76
66
final Asset asset = stack .asset (assetUid );
77
67
asset .fetch (new FetchResultCallback () {
78
68
@ Override
79
69
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 ();
110
74
}
111
75
});
76
+ latch .await (5 , TimeUnit .SECONDS );
112
77
}
113
78
114
79
@ 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 );
116
82
final AssetLibrary assetLibrary = stack .assetLibrary ();
117
83
assetLibrary .includeCount ();
118
84
assetLibrary .fetchAll (new FetchAssetsCallback () {
119
85
@ Override
120
86
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 ();
137
89
}
138
90
});
91
+ latch .await (5 , TimeUnit .SECONDS );
139
92
}
140
93
141
94
@ Test
142
- public void test_G_Include_Dimension () {
95
+ public void test_G_Include_Dimension () throws InterruptedException {
96
+ final CountDownLatch latch = new CountDownLatch (1 );
143
97
final Asset asset = stack .asset (assetUid );
144
98
asset .includeDimension ();
145
99
asset .fetch (new FetchResultCallback () {
146
100
@ Override
147
101
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 ();
152
104
}
153
105
});
106
+ latch .await (5 , TimeUnit .SECONDS );
154
107
}
155
108
156
109
157
110
@ Test
158
- public void test_H_include_fallback () {
111
+ public void test_H_include_fallback () throws InterruptedException {
112
+ final CountDownLatch latch = new CountDownLatch (1 );
159
113
final Asset asset = stack .asset (assetUid );
160
114
asset .includeFallback ();
161
115
asset .fetch (new FetchResultCallback () {
162
116
@ Override
163
117
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 ();
168
120
}
169
121
});
122
+ latch .await (5 , TimeUnit .SECONDS );
170
123
}
171
124
172
125
@ Test
@@ -196,74 +149,17 @@ public void test_GCP_NA() throws Exception {
196
149
}
197
150
198
151
@ 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 );
257
154
final AssetLibrary assetLibrary = stack .assetLibrary ().where ("title" ,"" );
258
155
assetLibrary .fetchAll (new FetchAssetsCallback () {
259
156
@ Override
260
157
public void onCompletion (ResponseType responseType , List <Asset > assets , Error error ) {
261
- for (Asset asset : assets ){
262
- Log .d ("RESULT:" , "resp: " + asset .toJSON ());
263
- }
264
158
assertEquals (0 , assets .size ());
159
+ latch .countDown ();
265
160
}
266
161
});
162
+ latch .await (5 , TimeUnit .SECONDS );
267
163
}
268
164
269
165
}
0 commit comments