Add Mapping

This commit is contained in:
2024-11-23 03:14:59 +01:00
parent 973da40603
commit c753d241e1
2 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
package ltd.hlaeja.util
import ltd.hlaeja.entity.TypeEntity
import ltd.hlaeja.library.deviceRegistry.Type
import org.springframework.http.HttpStatus.EXPECTATION_FAILED
import org.springframework.web.server.ResponseStatusException
fun TypeEntity.toTypeResponse(): Type.Response {
return Type.Response(id ?: throw ResponseStatusException(EXPECTATION_FAILED), name)
}

View File

@@ -0,0 +1,48 @@
package ltd.hlaeja.util
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.util.UUID
import kotlin.test.Test
import ltd.hlaeja.assertj.assertThat
import ltd.hlaeja.entity.TypeEntity
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Nested
import org.springframework.web.server.ResponseStatusException
class MappingKtTest {
companion object {
val timestamp: ZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 1), ZoneId.of("UTC"))
}
@Nested
inner class TypeMapping {
@Test
fun `entity to response successful`() {
// given
val entity = TypeEntity(
UUID.fromString("00000000-0000-0000-0000-000000000000"),
timestamp,
"name",
)
// when
val response = entity.toTypeResponse()
// then
assertThat(response.id).isUUID("00000000-0000-0000-0000-000000000000")
assertThat(response.name).isEqualTo("name")
}
@Test
fun `entity to response exception`() {
// then exception
assertThrows(ResponseStatusException::class.java) {
TypeEntity(null, timestamp, "name").toTypeResponse()
}
}
}
}