Add toTypeEntity to Mapping

This commit is contained in:
2024-11-23 03:25:15 +01:00
parent 2a98cdf38f
commit ba6f38a585
2 changed files with 36 additions and 0 deletions

View File

@@ -1,10 +1,13 @@
package ltd.hlaeja.util package ltd.hlaeja.util
import java.time.ZonedDateTime
import ltd.hlaeja.entity.TypeEntity import ltd.hlaeja.entity.TypeEntity
import ltd.hlaeja.library.deviceRegistry.Type import ltd.hlaeja.library.deviceRegistry.Type
import org.springframework.http.HttpStatus.EXPECTATION_FAILED import org.springframework.http.HttpStatus.EXPECTATION_FAILED
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
fun Type.Request.toTypeEntity(): TypeEntity = TypeEntity(null, ZonedDateTime.now(), name)
fun TypeEntity.toTypeResponse(): Type.Response { fun TypeEntity.toTypeResponse(): Type.Response {
return Type.Response(id ?: throw ResponseStatusException(EXPECTATION_FAILED), name) return Type.Response(id ?: throw ResponseStatusException(EXPECTATION_FAILED), name)
} }

View File

@@ -1,5 +1,8 @@
package ltd.hlaeja.util package ltd.hlaeja.util
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.ZoneId import java.time.ZoneId
import java.time.ZonedDateTime import java.time.ZonedDateTime
@@ -7,8 +10,11 @@ import java.util.UUID
import kotlin.test.Test import kotlin.test.Test
import ltd.hlaeja.assertj.assertThat import ltd.hlaeja.assertj.assertThat
import ltd.hlaeja.entity.TypeEntity import ltd.hlaeja.entity.TypeEntity
import ltd.hlaeja.library.deviceRegistry.Type
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Nested
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
@@ -17,9 +23,36 @@ class MappingKtTest {
val timestamp: ZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 1), ZoneId.of("UTC")) val timestamp: ZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 1), ZoneId.of("UTC"))
} }
@BeforeEach
fun setUp() {
mockkStatic(ZonedDateTime::class)
every { ZonedDateTime.now() } returns timestamp
}
@AfterEach
fun tearDown() {
unmockkStatic(ZonedDateTime::class)
}
@Nested @Nested
inner class TypeMapping { inner class TypeMapping {
@Test
fun `request to entity successful`() {
// given
val request = Type.Request(
"test",
)
// when
val result = request.toTypeEntity()
// then
assertThat(result.id).isNull()
assertThat(result.timestamp.toString()).isEqualTo("2000-01-01T00:00:00.000000001Z[UTC]")
assertThat(result.name).isEqualTo("test")
}
@Test @Test
fun `entity to response successful`() { fun `entity to response successful`() {
// given // given