Merge lp:~ubuntuone-client-engineering/ubuntuone-music-java-library/playlist-args-and-request-refactor into lp:ubuntuone-music-java-library

Proposed by Michał Karnicki on 2012-11-08
Status: Merged
Merged at revision: 31
Proposed branch: lp:~ubuntuone-client-engineering/ubuntuone-music-java-library/playlist-args-and-request-refactor
Merge into: lp:ubuntuone-music-java-library
Diff against target: 860 lines (+241/-149)
33 files modified
src/main/com/ubuntuone/api/music/U1MusicAPI.java (+12/-12)
src/main/com/ubuntuone/api/music/json/U1AlbumJson.java (+1/-1)
src/main/com/ubuntuone/api/music/json/U1ArtistJson.java (+1/-1)
src/main/com/ubuntuone/api/music/json/U1CustomJson.java (+23/-9)
src/main/com/ubuntuone/api/music/json/U1PlaylistJson.java (+1/-1)
src/main/com/ubuntuone/api/music/json/U1SongJson.java (+1/-1)
src/main/com/ubuntuone/api/music/request/U1AlbumListener.java (+1/-1)
src/main/com/ubuntuone/api/music/request/U1ArtListener.java (+1/-1)
src/main/com/ubuntuone/api/music/request/U1ArtistListener.java (+1/-1)
src/main/com/ubuntuone/api/music/request/U1CallbackListener.java (+47/-0)
src/main/com/ubuntuone/api/music/request/U1DownloadListener.java (+2/-2)
src/main/com/ubuntuone/api/music/request/U1PlaylistListener.java (+1/-1)
src/main/com/ubuntuone/api/music/request/U1RequestListener.java (+0/-47)
src/main/com/ubuntuone/api/music/request/U1SongListener.java (+1/-1)
src/main/com/ubuntuone/api/music/util/FailureListener.java (+18/-0)
src/main/com/ubuntuone/api/music/util/RequestListener.java (+6/-39)
src/main/com/ubuntuone/api/music/util/ResultRequestListener.java (+67/-0)
src/test/com/ubuntuone/api/music/CreatePlaylistTest.java (+3/-3)
src/test/com/ubuntuone/api/music/DeletePlaylistTest.java (+5/-5)
src/test/com/ubuntuone/api/music/GetAlbumsTest.java (+1/-1)
src/test/com/ubuntuone/api/music/GetArtTest.java (+1/-1)
src/test/com/ubuntuone/api/music/GetArtistsTest.java (+1/-1)
src/test/com/ubuntuone/api/music/GetPlaylistTest.java (+1/-1)
src/test/com/ubuntuone/api/music/GetPlaylistsTest.java (+1/-1)
src/test/com/ubuntuone/api/music/GetSongStreamTest.java (+1/-1)
src/test/com/ubuntuone/api/music/GetSongsTest.java (+1/-1)
src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java (+2/-2)
src/test/com/ubuntuone/api/music/json/U1AlbumJsonTest.java (+1/-1)
src/test/com/ubuntuone/api/music/json/U1ArtistJsonTest.java (+1/-1)
src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java (+26/-2)
src/test/com/ubuntuone/api/music/json/U1PlaylistJsonTest.java (+1/-1)
src/test/com/ubuntuone/api/music/json/U1SongJsonTest.java (+1/-1)
src/test/com/ubuntuone/music/util/U1ResponseBuilder.java (+10/-8)
To merge this branch: bzr merge lp:~ubuntuone-client-engineering/ubuntuone-music-java-library/playlist-args-and-request-refactor
Reviewer Review Type Date Requested Status
Brian Curtin (community) 2012-11-08 Approve on 2012-11-08
Review via email: mp+133587@code.launchpad.net

Commit message

Refactor RequestListener, so that ResultRequestListener is a parametrized class, while ResultListener has a no-arg onSuccess callback method.

Description of the change

When PUTing playlist to music API v2, name and song_id_list in request JSON body should be optional (at least one of them should be provided), so that it is easy to rename the playlist and update song list without touching its name. Updated JSON generating method, with tests.

Refactored RequestListener class to ResultRequestListener, which has parametrized type argument of onSuccess(.) callback, and introduced RequestListener, which has no argument in onSuccess() callback, for better code readability.

To test, branch and run:
$ ant test

To post a comment you must log in.
Brian Curtin (brian.curtin) wrote :

