Add Mapping
This commit is contained in:
10
src/main/kotlin/ltd/hlaeja/util/Mapping.kt
Normal file
10
src/main/kotlin/ltd/hlaeja/util/Mapping.kt
Normal 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)
|
||||||
|
}
|
||||||
48
src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt
Normal file
48
src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user