Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Optionally run album for artist search against live instead of mock
  • Loading branch information
thelinmichael committed May 16, 2014
commit 248e9af5403741a228c7013455b1a359278901de
14 changes: 7 additions & 7 deletions src/main/java/se/michaelthelin/spotify/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static Album createAlbum(JSONObject albumJson) {
album.setName(albumJson.getString("name"));
album.setPopularity(albumJson.getInt("popularity"));
album.setReleaseDate(createReleaseDate(albumJson.getJSONObject("release_date")));
album.setTracks(createSimpleTrackPage(albumJson));
album.setTracks(createSimpleTrackPage(albumJson.getJSONObject("tracks")));
album.setType(createSpotifyEntityType(albumJson.getString("type")));
album.setUri(albumJson.getString("uri"));

Expand Down Expand Up @@ -346,14 +346,14 @@ public static Page<Artist> createArtistPage(String artistPageJson) {
}

public static Page<Artist> createArtistPage(JSONObject artistPageJson) {
Page page = createItemlessPage(artistPageJson.getJSONObject("artists"));
page.setItems(createArtists(artistPageJson.getJSONObject("artists").getJSONArray("items")));
Page page = createItemlessPage(artistPageJson);
page.setItems(createArtists(artistPageJson.getJSONArray("items")));
return page;
}

private static Page<SimpleTrack> createSimpleTrackPage(JSONObject simpleTrackPageJson) {
Page page = createItemlessPage(simpleTrackPageJson.getJSONObject("tracks"));
page.setItems(createSimpleTracks(simpleTrackPageJson.getJSONObject("tracks").getJSONArray("items")));
Page page = createItemlessPage(simpleTrackPageJson);
page.setItems(createSimpleTracks(simpleTrackPageJson.getJSONArray("items")));
return page;
}

Expand All @@ -362,8 +362,8 @@ public static Page<SimpleAlbum> createSimpleAlbumPage(String simpleAlbumPageJson
}

public static Page<SimpleAlbum> createSimpleAlbumPage(JSONObject simpleAlbumPageJson) {
Page page = createItemlessPage(simpleAlbumPageJson.getJSONObject("albums"));
page.setItems(createSimpleAlbums(simpleAlbumPageJson.getJSONObject("albums").getJSONArray("items")));
Page page = createItemlessPage(simpleAlbumPageJson);
page.setItems(createSimpleAlbums(simpleAlbumPageJson.getJSONArray("items")));
return page;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public SettableFuture<Page<SimpleAlbum>> getAsync() {
try {
String jsonString = getJson();
JSONObject jsonObject = JSONObject.fromObject(jsonString);
searchResultFuture.set(JsonUtil.createSimpleAlbumPage(jsonObject));
searchResultFuture.set(JsonUtil.createSimpleAlbumPage(jsonObject.getJSONObject("albums")));
} catch (IOException e) {
searchResultFuture.setException(e);
} catch (UnexpectedResponseException e) {
Expand All @@ -33,7 +33,7 @@ public SettableFuture<Page<SimpleAlbum>> getAsync() {
}

public Page<SimpleAlbum> get() throws IOException, UnexpectedResponseException {
return JsonUtil.createSimpleAlbumPage(getJson());
return JsonUtil.createSimpleAlbumPage(JSONObject.fromObject(getJson()).getJSONObject("albums"));
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public SettableFuture<Page<Artist>> getAsync() {
try {
String jsonString = getJson();
JSONObject jsonObject = JSONObject.fromObject(jsonString);
searchResultFuture.set(JsonUtil.createArtistPage(jsonObject));
searchResultFuture.set(JsonUtil.createArtistPage(jsonObject.getJSONObject("artists")));
} catch (IOException e) {
searchResultFuture.setException(e);
} catch (UnexpectedResponseException e) {
Expand All @@ -33,7 +33,7 @@ public SettableFuture<Page<Artist>> getAsync() {
}

public Page<Artist> get() throws IOException, UnexpectedResponseException {
return JsonUtil.createArtistPage(getJson());
return JsonUtil.createArtistPage(JSONObject.fromObject(getJson()).getJSONObject("artists"));
}

public static Builder builder() {
Expand Down
55 changes: 55 additions & 0 deletions src/test/fixtures/artist-album.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"href" : "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=0&limit=2&album_type=single",
"items" : [ {
"album_type" : "single",
"external_urls" : {
"spotify" : "https://open.spotify.com/album/6HVPLh1TXzPnMqY7tAWLoL"
},
"href" : "https://api.spotify.com/v1/albums/6HVPLh1TXzPnMqY7tAWLoL",
"id" : "6HVPLh1TXzPnMqY7tAWLoL",
"images" : [ {
"height" : 640,
"url" : "https://d3rt1990lpmkn.cloudfront.net/original/4c252ea56b0276df6043db10747f4daf89714a93",
"width" : 640
}, {
"height" : 300,
"url" : "https://d3rt1990lpmkn.cloudfront.net/original/cc3db8170a3833140800a2cb291ed51977c6696a",
"width" : 300
}, {
"height" : 64,
"url" : "https://d3rt1990lpmkn.cloudfront.net/original/12a32170ff25f0e825472d20161cd75db433ee58",
"width" : 64
} ],
"name" : "Addicted To You",
"type" : "album",
"uri" : "spotify:album:6HVPLh1TXzPnMqY7tAWLoL"
}, {
"album_type" : "single",
"external_urls" : {
"spotify" : "https://open.spotify.com/album/5jvbjb51hQQAIycnBFS4cG"
},
"href" : "https://api.spotify.com/v1/albums/5jvbjb51hQQAIycnBFS4cG",
"id" : "5jvbjb51hQQAIycnBFS4cG",
"images" : [ {
"height" : 640,
"url" : "https://d3rt1990lpmkn.cloudfront.net/original/96e309e8f306742de5e23b6235e963c0dcbcf7bc",
"width" : 640
}, {
"height" : 300,
"url" : "https://d3rt1990lpmkn.cloudfront.net/original/8b32f93877ad126a2cecee24484d2bc0ed434e83",
"width" : 300
}, {
"height" : 64,
"url" : "https://d3rt1990lpmkn.cloudfront.net/original/797051eca4556983cbe70e23844c484c5522d907",
"width" : 64
} ],
"name" : "Addicted To You (Remixes)",
"type" : "album",
"uri" : "spotify:album:5jvbjb51hQQAIycnBFS4cG"
} ],
"limit" : 2,
"next" : "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=2&limit=2&album_type=single",
"offset" : 0,
"previous" : null,
"total" : 178
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import se.michaelthelin.spotify.Api;
import se.michaelthelin.spotify.HttpManager;
import se.michaelthelin.spotify.TestConfiguration;
import se.michaelthelin.spotify.TestUtil;
import se.michaelthelin.spotify.models.Album;
import se.michaelthelin.spotify.models.AlbumType;
import se.michaelthelin.spotify.models.Page;
import se.michaelthelin.spotify.models.SimpleAlbum;

Expand All @@ -18,6 +18,7 @@
import java.util.concurrent.TimeUnit;

import static junit.framework.Assert.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.fail;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -27,8 +28,11 @@ public class AlbumsForArtistsRequestTest {
public void shouldGetAlbumResultForArtistId_async() throws Exception {
final Api api = Api.DEFAULT_API;

final HttpManager mockedHttpManager = TestUtil.MockedHttpManager.returningJson("search-album.json");
final AlbumsForArtistRequest request = api.getAlbumsForArtist("53A0W3U0s8diEn9RhXQhVz").httpManager(mockedHttpManager).build();
final AlbumsForArtistRequest.Builder requestBuilder = api.getAlbumsForArtist("1vCWHaC5f2uS3yhpwWbIA6").limit(2).types(AlbumType.SINGLE);
if (TestConfiguration.USE_MOCK_RESPONSES) {
requestBuilder.httpManager(TestUtil.MockedHttpManager.returningJson("artist-album.json"));
}
final AlbumsForArtistRequest request = requestBuilder.build();

final CountDownLatch asyncCompleted = new CountDownLatch(1);

Expand All @@ -37,12 +41,22 @@ public void shouldGetAlbumResultForArtistId_async() throws Exception {
Futures.addCallback(albumsFuture, new FutureCallback<Page<SimpleAlbum>>() {
@Override
public void onSuccess(Page<SimpleAlbum> albumSearchResult) {
List<SimpleAlbum> albums = albumSearchResult.getItems();
assertEquals("https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=0&limit=2&album_type=single", albumSearchResult.getHref());
assertEquals(2, albumSearchResult.getLimit());
assertEquals(0, albumSearchResult.getOffset());
assertEquals(178, albumSearchResult.getTotal());
assertEquals("https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=2&limit=2&album_type=single", albumSearchResult.getNext());
assertEquals("null", albumSearchResult.getPrevious());

assertEquals(1, albums.size());
List<SimpleAlbum> albums = albumSearchResult.getItems();
assertEquals(2, albums.size());

SimpleAlbum firstAlbum = albums.get(0);
assertEquals("6akEvsycLGftJxYudPjmqK", firstAlbum.getId());
assertEquals(AlbumType.SINGLE, firstAlbum.getAlbumType());
assertEquals("https://open.spotify.com/album/6HVPLh1TXzPnMqY7tAWLoL", firstAlbum.getExternalUrls().get("spotify"));
assertEquals("https://api.spotify.com/v1/albums/6HVPLh1TXzPnMqY7tAWLoL", firstAlbum.getHref());
assertEquals("6HVPLh1TXzPnMqY7tAWLoL", firstAlbum.getId());
assertNotNull(firstAlbum.getImages());
asyncCompleted.countDown();
}

Expand All @@ -59,17 +73,30 @@ public void onFailure(Throwable throwable) {
public void shouldGetAlbumResultForArtistId_sync() throws Exception {
final Api api = Api.DEFAULT_API;

final HttpManager mockedHttpManager = TestUtil.MockedHttpManager.returningJson("search-album.json");
final AlbumsForArtistRequest request = api.getAlbumsForArtist("53A0W3U0s8diEn9RhXQhVz").httpManager(mockedHttpManager).build();

final Page<SimpleAlbum> albumsPage = request.get();

final List<SimpleAlbum> albums = albumsPage.getItems();

assertEquals(1, albums.size());

final SimpleAlbum firstAlbum = albums.get(0);
assertEquals("6akEvsycLGftJxYudPjmqK", firstAlbum.getId());
final AlbumsForArtistRequest.Builder requestBuilder = api.getAlbumsForArtist("1vCWHaC5f2uS3yhpwWbIA6").limit(2).types(AlbumType.SINGLE);
if (TestConfiguration.USE_MOCK_RESPONSES) {
requestBuilder.httpManager(TestUtil.MockedHttpManager.returningJson("artist-album.json"));
}
final AlbumsForArtistRequest request = requestBuilder.build();

final Page<SimpleAlbum> albumSearchResult = request.get();

assertEquals("https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=0&limit=2&album_type=single", albumSearchResult.getHref());
assertEquals(2, albumSearchResult.getLimit());
assertEquals(0, albumSearchResult.getOffset());
assertEquals(178, albumSearchResult.getTotal());
assertEquals("https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?offset=2&limit=2&album_type=single", albumSearchResult.getNext());
assertEquals("null", albumSearchResult.getPrevious());

final List<SimpleAlbum> albums = albumSearchResult.getItems();
assertEquals(2, albums.size());

SimpleAlbum firstAlbum = albums.get(0);
assertEquals(AlbumType.SINGLE, firstAlbum.getAlbumType());
assertEquals("https://open.spotify.com/album/6HVPLh1TXzPnMqY7tAWLoL", firstAlbum.getExternalUrls().get("spotify"));
assertEquals("https://api.spotify.com/v1/albums/6HVPLh1TXzPnMqY7tAWLoL", firstAlbum.getHref());
assertEquals("6HVPLh1TXzPnMqY7tAWLoL", firstAlbum.getId());
assertNotNull(firstAlbum.getImages());
}

}