From 81dcbb0044b2b6aeb6ce13a3a0655c8c77451d61 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Mon, 17 Jul 2017 22:24:47 +0200 Subject: [PATCH] Adds get music directory response/request handling. --- .../api/subsonic/SubsonicAPITest.kt | 57 +++++++++++++++---- .../resources/get_music_directory_ok.json | 35 ++++++++++++ ...ries_ok.json => get_music_folders_ok.json} | 0 .../api/subsonic/SubsonicAPIDefinition.kt | 4 ++ .../api/subsonic/models/MusicDirectory.kt | 10 ++++ .../subsonic/models/MusicDirectoryChild.kt | 9 +++ .../response/GetMusicDirectoryResponse.kt | 13 +++++ 7 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 subsonic-api/src/integrationTest/resources/get_music_directory_ok.json rename subsonic-api/src/integrationTest/resources/{get_music_directories_ok.json => get_music_folders_ok.json} (100%) create mode 100644 subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/MusicDirectory.kt create mode 100644 subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/MusicDirectoryChild.kt create mode 100644 subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/response/GetMusicDirectoryResponse.kt diff --git a/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPITest.kt b/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPITest.kt index 7691cf44..49bc6ab4 100644 --- a/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPITest.kt +++ b/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPITest.kt @@ -9,10 +9,7 @@ import org.amshove.kluent.`should not be` import org.junit.Before import org.junit.Rule import org.junit.Test -import org.moire.ultrasonic.api.subsonic.models.Artist -import org.moire.ultrasonic.api.subsonic.models.Index -import org.moire.ultrasonic.api.subsonic.models.License -import org.moire.ultrasonic.api.subsonic.models.MusicFolder +import org.moire.ultrasonic.api.subsonic.models.* import org.moire.ultrasonic.api.subsonic.response.SubsonicResponse import org.moire.ultrasonic.api.subsonic.rules.MockWebServerRule import retrofit2.Response @@ -25,15 +22,13 @@ import java.util.* */ class SubsonicAPITest { companion object { - val USERNAME = "some-user" - val PASSWORD = "some-password" + const val USERNAME = "some-user" + const val PASSWORD = "some-password" val CLIENT_VERSION = SubsonicAPIVersions.V1_13_0 - val CLIENT_ID = "test-client" + const val CLIENT_ID = "test-client" } - @JvmField - @Rule - val mockWebServerRule = MockWebServerRule() + @JvmField @Rule val mockWebServerRule = MockWebServerRule() private lateinit var api: SubsonicAPI @@ -82,7 +77,7 @@ class SubsonicAPITest { @Test fun `Should parse get music folders ok response`() { - enqueueResponse("get_music_directories_ok.json") + enqueueResponse("get_music_folders_ok.json") val response = api.getApi().getMusicFolders().execute() @@ -171,6 +166,46 @@ class SubsonicAPITest { response.indexes `should be` null } + @Test + fun `Should parse getMusicDirectory error response`() { + val response = checkErrorCallParsed { api.getApi().getMusicDirectory(1).execute() } + + response.musicDirectory `should be` null + } + + @Test + fun `GetMusicDirectory should add directory id to query params`() { + enqueueResponse("get_music_directory_ok.json") + val directoryId = 124L + + api.getApi().getMusicDirectory(directoryId).execute() + + mockWebServerRule.mockWebServer.takeRequest().requestLine `should contain` "id=$directoryId" + } + + @Test + fun `Should parse get music directory ok response`() { + enqueueResponse("get_music_directory_ok.json") + + val response = api.getApi().getMusicDirectory(1).execute() + + assertResponseSuccessful(response) + + response.body().musicDirectory `should not be` null + with(response.body().musicDirectory!!) { + id `should equal` 382L + name `should equal` "AC_DC" + starred `should equal` parseDate("2017-04-02T20:16:29.815Z") + childList.size `should be` 2 + childList[0] `should equal` MusicDirectoryChild(583L, 382L, true, "Black Ice", + "Black Ice", "AC/DC", 2008, "Hard Rock", 583L, + parseDate("2016-10-23T15:31:22.000Z"), parseDate("2017-04-02T20:16:15.724Z")) + childList[1] `should equal` MusicDirectoryChild(582L, 382L, true, "Rock or Bust", + "Rock or Bust", "AC/DC", 2014, "Hard Rock", 582L, + parseDate("2016-10-23T15:31:24.000Z"), null) + } + } + private fun enqueueResponse(resourceName: String) { mockWebServerRule.mockWebServer.enqueue(MockResponse() .setBody(loadJsonResponse(resourceName))) diff --git a/subsonic-api/src/integrationTest/resources/get_music_directory_ok.json b/subsonic-api/src/integrationTest/resources/get_music_directory_ok.json new file mode 100644 index 00000000..048b7de4 --- /dev/null +++ b/subsonic-api/src/integrationTest/resources/get_music_directory_ok.json @@ -0,0 +1,35 @@ +{ + "subsonic-response" : { + "status" : "ok", + "version" : "1.13.0", + "directory" : { + "id" : "382", + "name" : "AC_DC", + "starred" : "2017-04-02T20:16:29.815Z", + "child" : [ { + "id" : "583", + "parent" : "382", + "isDir" : true, + "title" : "Black Ice", + "album" : "Black Ice", + "artist" : "AC/DC", + "year" : 2008, + "genre" : "Hard Rock", + "coverArt" : "583", + "created" : "2016-10-23T15:31:22.000Z", + "starred" : "2017-04-02T20:16:15.724Z" + }, { + "id" : "582", + "parent" : "382", + "isDir" : true, + "title" : "Rock or Bust", + "album" : "Rock or Bust", + "artist" : "AC/DC", + "year" : 2014, + "genre" : "Hard Rock", + "coverArt" : "582", + "created" : "2016-10-23T15:31:24.000Z" + } ] + } + } +} \ No newline at end of file diff --git a/subsonic-api/src/integrationTest/resources/get_music_directories_ok.json b/subsonic-api/src/integrationTest/resources/get_music_folders_ok.json similarity index 100% rename from subsonic-api/src/integrationTest/resources/get_music_directories_ok.json rename to subsonic-api/src/integrationTest/resources/get_music_folders_ok.json diff --git a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIDefinition.kt b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIDefinition.kt index 9db3df4e..9a5c3ef5 100644 --- a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIDefinition.kt +++ b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIDefinition.kt @@ -1,6 +1,7 @@ package org.moire.ultrasonic.api.subsonic import org.moire.ultrasonic.api.subsonic.response.GetIndexesResponse +import org.moire.ultrasonic.api.subsonic.response.GetMusicDirectoryResponse import org.moire.ultrasonic.api.subsonic.response.LicenseResponse import org.moire.ultrasonic.api.subsonic.response.MusicFoldersResponse import org.moire.ultrasonic.api.subsonic.response.SubsonicResponse @@ -26,4 +27,7 @@ interface SubsonicAPIDefinition { @GET("getIndexes.view") fun getIndexes(@Query("musicFolderId") musicFolderId: Long?, @Query("ifModifiedSince") ifModifiedSince: Long?): Call + + @GET("getMusicDirectory.view") + fun getMusicDirectory(@Query("id") id: Long): Call } \ No newline at end of file diff --git a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/MusicDirectory.kt b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/MusicDirectory.kt new file mode 100644 index 00000000..cbe09064 --- /dev/null +++ b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/MusicDirectory.kt @@ -0,0 +1,10 @@ +package org.moire.ultrasonic.api.subsonic.models + +import com.fasterxml.jackson.annotation.JsonProperty +import java.util.* + +data class MusicDirectory(val id: Long, + val name: String, + val starred: Calendar?, + @JsonProperty("child") + val childList: List = emptyList()) \ No newline at end of file diff --git a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/MusicDirectoryChild.kt b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/MusicDirectoryChild.kt new file mode 100644 index 00000000..dc7bae18 --- /dev/null +++ b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/models/MusicDirectoryChild.kt @@ -0,0 +1,9 @@ +package org.moire.ultrasonic.api.subsonic.models + +import java.util.* + +data class MusicDirectoryChild(val id: Long, val parent: Long, val isDir: Boolean = false, + val title: String = "", val album: String = "", + val artist: String = "", val year: Int?, + val genre: String = "", val coverArt: Long = -1, + val created: Calendar, val starred: Calendar?) \ No newline at end of file diff --git a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/response/GetMusicDirectoryResponse.kt b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/response/GetMusicDirectoryResponse.kt new file mode 100644 index 00000000..a79cc1c3 --- /dev/null +++ b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/response/GetMusicDirectoryResponse.kt @@ -0,0 +1,13 @@ +package org.moire.ultrasonic.api.subsonic.response + +import com.fasterxml.jackson.annotation.JsonProperty +import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions +import org.moire.ultrasonic.api.subsonic.SubsonicError +import org.moire.ultrasonic.api.subsonic.models.MusicDirectory + +class GetMusicDirectoryResponse(status: Status, + version: SubsonicAPIVersions, + error: SubsonicError?, + @JsonProperty("directory") + val musicDirectory: MusicDirectory?) : + SubsonicResponse(status, version, error) \ No newline at end of file