GoogleMusicAPI implements IGoogleMusicAPI { @Override public final Collection<Song> getAllSongs() throws IOException, URISyntaxException { final Collection<Song> chunkedCollection = new ArrayList<Song>(); final FormBuilder form = new FormBuilder(); form.close(); final String response = client.dispatchPost(new URI( HTTPS_PLAY_GOOGLE_COM_MUSIC_SERVICES_LOADALLTRACKS), form); final List<String> jsSongCollectionWrappers = getJsSongCollectionWrappers(response); final Gson gson = new Gson(); final JsonParser parser = new JsonParser(); for (final String songCollectionWrapperJson : jsSongCollectionWrappers) { final JsonArray songCollectionWrapper = parser.parse( new StringReader(songCollectionWrapperJson)) .getAsJsonArray(); final JsonArray songCollection = songCollectionWrapper.get(0) .getAsJsonArray(); for (final JsonElement songValues : songCollection) { final JsonArray values = songValues.getAsJsonArray(); final Song s = new Song(); s.setId(gson.fromJson(values.get(0), String.class)); s.setTitle(gson.fromJson(values.get(1), String.class)); s.setName(gson.fromJson(values.get(1), String.class)); if (!Strings.isNullOrEmpty(gson.fromJson(values.get(2), String.class))) { s.setAlbumArtUrl("https:" + gson.fromJson(values.get(2), String.class)); } s.setArtist(gson.fromJson(values.get(3), String.class)); s.setAlbum(gson.fromJson(values.get(4), String.class)); s.setAlbumArtist(gson.fromJson(values.get(5), String.class)); s.setGenre(gson.fromJson(values.get(11), String.class)); s.setDurationMillis(gson.fromJson(values.get(13), Long.class)); s.setType(gson.fromJson(values.get(16), Integer.class)); s.setYear(gson.fromJson(values.get(18), Integer.class)); s.setPlaycount(gson.fromJson(values.get(22), Integer.class)); s.setRating(gson.fromJson(values.get(23), String.class)); if (!Strings.isNullOrEmpty(gson.fromJson(values.get(24), String.class))) { s.setCreationDate(gson.fromJson(values.get(24), Float.class) / 1000); } if (!Strings.isNullOrEmpty(gson.fromJson(values.get(36), String.class))) { s.setUrl("https:" + gson.fromJson(values.get(36), String.class)); } chunkedCollection.add(s); } } return chunkedCollection; } GoogleMusicAPI();  GoogleMusicAPI(
        final IGoogleHttpClient httpClient,
        final IJsonDeserializer jsonDeserializer,
        final File file); @Override final void login(final String email, final String password); @Override final Collection<Song> getAllSongs(); @Override final AddPlaylist addPlaylist(final String playlistName); @Override final Playlists getAllPlaylists(); @Override final Playlist getPlaylist(final String plID); @Override URI getSongURL(final Song song); @Override final DeletePlaylist deletePlaylist(final String id); @Override Collection<File> downloadSongs(final Collection<Song> songs); @Override File downloadSong(final Song song); @Override QueryResponse search(final String query); @Override void uploadSong(final File song);  }
@Test public void getSongsReturnsSongs() throws IOException, URISyntaxException { final int expectedSongListSize = 3; final String expectedSong1Title = "Song Title 1"; final String expectedSong1Artist = "Artist 1"; final String expectedSong1Album = "Album 1"; final int expectedSong1Year = 2012; final String expectedSong2Title = "Song Title 2"; final String expectedSong2Artist = "Artist 2"; final String expectedSong2Album = "Album 2"; final int expectedSong2Year = 2011; final String testData = Files.asCharSource( new File("src/test/resources/loadAllTracksResponse.html"), Charsets.UTF_8).read(); when( mockHttpClient.dispatchPost(isA(URI.class), isA(FormBuilder.class))).thenReturn(testData); final GoogleMusicAPI api = new GoogleMusicAPI(mockHttpClient, new JSON(), new File(".")); final Collection<Song> songs = api.getAllSongs(); assertEquals(expectedSongListSize, songs.size()); final List<Song> songList = Lists.newArrayList(songs.iterator()); final Song song1 = songList.get(0); assertEquals(expectedSong1Title, song1.getTitle()); assertEquals(expectedSong1Artist, song1.getArtist()); assertEquals(expectedSong1Album, song1.getAlbum()); assertEquals(expectedSong1Year, song1.getYear()); final Song song2 = songList.get(1); assertEquals(expectedSong2Title, song2.getTitle()); assertEquals(expectedSong2Artist, song2.getArtist()); assertEquals(expectedSong2Album, song2.getAlbum()); assertEquals(expectedSong2Year, song2.getYear()); }
@Test public void getSongsParsesSongWithCommas() throws IOException, URISyntaxException { final int expectedSongListSize = 3; final String expectedSong3Title = "Song Title, With Comma"; final String expectedSong3Artist = "Artist 2"; final String expectedSong3Album = "Album 2"; final int expectedSong3Year = 2014; final String testData = Files.asCharSource( new File("src/test/resources/loadAllTracksResponse.html"), Charsets.UTF_8).read(); when( mockHttpClient.dispatchPost(isA(URI.class), isA(FormBuilder.class))).thenReturn(testData); final GoogleMusicAPI api = new GoogleMusicAPI(mockHttpClient, new JSON(), new File(".")); final Collection<Song> songs = api.getAllSongs(); assertEquals(expectedSongListSize, songs.size()); final Song song = Lists.newArrayList(songs.iterator()).get(2); assertEquals(expectedSong3Title, song.getTitle()); assertEquals(expectedSong3Artist, song.getArtist()); assertEquals(expectedSong3Album, song.getAlbum()); assertEquals(expectedSong3Year, song.getYear()); }
@Test public void getSongsParsesResponseWithMultipleJSArrays() throws IOException, URISyntaxException { final int expectedSongListSize = 6; final String expectedSong3Title = "Song Title, With Comma"; final String expectedSong3Artist = "Artist 2"; final String expectedSong3Album = "Album 2"; final int expectedSong3Year = 2014; final String expectedSong5Title = "Song Title 5"; final String expectedSong5Artist = "Artist 5"; final String expectedSong5Album = "Album 5"; final int expectedSong5Year = 2011; final String testData = Files .asCharSource( new File( "src/test/resources/loadAllTracksResponseWithMultipleJSArrays.html"), Charsets.UTF_8).read(); when( mockHttpClient.dispatchPost(isA(URI.class), isA(FormBuilder.class))).thenReturn(testData); final GoogleMusicAPI api = new GoogleMusicAPI(mockHttpClient, new JSON(), new File(".")); final Collection<Song> songs = api.getAllSongs(); assertEquals(expectedSongListSize, songs.size()); final List<Song> songList = Lists.newArrayList(songs.iterator()); final Song song3 = songList.get(2); assertEquals(expectedSong3Title, song3.getTitle()); assertEquals(expectedSong3Artist, song3.getArtist()); assertEquals(expectedSong3Album, song3.getAlbum()); assertEquals(expectedSong3Year, song3.getYear()); final Song song5 = songList.get(4); assertEquals(expectedSong5Title, song5.getTitle()); assertEquals(expectedSong5Artist, song5.getArtist()); assertEquals(expectedSong5Album, song5.getAlbum()); assertEquals(expectedSong5Year, song5.getYear()); }