Skip to content
Browse files

Adding tests for pagination attributes and fixing out-by-one error ca…

…used by LastFm returning 1-indexed page numbers for populated result sets, and 0-indexed page numbers for empty results sets
  • Loading branch information...
1 parent 5003a41 commit 2b9beec7ab6ff5b43801c9fcf616f94ab7783745 @michaellavelle committed
View
8 src/main/java/org/springframework/social/lastfm/api/impl/TrackTemplate.java
@@ -78,7 +78,7 @@ public TrackTemplate(RestTemplate restTemplate,
return new PageImpl<TrackSearchResult>(new ArrayList<TrackSearchResult>(),pageable,pageInfo.getTotal());
}
- return new PageImpl<TrackSearchResult>(trackSearchListResponse.getTracks(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),pageInfo.getTotal());
+ return new PageImpl<TrackSearchResult>(trackSearchListResponse.getTracks(),new PageRequest(pageInfo.getZeroIndexedPage(),pageInfo.getPerPage()),pageInfo.getTotal());
@@ -116,11 +116,9 @@ public TrackTemplate(RestTemplate restTemplate,
.getForObject(buildLastFmApiUrl(methodParameters),
LastFmTracksResponse.class)
.getNestedResponse();
-
- PageInfo pageInfo = trackListResponse.getPageInfo();
-
+
- return new PageImpl<Track>(trackListResponse.getTracks(),new PageRequest(1,limit == null ? trackListResponse.getTracks().size() : limit),trackListResponse.getTracks().size());
+ return new PageImpl<Track>(trackListResponse.getTracks(),new PageRequest(0,limit == null ? trackListResponse.getTracks().size() : limit),trackListResponse.getTracks().size());
}
}
View
14 src/main/java/org/springframework/social/lastfm/api/impl/UserTemplate.java
@@ -116,7 +116,7 @@ public LastFmProfile getUserProfile(String userName) {
return new PageImpl<SimpleTrack>(new ArrayList<SimpleTrack>(),pageable,pageInfo.getTotal());
}
- return new PageImpl<SimpleTrack>(simpleTrackListResponse.getTracks(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),simpleTrackListResponse.getPageInfo().getTotal());
+ return new PageImpl<SimpleTrack>(simpleTrackListResponse.getTracks(),new PageRequest(pageInfo.getZeroIndexedPage(),pageInfo.getPerPage()),simpleTrackListResponse.getPageInfo().getTotal());
}
@@ -152,7 +152,7 @@ public LastFmProfile getUserProfile(String userName) {
return new PageImpl<Track>(new ArrayList<Track>(),pageable,pageInfo.getTotal());
}
- return new PageImpl<Track>(trackListResponse.getTracks(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),trackListResponse.getPageInfo().getTotal());
+ return new PageImpl<Track>(trackListResponse.getTracks(),new PageRequest(pageInfo.getZeroIndexedPage(),pageInfo.getPerPage()),trackListResponse.getPageInfo().getTotal());
}
@@ -187,7 +187,7 @@ public LastFmProfile getUserProfile(String userName) {
return new PageImpl<Track>(new ArrayList<Track>(),pageable,pageInfo.getTotal());
}
- return new PageImpl<Track>(trackListResponse.getTracks(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),trackListResponse.getPageInfo().getTotal());
+ return new PageImpl<Track>(trackListResponse.getTracks(),new PageRequest(pageInfo.getZeroIndexedPage(),pageInfo.getPerPage()),trackListResponse.getPageInfo().getTotal());
}
@@ -222,7 +222,7 @@ public LastFmProfile getUserProfile(String userName) {
return new PageImpl<Artist>(new ArrayList<Artist>(),pageable,pageInfo.getTotal());
}
- return new PageImpl<Artist>(artistListResponse.getArtists(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),pageInfo.getTotal());
+ return new PageImpl<Artist>(artistListResponse.getArtists(),new PageRequest(pageInfo.getZeroIndexedPage(),pageInfo.getPerPage()),pageInfo.getTotal());
}
@@ -308,7 +308,7 @@ public void shout(String userName, String message) {
return new PageImpl<Shout>(new ArrayList<Shout>(),pageable,pageInfo.getTotal());
}
- return new PageImpl<Shout>(shoutListResponse.getShouts(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),pageInfo.getTotal());
+ return new PageImpl<Shout>(shoutListResponse.getShouts(),new PageRequest(pageInfo.getZeroIndexedPage(),pageInfo.getPerPage()),pageInfo.getTotal());
}
@Override
@@ -340,7 +340,7 @@ public void shout(String userName, String message) {
return new PageImpl<Artist>(new ArrayList<Artist>(),pageable,pageInfo.getTotal());
}
- return new PageImpl<Artist>(artistListResponse.getArtists(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),pageInfo.getTotal());
+ return new PageImpl<Artist>(artistListResponse.getArtists(),new PageRequest(pageInfo.getZeroIndexedPage(),pageInfo.getPerPage()),pageInfo.getTotal());
}
@@ -376,7 +376,7 @@ public void shout(String userName, String message) {
return new PageImpl<LastFmProfile>(new ArrayList<LastFmProfile>(),pageable,pageInfo.getTotal());
}
- return new PageImpl<LastFmProfile>(userListResponse.getUsers(),new PageRequest(pageInfo.getPage(),pageInfo.getPerPage()),pageInfo.getTotal());
+ return new PageImpl<LastFmProfile>(userListResponse.getUsers(),new PageRequest(pageInfo.getZeroIndexedPage(),pageInfo.getPerPage()),pageInfo.getTotal());
}
View
2 ...main/java/org/springframework/social/lastfm/api/impl/json/LastFmTrackMatchesResponse.java
@@ -41,7 +41,7 @@ public PageInfo getPageInfo() {
pageInfo.setPerPage(itemsPerPage);
pageInfo.setTotal(totalResults);
pageInfo.setTotalPages(((int)((totalResults - 1)/itemsPerPage)) + 1);
- pageInfo.setPage(((int)(startIndex/itemsPerPage)) + 1);
+ pageInfo.setPage(((int)(startIndex/itemsPerPage)));
return pageInfo;
}
View
5 src/main/java/org/springframework/social/lastfm/api/impl/json/lists/PageInfo.java
@@ -21,6 +21,11 @@ public PageInfo(@JsonProperty("page") int page,@JsonProperty("perPage") int perP
this.totalPages = totalPages;
}
+ public int getZeroIndexedPage()
+ {
+ return page == 0 ? 0 : page - 1;
+ }
+
public PageInfo()
{
View
99 src/test/java/org/springframework/social/lastfm/api/UserTemplateTest.java
@@ -31,6 +31,7 @@
import java.util.List;
import org.junit.Test;
+import org.springframework.data.domain.Page;
import org.springframework.social.NotAuthorizedException;
import org.springframework.social.ResourceNotFoundException;
import org.springframework.social.lastfm.api.impl.UserAgentHelper;
@@ -86,8 +87,11 @@ public void getRecentTracks() {
withResponse(jsonResource("testdata/recent-tracks"),
responseHeaders));
- List<SimpleTrack> tracks = lastFm.userOperations().getRecentTracks(
- "mattslip").getContent();
+ Page<SimpleTrack> simpleTracks = lastFm.userOperations().getRecentTracks(
+ "mattslip");
+ assertEquals(0,simpleTracks.getNumber());
+ assertEquals(1,simpleTracks.getTotalElements());
+ List<SimpleTrack> tracks = simpleTracks.getContent();
assertSimpleTrackData(tracks.get(0));
}
@@ -102,7 +106,10 @@ public void getRecentTracksSingleTrackResponse() {
withResponse(jsonResource("testdata/recent-tracks-single-track-response"),
responseHeaders));
- List<SimpleTrack> tracks = lastFm.userOperations().getRecentTracks("mattslip").getContent();
+ Page<SimpleTrack> simpleTracks = lastFm.userOperations().getRecentTracks("mattslip");
+ assertEquals(0,simpleTracks.getNumber());
+ assertEquals(1,simpleTracks.getTotalElements());
+ List<SimpleTrack> tracks = simpleTracks.getContent();
assertSimpleTrackData(tracks.get(0));
}
@@ -123,7 +130,10 @@ public void getRecentTracksEmptyResponse() {
jsonResource("testdata/recent-tracks-empty"),
responseHeaders));
- List<SimpleTrack> tracks = lastFm.userOperations().getRecentTracks("mattslip").getContent();
+ Page<SimpleTrack> simpleTracks = lastFm.userOperations().getRecentTracks("mattslip");
+ List<SimpleTrack> tracks = simpleTracks.getContent();
+ assertEquals(0,simpleTracks.getNumber());
+ assertEquals(0,simpleTracks.getTotalElements());
assertNotNull(tracks);
assertEquals(0,tracks.size());
@@ -141,7 +151,10 @@ public void getTopTracks() {
withResponse(jsonResource("testdata/top-tracks"),
responseHeaders));
- List<Track> tracks = lastFm.userOperations().getTopTracks("mattslip").getContent();
+ Page<Track> tracksPage = lastFm.userOperations().getTopTracks("mattslip");
+ assertEquals(0,tracksPage.getNumber());
+ assertEquals(1,tracksPage.getTotalElements());
+ List<Track> tracks = tracksPage.getContent();
assertTrackData(tracks.get(0));
}
@@ -156,7 +169,10 @@ public void getTopArtists() {
withResponse(jsonResource("testdata/top-artists"),
responseHeaders));
- List<Artist> artists = lastFm.userOperations().getTopArtists("mattslip").getContent();
+ Page<Artist> artistsPage = lastFm.userOperations().getTopArtists("mattslip");
+ assertEquals(0,artistsPage.getNumber());
+ assertEquals(133,artistsPage.getTotalElements());
+ List<Artist> artists = artistsPage.getContent();
assertArtistData(artists.get(0));
}
@@ -171,7 +187,10 @@ public void getTopArtistsEmptyResponse() {
withResponse(jsonResource("testdata/top-artists-empty"),
responseHeaders));
- List<Artist> artists = lastFm.userOperations().getTopArtists("mattslip").getContent();
+ Page<Artist> artistsPage = lastFm.userOperations().getTopArtists("mattslip");
+ assertEquals(0,artistsPage.getNumber());
+ assertEquals(0,artistsPage.getTotalElements());
+ List<Artist> artists = artistsPage.getContent();
assertNotNull(artists);
assertEquals(0,artists.size());
}
@@ -187,7 +206,10 @@ public void getRecommendedArtists() {
withResponse(jsonResource("testdata/recommended-artists"),
responseHeaders));
- List<Artist> artists = lastFm.userOperations().getRecommendedArtists().getContent();
+ Page<Artist> artistsPage = lastFm.userOperations().getRecommendedArtists();
+ List<Artist> artists = artistsPage.getContent();
+ assertEquals(0,artistsPage.getNumber());
+ assertEquals(250,artistsPage.getTotalElements());
assertArtistData(artists.get(0));
@@ -205,7 +227,11 @@ public void getTopTracksSingleTrackResponse() {
withResponse(jsonResource("testdata/top-tracks-single-track-response"),
responseHeaders));
- List<Track> tracks = lastFm.userOperations().getTopTracks("mattslip").getContent();
+ Page<Track> tracksPage = lastFm.userOperations().getTopTracks("mattslip");
+ assertEquals(0,tracksPage.getNumber());
+ assertEquals(1,tracksPage.getTotalElements());
+ List<Track> tracks = tracksPage.getContent();
+
assertTrackData(tracks.get(0));
}
@@ -226,7 +252,10 @@ public void getTopTracksEmptyResponse() {
jsonResource("testdata/top-tracks-empty"),
responseHeaders));
- List<Track> tracks = lastFm.userOperations().getTopTracks("mattslip").getContent();
+ Page<Track> tracksPage = lastFm.userOperations().getTopTracks("mattslip");
+ assertEquals(0,tracksPage.getNumber());
+ assertEquals(0,tracksPage.getTotalElements());
+ List<Track> tracks = tracksPage.getContent();
assertNotNull(tracks);
assertEquals(0,tracks.size());
@@ -246,7 +275,10 @@ public void getLovedTracks() {
withResponse(jsonResource("testdata/loved-tracks"),
responseHeaders));
- List<Track> tracks = lastFm.userOperations().getLovedTracks("mattslip").getContent();
+ Page<Track> tracksPage = lastFm.userOperations().getLovedTracks("mattslip");
+ assertEquals(0,tracksPage.getNumber());
+ assertEquals(1,tracksPage.getTotalElements());
+ List<Track> tracks = tracksPage.getContent();
assertTrackData(tracks.get(0));
}
@@ -262,7 +294,10 @@ public void getSimilarTracks() {
withResponse(jsonResource("testdata/similar-tracks"),
responseHeaders));
- List<Track> tracks = lastFm.trackOperations().getSimilarTracks(new SimpleTrackDescriptor("Madonna","Music")).getContent();
+ Page<Track> tracksPage = lastFm.trackOperations().getSimilarTracks(new SimpleTrackDescriptor("Madonna","Music"));
+ List<Track> tracks = tracksPage.getContent();
+ assertEquals(0,tracksPage.getNumber());
+ assertEquals(250,tracksPage.getTotalElements());
assertEquals("Don't Tell Me",tracks.get(0).getName());
assertEquals("Madonna",tracks.get(0).getArtistName());
@@ -279,7 +314,10 @@ public void getShouts() {
withResponse(jsonResource("testdata/shouts"),
responseHeaders));
- List<Shout> shouts = lastFm.userOperations().getShouts("mattslip").getContent();
+ Page<Shout> shoutsPage = lastFm.userOperations().getShouts("mattslip");
+ assertEquals(0,shoutsPage.getNumber());
+ assertEquals(3,shoutsPage.getTotalElements());
+ List<Shout> shouts = shoutsPage.getContent();
assertShoutData(shouts.get(2));
@@ -297,7 +335,10 @@ public void getShoutsSingleShoutResponse() {
withResponse(jsonResource("testdata/single-shout-response"),
responseHeaders));
- List<Shout> shouts = lastFm.userOperations().getShouts("mattslip").getContent();
+ Page<Shout> shoutsPage = lastFm.userOperations().getShouts("mattslip");
+ assertEquals(0,shoutsPage.getNumber());
+ assertEquals(1,shoutsPage.getTotalElements());
+ List<Shout> shouts = shoutsPage.getContent();
assertShoutData(shouts.get(0));
}
@@ -313,7 +354,10 @@ public void getShoutsEmptyShoutsResponse() {
withResponse(jsonResource("testdata/empty-shouts-response"),
responseHeaders));
- List<Shout> shouts = lastFm.userOperations().getShouts("mattslip").getContent();
+ Page<Shout> shoutsPage = lastFm.userOperations().getShouts("mattslip");
+ List<Shout> shouts = shoutsPage.getContent();
+ assertEquals(0,shoutsPage.getNumber());
+ assertEquals(0,shoutsPage.getTotalElements());
assertNotNull(shouts);
assertEquals(shouts.size(),0);
}
@@ -329,7 +373,10 @@ public void getFriendsEmptyFriendsResponse() {
withResponse(jsonResource("testdata/empty-friends-response"),
responseHeaders));
- List<LastFmProfile> friends = lastFm.userOperations().getFriends("mattslip").getContent();
+ Page<LastFmProfile> friendsPage = lastFm.userOperations().getFriends("mattslip");
+ List<LastFmProfile> friends = friendsPage.getContent();
+ assertEquals(0,friendsPage.getNumber());
+ assertEquals(0,friendsPage.getTotalElements());
assertNotNull(friends);
assertEquals(0,friends.size());
}
@@ -345,7 +392,10 @@ public void getFriends() {
withResponse(jsonResource("testdata/friends"),
responseHeaders));
- List<LastFmProfile> friends = lastFm.userOperations().getFriends("mattslip").getContent();
+ Page<LastFmProfile> friendsPage = lastFm.userOperations().getFriends("mattslip");
+ List<LastFmProfile> friends = friendsPage.getContent();
+ assertEquals(0,friendsPage.getNumber());
+ assertEquals(3,friendsPage.getTotalElements());
assertNotNull(friends);
assertEquals(3,friends.size());
}
@@ -377,9 +427,12 @@ public void getFriendsSingleFriendResponse() {
withResponse(jsonResource("testdata/single-friend-response"),
responseHeaders));
- List<LastFmProfile> friends = lastFm.userOperations().getFriends("mattslip").getContent();
+ Page<LastFmProfile> friendsPage = lastFm.userOperations().getFriends("mattslip");
+ List<LastFmProfile> friends = friendsPage.getContent();
assertNotNull(friends);
assertEquals(1,friends.size());
+ assertEquals(0,friendsPage.getNumber());
+ assertEquals(1,friendsPage.getTotalElements());
assertEquals("michaellavelle",friends.get(0).getName());
assertEquals("Michael Lavelle",friends.get(0).getRealName());
assertEquals("http://www.last.fm/user/michaellavelle",friends.get(0).getUrl());
@@ -404,7 +457,10 @@ public void getLovedTracksSingleTrackResponse() {
jsonResource("testdata/loved-tracks-single-track-response"),
responseHeaders));
- List<Track> tracks = lastFm.userOperations().getLovedTracks("mattslip").getContent();
+ Page<Track> tracksPage = lastFm.userOperations().getLovedTracks("mattslip");
+ assertEquals(0,tracksPage.getNumber());
+ assertEquals(1,tracksPage.getTotalElements());
+ List<Track> tracks = tracksPage.getContent();
assertTrackData(tracks.get(0));
}
@@ -426,7 +482,10 @@ public void getLovedTracksEmptyResponse() {
jsonResource("testdata/loved-tracks-empty"),
responseHeaders));
- List<Track> tracks = lastFm.userOperations().getLovedTracks("mattslip").getContent();
+ Page<Track> tracksPage = lastFm.userOperations().getLovedTracks("mattslip");
+ List<Track> tracks = tracksPage.getContent();
+ assertEquals(0,tracksPage.getNumber());
+ assertEquals(0,tracksPage.getTotalElements());
assertNotNull(tracks);
assertEquals(0,tracks.size());

0 comments on commit 2b9beec

Please sign in to comment.
Something went wrong with that request. Please try again.