Merge lp:~karni/ubuntuone-music-java-library/playlist-methods into lp:ubuntuone-music-java-library

Proposed by Michał Karnicki
Status: Merged
Merged at revision: 10
Proposed branch: lp:~karni/ubuntuone-music-java-library/playlist-methods
Merge into: lp:ubuntuone-music-java-library
Prerequisite: lp:~karni/ubuntuone-music-java-library/content-streaming
Diff against target: 1005 lines (+698/-160)
13 files modified
.bzrignore (+1/-0)
ant.properties (+1/-1)
build.xml (+1/-1)
src/main/com/ubuntuone/api/music/U1MusicAPI.java (+58/-3)
src/main/com/ubuntuone/api/music/client/ResourceClient.java (+4/-1)
src/main/com/ubuntuone/api/music/json/U1CustomJson.java (+56/-0)
src/test/com/ubuntuone/api/music/CreatePlaylistTest.java (+213/-0)
src/test/com/ubuntuone/api/music/GetAlbumsTest.java (+49/-0)
src/test/com/ubuntuone/api/music/PutPlaylistTest.java (+0/-153)
src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java (+157/-0)
src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java (+59/-0)
src/test/com/ubuntuone/test/util/SameHttpRequestMatcher.java (+72/-1)
src/test/com/ubuntuone/test/util/StreamUtils.java (+27/-0)
To merge this branch: bzr merge lp:~karni/ubuntuone-music-java-library/playlist-methods
Reviewer Review Type Date Requested Status
Chad Miller Pending
Review via email: mp+120578@code.launchpad.net

Description of the change

