|
16 | 16 | import org.junit.Before; |
17 | 17 | import org.junit.Test; |
18 | 18 |
|
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
19 | 24 | import kaaes.spotify.webapi.android.models.Album; |
20 | 25 | import kaaes.spotify.webapi.android.models.AlbumsPager; |
21 | 26 | import kaaes.spotify.webapi.android.models.Artist; |
| 27 | +import kaaes.spotify.webapi.android.models.ArtistsCursorPager; |
22 | 28 | import kaaes.spotify.webapi.android.models.Pager; |
23 | 29 | import kaaes.spotify.webapi.android.models.Playlist; |
24 | 30 | import kaaes.spotify.webapi.android.models.PlaylistSimple; |
25 | 31 | import kaaes.spotify.webapi.android.models.Track; |
26 | 32 | import kaaes.spotify.webapi.android.models.Tracks; |
| 33 | +import retrofit.Callback; |
| 34 | +import retrofit.RetrofitError; |
27 | 35 |
|
28 | 36 | import static junit.framework.Assert.assertEquals; |
29 | 37 |
|
|
36 | 44 | * Running the tests: |
37 | 45 | * ./gradlew :spotify-api:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.access_token=valid_access_token |
38 | 46 | */ |
39 | | -public class SpotifyServiceTest { |
| 47 | +public class SpotifyServiceAndroidTest { |
40 | 48 |
|
41 | 49 | private Gson mGson = new GsonBuilder().create(); |
42 | 50 | private OkHttpClient mClient = new OkHttpClient(); |
@@ -167,6 +175,74 @@ public void getMyPlaylist() throws Exception { |
167 | 175 | compareAsJSON(response, playlist); |
168 | 176 | } |
169 | 177 |
|
| 178 | + @Test |
| 179 | + public void getFollowedArtists() throws Exception { |
| 180 | + final CountDownLatch latch = new CountDownLatch(1); |
| 181 | + |
| 182 | + final ArtistsCursorPager[] payload = {null}; |
| 183 | + |
| 184 | + mService.getFollowedArtists(new Callback<ArtistsCursorPager>() { |
| 185 | + @Override |
| 186 | + public void success(ArtistsCursorPager artistsCursorPager, retrofit.client.Response response) { |
| 187 | + payload[0] = artistsCursorPager; |
| 188 | + latch.countDown(); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void failure(RetrofitError error) { |
| 193 | + latch.countDown(); |
| 194 | + } |
| 195 | + }); |
| 196 | + |
| 197 | + latch.await(1, TimeUnit.SECONDS); |
| 198 | + |
| 199 | + Request request = new Request.Builder() |
| 200 | + .get() |
| 201 | + .headers(mAuthHeader) |
| 202 | + .url("https://api.spotify.com/v1/me/following?type=artist") |
| 203 | + .build(); |
| 204 | + |
| 205 | + Response response = mClient.newCall(request).execute(); |
| 206 | + assertEquals(200, response.code()); |
| 207 | + |
| 208 | + compareAsJSON(response, payload[0]); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void getFollowedArtistsWithOptions() throws Exception { |
| 213 | + final CountDownLatch latch = new CountDownLatch(1); |
| 214 | + |
| 215 | + final ArtistsCursorPager[] payload = {null}; |
| 216 | + |
| 217 | + Map<String, Object> options = new HashMap<>(); |
| 218 | + options.put(SpotifyService.LIMIT, 45); |
| 219 | + mService.getFollowedArtists(options, new Callback<ArtistsCursorPager>() { |
| 220 | + @Override |
| 221 | + public void success(ArtistsCursorPager artistsCursorPager, retrofit.client.Response response) { |
| 222 | + payload[0] = artistsCursorPager; |
| 223 | + latch.countDown(); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public void failure(RetrofitError error) { |
| 228 | + latch.countDown(); |
| 229 | + } |
| 230 | + }); |
| 231 | + |
| 232 | + latch.await(1, TimeUnit.SECONDS); |
| 233 | + |
| 234 | + Request request = new Request.Builder() |
| 235 | + .get() |
| 236 | + .headers(mAuthHeader) |
| 237 | + .url("https://api.spotify.com/v1/me/following?type=artist&limit=45") |
| 238 | + .build(); |
| 239 | + |
| 240 | + Response response = mClient.newCall(request).execute(); |
| 241 | + assertEquals(200, response.code()); |
| 242 | + |
| 243 | + compareAsJSON(response, payload[0]); |
| 244 | + } |
| 245 | + |
170 | 246 | private void compareAsJSON(Response response, Object model) throws Exception { |
171 | 247 | JsonParser parser = new JsonParser(); |
172 | 248 |
|
|
0 commit comments