Add helper functions to convert api Bookmark entity to domain entity.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2017-11-18 21:34:03 +01:00
parent 0cc0bbedaf
commit 6b2a682646
3 changed files with 103 additions and 47 deletions

View File

@ -3,10 +3,7 @@ package org.moire.ultrasonic.domain;
import org.moire.ultrasonic.domain.MusicDirectory.Entry;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Bookmark implements Serializable
{
@ -51,52 +48,20 @@ public class Bookmark implements Serializable
this.comment = comment;
}
public Date getCreated()
{
public Date getCreated() {
return created;
}
public void setCreated(String created)
{
if (created != null)
{
try
{
this.created = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(created);
}
catch (ParseException e)
{
this.created = null;
}
}
else
{
this.created = null;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getChanged()
{
public Date getChanged() {
return changed;
}
public void setChanged(String changed)
{
if (changed != null)
{
try
{
this.changed = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(changed);
}
catch (ParseException e)
{
this.changed = null;
}
}
else
{
this.changed = null;
}
public void setChanged(Date changed) {
this.changed = changed;
}
public Entry getEntry()
@ -108,4 +73,34 @@ public class Bookmark implements Serializable
{
this.entry = entry;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bookmark bookmark = (Bookmark) o;
if (position != bookmark.position) return false;
if (username != null ? !username.equals(bookmark.username) : bookmark.username != null)
return false;
if (comment != null ? !comment.equals(bookmark.comment) : bookmark.comment != null)
return false;
if (created != null ? !created.equals(bookmark.created) : bookmark.created != null)
return false;
if (changed != null ? !changed.equals(bookmark.changed) : bookmark.changed != null)
return false;
return entry != null ? entry.equals(bookmark.entry) : bookmark.entry == null;
}
@Override
public int hashCode() {
int result = position;
result = 31 * result + (username != null ? username.hashCode() : 0);
result = 31 * result + (comment != null ? comment.hashCode() : 0);
result = 31 * result + (created != null ? created.hashCode() : 0);
result = 31 * result + (changed != null ? changed.hashCode() : 0);
result = 31 * result + (entry != null ? entry.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,17 @@
// Contains helper functions to convert api Bookmark entity to domain entity
@file:JvmName("APIBookmarkConverter")
package org.moire.ultrasonic.data
import org.moire.ultrasonic.domain.Bookmark
import org.moire.ultrasonic.api.subsonic.models.Bookmark as ApiBookmark
fun ApiBookmark.toDomainEntity(): Bookmark = Bookmark().apply {
position = this@toDomainEntity.position.toInt()
username = this@toDomainEntity.username
comment = this@toDomainEntity.comment
created = this@toDomainEntity.created?.time
changed = this@toDomainEntity.changed?.time
entry = this@toDomainEntity.entry.toDomainEntity()
}
fun List<ApiBookmark>.toDomainEntitiesList(): List<Bookmark> = map { it.toDomainEntity() }

View File

@ -0,0 +1,44 @@
@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.Bookmark
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
import java.util.Calendar
/**
* Unit test for function that converts [Bookmark] api entity to domain.
*/
class APIBookmarkConverterTest {
@Test
fun `Should convert to domain entity`() {
val entity = Bookmark(412313L, "Awesemo", "Nice", Calendar.getInstance(),
Calendar.getInstance(), MusicDirectoryChild(id = 12333))
val domainEntity = entity.toDomainEntity()
with(domainEntity) {
position `should equal to` entity.position.toInt()
username `should equal to` entity.username
comment `should equal to` entity.comment
created `should equal` entity.created?.time
changed `should equal` entity.changed?.time
entry `should equal` entity.entry.toDomainEntity()
}
}
@Test
fun `Should convert list of entities to domain entities`() {
val entitiesList = listOf(Bookmark(443L), Bookmark(444L))
val domainEntitiesList = entitiesList.toDomainEntitiesList()
domainEntitiesList.size `should equal to` entitiesList.size
domainEntitiesList.forEachIndexed({ index, bookmark ->
bookmark `should equal` entitiesList[index].toDomainEntity()
})
}
}