Merge lp:~karni/ubuntuone-music-java-library/playlist-methods into lp:ubuntuone-music-java-library
- playlist-methods
- Merge into trunk
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 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Chad Miller | Pending | ||
Review via email: mp+120578@code.launchpad.net |
Commit message
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
1 | === modified file '.bzrignore' | |||
2 | --- .bzrignore 2012-06-05 15:16:38 +0000 | |||
3 | +++ .bzrignore 2012-08-21 14:13:25 +0000 | |||
4 | @@ -16,3 +16,4 @@ | |||
5 | 16 | libs/jmock-*.jar | 16 | libs/jmock-*.jar |
6 | 17 | libs/jmock-junit4-*.jar | 17 | libs/jmock-junit4-*.jar |
7 | 18 | libs/hamcrest-all-*.jar | 18 | libs/hamcrest-all-*.jar |
8 | 19 | src/main/com/ubuntuone/api/music/MusicTest.java | ||
9 | 19 | 20 | ||
10 | === modified file 'ant.properties' | |||
11 | --- ant.properties 2012-06-05 15:16:38 +0000 | |||
12 | +++ ant.properties 2012-08-21 14:13:25 +0000 | |||
13 | @@ -61,4 +61,4 @@ | |||
14 | 61 | 61 | ||
15 | 62 | # Only used by official ubuntuone-hackers team. | 62 | # Only used by official ubuntuone-hackers team. |
16 | 63 | 63 | ||
18 | 64 | project.version=0.1 | 64 | project.version=0.1.0 |
19 | 65 | 65 | ||
20 | === modified file 'build.xml' | |||
21 | --- build.xml 2012-08-21 14:13:25 +0000 | |||
22 | +++ build.xml 2012-08-21 14:13:25 +0000 | |||
23 | @@ -1,5 +1,5 @@ | |||
24 | 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> | 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
26 | 2 | <project name="ubuntuone-files-java-library" basedir="." default="jar"> | 2 | <project name="ubuntuone-music-java-library" basedir="." default="jar"> |
27 | 3 | 3 | ||
28 | 4 | <description> | 4 | <description> |
29 | 5 | *** UbuntuOne Music Java library *** | 5 | *** UbuntuOne Music Java library *** |
30 | 6 | 6 | ||
31 | === modified file 'src/main/com/ubuntuone/api/music/U1MusicAPI.java' | |||
32 | --- src/main/com/ubuntuone/api/music/U1MusicAPI.java 2012-08-21 14:13:25 +0000 | |||
33 | +++ src/main/com/ubuntuone/api/music/U1MusicAPI.java 2012-08-21 14:13:25 +0000 | |||
34 | @@ -53,6 +53,7 @@ | |||
35 | 53 | import com.ubuntuone.api.music.client.StreamingClient; | 53 | import com.ubuntuone.api.music.client.StreamingClient; |
36 | 54 | import com.ubuntuone.api.music.json.U1AlbumJson; | 54 | import com.ubuntuone.api.music.json.U1AlbumJson; |
37 | 55 | import com.ubuntuone.api.music.json.U1ArtistJson; | 55 | import com.ubuntuone.api.music.json.U1ArtistJson; |
38 | 56 | import com.ubuntuone.api.music.json.U1CustomJson; | ||
39 | 56 | import com.ubuntuone.api.music.json.U1PlaylistJson; | 57 | import com.ubuntuone.api.music.json.U1PlaylistJson; |
40 | 57 | import com.ubuntuone.api.music.json.U1PlaylistSongJson; | 58 | import com.ubuntuone.api.music.json.U1PlaylistSongJson; |
41 | 58 | import com.ubuntuone.api.music.json.U1SongJson; | 59 | import com.ubuntuone.api.music.json.U1SongJson; |
42 | @@ -196,6 +197,44 @@ | |||
43 | 196 | } | 197 | } |
44 | 197 | } | 198 | } |
45 | 198 | 199 | ||
46 | 200 | public void getAlbumsByArtist(String artistId, U1AlbumRequestListener callback) { | ||
47 | 201 | callback.onStart(); | ||
48 | 202 | String path = resourceClient.getPath(ALBUMS, artistId); | ||
49 | 203 | HttpResponse response = null; | ||
50 | 204 | try { | ||
51 | 205 | response = resourceClient.request(HttpGet.METHOD_NAME, path); | ||
52 | 206 | final int statusCode = getStatusCode(response); | ||
53 | 207 | if (statusCode == HttpStatus.SC_OK) { | ||
54 | 208 | U1AlbumJson.fromJson(getContentInputStream(response), callback); | ||
55 | 209 | } else { | ||
56 | 210 | handleHttpResponse(response, "Could not get artist albums.", callback); | ||
57 | 211 | } | ||
58 | 212 | } catch (JsonParseException jpe) { | ||
59 | 213 | // Received corrupt JSON. At this stage, response != null | ||
60 | 214 | final String bzrRev = getHeaderValue(ResponseHeader.X_BZR_REVISION_NUMBER, response); | ||
61 | 215 | final String date = getHeaderValue(ResponseHeader.DATE, response); | ||
62 | 216 | |||
63 | 217 | callback.onFailure(new Failure("Could not get artist albums. Corrupt JSON.", | ||
64 | 218 | jpe, 0, null, bzrRev, date)); | ||
65 | 219 | } catch (OutOfMemoryError e) { | ||
66 | 220 | callback.onFailure(new Failure("Out of memory!", e)); | ||
67 | 221 | throw e; // We're not expected to recover from this. | ||
68 | 222 | } catch (SSLException sslException) { | ||
69 | 223 | callback.onFailure(new Failure("SSL connection problem. This may be intermittent issue, " + | ||
70 | 224 | "please try again later.", sslException)); | ||
71 | 225 | } catch (IOException ioException) { | ||
72 | 226 | // Failed to read the response. | ||
73 | 227 | callback.onFailure(new Failure("Could not get artist albums (network error).", ioException)); | ||
74 | 228 | } catch (URISyntaxException uriSyntaxException) { | ||
75 | 229 | // Failed due to invalid URL within the library code. | ||
76 | 230 | throw new IllegalStateException(uriSyntaxException); | ||
77 | 231 | } catch (AuthorizerException signingException) { | ||
78 | 232 | callback.onFailure(new Failure("Could not get artist albums (signing exception).", signingException)); | ||
79 | 233 | } finally { | ||
80 | 234 | callback.onFinish(); | ||
81 | 235 | } | ||
82 | 236 | } | ||
83 | 237 | |||
84 | 199 | public void getSongs(U1SongRequestListener callback) { | 238 | public void getSongs(U1SongRequestListener callback) { |
85 | 200 | callback.onStart(); | 239 | callback.onStart(); |
86 | 201 | String path = resourceClient.getPath(SONGS); | 240 | String path = resourceClient.getPath(SONGS); |
87 | @@ -310,13 +349,29 @@ | |||
88 | 310 | } | 349 | } |
89 | 311 | } | 350 | } |
90 | 312 | 351 | ||
92 | 313 | public void createPlaylist(String name, ArrayList<U1Song> songList, | 352 | public void createPlaylist(String name, |
93 | 353 | ArrayList<U1Song> songs, U1PlaylistSongRequestListener callback) { | ||
94 | 354 | putPlaylist(null, name, songs, callback); | ||
95 | 355 | } | ||
96 | 356 | |||
97 | 357 | public void updatePlaylist(String playlistId, String name, | ||
98 | 358 | ArrayList<U1Song> songs, U1PlaylistSongRequestListener callback) { | ||
99 | 359 | putPlaylist(playlistId, name, songs, callback); | ||
100 | 360 | } | ||
101 | 361 | |||
102 | 362 | public void putPlaylist(String playlistId, String name, ArrayList<U1Song> songs, | ||
103 | 314 | U1PlaylistSongRequestListener callback) { | 363 | U1PlaylistSongRequestListener callback) { |
104 | 315 | callback.onStart(); | 364 | callback.onStart(); |
106 | 316 | String path = resourceClient.getPath(PLAYLISTS); | 365 | String path; |
107 | 366 | if (playlistId != null) { | ||
108 | 367 | path = resourceClient.getPath(PLAYLISTS, playlistId); | ||
109 | 368 | } else { | ||
110 | 369 | path = resourceClient.getPath(PLAYLISTS); | ||
111 | 370 | } | ||
112 | 317 | HttpResponse response = null; | 371 | HttpResponse response = null; |
113 | 318 | try { | 372 | try { |
115 | 319 | response = resourceClient.request(HttpPost.METHOD_NAME, path); | 373 | String data = U1CustomJson.toSongIdListJson(name, songs); |
116 | 374 | response = resourceClient.request(HttpPost.METHOD_NAME, path, null, data); | ||
117 | 320 | final int statusCode = getStatusCode(response); | 375 | final int statusCode = getStatusCode(response); |
118 | 321 | if (statusCode == HttpStatus.SC_OK) { | 376 | if (statusCode == HttpStatus.SC_OK) { |
119 | 322 | U1PlaylistSongJson.fromJson(getContentInputStream(response), callback); | 377 | U1PlaylistSongJson.fromJson(getContentInputStream(response), callback); |
120 | 323 | 378 | ||
121 | === modified file 'src/main/com/ubuntuone/api/music/client/ResourceClient.java' | |||
122 | --- src/main/com/ubuntuone/api/music/client/ResourceClient.java 2012-08-21 14:13:25 +0000 | |||
123 | +++ src/main/com/ubuntuone/api/music/client/ResourceClient.java 2012-08-21 14:13:25 +0000 | |||
124 | @@ -32,6 +32,7 @@ | |||
125 | 32 | import org.apache.http.HttpResponse; | 32 | import org.apache.http.HttpResponse; |
126 | 33 | import org.apache.http.NameValuePair; | 33 | import org.apache.http.NameValuePair; |
127 | 34 | import org.apache.http.client.HttpClient; | 34 | import org.apache.http.client.HttpClient; |
128 | 35 | import org.apache.http.client.methods.HttpPost; | ||
129 | 35 | import org.apache.http.client.methods.HttpPut; | 36 | import org.apache.http.client.methods.HttpPut; |
130 | 36 | import org.apache.http.client.methods.HttpUriRequest; | 37 | import org.apache.http.client.methods.HttpUriRequest; |
131 | 37 | import org.apache.http.client.utils.URLEncodedUtils; | 38 | import org.apache.http.client.utils.URLEncodedUtils; |
132 | @@ -95,7 +96,9 @@ | |||
133 | 95 | request.addHeader(HttpHeaders.USER_AGENT, userAgent); | 96 | request.addHeader(HttpHeaders.USER_AGENT, userAgent); |
134 | 96 | request.addHeader(HttpHeaders.CONNECTION, CONNECTION); | 97 | request.addHeader(HttpHeaders.CONNECTION, CONNECTION); |
135 | 97 | request.addHeader(HttpHeaders.ACCEPT, ACCEPT); | 98 | request.addHeader(HttpHeaders.ACCEPT, ACCEPT); |
137 | 98 | if (data != null && HttpPut.METHOD_NAME.equals(method)) { | 99 | if (data != null && HttpPost.METHOD_NAME.equals(method)) { |
138 | 100 | ((HttpPost) request).setEntity(new StringEntity(data, UTF8)); | ||
139 | 101 | } else if (data != null && HttpPut.METHOD_NAME.equals(method)) { | ||
140 | 99 | ((HttpPut) request).setEntity(new StringEntity(data, UTF8)); | 102 | ((HttpPut) request).setEntity(new StringEntity(data, UTF8)); |
141 | 100 | } | 103 | } |
142 | 101 | 104 | ||
143 | 102 | 105 | ||
144 | === added file 'src/main/com/ubuntuone/api/music/json/U1CustomJson.java' | |||
145 | --- src/main/com/ubuntuone/api/music/json/U1CustomJson.java 1970-01-01 00:00:00 +0000 | |||
146 | +++ src/main/com/ubuntuone/api/music/json/U1CustomJson.java 2012-08-21 14:13:25 +0000 | |||
147 | @@ -0,0 +1,56 @@ | |||
148 | 1 | /* | ||
149 | 2 | * Ubuntu One Music Java library - communicate with Ubuntu One music API | ||
150 | 3 | * | ||
151 | 4 | * Copyright 2012 Canonical Ltd. | ||
152 | 5 | * | ||
153 | 6 | * This file is part of Ubuntu One Files Java library. | ||
154 | 7 | * | ||
155 | 8 | * This program is free software: you can redistribute it and/or modify | ||
156 | 9 | * it under the terms of the GNU Affero General Public License as | ||
157 | 10 | * published by the Free Software Foundation, either version 3 of the | ||
158 | 11 | * License, or (at your option) any later version. | ||
159 | 12 | * | ||
160 | 13 | * This program is distributed in the hope that it will be useful, | ||
161 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
162 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
163 | 16 | * GNU Affero General Public License for more details. | ||
164 | 17 | * | ||
165 | 18 | * You should have received a copy of the GNU Affero General Public License | ||
166 | 19 | * along with this program. If not, see http://www.gnu.org/licenses | ||
167 | 20 | */ | ||
168 | 21 | |||
169 | 22 | package com.ubuntuone.api.music.json; | ||
170 | 23 | |||
171 | 24 | import java.io.IOException; | ||
172 | 25 | import java.io.StringWriter; | ||
173 | 26 | import java.util.ArrayList; | ||
174 | 27 | |||
175 | 28 | import org.codehaus.jackson.JsonFactory; | ||
176 | 29 | import org.codehaus.jackson.JsonGenerator; | ||
177 | 30 | |||
178 | 31 | import com.ubuntuone.api.music.model.U1Song; | ||
179 | 32 | |||
180 | 33 | public class U1CustomJson | ||
181 | 34 | { | ||
182 | 35 | public static String toSongIdListJson(String name, ArrayList<U1Song> songs) | ||
183 | 36 | throws IOException { | ||
184 | 37 | final JsonFactory factory = new JsonFactory(); | ||
185 | 38 | final StringWriter writer = new StringWriter(128); | ||
186 | 39 | final JsonGenerator g = factory.createJsonGenerator(writer); | ||
187 | 40 | |||
188 | 41 | g.writeStartObject(); | ||
189 | 42 | g.writeStringField("name", name); | ||
190 | 43 | |||
191 | 44 | g.writeArrayFieldStart("song_id_list"); { | ||
192 | 45 | for (U1Song song : songs) { | ||
193 | 46 | g.writeString(song.getId()); | ||
194 | 47 | } | ||
195 | 48 | } | ||
196 | 49 | g.writeEndArray(); | ||
197 | 50 | |||
198 | 51 | g.writeEndObject(); | ||
199 | 52 | g.close(); | ||
200 | 53 | |||
201 | 54 | return writer.toString(); | ||
202 | 55 | } | ||
203 | 56 | } | ||
204 | 0 | 57 | ||
205 | === added file 'src/test/com/ubuntuone/api/music/CreatePlaylistTest.java' | |||
206 | --- src/test/com/ubuntuone/api/music/CreatePlaylistTest.java 1970-01-01 00:00:00 +0000 | |||
207 | +++ src/test/com/ubuntuone/api/music/CreatePlaylistTest.java 2012-08-21 14:13:25 +0000 | |||
208 | @@ -0,0 +1,213 @@ | |||
209 | 1 | /* | ||
210 | 2 | * Ubuntu One Music Java library - communicate with Ubuntu One music API | ||
211 | 3 | * | ||
212 | 4 | * Copyright 2012 Canonical Ltd. | ||
213 | 5 | * | ||
214 | 6 | * This file is part of Ubuntu One Files Java library. | ||
215 | 7 | * | ||
216 | 8 | * This program is free software: you can redistribute it and/or modify | ||
217 | 9 | * it under the terms of the GNU Affero General Public License as | ||
218 | 10 | * published by the Free Software Foundation, either version 3 of the | ||
219 | 11 | * License, or (at your option) any later version. | ||
220 | 12 | * | ||
221 | 13 | * This program is distributed in the hope that it will be useful, | ||
222 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
223 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
224 | 16 | * GNU Affero General Public License for more details. | ||
225 | 17 | * | ||
226 | 18 | * You should have received a copy of the GNU Affero General Public License | ||
227 | 19 | * along with this program. If not, see http://www.gnu.org/licenses | ||
228 | 20 | */ | ||
229 | 21 | |||
230 | 22 | package com.ubuntuone.api.music; | ||
231 | 23 | |||
232 | 24 | import static com.ubuntuone.test.util.SameHttpRequestMatcher.sameRequest; | ||
233 | 25 | |||
234 | 26 | import java.io.IOException; | ||
235 | 27 | import java.util.ArrayList; | ||
236 | 28 | |||
237 | 29 | import org.apache.http.HttpHost; | ||
238 | 30 | import org.apache.http.HttpResponse; | ||
239 | 31 | import org.apache.http.HttpStatus; | ||
240 | 32 | import org.apache.http.client.HttpClient; | ||
241 | 33 | import org.apache.http.client.methods.HttpPost; | ||
242 | 34 | import org.apache.http.entity.StringEntity; | ||
243 | 35 | import org.jmock.Expectations; | ||
244 | 36 | import org.jmock.Mockery; | ||
245 | 37 | import org.jmock.Sequence; | ||
246 | 38 | import org.jmock.integration.junit4.JMock; | ||
247 | 39 | import org.jmock.integration.junit4.JUnit4Mockery; | ||
248 | 40 | import org.junit.Before; | ||
249 | 41 | import org.junit.Test; | ||
250 | 42 | import org.junit.runner.RunWith; | ||
251 | 43 | |||
252 | 44 | import com.ubuntuone.api.music.client.BaseClient; | ||
253 | 45 | import com.ubuntuone.api.music.client.Failure; | ||
254 | 46 | import com.ubuntuone.api.music.json.U1CustomJson; | ||
255 | 47 | import com.ubuntuone.api.music.model.U1Meta; | ||
256 | 48 | import com.ubuntuone.api.music.model.U1PlaylistSong; | ||
257 | 49 | import com.ubuntuone.api.music.model.U1Song; | ||
258 | 50 | import com.ubuntuone.api.music.util.RequestListener.U1PlaylistSongRequestListener; | ||
259 | 51 | import com.ubuntuone.music.util.U1ResponseBuilder; | ||
260 | 52 | import com.ubuntuone.music.util.U1SongBuilder; | ||
261 | 53 | import com.ubuntuone.music.util.Util; | ||
262 | 54 | import com.ubuntuone.test.util.DummyAuthorizer; | ||
263 | 55 | |||
264 | 56 | @RunWith(JMock.class) | ||
265 | 57 | public class CreatePlaylistTest | ||
266 | 58 | { | ||
267 | 59 | private final Mockery context = new JUnit4Mockery(); | ||
268 | 60 | |||
269 | 61 | private HttpResponse httpResponse; | ||
270 | 62 | |||
271 | 63 | private HttpHost httpHost = | ||
272 | 64 | new HttpHost(U1MusicAPI.RESOURCE_HOST, -1, BaseClient.HTTPS); | ||
273 | 65 | |||
274 | 66 | private HttpClient httpClient; | ||
275 | 67 | private U1MusicAPI musicApi; | ||
276 | 68 | |||
277 | 69 | private U1PlaylistSongRequestListener playlistSongCallback; | ||
278 | 70 | |||
279 | 71 | @Before | ||
280 | 72 | public void setUp() throws IOException { | ||
281 | 73 | httpResponse = Util.buildResponse(HttpStatus.SC_OK); | ||
282 | 74 | |||
283 | 75 | httpClient = context.mock(HttpClient.class); | ||
284 | 76 | musicApi = new U1MusicAPI("ua/1.0", httpClient, new DummyAuthorizer()); | ||
285 | 77 | |||
286 | 78 | playlistSongCallback = context.mock(U1PlaylistSongRequestListener.class); | ||
287 | 79 | } | ||
288 | 80 | |||
289 | 81 | @Test | ||
290 | 82 | public void testCreatePlaylistLifeCycleCallbacks() { | ||
291 | 83 | String name = "playlist name"; | ||
292 | 84 | ArrayList<U1Song> songList = new ArrayList<U1Song>(); | ||
293 | 85 | |||
294 | 86 | final Sequence requestSequence = context.sequence("lifecycle"); | ||
295 | 87 | context.checking(new Expectations() {{ | ||
296 | 88 | oneOf(playlistSongCallback).onStart(); | ||
297 | 89 | inSequence(requestSequence); | ||
298 | 90 | |||
299 | 91 | ignoring(httpClient); | ||
300 | 92 | allowing(playlistSongCallback).onSuccess(with(any(U1PlaylistSong.class))); | ||
301 | 93 | allowing(playlistSongCallback).onFailure(with(any(Failure.class))); | ||
302 | 94 | |||
303 | 95 | oneOf(playlistSongCallback).onFinish(); | ||
304 | 96 | inSequence(requestSequence); | ||
305 | 97 | }}); | ||
306 | 98 | |||
307 | 99 | musicApi.createPlaylist(name, songList, playlistSongCallback); | ||
308 | 100 | } | ||
309 | 101 | |||
310 | 102 | @Test | ||
311 | 103 | public void testCreatePlaylist() throws IOException { | ||
312 | 104 | final U1Meta meta = new U1Meta(); | ||
313 | 105 | String id = "playlistId"; | ||
314 | 106 | String name = "playlist name"; | ||
315 | 107 | ArrayList<U1Song> songList = new ArrayList<U1Song>(); | ||
316 | 108 | |||
317 | 109 | final U1Song song1 = new U1SongBuilder() | ||
318 | 110 | .withId("songId1").withAlbumId("Song album 1").build(); | ||
319 | 111 | songList.add(song1); | ||
320 | 112 | final U1PlaylistSong playlistSong1 = new U1PlaylistSong(id, name, song1); | ||
321 | 113 | |||
322 | 114 | final U1Song song2 = new U1SongBuilder() | ||
323 | 115 | .withId("songId2").withAlbumId("Song album 2").build(); | ||
324 | 116 | songList.add(song2); | ||
325 | 117 | final U1PlaylistSong playlistSong2 = new U1PlaylistSong(id, name, song2); | ||
326 | 118 | |||
327 | 119 | final U1Song song3 = new U1SongBuilder() | ||
328 | 120 | .withId("songId3").withAlbumId("Song album 3").build(); | ||
329 | 121 | songList.add(song3); | ||
330 | 122 | final U1PlaylistSong playlistSong3 = new U1PlaylistSong(id, name, song3); | ||
331 | 123 | |||
332 | 124 | final HttpPost httpRequest = new HttpPost("https://one.ubuntu.com" + | ||
333 | 125 | "/music/api/2.0/playlists/"); | ||
334 | 126 | |||
335 | 127 | httpRequest.setEntity(new StringEntity(U1CustomJson | ||
336 | 128 | .toSongIdListJson(name, songList))); | ||
337 | 129 | |||
338 | 130 | String responseJson = U1ResponseBuilder.toJson( | ||
339 | 131 | meta, U1MusicAPI.SONGS, id, name, song1, song2, song3); | ||
340 | 132 | |||
341 | 133 | httpResponse.setEntity(new StringEntity(responseJson, "UTF-8")); | ||
342 | 134 | |||
343 | 135 | final Sequence requestSequence = context.sequence("lifecycle"); | ||
344 | 136 | context.checking(new Expectations() {{ | ||
345 | 137 | oneOf(playlistSongCallback).onStart(); | ||
346 | 138 | inSequence(requestSequence); | ||
347 | 139 | |||
348 | 140 | oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest))); | ||
349 | 141 | inSequence(requestSequence); | ||
350 | 142 | will(returnValue(httpResponse)); | ||
351 | 143 | |||
352 | 144 | oneOf(playlistSongCallback).onSuccess(with(playlistSong1)); | ||
353 | 145 | inSequence(requestSequence); | ||
354 | 146 | oneOf(playlistSongCallback).onSuccess(with(playlistSong2)); | ||
355 | 147 | inSequence(requestSequence); | ||
356 | 148 | oneOf(playlistSongCallback).onSuccess(with(playlistSong3)); | ||
357 | 149 | inSequence(requestSequence); | ||
358 | 150 | |||
359 | 151 | oneOf(playlistSongCallback).onFinish(); | ||
360 | 152 | inSequence(requestSequence); | ||
361 | 153 | }}); | ||
362 | 154 | |||
363 | 155 | musicApi.createPlaylist(name, songList, playlistSongCallback); | ||
364 | 156 | } | ||
365 | 157 | |||
366 | 158 | @Test | ||
367 | 159 | public void testUpdatePlaylist() throws IOException { | ||
368 | 160 | final U1Meta meta = new U1Meta(); | ||
369 | 161 | String id = "playlistId"; | ||
370 | 162 | String name = "playlist name"; | ||
371 | 163 | ArrayList<U1Song> songList = new ArrayList<U1Song>(); | ||
372 | 164 | |||
373 | 165 | final U1Song song1 = new U1SongBuilder() | ||
374 | 166 | .withId("songId1").withAlbumId("Song album 1").build(); | ||
375 | 167 | songList.add(song1); | ||
376 | 168 | final U1PlaylistSong playlistSong1 = new U1PlaylistSong(id, name, song1); | ||
377 | 169 | |||
378 | 170 | final U1Song song2 = new U1SongBuilder() | ||
379 | 171 | .withId("songId2").withAlbumId("Song album 2").build(); | ||
380 | 172 | songList.add(song2); | ||
381 | 173 | final U1PlaylistSong playlistSong2 = new U1PlaylistSong(id, name, song2); | ||
382 | 174 | |||
383 | 175 | final U1Song song3 = new U1SongBuilder() | ||
384 | 176 | .withId("songId3").withAlbumId("Song album 3").build(); | ||
385 | 177 | songList.add(song3); | ||
386 | 178 | final U1PlaylistSong playlistSong3 = new U1PlaylistSong(id, name, song3); | ||
387 | 179 | |||
388 | 180 | final HttpPost httpRequest = new HttpPost("https://one.ubuntu.com" + | ||
389 | 181 | "/music/api/2.0/playlists/"); | ||
390 | 182 | |||
391 | 183 | httpRequest.setEntity(new StringEntity(U1CustomJson | ||
392 | 184 | .toSongIdListJson(name, songList))); | ||
393 | 185 | |||
394 | 186 | String responseJson = U1ResponseBuilder.toJson( | ||
395 | 187 | meta, U1MusicAPI.SONGS, id, name, song1, song2, song3); | ||
396 | 188 | |||
397 | 189 | httpResponse.setEntity(new StringEntity(responseJson, "UTF-8")); | ||
398 | 190 | |||
399 | 191 | final Sequence requestSequence = context.sequence("lifecycle"); | ||
400 | 192 | context.checking(new Expectations() {{ | ||
401 | 193 | oneOf(playlistSongCallback).onStart(); | ||
402 | 194 | inSequence(requestSequence); | ||
403 | 195 | |||
404 | 196 | oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest))); | ||
405 | 197 | inSequence(requestSequence); | ||
406 | 198 | will(returnValue(httpResponse)); | ||
407 | 199 | |||
408 | 200 | oneOf(playlistSongCallback).onSuccess(with(playlistSong1)); | ||
409 | 201 | inSequence(requestSequence); | ||
410 | 202 | oneOf(playlistSongCallback).onSuccess(with(playlistSong2)); | ||
411 | 203 | inSequence(requestSequence); | ||
412 | 204 | oneOf(playlistSongCallback).onSuccess(with(playlistSong3)); | ||
413 | 205 | inSequence(requestSequence); | ||
414 | 206 | |||
415 | 207 | oneOf(playlistSongCallback).onFinish(); | ||
416 | 208 | inSequence(requestSequence); | ||
417 | 209 | }}); | ||
418 | 210 | |||
419 | 211 | musicApi.createPlaylist(name, songList, playlistSongCallback); | ||
420 | 212 | } | ||
421 | 213 | } | ||
422 | 0 | 214 | ||
423 | === modified file 'src/test/com/ubuntuone/api/music/GetAlbumsTest.java' | |||
424 | --- src/test/com/ubuntuone/api/music/GetAlbumsTest.java 2012-08-21 14:13:25 +0000 | |||
425 | +++ src/test/com/ubuntuone/api/music/GetAlbumsTest.java 2012-08-21 14:13:25 +0000 | |||
426 | @@ -43,9 +43,11 @@ | |||
427 | 43 | import com.ubuntuone.api.music.client.BaseClient; | 43 | import com.ubuntuone.api.music.client.BaseClient; |
428 | 44 | import com.ubuntuone.api.music.client.Failure; | 44 | import com.ubuntuone.api.music.client.Failure; |
429 | 45 | import com.ubuntuone.api.music.model.U1Album; | 45 | import com.ubuntuone.api.music.model.U1Album; |
430 | 46 | import com.ubuntuone.api.music.model.U1Artist; | ||
431 | 46 | import com.ubuntuone.api.music.model.U1Meta; | 47 | import com.ubuntuone.api.music.model.U1Meta; |
432 | 47 | import com.ubuntuone.api.music.util.RequestListener.U1AlbumRequestListener; | 48 | import com.ubuntuone.api.music.util.RequestListener.U1AlbumRequestListener; |
433 | 48 | import com.ubuntuone.music.util.U1AlbumBuilder; | 49 | import com.ubuntuone.music.util.U1AlbumBuilder; |
434 | 50 | import com.ubuntuone.music.util.U1ArtistBuilder; | ||
435 | 49 | import com.ubuntuone.music.util.U1ResponseBuilder; | 51 | import com.ubuntuone.music.util.U1ResponseBuilder; |
436 | 50 | import com.ubuntuone.music.util.Util; | 52 | import com.ubuntuone.music.util.Util; |
437 | 51 | import com.ubuntuone.test.util.DummyAuthorizer; | 53 | import com.ubuntuone.test.util.DummyAuthorizer; |
438 | @@ -164,4 +166,51 @@ | |||
439 | 164 | 166 | ||
440 | 165 | musicApi.getAlbums(albumCallback); | 167 | musicApi.getAlbums(albumCallback); |
441 | 166 | } | 168 | } |
442 | 169 | |||
443 | 170 | @Test | ||
444 | 171 | public void testGetMultipleAlbumsByArtist() throws IOException { | ||
445 | 172 | final U1Meta meta = new U1Meta(); | ||
446 | 173 | final U1Album album1 = new U1AlbumBuilder() | ||
447 | 174 | .withId("albumId1").withTitle("Album 1") | ||
448 | 175 | .withArtist("Artist 1").build(); | ||
449 | 176 | final U1Album album2 = new U1AlbumBuilder() | ||
450 | 177 | .withId("albumId2").withTitle("Album 2") | ||
451 | 178 | .withArtist("Artist 2").build(); | ||
452 | 179 | final U1Album album3 = new U1AlbumBuilder() | ||
453 | 180 | .withId("albumId3").withTitle("Album 3") | ||
454 | 181 | .withArtist("Artist 3").build(); | ||
455 | 182 | |||
456 | 183 | final U1Artist artist = new U1ArtistBuilder().build(); | ||
457 | 184 | |||
458 | 185 | httpRequest = new HttpGet(String.format( | ||
459 | 186 | "https://one.ubuntu.com/music/api/2.0/albums/%s/", | ||
460 | 187 | artist.getId())); | ||
461 | 188 | |||
462 | 189 | String responseJson = U1ResponseBuilder.toJson( | ||
463 | 190 | meta, U1MusicAPI.ALBUMS, album1, album2, album3); | ||
464 | 191 | |||
465 | 192 | httpResponse.setEntity(new StringEntity(responseJson, "UTF-8")); | ||
466 | 193 | |||
467 | 194 | final Sequence requestSequence = context.sequence("lifecycle"); | ||
468 | 195 | context.checking(new Expectations() {{ | ||
469 | 196 | oneOf(albumCallback).onStart(); | ||
470 | 197 | inSequence(requestSequence); | ||
471 | 198 | |||
472 | 199 | oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest))); | ||
473 | 200 | inSequence(requestSequence); | ||
474 | 201 | will(returnValue(httpResponse)); | ||
475 | 202 | |||
476 | 203 | oneOf(albumCallback).onSuccess(with(album1)); | ||
477 | 204 | inSequence(requestSequence); | ||
478 | 205 | oneOf(albumCallback).onSuccess(with(album2)); | ||
479 | 206 | inSequence(requestSequence); | ||
480 | 207 | oneOf(albumCallback).onSuccess(with(album3)); | ||
481 | 208 | inSequence(requestSequence); | ||
482 | 209 | |||
483 | 210 | oneOf(albumCallback).onFinish(); | ||
484 | 211 | inSequence(requestSequence); | ||
485 | 212 | }}); | ||
486 | 213 | |||
487 | 214 | musicApi.getAlbumsByArtist(artist.getId(), albumCallback); | ||
488 | 215 | } | ||
489 | 167 | } | 216 | } |
490 | 168 | 217 | ||
491 | === removed file 'src/test/com/ubuntuone/api/music/PutPlaylistTest.java' | |||
492 | --- src/test/com/ubuntuone/api/music/PutPlaylistTest.java 2012-08-21 14:13:25 +0000 | |||
493 | +++ src/test/com/ubuntuone/api/music/PutPlaylistTest.java 1970-01-01 00:00:00 +0000 | |||
494 | @@ -1,153 +0,0 @@ | |||
495 | 1 | /* | ||
496 | 2 | * Ubuntu One Music Java library - communicate with Ubuntu One music API | ||
497 | 3 | * | ||
498 | 4 | * Copyright 2012 Canonical Ltd. | ||
499 | 5 | * | ||
500 | 6 | * This file is part of Ubuntu One Files Java library. | ||
501 | 7 | * | ||
502 | 8 | * This program is free software: you can redistribute it and/or modify | ||
503 | 9 | * it under the terms of the GNU Affero General Public License as | ||
504 | 10 | * published by the Free Software Foundation, either version 3 of the | ||
505 | 11 | * License, or (at your option) any later version. | ||
506 | 12 | * | ||
507 | 13 | * This program is distributed in the hope that it will be useful, | ||
508 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
509 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
510 | 16 | * GNU Affero General Public License for more details. | ||
511 | 17 | * | ||
512 | 18 | * You should have received a copy of the GNU Affero General Public License | ||
513 | 19 | * along with this program. If not, see http://www.gnu.org/licenses | ||
514 | 20 | */ | ||
515 | 21 | |||
516 | 22 | package com.ubuntuone.api.music; | ||
517 | 23 | |||
518 | 24 | import static com.ubuntuone.test.util.SameHttpRequestMatcher.sameRequest; | ||
519 | 25 | |||
520 | 26 | import java.io.IOException; | ||
521 | 27 | import java.util.ArrayList; | ||
522 | 28 | |||
523 | 29 | import org.apache.http.HttpHost; | ||
524 | 30 | import org.apache.http.HttpResponse; | ||
525 | 31 | import org.apache.http.HttpStatus; | ||
526 | 32 | import org.apache.http.client.HttpClient; | ||
527 | 33 | import org.apache.http.client.methods.HttpPost; | ||
528 | 34 | import org.apache.http.entity.StringEntity; | ||
529 | 35 | import org.jmock.Expectations; | ||
530 | 36 | import org.jmock.Mockery; | ||
531 | 37 | import org.jmock.Sequence; | ||
532 | 38 | import org.jmock.integration.junit4.JMock; | ||
533 | 39 | import org.jmock.integration.junit4.JUnit4Mockery; | ||
534 | 40 | import org.junit.Before; | ||
535 | 41 | import org.junit.Test; | ||
536 | 42 | import org.junit.runner.RunWith; | ||
537 | 43 | |||
538 | 44 | import com.ubuntuone.api.music.client.BaseClient; | ||
539 | 45 | import com.ubuntuone.api.music.client.Failure; | ||
540 | 46 | import com.ubuntuone.api.music.model.U1Meta; | ||
541 | 47 | import com.ubuntuone.api.music.model.U1PlaylistSong; | ||
542 | 48 | import com.ubuntuone.api.music.model.U1Song; | ||
543 | 49 | import com.ubuntuone.api.music.util.RequestListener.U1PlaylistSongRequestListener; | ||
544 | 50 | import com.ubuntuone.music.util.U1ResponseBuilder; | ||
545 | 51 | import com.ubuntuone.music.util.U1SongBuilder; | ||
546 | 52 | import com.ubuntuone.music.util.Util; | ||
547 | 53 | import com.ubuntuone.test.util.DummyAuthorizer; | ||
548 | 54 | |||
549 | 55 | @RunWith(JMock.class) | ||
550 | 56 | public class PutPlaylistTest | ||
551 | 57 | { | ||
552 | 58 | private final Mockery context = new JUnit4Mockery(); | ||
553 | 59 | |||
554 | 60 | private HttpResponse httpResponse; | ||
555 | 61 | |||
556 | 62 | private HttpHost httpHost = | ||
557 | 63 | new HttpHost(U1MusicAPI.RESOURCE_HOST, -1, BaseClient.HTTPS); | ||
558 | 64 | |||
559 | 65 | private HttpClient httpClient; | ||
560 | 66 | private U1MusicAPI musicApi; | ||
561 | 67 | |||
562 | 68 | private U1PlaylistSongRequestListener playlistSongCallback; | ||
563 | 69 | |||
564 | 70 | @Before | ||
565 | 71 | public void setUp() throws IOException { | ||
566 | 72 | httpResponse = Util.buildResponse(HttpStatus.SC_OK); | ||
567 | 73 | |||
568 | 74 | httpClient = context.mock(HttpClient.class); | ||
569 | 75 | musicApi = new U1MusicAPI("ua/1.0", httpClient, new DummyAuthorizer()); | ||
570 | 76 | |||
571 | 77 | playlistSongCallback = context.mock(U1PlaylistSongRequestListener.class); | ||
572 | 78 | } | ||
573 | 79 | |||
574 | 80 | @Test | ||
575 | 81 | public void testCreatePlaylistLifeCycleCallbacks() { | ||
576 | 82 | String name = "playlist name"; | ||
577 | 83 | ArrayList<U1Song> songList = new ArrayList<U1Song>(); | ||
578 | 84 | |||
579 | 85 | final Sequence requestSequence = context.sequence("lifecycle"); | ||
580 | 86 | context.checking(new Expectations() {{ | ||
581 | 87 | oneOf(playlistSongCallback).onStart(); | ||
582 | 88 | inSequence(requestSequence); | ||
583 | 89 | |||
584 | 90 | ignoring(httpClient); | ||
585 | 91 | allowing(playlistSongCallback).onSuccess(with(any(U1PlaylistSong.class))); | ||
586 | 92 | allowing(playlistSongCallback).onFailure(with(any(Failure.class))); | ||
587 | 93 | |||
588 | 94 | oneOf(playlistSongCallback).onFinish(); | ||
589 | 95 | inSequence(requestSequence); | ||
590 | 96 | }}); | ||
591 | 97 | |||
592 | 98 | musicApi.createPlaylist(name, songList, playlistSongCallback); | ||
593 | 99 | } | ||
594 | 100 | |||
595 | 101 | @Test | ||
596 | 102 | public void testCreatePlaylist() throws IOException { | ||
597 | 103 | final U1Meta meta = new U1Meta(); | ||
598 | 104 | String id = "playlistId"; | ||
599 | 105 | String name = "playlist name"; | ||
600 | 106 | ArrayList<U1Song> songList = new ArrayList<U1Song>(); | ||
601 | 107 | |||
602 | 108 | final U1Song song1 = new U1SongBuilder() | ||
603 | 109 | .withId("songId1").withAlbumId("Song album 1").build(); | ||
604 | 110 | songList.add(song1); | ||
605 | 111 | final U1PlaylistSong playlistSong1 = new U1PlaylistSong(id, name, song1); | ||
606 | 112 | |||
607 | 113 | final U1Song song2 = new U1SongBuilder() | ||
608 | 114 | .withId("songId2").withAlbumId("Song album 2").build(); | ||
609 | 115 | songList.add(song2); | ||
610 | 116 | final U1PlaylistSong playlistSong2 = new U1PlaylistSong(id, name, song2); | ||
611 | 117 | |||
612 | 118 | final U1Song song3 = new U1SongBuilder() | ||
613 | 119 | .withId("songId3").withAlbumId("Song album 3").build(); | ||
614 | 120 | songList.add(song3); | ||
615 | 121 | final U1PlaylistSong playlistSong3 = new U1PlaylistSong(id, name, song3); | ||
616 | 122 | |||
617 | 123 | final HttpPost httpRequest = new HttpPost("https://one.ubuntu.com" + | ||
618 | 124 | "/music/api/2.0/playlists/"); | ||
619 | 125 | |||
620 | 126 | String responseJson = U1ResponseBuilder.toJson( | ||
621 | 127 | meta, U1MusicAPI.SONGS, id, name, song1, song2, song3); | ||
622 | 128 | |||
623 | 129 | httpResponse.setEntity(new StringEntity(responseJson, "UTF-8")); | ||
624 | 130 | |||
625 | 131 | final Sequence requestSequence = context.sequence("lifecycle"); | ||
626 | 132 | context.checking(new Expectations() {{ | ||
627 | 133 | oneOf(playlistSongCallback).onStart(); | ||
628 | 134 | inSequence(requestSequence); | ||
629 | 135 | |||
630 | 136 | oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest))); | ||
631 | 137 | inSequence(requestSequence); | ||
632 | 138 | will(returnValue(httpResponse)); | ||
633 | 139 | |||
634 | 140 | oneOf(playlistSongCallback).onSuccess(with(playlistSong1)); | ||
635 | 141 | inSequence(requestSequence); | ||
636 | 142 | oneOf(playlistSongCallback).onSuccess(with(playlistSong2)); | ||
637 | 143 | inSequence(requestSequence); | ||
638 | 144 | oneOf(playlistSongCallback).onSuccess(with(playlistSong3)); | ||
639 | 145 | inSequence(requestSequence); | ||
640 | 146 | |||
641 | 147 | oneOf(playlistSongCallback).onFinish(); | ||
642 | 148 | inSequence(requestSequence); | ||
643 | 149 | }}); | ||
644 | 150 | |||
645 | 151 | musicApi.createPlaylist(name, songList, playlistSongCallback); | ||
646 | 152 | } | ||
647 | 153 | } | ||
648 | 154 | 0 | ||
649 | === added file 'src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java' | |||
650 | --- src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java 1970-01-01 00:00:00 +0000 | |||
651 | +++ src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java 2012-08-21 14:13:25 +0000 | |||
652 | @@ -0,0 +1,157 @@ | |||
653 | 1 | /* | ||
654 | 2 | * Ubuntu One Music Java library - communicate with Ubuntu One music API | ||
655 | 3 | * | ||
656 | 4 | * Copyright 2012 Canonical Ltd. | ||
657 | 5 | * | ||
658 | 6 | * This file is part of Ubuntu One Files Java library. | ||
659 | 7 | * | ||
660 | 8 | * This program is free software: you can redistribute it and/or modify | ||
661 | 9 | * it under the terms of the GNU Affero General Public License as | ||
662 | 10 | * published by the Free Software Foundation, either version 3 of the | ||
663 | 11 | * License, or (at your option) any later version. | ||
664 | 12 | * | ||
665 | 13 | * This program is distributed in the hope that it will be useful, | ||
666 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
667 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
668 | 16 | * GNU Affero General Public License for more details. | ||
669 | 17 | * | ||
670 | 18 | * You should have received a copy of the GNU Affero General Public License | ||
671 | 19 | * along with this program. If not, see http://www.gnu.org/licenses | ||
672 | 20 | */ | ||
673 | 21 | |||
674 | 22 | package com.ubuntuone.api.music; | ||
675 | 23 | |||
676 | 24 | import static com.ubuntuone.test.util.SameHttpRequestMatcher.sameRequest; | ||
677 | 25 | |||
678 | 26 | import java.io.IOException; | ||
679 | 27 | import java.util.ArrayList; | ||
680 | 28 | |||
681 | 29 | import org.apache.http.HttpHost; | ||
682 | 30 | import org.apache.http.HttpResponse; | ||
683 | 31 | import org.apache.http.HttpStatus; | ||
684 | 32 | import org.apache.http.client.HttpClient; | ||
685 | 33 | import org.apache.http.client.methods.HttpPost; | ||
686 | 34 | import org.apache.http.entity.StringEntity; | ||
687 | 35 | import org.jmock.Expectations; | ||
688 | 36 | import org.jmock.Mockery; | ||
689 | 37 | import org.jmock.Sequence; | ||
690 | 38 | import org.jmock.integration.junit4.JMock; | ||
691 | 39 | import org.jmock.integration.junit4.JUnit4Mockery; | ||
692 | 40 | import org.junit.Before; | ||
693 | 41 | import org.junit.Test; | ||
694 | 42 | import org.junit.runner.RunWith; | ||
695 | 43 | |||
696 | 44 | import com.ubuntuone.api.music.client.BaseClient; | ||
697 | 45 | import com.ubuntuone.api.music.client.Failure; | ||
698 | 46 | import com.ubuntuone.api.music.json.U1CustomJson; | ||
699 | 47 | import com.ubuntuone.api.music.model.U1Meta; | ||
700 | 48 | import com.ubuntuone.api.music.model.U1PlaylistSong; | ||
701 | 49 | import com.ubuntuone.api.music.model.U1Song; | ||
702 | 50 | import com.ubuntuone.api.music.util.RequestListener.U1PlaylistSongRequestListener; | ||
703 | 51 | import com.ubuntuone.music.util.U1ResponseBuilder; | ||
704 | 52 | import com.ubuntuone.music.util.U1SongBuilder; | ||
705 | 53 | import com.ubuntuone.music.util.Util; | ||
706 | 54 | import com.ubuntuone.test.util.DummyAuthorizer; | ||
707 | 55 | |||
708 | 56 | @RunWith(JMock.class) | ||
709 | 57 | public class UpdatePlaylistTest | ||
710 | 58 | { | ||
711 | 59 | private final Mockery context = new JUnit4Mockery(); | ||
712 | 60 | |||
713 | 61 | private HttpResponse httpResponse; | ||
714 | 62 | |||
715 | 63 | private HttpHost httpHost = | ||
716 | 64 | new HttpHost(U1MusicAPI.RESOURCE_HOST, -1, BaseClient.HTTPS); | ||
717 | 65 | |||
718 | 66 | private HttpClient httpClient; | ||
719 | 67 | private U1MusicAPI musicApi; | ||
720 | 68 | |||
721 | 69 | private U1PlaylistSongRequestListener playlistSongCallback; | ||
722 | 70 | |||
723 | 71 | @Before | ||
724 | 72 | public void setUp() throws IOException { | ||
725 | 73 | httpResponse = Util.buildResponse(HttpStatus.SC_OK); | ||
726 | 74 | |||
727 | 75 | httpClient = context.mock(HttpClient.class); | ||
728 | 76 | musicApi = new U1MusicAPI("ua/1.0", httpClient, new DummyAuthorizer()); | ||
729 | 77 | |||
730 | 78 | playlistSongCallback = context.mock(U1PlaylistSongRequestListener.class); | ||
731 | 79 | } | ||
732 | 80 | |||
733 | 81 | @Test | ||
734 | 82 | public void testCreatePlaylistLifeCycleCallbacks() { | ||
735 | 83 | String name = "playlist name"; | ||
736 | 84 | ArrayList<U1Song> songList = new ArrayList<U1Song>(); | ||
737 | 85 | |||
738 | 86 | final Sequence requestSequence = context.sequence("lifecycle"); | ||
739 | 87 | context.checking(new Expectations() {{ | ||
740 | 88 | oneOf(playlistSongCallback).onStart(); | ||
741 | 89 | inSequence(requestSequence); | ||
742 | 90 | |||
743 | 91 | ignoring(httpClient); | ||
744 | 92 | allowing(playlistSongCallback).onSuccess(with(any(U1PlaylistSong.class))); | ||
745 | 93 | allowing(playlistSongCallback).onFailure(with(any(Failure.class))); | ||
746 | 94 | |||
747 | 95 | oneOf(playlistSongCallback).onFinish(); | ||
748 | 96 | inSequence(requestSequence); | ||
749 | 97 | }}); | ||
750 | 98 | |||
751 | 99 | musicApi.createPlaylist(name, songList, playlistSongCallback); | ||
752 | 100 | } | ||
753 | 101 | |||
754 | 102 | @Test | ||
755 | 103 | public void testCreatePlaylist() throws IOException { | ||
756 | 104 | final U1Meta meta = new U1Meta(); | ||
757 | 105 | String id = "playlistId"; | ||
758 | 106 | String name = "playlist name"; | ||
759 | 107 | ArrayList<U1Song> songList = new ArrayList<U1Song>(); | ||
760 | 108 | |||
761 | 109 | final U1Song song1 = new U1SongBuilder() | ||
762 | 110 | .withId("songId1").withAlbumId("Song album 1").build(); | ||
763 | 111 | songList.add(song1); | ||
764 | 112 | final U1PlaylistSong playlistSong1 = new U1PlaylistSong(id, name, song1); | ||
765 | 113 | |||
766 | 114 | final U1Song song2 = new U1SongBuilder() | ||
767 | 115 | .withId("songId2").withAlbumId("Song album 2").build(); | ||
768 | 116 | songList.add(song2); | ||
769 | 117 | final U1PlaylistSong playlistSong2 = new U1PlaylistSong(id, name, song2); | ||
770 | 118 | |||
771 | 119 | final U1Song song3 = new U1SongBuilder() | ||
772 | 120 | .withId("songId3").withAlbumId("Song album 3").build(); | ||
773 | 121 | songList.add(song3); | ||
774 | 122 | final U1PlaylistSong playlistSong3 = new U1PlaylistSong(id, name, song3); | ||
775 | 123 | |||
776 | 124 | final HttpPost httpRequest = new HttpPost("https://one.ubuntu.com" + | ||
777 | 125 | "/music/api/2.0/playlists/"); | ||
778 | 126 | |||
779 | 127 | httpRequest.setEntity(new StringEntity(U1CustomJson | ||
780 | 128 | .toSongIdListJson(name, songList))); | ||
781 | 129 | |||
782 | 130 | String responseJson = U1ResponseBuilder.toJson( | ||
783 | 131 | meta, U1MusicAPI.SONGS, id, name, song1, song2, song3); | ||
784 | 132 | |||
785 | 133 | httpResponse.setEntity(new StringEntity(responseJson, "UTF-8")); | ||
786 | 134 | |||
787 | 135 | final Sequence requestSequence = context.sequence("lifecycle"); | ||
788 | 136 | context.checking(new Expectations() {{ | ||
789 | 137 | oneOf(playlistSongCallback).onStart(); | ||
790 | 138 | inSequence(requestSequence); | ||
791 | 139 | |||
792 | 140 | oneOf(httpClient).execute(with(httpHost), with(sameRequest(httpRequest))); | ||
793 | 141 | inSequence(requestSequence); | ||
794 | 142 | will(returnValue(httpResponse)); | ||
795 | 143 | |||
796 | 144 | oneOf(playlistSongCallback).onSuccess(with(playlistSong1)); | ||
797 | 145 | inSequence(requestSequence); | ||
798 | 146 | oneOf(playlistSongCallback).onSuccess(with(playlistSong2)); | ||
799 | 147 | inSequence(requestSequence); | ||
800 | 148 | oneOf(playlistSongCallback).onSuccess(with(playlistSong3)); | ||
801 | 149 | inSequence(requestSequence); | ||
802 | 150 | |||
803 | 151 | oneOf(playlistSongCallback).onFinish(); | ||
804 | 152 | inSequence(requestSequence); | ||
805 | 153 | }}); | ||
806 | 154 | |||
807 | 155 | musicApi.createPlaylist(name, songList, playlistSongCallback); | ||
808 | 156 | } | ||
809 | 157 | } | ||
810 | 0 | 158 | ||
811 | === added file 'src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java' | |||
812 | --- src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java 1970-01-01 00:00:00 +0000 | |||
813 | +++ src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java 2012-08-21 14:13:25 +0000 | |||
814 | @@ -0,0 +1,59 @@ | |||
815 | 1 | /* | ||
816 | 2 | * Ubuntu One Music Java library - communicate with Ubuntu One music API | ||
817 | 3 | * | ||
818 | 4 | * Copyright 2012 Canonical Ltd. | ||
819 | 5 | * | ||
820 | 6 | * This file is part of Ubuntu One Files Java library. | ||
821 | 7 | * | ||
822 | 8 | * This program is free software: you can redistribute it and/or modify | ||
823 | 9 | * it under the terms of the GNU Affero General Public License as | ||
824 | 10 | * published by the Free Software Foundation, either version 3 of the | ||
825 | 11 | * License, or (at your option) any later version. | ||
826 | 12 | * | ||
827 | 13 | * This program is distributed in the hope that it will be useful, | ||
828 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
829 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
830 | 16 | * GNU Affero General Public License for more details. | ||
831 | 17 | * | ||
832 | 18 | * You should have received a copy of the GNU Affero General Public License | ||
833 | 19 | * along with this program. If not, see http://www.gnu.org/licenses | ||
834 | 20 | */ | ||
835 | 21 | |||
836 | 22 | package com.ubuntuone.api.music.json; | ||
837 | 23 | |||
838 | 24 | import static junit.framework.Assert.assertEquals; | ||
839 | 25 | |||
840 | 26 | import java.io.IOException; | ||
841 | 27 | import java.util.ArrayList; | ||
842 | 28 | |||
843 | 29 | import org.junit.Test; | ||
844 | 30 | |||
845 | 31 | import com.ubuntuone.api.music.model.U1Song; | ||
846 | 32 | import com.ubuntuone.music.util.U1SongBuilder; | ||
847 | 33 | |||
848 | 34 | public class U1CustomJsonTest | ||
849 | 35 | { | ||
850 | 36 | @Test | ||
851 | 37 | public void testEncodeU1SongIdListPlaylist() throws IOException { | ||
852 | 38 | final ArrayList<U1Song> songList = new ArrayList<U1Song>(); | ||
853 | 39 | |||
854 | 40 | final U1Song song1 = new U1SongBuilder() | ||
855 | 41 | .withId("id1").build(); | ||
856 | 42 | songList.add(song1); | ||
857 | 43 | final U1Song song2 = new U1SongBuilder() | ||
858 | 44 | .withId("id2").build(); | ||
859 | 45 | songList.add(song2); | ||
860 | 46 | |||
861 | 47 | String name = "the playlist name"; | ||
862 | 48 | |||
863 | 49 | String json = U1CustomJson.toSongIdListJson(name, songList); | ||
864 | 50 | |||
865 | 51 | String expectedJson = "{\"name\":\"" + name + "\"," + | ||
866 | 52 | "\"song_id_list\":[" + | ||
867 | 53 | "\"" + song1.getId() + "\"," + | ||
868 | 54 | "\"" + song2.getId() + "\"" + | ||
869 | 55 | "]}"; | ||
870 | 56 | |||
871 | 57 | assertEquals(expectedJson, json); | ||
872 | 58 | } | ||
873 | 59 | } | ||
874 | 0 | 60 | ||
875 | === modified file 'src/test/com/ubuntuone/test/util/SameHttpRequestMatcher.java' | |||
876 | --- src/test/com/ubuntuone/test/util/SameHttpRequestMatcher.java 2012-08-21 14:13:25 +0000 | |||
877 | +++ src/test/com/ubuntuone/test/util/SameHttpRequestMatcher.java 2012-08-21 14:13:25 +0000 | |||
878 | @@ -21,7 +21,14 @@ | |||
879 | 21 | 21 | ||
880 | 22 | package com.ubuntuone.test.util; | 22 | package com.ubuntuone.test.util; |
881 | 23 | 23 | ||
882 | 24 | import java.io.IOException; | ||
883 | 25 | import java.io.InputStream; | ||
884 | 26 | |||
885 | 27 | import org.apache.http.ParseException; | ||
886 | 28 | import org.apache.http.client.methods.HttpPost; | ||
887 | 29 | import org.apache.http.client.methods.HttpPut; | ||
888 | 24 | import org.apache.http.client.methods.HttpUriRequest; | 30 | import org.apache.http.client.methods.HttpUriRequest; |
889 | 31 | import org.apache.http.util.EntityUtils; | ||
890 | 25 | import org.hamcrest.Description; | 32 | import org.hamcrest.Description; |
891 | 26 | import org.hamcrest.Factory; | 33 | import org.hamcrest.Factory; |
892 | 27 | import org.hamcrest.Matcher; | 34 | import org.hamcrest.Matcher; |
893 | @@ -40,6 +47,31 @@ | |||
894 | 40 | public void describeTo(Description description) { | 47 | public void describeTo(Description description) { |
895 | 41 | description.appendText("a request with request line ") | 48 | description.appendText("a request with request line ") |
896 | 42 | .appendValue(request.getRequestLine()); | 49 | .appendValue(request.getRequestLine()); |
897 | 50 | if (request.getClass() == HttpPost.class) { | ||
898 | 51 | HttpPost post = (HttpPost) request; | ||
899 | 52 | if (post.getEntity() != null) { | ||
900 | 53 | try { | ||
901 | 54 | description.appendText("and POST body ") | ||
902 | 55 | .appendValue(EntityUtils.toString(post.getEntity())); | ||
903 | 56 | } catch (ParseException e) { | ||
904 | 57 | description.appendText("and broken POST body!"); | ||
905 | 58 | } catch (IOException e) { | ||
906 | 59 | description.appendText("and broken POST body!"); | ||
907 | 60 | } | ||
908 | 61 | } | ||
909 | 62 | } else if (request.getClass() == HttpPut.class) { | ||
910 | 63 | HttpPut put = (HttpPut) request; | ||
911 | 64 | if (put.getEntity() != null) { | ||
912 | 65 | try { | ||
913 | 66 | description.appendText("and PUT body ") | ||
914 | 67 | .appendValue(EntityUtils.toString(put.getEntity())); | ||
915 | 68 | } catch (ParseException e) { | ||
916 | 69 | description.appendText("and broken PUT body "); | ||
917 | 70 | } catch (IOException e) { | ||
918 | 71 | description.appendText("and broken PUT body "); | ||
919 | 72 | } | ||
920 | 73 | } | ||
921 | 74 | } | ||
922 | 43 | } | 75 | } |
923 | 44 | 76 | ||
924 | 45 | @Override | 77 | @Override |
925 | @@ -48,8 +80,47 @@ | |||
926 | 48 | return false; | 80 | return false; |
927 | 49 | if (item == request) | 81 | if (item == request) |
928 | 50 | return true; | 82 | return true; |
929 | 83 | boolean sameBody = true; | ||
930 | 84 | if (request.getClass() == HttpPost.class) { | ||
931 | 85 | HttpPost postRequest = (HttpPost) request; | ||
932 | 86 | HttpPost postItem = (HttpPost) item; | ||
933 | 87 | if (postRequest.getEntity() != null) { | ||
934 | 88 | try { | ||
935 | 89 | InputStream in1 = postRequest.getEntity().getContent(); | ||
936 | 90 | if (postItem.getEntity() != null) { | ||
937 | 91 | InputStream in2 = postItem.getEntity().getContent(); | ||
938 | 92 | sameBody = StreamUtils.streamsHaveSameContent(in1, in2); | ||
939 | 93 | } else { | ||
940 | 94 | sameBody = false; | ||
941 | 95 | } | ||
942 | 96 | } catch (IllegalStateException e) { | ||
943 | 97 | return false; | ||
944 | 98 | } catch (IOException e) { | ||
945 | 99 | return false; | ||
946 | 100 | } | ||
947 | 101 | } | ||
948 | 102 | } else if (request.getClass() == HttpPut.class) { | ||
949 | 103 | HttpPut putRequest = (HttpPut) request; | ||
950 | 104 | HttpPut putItem = (HttpPut) item; | ||
951 | 105 | if (putRequest.getEntity() != null) { | ||
952 | 106 | try { | ||
953 | 107 | InputStream in1 = putRequest.getEntity().getContent(); | ||
954 | 108 | if (putItem.getEntity() != null) { | ||
955 | 109 | InputStream in2 = putItem.getEntity().getContent(); | ||
956 | 110 | sameBody = StreamUtils.streamsHaveSameContent(in1, in2); | ||
957 | 111 | } else { | ||
958 | 112 | sameBody = false; | ||
959 | 113 | } | ||
960 | 114 | } catch (IllegalStateException e) { | ||
961 | 115 | return false; | ||
962 | 116 | } catch (IOException e) { | ||
963 | 117 | return false; | ||
964 | 118 | } | ||
965 | 119 | } | ||
966 | 120 | } | ||
967 | 51 | return request.getMethod().equals(item.getMethod()) | 121 | return request.getMethod().equals(item.getMethod()) |
969 | 52 | && request.getURI().equals(item.getURI()); | 122 | && request.getURI().equals(item.getURI()) |
970 | 123 | && sameBody; | ||
971 | 53 | } | 124 | } |
972 | 54 | 125 | ||
973 | 55 | @Factory | 126 | @Factory |
974 | 56 | 127 | ||
975 | === added file 'src/test/com/ubuntuone/test/util/StreamUtils.java' | |||
976 | --- src/test/com/ubuntuone/test/util/StreamUtils.java 1970-01-01 00:00:00 +0000 | |||
977 | +++ src/test/com/ubuntuone/test/util/StreamUtils.java 2012-08-21 14:13:25 +0000 | |||
978 | @@ -0,0 +1,27 @@ | |||
979 | 1 | package com.ubuntuone.test.util; | ||
980 | 2 | |||
981 | 3 | import java.io.IOException; | ||
982 | 4 | import java.io.InputStream; | ||
983 | 5 | |||
984 | 6 | public class StreamUtils | ||
985 | 7 | { | ||
986 | 8 | public static boolean streamsHaveSameContent(InputStream in1, InputStream in2) { | ||
987 | 9 | boolean same = true; | ||
988 | 10 | int b1, b2; | ||
989 | 11 | try { | ||
990 | 12 | while ((b1 = in1.read()) != -1) { | ||
991 | 13 | b2 = in2.read(); | ||
992 | 14 | if (b1 != b2) { | ||
993 | 15 | same = false; | ||
994 | 16 | break; | ||
995 | 17 | } | ||
996 | 18 | } | ||
997 | 19 | if (in2.read() != -1) { | ||
998 | 20 | same = false; | ||
999 | 21 | } | ||
1000 | 22 | } catch (IOException e) { | ||
1001 | 23 | same = false; | ||
1002 | 24 | } | ||
1003 | 25 | return same; | ||
1004 | 26 | } | ||
1005 | 27 | } |