Implement converting Indexes Subsonic api entity to domain entity.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2017-08-12 20:19:27 +02:00
parent d66556e307
commit b57a425e9d
3 changed files with 104 additions and 11 deletions

View File

@ -102,4 +102,31 @@ public class Artist implements Serializable
{
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Artist artist = (Artist) o;
if (closeness != artist.closeness) return false;
if (id != null ? !id.equals(artist.id) : artist.id != null) return false;
if (name != null ? !name.equals(artist.name) : artist.name != null) return false;
if (index != null ? !index.equals(artist.index) : artist.index != null) return false;
if (coverArt != null ? !coverArt.equals(artist.coverArt) : artist.coverArt != null)
return false;
return albumCount != null ? albumCount.equals(artist.albumCount) : artist.albumCount == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (index != null ? index.hashCode() : 0);
result = 31 * result + (coverArt != null ? coverArt.hashCode() : 0);
result = 31 * result + (albumCount != null ? albumCount.hashCode() : 0);
result = 31 * result + closeness;
return result;
}
}

View File

@ -1,8 +1,13 @@
// Converts entities from [org.moire.ultrasonic.api.subsonic.SubsonicAPIClient] to app entities.
// Converts entities from [org.moire.ultrasonic.api.subsonic.SubsonicAPIClient] to app domain entities.
@file:JvmName("APIConverter")
package org.moire.ultrasonic.data
import org.moire.ultrasonic.api.subsonic.models.Index
import org.moire.ultrasonic.domain.Artist
import org.moire.ultrasonic.domain.Indexes
import org.moire.ultrasonic.domain.MusicFolder
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.MusicFolder as APIMusicFolder
fun APIMusicFolder.toDomainEntity(): MusicFolder = MusicFolder(this.id.toString(), this.name)
@ -10,6 +15,14 @@ fun APIMusicFolder.toDomainEntity(): MusicFolder = MusicFolder(this.id.toString(
fun List<APIMusicFolder>.toDomainEntityList(): List<MusicFolder>
= this.map { it.toDomainEntity() }
fun convertMusicFolderList(entitiesList: List<APIMusicFolder>): List<MusicFolder> {
return entitiesList.map { convertMusicFolder(it) }
}
fun APIIndexes.toDomainEntity(): Indexes = Indexes(this.lastModified, this.ignoredArticles,
this.shortcuts.foldIndexToArtistList(), this.indexList.foldIndexToArtistList())
private fun List<Index>.foldIndexToArtistList(): List<Artist> = this.fold(listOf(), {
acc, index -> acc + index.artists.map { it.toDomainEntity() }
})
fun APIArtist.toDomainEntity(): Artist = Artist().apply {
id = this@toDomainEntity.id.toString()
name = this@toDomainEntity.name
}

View File

@ -3,8 +3,13 @@
package org.moire.ultrasonic.data
import org.amshove.kluent.`should equal to`
import org.amshove.kluent.`should equal`
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.Indexes
import org.moire.ultrasonic.api.subsonic.models.MusicFolder
import java.util.Calendar
/**
* Unit test for functions in SubsonicAPIConverter file.
@ -18,8 +23,8 @@ class APIConverterTest {
val convertedEntity = entity.toDomainEntity()
convertedEntity.name `should equal to` "some-name"
convertedEntity.id `should equal to` 10.toString()
convertedEntity.name `should equal to` entity.name
convertedEntity.id `should equal to` entity.id.toString()
}
@Test
@ -31,13 +36,61 @@ class APIConverterTest {
val convertedList = entityList.toDomainEntityList()
convertedList.size `should equal to` 2
convertedList[0].id `should equal to` 3.toString()
convertedList[0].name `should equal to` "some-name-3"
convertedList[1].id `should equal to` 4.toString()
convertedList[1].name `should equal to` "some-name-4"
with(convertedList) {
size `should equal to` entityList.size
this[0].id `should equal to` entityList[0].id.toString()
this[0].name `should equal to` entityList[0].name
this[1].id `should equal to` entityList[1].id.toString()
this[1].name `should equal to` entityList[1].name
}
}
@Test
fun `Should convert artist entity`() {
val entity = createArtist(10, "artist-name", Calendar.getInstance())
val convertedEntity = entity.toDomainEntity()
with(convertedEntity) {
id `should equal to` entity.id.toString()
name `should equal to` entity.name
}
}
@Test
fun `Should convert Indexes entity`() {
val artistsA = listOf(createArtist(4, "AC/DC"), createArtist(45, "ABBA"))
val artistsT = listOf(createArtist(10, "Taproot"), createArtist(12, "Teebee"))
val entity = createIndexes(154, "Le Tre Ze", listOf(
createIndex("A", artistsA),
createIndex("T", artistsT)
), emptyList())
val convertedEntity = entity.toDomainEntity()
val expectedArtists = (artistsA + artistsT).map { it.toDomainEntity() }.toMutableList()
with(convertedEntity) {
lastModified `should equal to` entity.lastModified
ignoredArticles `should equal to` entity.ignoredArticles
artists.size `should equal to` expectedArtists.size
artists `should equal` expectedArtists
shortcuts `should equal` emptyList()
}
}
private fun createMusicFolder(id: Long = 0, name: String = ""): MusicFolder =
MusicFolder(id, name)
private fun createArtist(id: Long = -1, name: String = "", starred: Calendar? = null): Artist
= Artist(id, name, starred)
private fun createIndex(name: String = "", artistList: List<Artist> = emptyList()): Index
= Index(name, artistList)
private fun createIndexes(
lastModified: Long = 0,
ignoredArticles: String,
indexList: List<Index> = emptyList(),
shortcuts: List<Index> = emptyList()): Indexes
= Indexes(lastModified, ignoredArticles, indexList, shortcuts)
}