Without being able to run it, the code reads fine given what I've been told about it.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/main/com/ubuntuone/api/music/U1MusicAPI.java'
2--- src/main/com/ubuntuone/api/music/U1MusicAPI.java 2012-11-08 16:22:16 +0000
3+++ src/main/com/ubuntuone/api/music/U1MusicAPI.java 2012-11-08 22:55:22 +0000
4@@ -57,17 +57,17 @@
5 import com.ubuntuone.api.music.util.CancelTrigger;
6 import com.ubuntuone.api.music.util.CancelTrigger.RequestCanceledException;
7 import com.ubuntuone.api.music.util.EntityUtils;
8+import com.ubuntuone.api.music.util.FailureListener;
9 import com.ubuntuone.api.music.util.OnProgressListener;
10 import com.ubuntuone.api.music.util.OnProgressListener.ProgressInputStream;
11-import com.ubuntuone.api.music.util.RequestListener;
12-import com.ubuntuone.api.music.util.RequestListener.U1AlbumRequestListener;
13-import com.ubuntuone.api.music.util.RequestListener.U1ArtRequestListener;
14-import com.ubuntuone.api.music.util.RequestListener.U1ArtistRequestListener;
15-import com.ubuntuone.api.music.util.RequestListener.U1PlaylistRequestListener;
16-import com.ubuntuone.api.music.util.RequestListener.U1SongRequestListener;
17-import com.ubuntuone.api.music.util.RequestListener.U1StreamRequestListener;
18-import com.ubuntuone.api.music.util.RequestListener.U1VoidRequestListener;
19+import com.ubuntuone.api.music.util.RequestListener.U1RequestListener;
20 import com.ubuntuone.api.music.util.ResponseHeader;
21+import com.ubuntuone.api.music.util.ResultRequestListener.U1AlbumRequestListener;
22+import com.ubuntuone.api.music.util.ResultRequestListener.U1ArtRequestListener;
23+import com.ubuntuone.api.music.util.ResultRequestListener.U1ArtistRequestListener;
24+import com.ubuntuone.api.music.util.ResultRequestListener.U1PlaylistRequestListener;
25+import com.ubuntuone.api.music.util.ResultRequestListener.U1SongRequestListener;
26+import com.ubuntuone.api.music.util.ResultRequestListener.U1StreamRequestListener;
27 import com.ubuntuone.api.music.util.StreamUtils;
28 import com.ubuntuone.api.sso.authorizer.Authorizer;
29 import com.ubuntuone.api.sso.authorizer.AuthorizerException;
30@@ -402,7 +402,7 @@
31 }
32 HttpResponse response = null;
33 try {
34- String data = U1CustomJson.toSongIdListJson(name, songIds);
35+ String data = U1CustomJson.toPlaylistJson(name, songIds);
36 response = resourceClient.request(HttpPost.METHOD_NAME, path, null, data);
37 final int statusCode = getStatusCode(response);
38 if (statusCode == HttpStatus.SC_OK) {
39@@ -436,7 +436,7 @@
40 }
41 }
42
43- public void deletePlaylist(String playlistId, U1VoidRequestListener callback) {
44+ public void deletePlaylist(String playlistId, U1RequestListener callback) {
45 callback.onStart();
46 String path = resourceClient.getPath(PLAYLISTS, playlistId);
47 HttpResponse response = null;
48@@ -444,7 +444,7 @@
49 response = resourceClient.request(HttpDelete.METHOD_NAME, path);
50 final int statusCode = getStatusCode(response);
51 if (statusCode == HttpStatus.SC_OK) {
52- callback.onSuccess(null);
53+ callback.onSuccess();
54 } else {
55 handleHttpResponse(response, "Could not delete playlist.", callback);
56 }
57@@ -608,7 +608,7 @@
58 }
59
60 private void handleHttpResponse(HttpResponse response,
61- String message, RequestListener<?> callback) {
62+ String message, FailureListener callback) {
63 final int statusCode = getStatusCode(response);
64
65 switch (statusCode) {
66
67=== modified file 'src/main/com/ubuntuone/api/music/json/U1AlbumJson.java'
68--- src/main/com/ubuntuone/api/music/json/U1AlbumJson.java 2012-09-04 21:42:33 +0000
69+++ src/main/com/ubuntuone/api/music/json/U1AlbumJson.java 2012-11-08 22:55:22 +0000
70@@ -34,7 +34,7 @@
71
72 import com.ubuntuone.api.music.U1MusicAPI;
73 import com.ubuntuone.api.music.model.U1Album;
74-import com.ubuntuone.api.music.util.RequestListener.U1AlbumRequestListener;
75+import com.ubuntuone.api.music.util.ResultRequestListener.U1AlbumRequestListener;
76
77 public class U1AlbumJson
78 {
79
80=== modified file 'src/main/com/ubuntuone/api/music/json/U1ArtistJson.java'
81--- src/main/com/ubuntuone/api/music/json/U1ArtistJson.java 2012-09-04 21:42:33 +0000
82+++ src/main/com/ubuntuone/api/music/json/U1ArtistJson.java 2012-11-08 22:55:22 +0000
83@@ -35,7 +35,7 @@
84
85 import com.ubuntuone.api.music.U1MusicAPI;
86 import com.ubuntuone.api.music.model.U1Artist;
87-import com.ubuntuone.api.music.util.RequestListener.U1ArtistRequestListener;
88+import com.ubuntuone.api.music.util.ResultRequestListener.U1ArtistRequestListener;
89
90 public class U1ArtistJson
91 {
92
93=== modified file 'src/main/com/ubuntuone/api/music/json/U1CustomJson.java'
94--- src/main/com/ubuntuone/api/music/json/U1CustomJson.java 2012-09-19 19:17:30 +0000
95+++ src/main/com/ubuntuone/api/music/json/U1CustomJson.java 2012-11-08 22:55:22 +0000
96@@ -30,22 +30,36 @@
97
98 public class U1CustomJson
99 {
100- public static String toSongIdListJson(String name, ArrayList<String> songIds)
101- throws IOException {
102+ /**
103+ * Either name or songIds has to be provided. Otherwise, request with such
104+ * body will fail.
105+ *
106+ * @param name
107+ * The name of the playlist.
108+ * @param songIdList
109+ * The song id list of the playlist.
110+ * @return Request JSON body.
111+ * @throws IOException
112+ * If JSON generation fails with IOException.
113+ */
114+ public static String toPlaylistJson(String name,
115+ ArrayList<String> songIdList) throws IOException {
116 final JsonFactory factory = new JsonFactory();
117 final StringWriter writer = new StringWriter(128);
118 final JsonGenerator g = factory.createJsonGenerator(writer);
119
120 g.writeStartObject();
121- g.writeStringField("name", name);
122-
123- g.writeArrayFieldStart("song_id_list"); {
124- for (String songId: songIds) {
125- g.writeString(songId);
126+ if (name != null) {
127+ g.writeStringField("name", name);
128+ }
129+ if (songIdList != null) {
130+ g.writeArrayFieldStart("song_id_list"); {
131+ for (String songId: songIdList) {
132+ g.writeString(songId);
133+ }
134 }
135+ g.writeEndArray();
136 }
137- g.writeEndArray();
138-
139 g.writeEndObject();
140 g.close();
141
142
143=== modified file 'src/main/com/ubuntuone/api/music/json/U1PlaylistJson.java'
144--- src/main/com/ubuntuone/api/music/json/U1PlaylistJson.java 2012-09-04 21:42:33 +0000
145+++ src/main/com/ubuntuone/api/music/json/U1PlaylistJson.java 2012-11-08 22:55:22 +0000
146@@ -35,7 +35,7 @@
147
148 import com.ubuntuone.api.music.U1MusicAPI;
149 import com.ubuntuone.api.music.model.U1Playlist;
150-import com.ubuntuone.api.music.util.RequestListener.U1PlaylistRequestListener;
151+import com.ubuntuone.api.music.util.ResultRequestListener.U1PlaylistRequestListener;
152
153 public class U1PlaylistJson
154 {
155
156=== modified file 'src/main/com/ubuntuone/api/music/json/U1SongJson.java'
157--- src/main/com/ubuntuone/api/music/json/U1SongJson.java 2012-09-04 21:42:33 +0000
158+++ src/main/com/ubuntuone/api/music/json/U1SongJson.java 2012-11-08 22:55:22 +0000
159@@ -34,7 +34,7 @@
160
161 import com.ubuntuone.api.music.U1MusicAPI;
162 import com.ubuntuone.api.music.model.U1Song;
163-import com.ubuntuone.api.music.util.RequestListener.U1SongRequestListener;
164+import com.ubuntuone.api.music.util.ResultRequestListener.U1SongRequestListener;
165
166 public class U1SongJson
167 {
168
169=== modified file 'src/main/com/ubuntuone/api/music/request/U1AlbumListener.java'
170--- src/main/com/ubuntuone/api/music/request/U1AlbumListener.java 2012-06-07 01:31:25 +0000
171+++ src/main/com/ubuntuone/api/music/request/U1AlbumListener.java 2012-11-08 22:55:22 +0000
172@@ -23,7 +23,7 @@
173
174 import com.ubuntuone.api.music.client.Failure;
175 import com.ubuntuone.api.music.model.U1Album;
176-import com.ubuntuone.api.music.util.RequestListener.U1AlbumRequestListener;
177+import com.ubuntuone.api.music.util.ResultRequestListener.U1AlbumRequestListener;
178
179 public abstract class U1AlbumListener implements U1AlbumRequestListener
180 {
181
182=== modified file 'src/main/com/ubuntuone/api/music/request/U1ArtListener.java'
183--- src/main/com/ubuntuone/api/music/request/U1ArtListener.java 2012-06-18 04:19:36 +0000
184+++ src/main/com/ubuntuone/api/music/request/U1ArtListener.java 2012-11-08 22:55:22 +0000
185@@ -24,7 +24,7 @@
186 import java.io.File;
187
188 import com.ubuntuone.api.music.client.Failure;
189-import com.ubuntuone.api.music.util.RequestListener.U1ArtRequestListener;
190+import com.ubuntuone.api.music.util.ResultRequestListener.U1ArtRequestListener;
191
192 public abstract class U1ArtListener implements U1ArtRequestListener
193 {
194
195=== modified file 'src/main/com/ubuntuone/api/music/request/U1ArtistListener.java'
196--- src/main/com/ubuntuone/api/music/request/U1ArtistListener.java 2012-06-07 01:31:25 +0000
197+++ src/main/com/ubuntuone/api/music/request/U1ArtistListener.java 2012-11-08 22:55:22 +0000
198@@ -23,7 +23,7 @@
199
200 import com.ubuntuone.api.music.client.Failure;
201 import com.ubuntuone.api.music.model.U1Artist;
202-import com.ubuntuone.api.music.util.RequestListener.U1ArtistRequestListener;
203+import com.ubuntuone.api.music.util.ResultRequestListener.U1ArtistRequestListener;
204
205 public abstract class U1ArtistListener implements U1ArtistRequestListener
206 {
207
208=== added file 'src/main/com/ubuntuone/api/music/request/U1CallbackListener.java'
209--- src/main/com/ubuntuone/api/music/request/U1CallbackListener.java 1970-01-01 00:00:00 +0000
210+++ src/main/com/ubuntuone/api/music/request/U1CallbackListener.java 2012-11-08 22:55:22 +0000
211@@ -0,0 +1,47 @@
212+/*
213+ * Ubuntu One Music Java library - communicate with Ubuntu One music API
214+ *
215+ * Copyright 2012 Canonical Ltd.
216+ *
217+ * This file is part of Ubuntu One Files Java library.
218+ *
219+ * This program is free software: you can redistribute it and/or modify
220+ * it under the terms of the GNU Affero General Public License as
221+ * published by the Free Software Foundation, either version 3 of the
222+ * License, or (at your option) any later version.
223+ *
224+ * This program is distributed in the hope that it will be useful,
225+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
226+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
227+ * GNU Affero General Public License for more details.
228+ *
229+ * You should have received a copy of the GNU Affero General Public License
230+ * along with this program. If not, see http://www.gnu.org/licenses
231+ */
232+
233+package com.ubuntuone.api.music.request;
234+
235+import com.ubuntuone.api.music.client.Failure;
236+import com.ubuntuone.api.music.util.RequestListener.U1RequestListener;
237+
238+/**
239+ * Class used for callbacks from methods, that do not return result
240+ * in onSuccess(), but simply call it to indicate success.
241+ */
242+public abstract class U1CallbackListener implements U1RequestListener
243+{
244+ @Override
245+ public void onStart() {
246+ }
247+
248+ @Override
249+ public abstract void onSuccess();
250+
251+ @Override
252+ public void onFailure(Failure failure) {
253+ }
254+
255+ @Override
256+ public void onFinish() {
257+ }
258+}
259
260=== modified file 'src/main/com/ubuntuone/api/music/request/U1DownloadListener.java'
261--- src/main/com/ubuntuone/api/music/request/U1DownloadListener.java 2012-06-19 01:05:51 +0000
262+++ src/main/com/ubuntuone/api/music/request/U1DownloadListener.java 2012-11-08 22:55:22 +0000
263@@ -24,9 +24,9 @@
264 import com.ubuntuone.api.music.client.Failure;
265 import com.ubuntuone.api.music.util.OnCancelListener;
266 import com.ubuntuone.api.music.util.OnProgressListener;
267-import com.ubuntuone.api.music.util.RequestListener;
268+import com.ubuntuone.api.music.util.ResultRequestListener;
269
270-public abstract class U1DownloadListener implements RequestListener<Boolean>,
271+public abstract class U1DownloadListener implements ResultRequestListener<Boolean>,
272 OnProgressListener, OnCancelListener
273 {
274 @Override
275
276=== modified file 'src/main/com/ubuntuone/api/music/request/U1PlaylistListener.java'
277--- src/main/com/ubuntuone/api/music/request/U1PlaylistListener.java 2012-06-07 01:31:25 +0000
278+++ src/main/com/ubuntuone/api/music/request/U1PlaylistListener.java 2012-11-08 22:55:22 +0000
279@@ -23,7 +23,7 @@
280
281 import com.ubuntuone.api.music.client.Failure;
282 import com.ubuntuone.api.music.model.U1Playlist;
283-import com.ubuntuone.api.music.util.RequestListener.U1PlaylistRequestListener;
284+import com.ubuntuone.api.music.util.ResultRequestListener.U1PlaylistRequestListener;
285
286 public abstract class U1PlaylistListener implements U1PlaylistRequestListener
287 {
288
289=== removed file 'src/main/com/ubuntuone/api/music/request/U1RequestListener.java'
290--- src/main/com/ubuntuone/api/music/request/U1RequestListener.java 2012-06-18 03:10:10 +0000
291+++ src/main/com/ubuntuone/api/music/request/U1RequestListener.java 1970-01-01 00:00:00 +0000
292@@ -1,47 +0,0 @@
293-/*
294- * Ubuntu One Music Java library - communicate with Ubuntu One music API
295- *
296- * Copyright 2012 Canonical Ltd.
297- *
298- * This file is part of Ubuntu One Files Java library.
299- *
300- * This program is free software: you can redistribute it and/or modify
301- * it under the terms of the GNU Affero General Public License as
302- * published by the Free Software Foundation, either version 3 of the
303- * License, or (at your option) any later version.
304- *
305- * This program is distributed in the hope that it will be useful,
306- * but WITHOUT ANY WARRANTY; without even the implied warranty of
307- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
308- * GNU Affero General Public License for more details.
309- *
310- * You should have received a copy of the GNU Affero General Public License
311- * along with this program. If not, see http://www.gnu.org/licenses
312- */
313-
314-package com.ubuntuone.api.music.request;
315-
316-import com.ubuntuone.api.music.client.Failure;
317-import com.ubuntuone.api.music.util.RequestListener.U1VoidRequestListener;
318-
319-/**
320- * Class used for callbacks from methods, that do not return result
321- * in onSuccess(), but simply call it to indicate success.
322- */
323-public abstract class U1RequestListener implements U1VoidRequestListener
324-{
325- @Override
326- public void onStart() {
327- }
328-
329- @Override
330- public abstract void onSuccess(Void v);
331-
332- @Override
333- public void onFailure(Failure failure) {
334- }
335-
336- @Override
337- public void onFinish() {
338- }
339-}
340
341=== modified file 'src/main/com/ubuntuone/api/music/request/U1SongListener.java'
342--- src/main/com/ubuntuone/api/music/request/U1SongListener.java 2012-06-07 01:31:25 +0000
343+++ src/main/com/ubuntuone/api/music/request/U1SongListener.java 2012-11-08 22:55:22 +0000
344@@ -23,7 +23,7 @@
345
346 import com.ubuntuone.api.music.client.Failure;
347 import com.ubuntuone.api.music.model.U1Song;
348-import com.ubuntuone.api.music.util.RequestListener.U1SongRequestListener;
349+import com.ubuntuone.api.music.util.ResultRequestListener.U1SongRequestListener;
350
351 public abstract class U1SongListener implements U1SongRequestListener
352 {
353
354=== added file 'src/main/com/ubuntuone/api/music/util/FailureListener.java'
355--- src/main/com/ubuntuone/api/music/util/FailureListener.java 1970-01-01 00:00:00 +0000
356+++ src/main/com/ubuntuone/api/music/util/FailureListener.java 2012-11-08 22:55:22 +0000
357@@ -0,0 +1,18 @@
358+package com.ubuntuone.api.music.util;
359+
360+import org.apache.http.HttpStatus;
361+
362+import com.ubuntuone.api.music.client.Failure;
363+
364+public abstract interface FailureListener {
365+ /**
366+ * Called when the request has failed. The message should be descriptive
367+ * in most cases and suitable to display to the user.
368+ * {@link Failure#getStatusCode} field can be compared with
369+ * {@link HttpStatus} constants for further details.
370+ *
371+ * @param failure
372+ * The {@link Failure} returned.
373+ */
374+ public void onFailure(Failure failure);
375+}
376
377=== modified file 'src/main/com/ubuntuone/api/music/util/RequestListener.java'
378--- src/main/com/ubuntuone/api/music/util/RequestListener.java 2012-08-27 17:20:36 +0000
379+++ src/main/com/ubuntuone/api/music/util/RequestListener.java 2012-11-08 22:55:22 +0000
380@@ -21,24 +21,11 @@
381
382 package com.ubuntuone.api.music.util;
383
384-import java.io.File;
385-import java.io.InputStream;
386-
387-import org.apache.http.HttpStatus;
388-
389-import com.ubuntuone.api.music.client.Failure;
390-import com.ubuntuone.api.music.model.U1Album;
391-import com.ubuntuone.api.music.model.U1Artist;
392-import com.ubuntuone.api.music.model.U1Playlist;
393-import com.ubuntuone.api.music.model.U1Song;
394
395 /**
396 * Request listener used to track U1 API method execution.
397- *
398- * @param <T>
399- * The type of result returned in {@link RequestListener#onSuccess(T)}.
400 */
401-public interface RequestListener<T>
402+public interface RequestListener extends FailureListener
403 {
404 /**
405 * Called when the request processing has started.
406@@ -47,34 +34,14 @@
407
408 /**
409 * Called when the request has successfully completed.
410- *
411- * @param result
412- * The request result of type T.
413- */
414- public void onSuccess(T result);
415-
416- /**
417- * Called when the request has failed. The message should be descriptive
418- * in most cases and suitable to display to the user.
419- * {@link Failure#getStatusCode} field can be compared with
420- * {@link HttpStatus} constants for further details.
421- *
422- * @param failure
423- * The {@link Failure} returned.
424- */
425- public void onFailure(Failure failure);
426+ */
427+ public void onSuccess();
428
429 /**
430 * Called when the request processing has finished.
431 */
432 public void onFinish();
433
434- public interface U1ArtistRequestListener extends RequestListener<U1Artist> {};
435- public interface U1AlbumRequestListener extends RequestListener<U1Album> {};
436- public interface U1SongRequestListener extends RequestListener<U1Song> {};
437- public interface U1PlaylistRequestListener extends RequestListener<U1Playlist> {};
438- public interface U1PlaylistSongRequestListener extends RequestListener<U1Song> {};
439- public interface U1ArtRequestListener extends RequestListener<File> {};
440- public interface U1VoidRequestListener extends RequestListener<Void> {};
441- public interface U1StreamRequestListener extends RequestListener<InputStream> {};
442-}
443\ No newline at end of file
444+
445+ public interface U1RequestListener extends RequestListener {};
446+}
447
448=== added file 'src/main/com/ubuntuone/api/music/util/ResultRequestListener.java'
449--- src/main/com/ubuntuone/api/music/util/ResultRequestListener.java 1970-01-01 00:00:00 +0000
450+++ src/main/com/ubuntuone/api/music/util/ResultRequestListener.java 2012-11-08 22:55:22 +0000
451@@ -0,0 +1,67 @@
452+/*
453+ * Ubuntu One Music Java library - communicate with Ubuntu One music API
454+ *
455+ * Copyright 2012 Canonical Ltd.
456+ *
457+ * This file is part of Ubuntu One Files Java library.
458+ *
459+ * This program is free software: you can redistribute it and/or modify
460+ * it under the terms of the GNU Affero General Public License as
461+ * published by the Free Software Foundation, either version 3 of the
462+ * License, or (at your option) any later version.
463+ *
464+ * This program is distributed in the hope that it will be useful,
465+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
466+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
467+ * GNU Affero General Public License for more details.
468+ *
469+ * You should have received a copy of the GNU Affero General Public License
470+ * along with this program. If not, see http://www.gnu.org/licenses
471+ */
472+
473+package com.ubuntuone.api.music.util;
474+
475+import java.io.File;
476+import java.io.InputStream;
477+
478+import com.ubuntuone.api.music.model.U1Album;
479+import com.ubuntuone.api.music.model.U1Artist;
480+import com.ubuntuone.api.music.model.U1Playlist;
481+import com.ubuntuone.api.music.model.U1Song;
482+
483+/**
484+ * Request listener used to track U1 API method execution.
485+ *
486+ * @param <T>
487+ * The type of result returned in {@link ResultRequestListener#onSuccess(T)}.
488+ */
489+public interface ResultRequestListener<T> extends FailureListener
490+{
491+ /**
492+ * Called when the request processing has started.
493+ */
494+ public void onStart();
495+
496+ /**
497+ * Called when the request has successfully completed.
498+ *
499+ * @param result
500+ * The request result of type T.
501+ */
502+ public void onSuccess(T result);
503+
504+ /**
505+ * Called when the request processing has finished.
506+ */
507+ public void onFinish();
508+
509+ public interface U1ArtistRequestListener extends ResultRequestListener<U1Artist> {};
510+ public interface U1AlbumRequestListener extends ResultRequestListener<U1Album> {};
511+ public interface U1SongRequestListener extends ResultRequestListener<U1Song> {};
512+
513+ public interface U1PlaylistRequestListener extends ResultRequestListener<U1Playlist> {};
514+ public interface U1PlaylistSongRequestListener extends ResultRequestListener<U1Song> {};
515+
516+ public interface U1ArtRequestListener extends ResultRequestListener<File> {};
517+ public interface U1StreamRequestListener extends ResultRequestListener<InputStream> {};
518+}
519
520=== modified file 'src/test/com/ubuntuone/api/music/CreatePlaylistTest.java'
521--- src/test/com/ubuntuone/api/music/CreatePlaylistTest.java 2012-09-28 01:45:13 +0000
522+++ src/test/com/ubuntuone/api/music/CreatePlaylistTest.java 2012-11-08 22:55:22 +0000
523@@ -48,7 +48,7 @@
524 import com.ubuntuone.api.music.json.U1CustomJson;
525 import com.ubuntuone.api.music.model.U1Meta;
526 import com.ubuntuone.api.music.model.U1Song;
527-import com.ubuntuone.api.music.util.RequestListener.U1SongRequestListener;
528+import com.ubuntuone.api.music.util.ResultRequestListener.U1SongRequestListener;
529 import com.ubuntuone.music.util.U1ResponseBuilder;
530 import com.ubuntuone.music.util.U1SongBuilder;
531 import com.ubuntuone.music.util.Util;
532@@ -125,7 +125,7 @@
533 "/api/music/v2/playlists/");
534
535 httpRequest.setEntity(new StringEntity(U1CustomJson
536- .toSongIdListJson(name, songIdList)));
537+ .toPlaylistJson(name, songIdList)));
538
539 String responseJson = U1ResponseBuilder.toJson(
540 meta, U1MusicAPI.SONGS, id, name, song1, song2, song3);
541@@ -178,7 +178,7 @@
542 "/api/music/v2/playlists/");
543
544 httpRequest.setEntity(new StringEntity(U1CustomJson
545- .toSongIdListJson(name, songIdList)));
546+ .toPlaylistJson(name, songIdList)));
547
548 String responseJson = U1ResponseBuilder.toJson(
549 meta, U1MusicAPI.SONGS, id, name, song1, song2, song3);
550
551=== modified file 'src/test/com/ubuntuone/api/music/DeletePlaylistTest.java'
552--- src/test/com/ubuntuone/api/music/DeletePlaylistTest.java 2012-09-28 01:45:13 +0000
553+++ src/test/com/ubuntuone/api/music/DeletePlaylistTest.java 2012-11-08 22:55:22 +0000
554@@ -45,7 +45,7 @@
555 import com.ubuntuone.api.music.client.BaseClient;
556 import com.ubuntuone.api.music.client.Failure;
557 import com.ubuntuone.api.music.model.U1Meta;
558-import com.ubuntuone.api.music.util.RequestListener.U1VoidRequestListener;
559+import com.ubuntuone.api.music.util.RequestListener.U1RequestListener;
560 import com.ubuntuone.music.util.U1ResponseBuilder;
561 import com.ubuntuone.music.util.Util;
562 import com.ubuntuone.test.util.DummyAuthorizer;
563@@ -64,7 +64,7 @@
564 private U1MusicAPI musicApi;
565
566 String playlistId = "playlistId";
567- private U1VoidRequestListener requestCallback;
568+ private U1RequestListener requestCallback;
569
570 @Before
571 public void setUp() throws IOException {
572@@ -75,7 +75,7 @@
573 httpClient = context.mock(HttpClient.class);
574 musicApi = new U1MusicAPI("ua/1.0", httpClient, new DummyAuthorizer());
575
576- requestCallback = context.mock(U1VoidRequestListener.class);
577+ requestCallback = context.mock(U1RequestListener.class);
578 }
579
580 @Test
581@@ -86,7 +86,7 @@
582 inSequence(requestSequence);
583
584 ignoring(httpClient);
585- allowing(requestCallback).onSuccess(with(any(Void.class)));
586+ allowing(requestCallback).onSuccess();
587 allowing(requestCallback).onFailure(with(any(Failure.class)));
588
589 oneOf(requestCallback).onFinish();
590@@ -118,7 +118,7 @@
591 inSequence(requestSequence);
592 will(returnValue(httpResponse));
593
594- oneOf(requestCallback).onSuccess(null);
595+ oneOf(requestCallback).onSuccess();
596 inSequence(requestSequence);
597
598 oneOf(requestCallback).onFinish();
599
600=== modified file 'src/test/com/ubuntuone/api/music/GetAlbumsTest.java'
601--- src/test/com/ubuntuone/api/music/GetAlbumsTest.java 2012-09-28 01:45:13 +0000
602+++ src/test/com/ubuntuone/api/music/GetAlbumsTest.java 2012-11-08 22:55:22 +0000
603@@ -47,7 +47,7 @@
604 import com.ubuntuone.api.music.model.U1Album;
605 import com.ubuntuone.api.music.model.U1Artist;
606 import com.ubuntuone.api.music.model.U1Meta;
607-import com.ubuntuone.api.music.util.RequestListener.U1AlbumRequestListener;
608+import com.ubuntuone.api.music.util.ResultRequestListener.U1AlbumRequestListener;
609 import com.ubuntuone.music.util.U1AlbumBuilder;
610 import com.ubuntuone.music.util.U1ArtistBuilder;
611 import com.ubuntuone.music.util.U1ResponseBuilder;
612
613=== modified file 'src/test/com/ubuntuone/api/music/GetArtTest.java'
614--- src/test/com/ubuntuone/api/music/GetArtTest.java 2012-09-19 19:17:30 +0000
615+++ src/test/com/ubuntuone/api/music/GetArtTest.java 2012-11-08 22:55:22 +0000
616@@ -51,7 +51,7 @@
617 import com.ubuntuone.api.music.model.U1Artist;
618 import com.ubuntuone.api.music.model.U1Song;
619 import com.ubuntuone.api.music.util.HashUtils;
620-import com.ubuntuone.api.music.util.RequestListener.U1ArtRequestListener;
621+import com.ubuntuone.api.music.util.ResultRequestListener.U1ArtRequestListener;
622 import com.ubuntuone.music.util.U1AlbumBuilder;
623 import com.ubuntuone.music.util.U1ArtistBuilder;
624 import com.ubuntuone.music.util.U1SongBuilder;
625
626=== modified file 'src/test/com/ubuntuone/api/music/GetArtistsTest.java'
627--- src/test/com/ubuntuone/api/music/GetArtistsTest.java 2012-09-28 01:45:13 +0000
628+++ src/test/com/ubuntuone/api/music/GetArtistsTest.java 2012-11-08 22:55:22 +0000
629@@ -46,7 +46,7 @@
630 import com.ubuntuone.api.music.client.Failure;
631 import com.ubuntuone.api.music.model.U1Artist;
632 import com.ubuntuone.api.music.model.U1Meta;
633-import com.ubuntuone.api.music.util.RequestListener.U1ArtistRequestListener;
634+import com.ubuntuone.api.music.util.ResultRequestListener.U1ArtistRequestListener;
635 import com.ubuntuone.music.util.U1ArtistBuilder;
636 import com.ubuntuone.music.util.U1ResponseBuilder;
637 import com.ubuntuone.music.util.Util;
638
639=== modified file 'src/test/com/ubuntuone/api/music/GetPlaylistTest.java'
640--- src/test/com/ubuntuone/api/music/GetPlaylistTest.java 2012-09-28 01:45:13 +0000
641+++ src/test/com/ubuntuone/api/music/GetPlaylistTest.java 2012-11-08 22:55:22 +0000
642@@ -46,7 +46,7 @@
643 import com.ubuntuone.api.music.client.Failure;
644 import com.ubuntuone.api.music.model.U1Meta;
645 import com.ubuntuone.api.music.model.U1Song;
646-import com.ubuntuone.api.music.util.RequestListener.U1SongRequestListener;
647+import com.ubuntuone.api.music.util.ResultRequestListener.U1SongRequestListener;
648 import com.ubuntuone.music.util.U1ResponseBuilder;
649 import com.ubuntuone.music.util.U1SongBuilder;
650 import com.ubuntuone.music.util.Util;
651
652=== modified file 'src/test/com/ubuntuone/api/music/GetPlaylistsTest.java'
653--- src/test/com/ubuntuone/api/music/GetPlaylistsTest.java 2012-09-28 01:45:13 +0000
654+++ src/test/com/ubuntuone/api/music/GetPlaylistsTest.java 2012-11-08 22:55:22 +0000
655@@ -46,7 +46,7 @@
656 import com.ubuntuone.api.music.client.Failure;
657 import com.ubuntuone.api.music.model.U1Meta;
658 import com.ubuntuone.api.music.model.U1Playlist;
659-import com.ubuntuone.api.music.util.RequestListener.U1PlaylistRequestListener;
660+import com.ubuntuone.api.music.util.ResultRequestListener.U1PlaylistRequestListener;
661 import com.ubuntuone.music.util.U1PlaylistBuilder;
662 import com.ubuntuone.music.util.U1ResponseBuilder;
663 import com.ubuntuone.music.util.Util;
664
665=== modified file 'src/test/com/ubuntuone/api/music/GetSongStreamTest.java'
666--- src/test/com/ubuntuone/api/music/GetSongStreamTest.java 2012-11-08 16:22:16 +0000
667+++ src/test/com/ubuntuone/api/music/GetSongStreamTest.java 2012-11-08 22:55:22 +0000
668@@ -47,7 +47,7 @@
669 import com.ubuntuone.api.music.client.BaseClient;
670 import com.ubuntuone.api.music.client.Failure;
671 import com.ubuntuone.api.music.model.U1Song;
672-import com.ubuntuone.api.music.util.RequestListener.U1StreamRequestListener;
673+import com.ubuntuone.api.music.util.ResultRequestListener.U1StreamRequestListener;
674 import com.ubuntuone.music.util.U1SongBuilder;
675 import com.ubuntuone.music.util.Util;
676 import com.ubuntuone.test.util.DummyAuthorizer;
677
678=== modified file 'src/test/com/ubuntuone/api/music/GetSongsTest.java'
679--- src/test/com/ubuntuone/api/music/GetSongsTest.java 2012-09-28 01:45:13 +0000
680+++ src/test/com/ubuntuone/api/music/GetSongsTest.java 2012-11-08 22:55:22 +0000
681@@ -47,7 +47,7 @@
682 import com.ubuntuone.api.music.model.U1Album;
683 import com.ubuntuone.api.music.model.U1Meta;
684 import com.ubuntuone.api.music.model.U1Song;
685-import com.ubuntuone.api.music.util.RequestListener.U1SongRequestListener;
686+import com.ubuntuone.api.music.util.ResultRequestListener.U1SongRequestListener;
687 import com.ubuntuone.music.util.U1AlbumBuilder;
688 import com.ubuntuone.music.util.U1ResponseBuilder;
689 import com.ubuntuone.music.util.U1SongBuilder;
690
691=== modified file 'src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java'
692--- src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java 2012-09-28 01:45:13 +0000
693+++ src/test/com/ubuntuone/api/music/UpdatePlaylistTest.java 2012-11-08 22:55:22 +0000
694@@ -48,7 +48,7 @@
695 import com.ubuntuone.api.music.json.U1CustomJson;
696 import com.ubuntuone.api.music.model.U1Meta;
697 import com.ubuntuone.api.music.model.U1Song;
698-import com.ubuntuone.api.music.util.RequestListener.U1SongRequestListener;
699+import com.ubuntuone.api.music.util.ResultRequestListener.U1SongRequestListener;
700 import com.ubuntuone.music.util.U1ResponseBuilder;
701 import com.ubuntuone.music.util.U1SongBuilder;
702 import com.ubuntuone.music.util.Util;
703@@ -125,7 +125,7 @@
704 "/api/music/v2/playlists/");
705
706 httpRequest.setEntity(new StringEntity(U1CustomJson
707- .toSongIdListJson(name, songList)));
708+ .toPlaylistJson(name, songList)));
709
710 String responseJson = U1ResponseBuilder.toJson(
711 meta, U1MusicAPI.SONGS, id, name, song1, song2, song3);
712
713=== modified file 'src/test/com/ubuntuone/api/music/json/U1AlbumJsonTest.java'
714--- src/test/com/ubuntuone/api/music/json/U1AlbumJsonTest.java 2012-06-12 02:39:48 +0000
715+++ src/test/com/ubuntuone/api/music/json/U1AlbumJsonTest.java 2012-11-08 22:55:22 +0000
716@@ -35,7 +35,7 @@
717 import com.ubuntuone.api.music.U1MusicAPI;
718 import com.ubuntuone.api.music.model.U1Album;
719 import com.ubuntuone.api.music.model.U1Meta;
720-import com.ubuntuone.api.music.util.RequestListener.U1AlbumRequestListener;
721+import com.ubuntuone.api.music.util.ResultRequestListener.U1AlbumRequestListener;
722 import com.ubuntuone.music.util.U1AlbumBuilder;
723 import com.ubuntuone.music.util.U1ResponseBuilder;
724
725
726=== modified file 'src/test/com/ubuntuone/api/music/json/U1ArtistJsonTest.java'
727--- src/test/com/ubuntuone/api/music/json/U1ArtistJsonTest.java 2012-09-04 19:28:22 +0000
728+++ src/test/com/ubuntuone/api/music/json/U1ArtistJsonTest.java 2012-11-08 22:55:22 +0000
729@@ -35,7 +35,7 @@
730 import com.ubuntuone.api.music.U1MusicAPI;
731 import com.ubuntuone.api.music.model.U1Artist;
732 import com.ubuntuone.api.music.model.U1Meta;
733-import com.ubuntuone.api.music.util.RequestListener.U1ArtistRequestListener;
734+import com.ubuntuone.api.music.util.ResultRequestListener.U1ArtistRequestListener;
735 import com.ubuntuone.music.util.U1ArtistBuilder;
736 import com.ubuntuone.music.util.U1ResponseBuilder;
737
738
739=== modified file 'src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java'
740--- src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java 2012-09-19 19:17:30 +0000
741+++ src/test/com/ubuntuone/api/music/json/U1CustomJsonTest.java 2012-11-08 22:55:22 +0000
742@@ -31,7 +31,7 @@
743 public class U1CustomJsonTest
744 {
745 @Test
746- public void testEncodeU1SongIdListPlaylist() throws IOException {
747+ public void testToPlaylistJson() throws IOException {
748 final ArrayList<String> songIdList = new ArrayList<String>();
749
750 songIdList.add("id1");
751@@ -39,7 +39,7 @@
752
753 String name = "the playlist name";
754
755- String json = U1CustomJson.toSongIdListJson(name, songIdList);
756+ String json = U1CustomJson.toPlaylistJson(name, songIdList);
757
758 String expectedJson = "{\"name\":\"" + name + "\"," +
759 "\"song_id_list\":[" +
760@@ -48,4 +48,28 @@
761
762 assertEquals(expectedJson, json);
763 }
764+
765+ @Test
766+ public void testToPlaylistJsonNameOnly() throws IOException {
767+ String name = "the playlist name";
768+
769+ String json = U1CustomJson.toPlaylistJson(name, null);
770+
771+ String expectedJson = "{\"name\":\"" + name + "\"}";
772+
773+ assertEquals(expectedJson, json);
774+ }
775+
776+ @Test
777+ public void testToPlaylistJsonSongIdListOnly() throws IOException {
778+ final ArrayList<String> songIdList = new ArrayList<String>();
779+ songIdList.add("id1");
780+ songIdList.add("id2");
781+
782+ String json = U1CustomJson.toPlaylistJson(null, songIdList);
783+
784+ String expectedJson = "{\"song_id_list\":[\"id1\",\"id2\"]}";
785+
786+ assertEquals(expectedJson, json);
787+ }
788 }
789
790=== modified file 'src/test/com/ubuntuone/api/music/json/U1PlaylistJsonTest.java'
791--- src/test/com/ubuntuone/api/music/json/U1PlaylistJsonTest.java 2012-06-12 14:44:04 +0000
792+++ src/test/com/ubuntuone/api/music/json/U1PlaylistJsonTest.java 2012-11-08 22:55:22 +0000
793@@ -35,7 +35,7 @@
794 import com.ubuntuone.api.music.U1MusicAPI;
795 import com.ubuntuone.api.music.model.U1Meta;
796 import com.ubuntuone.api.music.model.U1Playlist;
797-import com.ubuntuone.api.music.util.RequestListener.U1PlaylistRequestListener;
798+import com.ubuntuone.api.music.util.ResultRequestListener.U1PlaylistRequestListener;
799 import com.ubuntuone.music.util.U1PlaylistBuilder;
800 import com.ubuntuone.music.util.U1ResponseBuilder;
801
802
803=== modified file 'src/test/com/ubuntuone/api/music/json/U1SongJsonTest.java'
804--- src/test/com/ubuntuone/api/music/json/U1SongJsonTest.java 2012-06-12 02:39:48 +0000
805+++ src/test/com/ubuntuone/api/music/json/U1SongJsonTest.java 2012-11-08 22:55:22 +0000
806@@ -35,7 +35,7 @@
807 import com.ubuntuone.api.music.U1MusicAPI;
808 import com.ubuntuone.api.music.model.U1Meta;
809 import com.ubuntuone.api.music.model.U1Song;
810-import com.ubuntuone.api.music.util.RequestListener.U1SongRequestListener;
811+import com.ubuntuone.api.music.util.ResultRequestListener.U1SongRequestListener;
812 import com.ubuntuone.music.util.U1ResponseBuilder;
813 import com.ubuntuone.music.util.U1SongBuilder;
814
815
816=== modified file 'src/test/com/ubuntuone/music/util/U1ResponseBuilder.java'
817--- src/test/com/ubuntuone/music/util/U1ResponseBuilder.java 2012-08-27 17:20:36 +0000
818+++ src/test/com/ubuntuone/music/util/U1ResponseBuilder.java 2012-11-08 22:55:22 +0000
819@@ -33,6 +33,9 @@
820
821 public class U1ResponseBuilder
822 {
823+ private static final String META = "meta";
824+ private static final String STATUS = "status";
825+ private static final String MSG = "msg";
826 private static final String RESPONSE = "response";
827
828 public static String toJson(U1Meta meta, String title, JSONString... items)
829@@ -48,24 +51,23 @@
830 final JsonGenerator g = factory.createJsonGenerator(writer);
831
832 g.writeStartObject(); {
833- // meta
834- g.writeObjectFieldStart("meta"); {
835- g.writeNumberField("status", meta.getStatus());
836- g.writeStringField("msg", meta.getMessage());
837+ g.writeObjectFieldStart(META); {
838+ g.writeNumberField(STATUS, meta.getStatus());
839+ g.writeStringField(MSG, meta.getMessage());
840 }
841 g.writeEndObject();
842- // response
843+
844 g.writeObjectFieldStart(RESPONSE); {
845- // item array
846 g.writeArrayFieldStart(title);
847 if (items != null) {
848 for (JSONString item : items) {
849 g.writeRawValue(item.toJSONString());
850 }
851 }
852- g.writeEndArray(); // title
853+ g.writeEndArray();
854 }
855- g.writeEndObject(); // response
856+ g.writeEndObject();
857+
858 }
859 g.writeEndObject();
860 g.close();

Subscribers

People subscribed via source and target branches