Add converter function for ChatMessage entity to domain entity.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2017-11-16 21:32:36 +01:00
parent 9cecebb314
commit 6c1039126e
2 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,15 @@
// Contains helper functions to convert from api ChatMessage entity to domain entity
@file:JvmName("APIChatMessageConverter")
package org.moire.ultrasonic.data
import org.moire.ultrasonic.domain.ChatMessage
import org.moire.ultrasonic.api.subsonic.models.ChatMessage as ApiChatMessage
fun ApiChatMessage.toDomainEntity(): ChatMessage = ChatMessage().apply {
username = this@toDomainEntity.username
time = this@toDomainEntity.time
message = this@toDomainEntity.message
}
fun List<ApiChatMessage>.toDomainEntitiesList(): List<ChatMessage> = this
.map { it.toDomainEntity() }

View File

@ -0,0 +1,40 @@
@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.ChatMessage
/**
* Unit test for functions converting api [ChatMessage] to domain entity.
*/
class APIChatMessageConverterTest {
@Test
fun `Should convert entity`() {
val entity = ChatMessage("Woohoo", 553434L, "Wow")
val domainEntity = entity.toDomainEntity()
with(domainEntity) {
username `should equal to` entity.username
time `should equal to` entity.time
message `should equal to` entity.message
}
}
@Test
fun `Should convert list of entities`() {
val entitiesList = listOf(ChatMessage("AAA"), ChatMessage("BBB"))
val domainEntitiesList = entitiesList.toDomainEntitiesList()
with(domainEntitiesList) {
size `should equal to` entitiesList.size
forEachIndexed { index, chatMessage ->
chatMessage `should equal` entitiesList[index].toDomainEntity()
}
}
}
}