mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-01-30 08:54:51 +01:00
Add methods to convert api share entity to domain entity.
Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
parent
d5cdc18174
commit
3bbd1fb16b
@ -7,8 +7,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Share implements Serializable
|
||||
{
|
||||
public class Share implements Serializable {
|
||||
private static final long serialVersionUID = 1487561657691009668L;
|
||||
private static final Pattern urlPattern = Pattern.compile(".*/([^/?]+).*");
|
||||
private String id;
|
||||
@ -120,4 +119,40 @@ public class Share implements Serializable
|
||||
{
|
||||
entries.add(entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Share share = (Share) o;
|
||||
|
||||
if (id != null ? !id.equals(share.id) : share.id != null) return false;
|
||||
if (url != null ? !url.equals(share.url) : share.url != null) return false;
|
||||
if (description != null ? !description.equals(share.description) : share.description != null)
|
||||
return false;
|
||||
if (username != null ? !username.equals(share.username) : share.username != null)
|
||||
return false;
|
||||
if (created != null ? !created.equals(share.created) : share.created != null) return false;
|
||||
if (lastVisited != null ? !lastVisited.equals(share.lastVisited) : share.lastVisited != null)
|
||||
return false;
|
||||
if (expires != null ? !expires.equals(share.expires) : share.expires != null) return false;
|
||||
if (visitCount != null ? !visitCount.equals(share.visitCount) : share.visitCount != null)
|
||||
return false;
|
||||
return entries != null ? entries.equals(share.entries) : share.entries == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id != null ? id.hashCode() : 0;
|
||||
result = 31 * result + (url != null ? url.hashCode() : 0);
|
||||
result = 31 * result + (description != null ? description.hashCode() : 0);
|
||||
result = 31 * result + (username != null ? username.hashCode() : 0);
|
||||
result = 31 * result + (created != null ? created.hashCode() : 0);
|
||||
result = 31 * result + (lastVisited != null ? lastVisited.hashCode() : 0);
|
||||
result = 31 * result + (expires != null ? expires.hashCode() : 0);
|
||||
result = 31 * result + (visitCount != null ? visitCount.hashCode() : 0);
|
||||
result = 31 * result + (entries != null ? entries.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,11 @@ package org.moire.ultrasonic.data
|
||||
import org.moire.ultrasonic.domain.MusicDirectory
|
||||
import org.moire.ultrasonic.domain.Playlist
|
||||
import java.text.SimpleDateFormat
|
||||
import kotlin.LazyThreadSafetyMode.NONE
|
||||
import org.moire.ultrasonic.api.subsonic.models.Playlist as APIPlaylist
|
||||
|
||||
internal val playlistDateFormat by lazy(NONE) { SimpleDateFormat.getInstance() }
|
||||
|
||||
fun APIPlaylist.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory().apply {
|
||||
name = this@toMusicDirectoryDomainEntity.name
|
||||
addAll(this@toMusicDirectoryDomainEntity.entriesList.map { it.toDomainEntity() })
|
||||
@ -15,7 +18,7 @@ fun APIPlaylist.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory(
|
||||
|
||||
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) },
|
||||
this.created?.let { playlistDateFormat.format(it.time) },
|
||||
public.toString())
|
||||
|
||||
fun List<APIPlaylist>.toDomainEntitiesList(): List<Playlist> = this.map { it.toDomainEntity() }
|
||||
|
@ -0,0 +1,26 @@
|
||||
// Contains helper method to convert subsonic api share to domain model
|
||||
@file:JvmName("APIShareConverter")
|
||||
package org.moire.ultrasonic.data
|
||||
|
||||
import org.moire.ultrasonic.domain.Share
|
||||
import java.text.SimpleDateFormat
|
||||
import kotlin.LazyThreadSafetyMode.NONE
|
||||
import org.moire.ultrasonic.api.subsonic.models.Share as APIShare
|
||||
|
||||
internal val shareTimeFormat by lazy(NONE) { SimpleDateFormat.getInstance() }
|
||||
|
||||
fun List<APIShare>.toDomainEntitiesList(): List<Share> = this.map {
|
||||
it.toDomainEntity()
|
||||
}
|
||||
|
||||
fun APIShare.toDomainEntity(): Share = Share().apply {
|
||||
created = this@toDomainEntity.created?.let { shareTimeFormat.format(it.time) }
|
||||
description = this@toDomainEntity.description
|
||||
expires = this@toDomainEntity.expires?.let { shareTimeFormat.format(it.time) }
|
||||
id = this@toDomainEntity.id.toString()
|
||||
lastVisited = this@toDomainEntity.lastVisited?.let { shareTimeFormat.format(it.time) }
|
||||
url = this@toDomainEntity.url
|
||||
username = this@toDomainEntity.username
|
||||
visitCount = this@toDomainEntity.visitCount.toLong()
|
||||
entries.addAll(this@toDomainEntity.items.toDomainEntityList())
|
||||
}
|
@ -7,11 +7,10 @@ import org.amshove.kluent.`should equal`
|
||||
import org.junit.Test
|
||||
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
|
||||
import org.moire.ultrasonic.api.subsonic.models.Playlist
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
|
||||
/**
|
||||
* Unit test for extension functions in [APIPlaylistConverter.kt] file.
|
||||
* Unit test for extension functions that converts api playlist entity to domain.
|
||||
*/
|
||||
class APIPlaylistConverterTest {
|
||||
@Test
|
||||
@ -47,8 +46,7 @@ class APIPlaylistConverterTest {
|
||||
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)
|
||||
created `should equal to` playlistDateFormat.format(entity.created?.time)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,54 @@
|
||||
@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.MusicDirectoryChild
|
||||
import org.moire.ultrasonic.api.subsonic.models.Share
|
||||
import java.util.Calendar
|
||||
|
||||
/**
|
||||
* Unit test for api to domain share entity converter functions.
|
||||
*/
|
||||
class APIShareConverterTest {
|
||||
@Test
|
||||
fun `Should convert share entity to domain`() {
|
||||
val entity = createFakeShare()
|
||||
|
||||
val domainEntity = entity.toDomainEntity()
|
||||
|
||||
with(domainEntity) {
|
||||
id `should equal to` entity.id.toString()
|
||||
url `should equal to` entity.url
|
||||
description `should equal to` entity.description
|
||||
username `should equal to` entity.username
|
||||
created `should equal to` shareTimeFormat.format(entity.created?.time)
|
||||
lastVisited `should equal to` shareTimeFormat.format(entity.lastVisited?.time)
|
||||
expires `should equal to` shareTimeFormat.format(entity.expires?.time)
|
||||
visitCount `should equal to` entity.visitCount.toLong()
|
||||
entries `should equal` entity.items.toDomainEntityList()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createFakeShare(): Share {
|
||||
return Share(id = 45L, url = "some-long-url", username = "Bender",
|
||||
created = Calendar.getInstance(), expires = Calendar.getInstance(), visitCount = 24,
|
||||
description = "Kiss my shiny metal ass", lastVisited = Calendar.getInstance(),
|
||||
items = listOf(MusicDirectoryChild()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should parse list of shares into domain entity list`() {
|
||||
val entityList = listOf(
|
||||
createFakeShare(),
|
||||
createFakeShare().copy(id = 554L, lastVisited = null))
|
||||
|
||||
val domainEntityList = entityList.toDomainEntitiesList()
|
||||
|
||||
domainEntityList.size `should equal to` entityList.size
|
||||
domainEntityList[0] `should equal` entityList[0].toDomainEntity()
|
||||
domainEntityList[1] `should equal` entityList[1].toDomainEntity()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user