mirror of
https://gitlab.com/ultrasonic/ultrasonic.git
synced 2025-04-18 02:02:23 +03:00
Add helper functions to convert api Genre to domain entity.
Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
parent
1502c4c9c8
commit
d693971baf
@ -31,7 +31,25 @@ public class Genre implements Serializable
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Genre genre = (Genre) o;
|
||||
|
||||
if (name != null ? !name.equals(genre.name) : genre.name != null) return false;
|
||||
return index != null ? index.equals(genre.index) : genre.index == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = name != null ? name.hashCode() : 0;
|
||||
result = 31 * result + (index != null ? index.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return name;
|
||||
|
@ -0,0 +1,13 @@
|
||||
// Collection of functions to convert api Genre entity to domain entity
|
||||
@file:JvmName("ApiGenreConverter")
|
||||
package org.moire.ultrasonic.data
|
||||
|
||||
import org.moire.ultrasonic.domain.Genre
|
||||
import org.moire.ultrasonic.api.subsonic.models.Genre as APIGenre
|
||||
|
||||
fun APIGenre.toDomainEntity(): Genre = Genre().apply {
|
||||
name = this@toDomainEntity.name
|
||||
index = this@toDomainEntity.name.substring(0, 1)
|
||||
}
|
||||
|
||||
fun List<APIGenre>.toDomainEntityList(): List<Genre> = this.map { it.toDomainEntity() }
|
@ -0,0 +1,38 @@
|
||||
@file:Suppress("IllegalIdentifier")
|
||||
|
||||
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.Genre
|
||||
|
||||
/**
|
||||
* Unit test for for converter from api [Genre] to domain entity.
|
||||
*/
|
||||
class ApiGenreConverterTest {
|
||||
@Test
|
||||
fun `Should convert to domain entity`() {
|
||||
val entity = Genre(songCount = 220, albumCount = 123, name = "some-name")
|
||||
|
||||
val domainEntity = entity.toDomainEntity()
|
||||
|
||||
with(domainEntity) {
|
||||
name `should equal to` entity.name
|
||||
index `should equal to` "s"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should convert a list entites to domain entities`() {
|
||||
val entitiesList = listOf(
|
||||
Genre(41, 2, "some-name"),
|
||||
Genre(12, 3, "other-name"))
|
||||
|
||||
val domainEntitiesList = entitiesList.toDomainEntityList()
|
||||
|
||||
domainEntitiesList.size `should equal to` entitiesList.size
|
||||
domainEntitiesList[0] `should equal` entitiesList[0].toDomainEntity()
|
||||
domainEntitiesList[1] `should equal` entitiesList[1].toDomainEntity()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user