From d82ff15f57b155129fd8308b4ae9dda7692d3bf6 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Fri, 1 Sep 2017 22:46:37 +0200 Subject: [PATCH] Add converting subsonic api Playlist entity to domain entity. Signed-off-by: Yahor Berdnikau --- .../org/moire/ultrasonic/domain/Playlist.java | 48 +++++++++++++++++-- .../ultrasonic/data/SubsonicAPIConverter.kt | 9 ++++ .../moire/ultrasonic/data/APIConverterTest.kt | 36 +++++++++++++- 3 files changed, 87 insertions(+), 6 deletions(-) diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/domain/Playlist.java b/ultrasonic/src/main/java/org/moire/ultrasonic/domain/Playlist.java index c2a6db77..a7749031 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/domain/Playlist.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/domain/Playlist.java @@ -125,9 +125,47 @@ public class Playlist implements Serializable this.pub = pub; } - @Override - public String toString() - { - return name; - } + @Override + public String toString() { + return "Playlist{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", owner='" + owner + '\'' + + ", comment='" + comment + '\'' + + ", songCount='" + songCount + '\'' + + ", created='" + created + '\'' + + ", pub=" + pub + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Playlist playlist = (Playlist) o; + + if (id != null ? !id.equals(playlist.id) : playlist.id != null) return false; + if (name != null ? !name.equals(playlist.name) : playlist.name != null) return false; + if (owner != null ? !owner.equals(playlist.owner) : playlist.owner != null) return false; + if (comment != null ? !comment.equals(playlist.comment) : playlist.comment != null) + return false; + if (songCount != null ? !songCount.equals(playlist.songCount) : playlist.songCount != null) + return false; + if (created != null ? !created.equals(playlist.created) : playlist.created != null) + return false; + return pub != null ? pub.equals(playlist.pub) : playlist.pub == null; + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (owner != null ? owner.hashCode() : 0); + result = 31 * result + (comment != null ? comment.hashCode() : 0); + result = 31 * result + (songCount != null ? songCount.hashCode() : 0); + result = 31 * result + (created != null ? created.hashCode() : 0); + result = 31 * result + (pub != null ? pub.hashCode() : 0); + return result; + } } \ No newline at end of file diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/SubsonicAPIConverter.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/SubsonicAPIConverter.kt index 1f424417..3454e9ba 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/SubsonicAPIConverter.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/SubsonicAPIConverter.kt @@ -11,7 +11,9 @@ import org.moire.ultrasonic.domain.Artist import org.moire.ultrasonic.domain.Indexes import org.moire.ultrasonic.domain.MusicDirectory import org.moire.ultrasonic.domain.MusicFolder +import org.moire.ultrasonic.domain.Playlist import org.moire.ultrasonic.domain.SearchResult +import java.text.SimpleDateFormat import org.moire.ultrasonic.api.subsonic.models.Artist as APIArtist import org.moire.ultrasonic.api.subsonic.models.Indexes as APIIndexes import org.moire.ultrasonic.api.subsonic.models.MusicDirectory as APIMusicDirectory @@ -109,3 +111,10 @@ fun APIPlaylist.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory( name = this@toMusicDirectoryDomainEntity.name addAll(this@toMusicDirectoryDomainEntity.entriesList.map { it.toDomainEntity() }) } + +fun APIPlaylist.toDomainEntity(): Playlist = Playlist(this.id.toString(), this.name, this.owner, + this.comment, this.songCount.toString(), + this.created?.let { SimpleDateFormat.getDateTimeInstance().format(it.time) }, + public.toString()) + +fun List.toDomainEntitiesList(): List = this.map { it.toDomainEntity() } diff --git a/ultrasonic/src/test/kotlin/org/moire/ultrasonic/data/APIConverterTest.kt b/ultrasonic/src/test/kotlin/org/moire/ultrasonic/data/APIConverterTest.kt index 291662b0..4b7584f0 100644 --- a/ultrasonic/src/test/kotlin/org/moire/ultrasonic/data/APIConverterTest.kt +++ b/ultrasonic/src/test/kotlin/org/moire/ultrasonic/data/APIConverterTest.kt @@ -17,6 +17,7 @@ import org.moire.ultrasonic.api.subsonic.models.Playlist import org.moire.ultrasonic.api.subsonic.models.SearchResult import org.moire.ultrasonic.api.subsonic.models.SearchThreeResult import org.moire.ultrasonic.api.subsonic.models.SearchTwoResult +import java.text.SimpleDateFormat import java.util.Calendar /** @@ -24,7 +25,7 @@ import java.util.Calendar * * @author Yahor Berdnikau */ -@Suppress("TooManyFunctions") +@Suppress("TooManyFunctions", "LargeClass") class APIConverterTest { @Test fun `Should convert MusicFolder entity`() { @@ -275,6 +276,39 @@ class APIConverterTest { } } + @Test + fun `Should convert playlist to domain entity`() { + val entity = Playlist(id = 634, name = "some-name", owner = "some-owner", + comment = "some-comment", public = false, songCount = 256, duration = 1150, + created = Calendar.getInstance(), changed = Calendar.getInstance(), + coverArt = "some-art") + + val convertedEntity = entity.toDomainEntity() + + with(convertedEntity) { + id `should equal to` entity.id.toString() + name `should equal to` entity.name + comment `should equal to` entity.comment + owner `should equal to` entity.owner + public `should equal to` entity.public + songCount `should equal to` entity.songCount.toString() + created `should equal to` SimpleDateFormat.getDateTimeInstance() + .format(entity.created?.time) + } + } + + @Test + fun `Should convert list of playlists to list of domain entities`() { + val entitiesList = listOf(Playlist(id = 23, name = "some-name", songCount = 10)) + + val convertedList = entitiesList.toDomainEntitiesList() + + with(convertedList) { + size `should equal to` entitiesList.size + this[0] `should equal` entitiesList[0].toDomainEntity() + } + } + private fun createMusicFolder(id: Long = 0, name: String = ""): MusicFolder = MusicFolder(id, name)