Added playlist creation and updating methods.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file '.bzrignore'
--- .bzrignore 2012-06-05 15:16:38 +0000
+++ .bzrignore 2012-08-21 14:13:25 +0000
@@ -16,3 +16,4 @@
16libs/jmock-*.jar16libs/jmock-*.jar
17libs/jmock-junit4-*.jar17libs/jmock-junit4-*.jar
18libs/hamcrest-all-*.jar18libs/hamcrest-all-*.jar
19src/main/com/ubuntuone/api/music/MusicTest.java
1920
=== modified file 'ant.properties'
--- ant.properties 2012-06-05 15:16:38 +0000
+++ ant.properties 2012-08-21 14:13:25 +0000
@@ -61,4 +61,4 @@
6161
62# Only used by official ubuntuone-hackers team.62# Only used by official ubuntuone-hackers team.
6363
64project.version=0.164project.version=0.1.0
6565
=== modified file 'build.xml'
--- build.xml 2012-08-21 14:13:25 +0000
+++ build.xml 2012-08-21 14:13:25 +0000
@@ -1,5 +1,5 @@
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2<project name="ubuntuone-files-java-library" basedir="." default="jar">2<project name="ubuntuone-music-java-library" basedir="." default="jar">
33
4 <description>4 <description>
5 *** UbuntuOne Music Java library ***5 *** UbuntuOne Music Java library ***
66
=== modified file 'src/main/com/ubuntuone/api/music/U1MusicAPI.java'
--- src/main/com/ubuntuone/api/music/U1MusicAPI.java 2012-08-21 14:13:25 +0000
+++ src/main/com/ubuntuone/api/music/U1MusicAPI.java 2012-08-21 14:13:25 +0000
@@ -53,6 +53,7 @@
53import com.ubuntuone.api.music.client.StreamingClient;53import com.ubuntuone.api.music.client.StreamingClient;
54import com.ubuntuone.api.music.json.U1AlbumJson;54import com.ubuntuone.api.music.json.U1AlbumJson;
55import com.ubuntuone.api.music.json.U1ArtistJson;55import com.ubuntuone.api.music.json.U1ArtistJson;
56import com.ubuntuone.api.music.json.U1CustomJson;
56import com.ubuntuone.api.music.json.U1PlaylistJson;57import com.ubuntuone.api.music.json.U1PlaylistJson;
57import com.ubuntuone.api.music.json.U1PlaylistSongJson;58import com.ubuntuone.api.music.json.U1PlaylistSongJson;
58import com.ubuntuone.api.music.json.U1SongJson;59import com.ubuntuone.api.music.json.U1SongJson;
@@ -196,6 +197,44 @@
196 }197 }
197 }198 }
198 199
200 public void getAlbumsByArtist(String artistId, U1AlbumRequestListener callback) {
201 callback.onStart();
202 String path = resourceClient.getPath(ALBUMS, artistId);
203 HttpResponse response = null;
204 try {
205 response = resourceClient.request(HttpGet.METHOD_NAME, path);
206 final int statusCode = getStatusCode(response);
207 if (statusCode == HttpStatus.SC_OK) {
208 U1AlbumJson.fromJson(getContentInputStream(response), callback);
209 } else {
210 handleHttpResponse(response, "Could not get artist albums.", callback);
211 }
212 } catch (JsonParseException jpe) {
213 // Received corrupt JSON. At this stage, response != null
214 final String bzrRev = getHeaderValue(ResponseHeader.X_BZR_REVISION_NUMBER, response);
215 final String date = getHeaderValue(ResponseHeader.DATE, response);
216
217 callback.onFailure(new Failure("Could not get artist albums. Corrupt JSON.",
218 jpe, 0, null, bzrRev, date));
219 } catch (OutOfMemoryError e) {
220 callback.onFailure(new Failure("Out of memory!", e));
221 throw e; // We're not expected to recover from this.
222 } catch (SSLException sslException) {
223 callback.onFailure(new Failure("SSL connection problem. This may be intermittent issue, " +
224 "please try again later.", sslException));
225 } catch (IOException ioException) {
226 // Failed to read the response.
227 callback.onFailure(new Failure("Could not get artist albums (network error).", ioException));
228 } catch (URISyntaxException uriSyntaxException) {
229 // Failed due to invalid URL within the library code.
230 throw new IllegalStateException(uriSyntaxException);
231 } catch (AuthorizerException signingException) {
232 callback.onFailure(new Failure("Could not get artist albums (signing exception).", signingException));
233 } finally {
234 callback.onFinish();
235 }
236 }
237
199 public void getSongs(U1SongRequestListener callback) {238 public void getSongs(U1SongRequestListener callback) {
200 callback.onStart();239 callback.onStart();
201 String path = resourceClient.getPath(SONGS);240 String path = resourceClient.getPath(SONGS);
@@ -310,13 +349,29 @@
310 }349 }
311 }350 }
312 351
313 public void createPlaylist(String name, ArrayList<U1Song> songList,352 public void createPlaylist(String name,
353 ArrayList<U1Song> songs, U1PlaylistSongRequestListener callback) {
354 putPlaylist(null, name, songs, callback);
355 }
356
357 public void updatePlaylist(String playlistId, String name,
358 ArrayList<U1Song> songs, U1PlaylistSongRequestListener callback) {
359 putPlaylist(playlistId, name, songs, callback);
360 }
361
362 public void putPlaylist(String playlistId, String name, ArrayList<U1Song> songs,
314 U1PlaylistSongRequestListener callback) {363 U1PlaylistSongRequestListener callback) {
315 callback.onStart();364 callback.onStart();
316 String path = resourceClient.getPath(PLAYLISTS);365 String path;
366 if (playlistId != null) {
367 path = resourceClient.getPath(PLAYLISTS, playlistId);
368 } else {
369 path = resourceClient.getPath(PLAYLISTS);
370 }
317 HttpResponse response = null;371 HttpResponse response = null;
318 try {372 try {
319 response = resourceClient.request(HttpPost.METHOD_NAME, path);373 String data = U1CustomJson.toSongIdListJson(name, songs);
374 response = resourceClient.request(HttpPost.METHOD_NAME, path, null, data);
320 final int statusCode = getStatusCode(response);375 final int statusCode = getStatusCode(response);
321 if (statusCode == HttpStatus.SC_OK) {376 if (statusCode == HttpStatus.SC_OK) {
322 U1PlaylistSongJson.fromJson(getContentInputStream(response), callback);377 U1PlaylistSongJson.fromJson(getContentInputStream(response), callback);
323378
=== modified file 'src/main/com/ubuntuone/api/music/client/ResourceClient.java'
--- src/main/com/ubuntuone/api/music/client/ResourceClient.java 2012-08-21 14:13:25 +0000
+++ src/main/com/ubuntuone/api/music/client/ResourceClient.java 2012-08-21 14:13:25 +0000
@@ -32,6 +32,7 @@
32import org.apache.http.HttpResponse;32import org.apache.http.HttpResponse;
33import org.apache.http.NameValuePair;33import org.apache.http.NameValuePair;
34import org.apache.http.client.HttpClient;34import org.apache.http.client.HttpClient;
35import org.apache.http.client.methods.HttpPost;
35import org.apache.http.client.methods.HttpPut;36import org.apache.http.client.methods.HttpPut;
36import org.apache.http.client.methods.HttpUriRequest;37import org.apache.http.client.methods.HttpUriRequest;
37import org.apache.http.client.utils.URLEncodedUtils;38import org.apache.http.client.utils.URLEncodedUtils;
@@ -95,7 +96,9 @@
95 request.addHeader(HttpHeaders.USER_AGENT, userAgent);96 request.addHeader(HttpHeaders.USER_AGENT, userAgent);
96 request.addHeader(HttpHeaders.CONNECTION, CONNECTION);97 request.addHeader(HttpHeaders.CONNECTION, CONNECTION);
97 request.addHeader(HttpHeaders.ACCEPT, ACCEPT);98 request.addHeader(HttpHeaders.ACCEPT, ACCEPT);
98 if (data != null && HttpPut.METHOD_NAME.equals(method)) {99 if (data != null && HttpPost.METHOD_NAME.equals(method)) {
100 ((HttpPost) request).setEntity(new StringEntity(data, UTF8));
101 } else if (data != null && HttpPut.METHOD_NAME.equals(method)) {
99 ((HttpPut) request).setEntity(new StringEntity(data, UTF8));102 ((HttpPut) request).setEntity(new StringEntity(data, UTF8));
100 }103 }
101 104
102105
=== added file 'src/main/com/ubuntuone/api/music/json/U1CustomJson.java'
--- src/main/com/ubuntuone/api/music/json/U1CustomJson.java 1970-01-01 00:00:00 +0000
+++ src/main/com/ubuntuone/api/music/json/U1CustomJson.java 2012-08-21 14:13:25 +0000
@@ -0,0 +1,56 @@
1/*
2 * Ubuntu One Music Java library - communicate with Ubuntu One music API
3 *
4 * Copyright 2012 Canonical Ltd.
5 *
6 * This file is part of Ubuntu One Files Java library.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see http://www.gnu.org/licenses
20 */
21
22package com.ubuntuone.api.music.json;
23
24import java.io.IOException;
25import java.io.StringWriter;
26import java.util.ArrayList;
27
28import org.codehaus.jackson.JsonFactory;
29import org.codehaus.jackson.JsonGenerator;
30
31import com.ubuntuone.api.music.model.U1Song;
32
33public class U1CustomJson
34{
35 public static String toSongIdListJson(String name, ArrayList<U1Song> songs)
36 throws IOException {
37 final JsonFactory factory = new JsonFactory();
38 final StringWriter writer = new StringWriter(128);
39 final JsonGenerator g = factory.createJsonGenerator(writer);
40
41 g.writeStartObject();
42 g.writeStringField("name", name);
43
44 g.writeArrayFieldStart("song_id_list"); {
45 for (U1Song song : songs) {
46 g.writeString(song.getId());
47 }
48 }
49 g.writeEndArray();
50
51 g.writeEndObject();
52 g.close();
53
54 return writer.toString();
55 }
56}
057
=== added file 'src/test/com/ubuntuone/api/music/CreatePlaylistTest.java'
--- src/test/com/ubuntuone/api/music/CreatePlaylistTest.java 1970-01-01 00:00:00 +0000
+++ src/test/com/ubuntuone/api/music/CreatePlaylistTest.java 2012-08-21 14:13:25 +0000
@@ -0,0 +1,213 @@
1/*
2 * Ubuntu One Music Java library - communicate with Ubuntu One music API
3 *
4 * Copyright 2012 Canonical Ltd.
5 *
6 * This file is part of Ubuntu One Files Java library.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see http://www.gnu.org/licenses
20 */
21
22package com.ubuntuone.api.music;
23
24import static com.ubuntuone.test.util.SameHttpRequestMatcher.sameRequest;
25
26import java.io.IOException;
27import java.util.ArrayList;
28
29import org.apache.http.HttpHost;
30import org.apache.http.HttpResponse;
31import org.apache.http.HttpStatus;
32import org.apache.http.client.HttpClient;
33import org.apache.http.client.methods.HttpPost;
34import org.apache.http.entity.StringEntity;
35import org.jmock.Expectations;
36import org.jmock.Mockery;
37import org.jmock.Sequence;
38import org.jmock.integration.junit4.JMock;
39import org.jmock.integration.junit4.JUnit4Mockery;
40import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43
44import com.ubuntuone.api.music.client.BaseClient;
45import com.ubuntuone.api.music.client.Failure;
46import com.ubuntuone.api.music.json.U1CustomJson;
47import com.ubuntuone.api.music.model.U1Meta;
48import com.ubuntuone.api.music.model.U1PlaylistSong;
49import com.ubuntuone.api.music.model.U1Song;
50import com.ubuntuone.api.music.util.RequestListener.U1PlaylistSongRequestListener;
51import com.ubuntuone.music.util.U1ResponseBuilder;
52import com.ubuntuone.music.util.U1SongBuilder;
53import com.ubuntuone.music.util.Util;
54import com.ubuntuone.test.util.DummyAuthorizer;
55
56@RunWith(JMock.class)
57public class CreatePlaylistTest
58{
59 private final Mockery context = new JUnit4Mockery();
60
61 private HttpResponse httpResponse;
62
63 private HttpHost httpHost =
64 new HttpHost(U1MusicAPI.RESOURCE_HOST, -1, BaseClient.HTTPS);
65
66 private HttpClient httpClient;
67 private U1MusicAPI musicApi;
68
69 private U1PlaylistSongRequestListener playlistSongCallback;
70
71 @Before
72 public void setUp() throws IOException {
73 httpResponse = Util.buildResponse(HttpStatus.SC_OK);
74
75 httpClient = context.mock(HttpClient.class);
76 musicApi = new U1MusicAPI("ua/1.0", httpClient, new DummyAuthorizer());
77
78 playlistSongCallback = context.mock(U1PlaylistSongRequestListener.class);
79 }
80
81 @Test
82 public void testCreatePlaylistLifeCycleCallbacks() {
83 String name = "playlist name";
84 ArrayList<U1Song> songList = new ArrayList<U1Song>();
85
86 final Sequence requestSequence = context.sequence("lifecycle");
87 context.checking(new Expectations() {{
88 oneOf(playlistSongCallback).onStart();
89 inSequence(requestSequence);
90
91 ignoring(httpClient);
92 allowing(playlistSongCallback).onSuccess(with(any(U1PlaylistSong.class)));
93 allowing(playlistSongCallback).onFailure(with(any(Failure.class)));
94
95 oneOf(playlistSongCallback).onFinish();
96 inSequence(requestSequence);
97 }});
98
99 musicApi.createPlaylist(name, songList, playlistSongCallback);
100 }
101
102 @Test
103 public void testCreatePlaylist() throws IOException {
104 final U1Meta meta = new U1Meta();
105 String id = "playlistId";
106 String name = "playlist name";
107 ArrayList<U1Song> songList = new ArrayList<U1Song>();
108
109 final U1Song song1 = new U1SongBuilder()
110 .withId("songId1").withAlbumId("Song album 1").build();
111 songList.add(song1);
112 final U1PlaylistSong playlistSong1 = new U1PlaylistSong(id, name, song1);
113
114 final U1Song song2 = new U1SongBuilder()
115 .withId("songId2").withAlbumId("Song album 2").build();
116 songList.add(song2);
117 final U1PlaylistSong playlistSong2 = new U1PlaylistSong(id, name, song2);
118
119 final U1Song song3 = new U1SongBuilder()
120 .withId("songId3").withAlbumId("Song album 3").build();
121 songList.add(song3);
122 final U1PlaylistSong playlistSong3 = new U1PlaylistSong(id, name, song3);
123
124 final HttpPost httpRequest = new HttpPost("https://one.ubuntu.com" +
125 "/music/api/2.0/playlists/");
126
127 httpRequest.setEntity(new StringEntity(U1CustomJson
128 .toSongIdListJson(name, songList)));
129
130 String responseJson = U1ResponseBuilder.toJson(
131 meta, U1MusicAPI.SONGS, id, name, song1, song2, song3);
132
133 httpResponse.setEntity(new StringEntity(responseJson, "UTF-8"));
134
135 final Sequence requestSequence = context.sequence("lifecycle");
136 context.checking(new Expectations() {{
137 oneOf(playlistSongCallback).onStart();
138 inSequence(requestSequence);
139
140 oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest)));
141 inSequence(requestSequence);
142 will(returnValue(httpResponse));
143
144 oneOf(playlistSongCallback).onSuccess(with(playlistSong1));
145 inSequence(requestSequence);
146 oneOf(playlistSongCallback).onSuccess(with(playlistSong2));
147 inSequence(requestSequence);
148 oneOf(playlistSongCallback).onSuccess(with(playlistSong3));
149 inSequence(requestSequence);
150
151 oneOf(playlistSongCallback).onFinish();
152 inSequence(requestSequence);
153 }});
154
155 musicApi.createPlaylist(name, songList, playlistSongCallback);
156 }
157
158 @Test
159 public void testUpdatePlaylist() throws IOException {
160 final U1Meta meta = new U1Meta();
161 String id = "playlistId";
162 String name = "playlist name";
163 ArrayList<U1Song> songList = new ArrayList<U1Song>();
164
165 final U1Song song1 = new U1SongBuilder()
166 .withId("songId1").withAlbumId("Song album 1").build();
167 songList.add(song1);
168 final U1PlaylistSong playlistSong1 = new U1PlaylistSong(id, name, song1);
169
170 final U1Song song2 = new U1SongBuilder()
171 .withId("songId2").withAlbumId("Song album 2").build();
172 songList.add(song2);
173 final U1PlaylistSong playlistSong2 = new U1PlaylistSong(id, name, song2);
174
175 final U1Song song3 = new U1SongBuilder()
176 .withId("songId3").withAlbumId("Song album 3").build();
177 songList.add(song3);
178 final U1PlaylistSong playlistSong3 = new U1PlaylistSong(id, name, song3);
179
180 final HttpPost httpRequest = new HttpPost("https://one.ubuntu.com" +
181 "/music/api/2.0/playlists/");
182
183 httpRequest.setEntity(new StringEntity(U1CustomJson
184 .toSongIdListJson(name, songList)));
185
186 String responseJson = U1ResponseBuilder.toJson(
187 meta, U1MusicAPI.SONGS, id, name, song1, song2, song3);
188
189 httpResponse.setEntity(new StringEntity(responseJson, "UTF-8"));
190
191 final Sequence requestSequence = context.sequence("lifecycle");
192 context.checking(new Expectations() {{
193 oneOf(playlistSongCallback).onStart();
194 inSequence(requestSequence);
195
196 oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest)));
197 inSequence(requestSequence);
198 will(returnValue(httpResponse));
199
200 oneOf(playlistSongCallback).onSuccess(with(playlistSong1));
201 inSequence(requestSequence);
202 oneOf(playlistSongCallback).onSuccess(with(playlistSong2));
203 inSequence(requestSequence);
204 oneOf(playlistSongCallback).onSuccess(with(playlistSong3));
205 inSequence(requestSequence);
206
207 oneOf(playlistSongCallback).onFinish();
208 inSequence(requestSequence);
209 }});
210
211 musicApi.createPlaylist(name, songList, playlistSongCallback);
212 }
213}
0214
=== modified file 'src/test/com/ubuntuone/api/music/GetAlbumsTest.java'
--- src/test/com/ubuntuone/api/music/GetAlbumsTest.java 2012-08-21 14:13:25 +0000
+++ src/test/com/ubuntuone/api/music/GetAlbumsTest.java 2012-08-21 14:13:25 +0000
@@ -43,9 +43,11 @@
43import com.ubuntuone.api.music.client.BaseClient;43import com.ubuntuone.api.music.client.BaseClient;
44import com.ubuntuone.api.music.client.Failure;44import com.ubuntuone.api.music.client.Failure;
45import com.ubuntuone.api.music.model.U1Album;45import com.ubuntuone.api.music.model.U1Album;
46import com.ubuntuone.api.music.model.U1Artist;
46import com.ubuntuone.api.music.model.U1Meta;47import com.ubuntuone.api.music.model.U1Meta;
47import com.ubuntuone.api.music.util.RequestListener.U1AlbumRequestListener;48import com.ubuntuone.api.music.util.RequestListener.U1AlbumRequestListener;
48import com.ubuntuone.music.util.U1AlbumBuilder;49import com.ubuntuone.music.util.U1AlbumBuilder;
50import com.ubuntuone.music.util.U1ArtistBuilder;
49import com.ubuntuone.music.util.U1ResponseBuilder;51import com.ubuntuone.music.util.U1ResponseBuilder;
50import com.ubuntuone.music.util.Util;52import com.ubuntuone.music.util.Util;
51import com.ubuntuone.test.util.DummyAuthorizer;53import com.ubuntuone.test.util.DummyAuthorizer;
@@ -164,4 +166,51 @@
164 166
165 musicApi.getAlbums(albumCallback);167 musicApi.getAlbums(albumCallback);
166 }168 }
169
170 @Test
171 public void testGetMultipleAlbumsByArtist() throws IOException {
172 final U1Meta meta = new U1Meta();
173 final U1Album album1 = new U1AlbumBuilder()
174 .withId("albumId1").withTitle("Album 1")
175 .withArtist("Artist 1").build();
176 final U1Album album2 = new U1AlbumBuilder()
177 .withId("albumId2").withTitle("Album 2")
178 .withArtist("Artist 2").build();
179 final U1Album album3 = new U1AlbumBuilder()
180 .withId("albumId3").withTitle("Album 3")
181 .withArtist("Artist 3").build();
182
183 final U1Artist artist = new U1ArtistBuilder().build();
184
185 httpRequest = new HttpGet(String.format(
186 "https://one.ubuntu.com/music/api/2.0/albums/%s/",
187 artist.getId()));
188
189 String responseJson = U1ResponseBuilder.toJson(
190 meta, U1MusicAPI.ALBUMS, album1, album2, album3);
191
192 httpResponse.setEntity(new StringEntity(responseJson, "UTF-8"));
193
194 final Sequence requestSequence = context.sequence("lifecycle");
195 context.checking(new Expectations() {{
196 oneOf(albumCallback).onStart();
197 inSequence(requestSequence);
198
199 oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest)));
200 inSequence(requestSequence);
201 will(returnValue(httpResponse));
202
203 oneOf(albumCallback).onSuccess(with(album1));
204 inSequence(requestSequence);
205 oneOf(albumCallback).onSuccess(with(album2));
206 inSequence(requestSequence);
207 oneOf(albumCallback).onSuccess(with(album3));
208 inSequence(requestSequence);
209
210 oneOf(albumCallback).onFinish();
211 inSequence(requestSequence);
212 }});
213
214 musicApi.getAlbumsByArtist(artist.getId(), albumCallback);
215 }
167}216}
168217
=== removed file 'src/test/com/ubuntuone/api/music/PutPlaylistTest.java'
--- src/test/com/ubuntuone/api/music/PutPlaylistTest.java 2012-08-21 14:13:25 +0000
+++ src/test/com/ubuntuone/api/music/PutPlaylistTest.java 1970-01-01 00:00:00 +0000
@@ -1,153 +0,0 @@
1/*
2 * Ubuntu One Music Java library - communicate with Ubuntu One music API
3 *
4 * Copyright 2012 Canonical Ltd.
5 *
6 * This file is part of Ubuntu One Files Java library.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see http://www.gnu.org/licenses
20 */
21
22package com.ubuntuone.api.music;
23
24import static com.ubuntuone.test.util.SameHttpRequestMatcher.sameRequest;
25
26import java.io.IOException;
27import java.util.ArrayList;
28
29import org.apache.http.HttpHost;
30import org.apache.http.HttpResponse;
31import org.apache.http.HttpStatus;
32import org.apache.http.client.HttpClient;
33import org.apache.http.client.methods.HttpPost;
34import org.apache.http.entity.StringEntity;
35import org.jmock.Expectations;
36import org.jmock.Mockery;
37import org.jmock.Sequence;
38import org.jmock.integration.junit4.JMock;
39import org.jmock.integration.junit4.JUnit4Mockery;
40import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43
44import com.ubuntuone.api.music.client.BaseClient;
45import com.ubuntuone.api.music.client.Failure;
46import com.ubuntuone.api.music.model.U1Meta;
47import com.ubuntuone.api.music.model.U1PlaylistSong;
48import com.ubuntuone.api.music.model.U1Song;
49import com.ubuntuone.api.music.util.RequestListener.U1PlaylistSongRequestListener;
50import com.ubuntuone.music.util.U1ResponseBuilder;
51import com.ubuntuone.music.util.U1SongBuilder;
52import com.ubuntuone.music.util.Util;
53import com.ubuntuone.test.util.DummyAuthorizer;
54
55@RunWith(JMock.class)
56public class PutPlaylistTest
57{
58 private final Mockery context = new JUnit4Mockery();
59
60 private HttpResponse httpResponse;
61
62 private HttpHost httpHost =
63 new HttpHost(U1MusicAPI.RESOURCE_HOST, -1, BaseClient.HTTPS);
64
65 private HttpClient httpClient;
66 private U1MusicAPI musicApi;
67
68 private U1PlaylistSongRequestListener playlistSongCallback;
69
70 @Before
71 public void setUp() throws IOException {
72 httpResponse = Util.buildResponse(HttpStatus.SC_OK);
73
74 httpClient = context.mock(HttpClient.class);
75 musicApi = new U1MusicAPI("ua/1.0", httpClient, new DummyAuthorizer());
76
77 playlistSongCallback = context.mock(U1PlaylistSongRequestListener.class);
78 }
79
80 @Test
81 public void testCreatePlaylistLifeCycleCallbacks() {
82 String name = "playlist name";
83 ArrayList<U1Song> songList = new ArrayList<U1Song>();
84
85 final Sequence requestSequence = context.sequence("lifecycle");
86 context.checking(new Expectations() {{
87 oneOf(playlistSongCallback).onStart();
88 inSequence(requestSequence);
89
90 ignoring(httpClient);
91 allowing(playlistSongCallback).onSuccess(with(any(U1PlaylistSong.class)));
92 allowing(playlistSongCallback).onFailure(with(any(Failure.class)));
93
94 oneOf(playlistSongCallback).onFinish();
95 inSequence(requestSequence);
96 }});
97
98 musicApi.createPlaylist(name, songList, playlistSongCallback);
99 }
100
101 @Test
102 public void testCreatePlaylist() throws IOException {
103 final U1Meta meta = new U1Meta();
104 String id = "playlistId";
105 String name = "playlist name";
106 ArrayList<U1Song> songList = new ArrayList<U1Song>();
107
108 final U1Song song1 = new U1SongBuilder()
109 .withId("songId1").withAlbumId("Song album 1").build();
110 songList.add(song1);
111 final U1PlaylistSong playlistSong1 = new U1PlaylistSong(id, name, song1);
112
113 final U1Song song2 = new U1SongBuilder()
114 .withId("songId2").withAlbumId("Song album 2").build();
115 songList.add(song2);
116 final U1PlaylistSong playlistSong2 = new U1PlaylistSong(id, name, song2);
117
118 final U1Song song3 = new U1SongBuilder()
119 .withId("songId3").withAlbumId("Song album 3").build();
120 songList.add(song3);
121 final U1PlaylistSong playlistSong3 = new U1PlaylistSong(id, name, song3);
122
123 final HttpPost httpRequest = new HttpPost("https://one.ubuntu.com" +
124 "/music/api/2.0/playlists/");
125
126 String responseJson = U1ResponseBuilder.toJson(
127 meta, U1MusicAPI.SONGS, id, name, song1, song2, song3);
128
129 httpResponse.setEntity(new StringEntity(responseJson, "UTF-8"));
130
131 final Sequence requestSequence = context.sequence("lifecycle");
132 context.checking(new Expectations() {{
133 oneOf(playlistSongCallback).onStart();
134 inSequence(requestSequence);
135
136 oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest)));
137 inSequence(requestSequence);
138 will(returnValue(httpResponse));
139
140 oneOf(playlistSongCallback).onSuccess(with(playlistSong1));
141 inSequence(requestSequence);
142 oneOf(playlistSongCallback).onSuccess(with(playlistSong2));
143 inSequence(requestSequence);
144 oneOf(playlistSongCallback).onSuccess(with(playlistSong3));
145 inSequence(requestSequence);
146
147 oneOf(playlistSongCallback).onFinish();
148 inSequence(requestSequence);
149 }});
150
151 musicApi.createPlaylist(name, songList, playlistSongCallback);
152 }
153}
1540
=== added file 'src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java'
--- src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java 1970-01-01 00:00:00 +0000
+++ src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java 2012-08-21 14:13:25 +0000
@@ -0,0 +1,157 @@
1/*
2 * Ubuntu One Music Java library - communicate with Ubuntu One music API
3 *
4 * Copyright 2012 Canonical Ltd.
5 *
6 * This file is part of Ubuntu One Files Java library.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see http://www.gnu.org/licenses
20 */
21
22package com.ubuntuone.api.music;
23
24import static com.ubuntuone.test.util.SameHttpRequestMatcher.sameRequest;
25
26import java.io.IOException;
27import java.util.ArrayList;
28
29import org.apache.http.HttpHost;
30import org.apache.http.HttpResponse;
31import org.apache.http.HttpStatus;
32import org.apache.http.client.HttpClient;
33import org.apache.http.client.methods.HttpPost;
34import org.apache.http.entity.StringEntity;
35import org.jmock.Expectations;
36import org.jmock.Mockery;
37import org.jmock.Sequence;
38import org.jmock.integration.junit4.JMock;
39import org.jmock.integration.junit4.JUnit4Mockery;
40import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43
44import com.ubuntuone.api.music.client.BaseClient;
45import com.ubuntuone.api.music.client.Failure;
46import com.ubuntuone.api.music.json.U1CustomJson;
47import com.ubuntuone.api.music.model.U1Meta;
48import com.ubuntuone.api.music.model.U1PlaylistSong;
49import com.ubuntuone.api.music.model.U1Song;
50import com.ubuntuone.api.music.util.RequestListener.U1PlaylistSongRequestListener;
51import com.ubuntuone.music.util.U1ResponseBuilder;
52import com.ubuntuone.music.util.U1SongBuilder;
53import com.ubuntuone.music.util.Util;
54import com.ubuntuone.test.util.DummyAuthorizer;
55
56@RunWith(JMock.class)
57public class UpdatePlaylistTest
58{
59 private final Mockery context = new JUnit4Mockery();
60
61 private HttpResponse httpResponse;
62
63 private HttpHost httpHost =
64 new HttpHost(U1MusicAPI.RESOURCE_HOST, -1, BaseClient.HTTPS);
65
66 private HttpClient httpClient;
67 private U1MusicAPI musicApi;
68
69 private U1PlaylistSongRequestListener playlistSongCallback;
70
71 @Before
72 public void setUp() throws IOException {
73 httpResponse = Util.buildResponse(HttpStatus.SC_OK);
74
75 httpClient = context.mock(HttpClient.class);
76 musicApi = new U1MusicAPI("ua/1.0", httpClient, new DummyAuthorizer());
77
78 playlistSongCallback = context.mock(U1PlaylistSongRequestListener.class);
79 }
80
81 @Test
82 public void testCreatePlaylistLifeCycleCallbacks() {
83 String name = "playlist name";
84 ArrayList<U1Song> songList = new ArrayList<U1Song>();
85
86 final Sequence requestSequence = context.sequence("lifecycle");
87 context.checking(new Expectations() {{
88 oneOf(playlistSongCallback).onStart();
89 inSequence(requestSequence);
90
91 ignoring(httpClient);
92 allowing(playlistSongCallback).onSuccess(with(any(U1PlaylistSong.class)));
93 allowing(playlistSongCallback).onFailure(with(any(Failure.class)));
94
95 oneOf(playlistSongCallback).onFinish();
96 inSequence(requestSequence);
97 }});
98
99 musicApi.createPlaylist(name, songList, playlistSongCallback);
100 }
101
102 @Test
103 public void testCreatePlaylist() throws IOException {
104 final U1Meta meta = new U1Meta();
105 String id = "playlistId";
106 String name = "playlist name";
107 ArrayList<U1Song> songList = new ArrayList<U1Song>();
108
109 final U1Song song1 = new U1SongBuilder()
110 .withId("songId1").withAlbumId("Song album 1").build();
111 songList.add(song1);
112 final U1PlaylistSong playlistSong1 = new U1PlaylistSong(id, name, song1);
113
114 final U1Song song2 = new U1SongBuilder()
115 .withId("songId2").withAlbumId("Song album 2").build();
116 songList.add(song2);
117 final U1PlaylistSong playlistSong2 = new U1PlaylistSong(id, name, song2);
118
119 final U1Song song3 = new U1SongBuilder()
120 .withId("songId3").withAlbumId("Song album 3").build();
121 songList.add(song3);
122 final U1PlaylistSong playlistSong3 = new U1PlaylistSong(id, name, song3);
123
124 final HttpPost httpRequest = new HttpPost("https://one.ubuntu.com" +
125 "/music/api/2.0/playlists/");
126
127 httpRequest.setEntity(new StringEntity(U1CustomJson
128 .toSongIdListJson(name, songList)));
129
130 String responseJson = U1ResponseBuilder.toJson(
131 meta, U1MusicAPI.SONGS, id, name, song1, song2, song3);
132
133 httpResponse.setEntity(new StringEntity(responseJson, "UTF-8"));
134
135 final Sequence requestSequence = context.sequence("lifecycle");
136 context.checking(new Expectations() {{
137 oneOf(playlistSongCallback).onStart();
138 inSequence(requestSequence);
139
140 oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest)));
141 inSequence(requestSequence);
142 will(returnValue(httpResponse));
143
144 oneOf(playlistSongCallback).onSuccess(with(playlistSong1));
145 inSequence(requestSequence);
146 oneOf(playlistSongCallback).onSuccess(with(playlistSong2));
147 inSequence(requestSequence);
148 oneOf(playlistSongCallback).onSuccess(with(playlistSong3));
149 inSequence(requestSequence);
150
151 oneOf(playlistSongCallback).onFinish();
152 inSequence(requestSequence);
153 }});
154
155 musicApi.createPlaylist(name, songList, playlistSongCallback);
156 }
157}
0158
=== added file 'src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java'
--- src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java 1970-01-01 00:00:00 +0000
+++ src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java 2012-08-21 14:13:25 +0000
@@ -0,0 +1,59 @@
1/*
2 * Ubuntu One Music Java library - communicate with Ubuntu One music API
3 *
4 * Copyright 2012 Canonical Ltd.
5 *
6 * This file is part of Ubuntu One Files Java library.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see http://www.gnu.org/licenses
20 */
21
22package com.ubuntuone.api.music.json;
23
24import static junit.framework.Assert.assertEquals;
25
26import java.io.IOException;
27import java.util.ArrayList;
28
29import org.junit.Test;
30
31import com.ubuntuone.api.music.model.U1Song;
32import com.ubuntuone.music.util.U1SongBuilder;
33
34public class U1CustomJsonTest
35{
36 @Test
37 public void testEncodeU1SongIdListPlaylist() throws IOException {
38 final ArrayList<U1Song> songList = new ArrayList<U1Song>();
39
40 final U1Song song1 = new U1SongBuilder()
41 .withId("id1").build();
42 songList.add(song1);
43 final U1Song song2 = new U1SongBuilder()
44 .withId("id2").build();
45 songList.add(song2);
46
47 String name = "the playlist name";
48
49 String json = U1CustomJson.toSongIdListJson(name, songList);
50
51 String expectedJson = "{\"name\":\"" + name + "\"," +
52 "\"song_id_list\":[" +
53 "\"" + song1.getId() + "\"," +
54 "\"" + song2.getId() + "\"" +
55 "]}";
56
57 assertEquals(expectedJson, json);
58 }
59}
060
=== modified file 'src/test/com/ubuntuone/test/util/SameHttpRequestMatcher.java'
--- src/test/com/ubuntuone/test/util/SameHttpRequestMatcher.java 2012-08-21 14:13:25 +0000
+++ src/test/com/ubuntuone/test/util/SameHttpRequestMatcher.java 2012-08-21 14:13:25 +0000
@@ -21,7 +21,14 @@
2121
22package com.ubuntuone.test.util;22package com.ubuntuone.test.util;
2323
24import java.io.IOException;
25import java.io.InputStream;
26
27import org.apache.http.ParseException;
28import org.apache.http.client.methods.HttpPost;
29import org.apache.http.client.methods.HttpPut;
24import org.apache.http.client.methods.HttpUriRequest;30import org.apache.http.client.methods.HttpUriRequest;
31import org.apache.http.util.EntityUtils;
25import org.hamcrest.Description;32import org.hamcrest.Description;
26import org.hamcrest.Factory;33import org.hamcrest.Factory;
27import org.hamcrest.Matcher;34import org.hamcrest.Matcher;
@@ -40,6 +47,31 @@
40 public void describeTo(Description description) {47 public void describeTo(Description description) {
41 description.appendText("a request with request line ")48 description.appendText("a request with request line ")
42 .appendValue(request.getRequestLine());49 .appendValue(request.getRequestLine());
50 if (request.getClass() == HttpPost.class) {
51 HttpPost post = (HttpPost) request;
52 if (post.getEntity() != null) {
53 try {
54 description.appendText("and POST body ")
55 .appendValue(EntityUtils.toString(post.getEntity()));
56 } catch (ParseException e) {
57 description.appendText("and broken POST body!");
58 } catch (IOException e) {
59 description.appendText("and broken POST body!");
60 }
61 }
62 } else if (request.getClass() == HttpPut.class) {
63 HttpPut put = (HttpPut) request;
64 if (put.getEntity() != null) {
65 try {
66 description.appendText("and PUT body ")
67 .appendValue(EntityUtils.toString(put.getEntity()));
68 } catch (ParseException e) {
69 description.appendText("and broken PUT body ");
70 } catch (IOException e) {
71 description.appendText("and broken PUT body ");
72 }
73 }
74 }
43 }75 }
4476
45 @Override77 @Override
@@ -48,8 +80,47 @@
48 return false;80 return false;
49 if (item == request)81 if (item == request)
50 return true;82 return true;
83 boolean sameBody = true;
84 if (request.getClass() == HttpPost.class) {
85 HttpPost postRequest = (HttpPost) request;
86 HttpPost postItem = (HttpPost) item;
87 if (postRequest.getEntity() != null) {
88 try {
89 InputStream in1 = postRequest.getEntity().getContent();
90 if (postItem.getEntity() != null) {
91 InputStream in2 = postItem.getEntity().getContent();
92 sameBody = StreamUtils.streamsHaveSameContent(in1, in2);
93 } else {
94 sameBody = false;
95 }
96 } catch (IllegalStateException e) {
97 return false;
98 } catch (IOException e) {
99 return false;
100 }
101 }
102 } else if (request.getClass() == HttpPut.class) {
103 HttpPut putRequest = (HttpPut) request;
104 HttpPut putItem = (HttpPut) item;
105 if (putRequest.getEntity() != null) {
106 try {
107 InputStream in1 = putRequest.getEntity().getContent();
108 if (putItem.getEntity() != null) {
109 InputStream in2 = putItem.getEntity().getContent();
110 sameBody = StreamUtils.streamsHaveSameContent(in1, in2);
111 } else {
112 sameBody = false;
113 }
114 } catch (IllegalStateException e) {
115 return false;
116 } catch (IOException e) {
117 return false;
118 }
119 }
120 }
51 return request.getMethod().equals(item.getMethod())121 return request.getMethod().equals(item.getMethod())
52 && request.getURI().equals(item.getURI());122 && request.getURI().equals(item.getURI())
123 && sameBody;
53 }124 }
54 125
55 @Factory126 @Factory
56127
=== added file 'src/test/com/ubuntuone/test/util/StreamUtils.java'
--- src/test/com/ubuntuone/test/util/StreamUtils.java 1970-01-01 00:00:00 +0000
+++ src/test/com/ubuntuone/test/util/StreamUtils.java 2012-08-21 14:13:25 +0000
@@ -0,0 +1,27 @@
1package com.ubuntuone.test.util;
2
3import java.io.IOException;
4import java.io.InputStream;
5
6public class StreamUtils
7{
8 public static boolean streamsHaveSameContent(InputStream in1, InputStream in2) {
9 boolean same = true;
10 int b1, b2;
11 try {
12 while ((b1 = in1.read()) != -1) {
13 b2 = in2.read();
14 if (b1 != b2) {
15 same = false;
16 break;
17 }
18 }
19 if (in2.read() != -1) {
20 same = false;
21 }
22 } catch (IOException e) {
23 same = false;
24 }
25 return same;
26 }
27}

Subscribers

People subscribed via source and target branches