From ba6f38a5855e0f0ad426948d884ad9bfe1a88d74 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Sat, 23 Nov 2024 03:25:15 +0100 Subject: [PATCH] Add toTypeEntity to Mapping --- src/main/kotlin/ltd/hlaeja/util/Mapping.kt | 3 ++ .../kotlin/ltd/hlaeja/util/MappingKtTest.kt | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/main/kotlin/ltd/hlaeja/util/Mapping.kt b/src/main/kotlin/ltd/hlaeja/util/Mapping.kt index d25e234..1beca18 100644 --- a/src/main/kotlin/ltd/hlaeja/util/Mapping.kt +++ b/src/main/kotlin/ltd/hlaeja/util/Mapping.kt @@ -1,10 +1,13 @@ package ltd.hlaeja.util +import java.time.ZonedDateTime 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 Type.Request.toTypeEntity(): TypeEntity = TypeEntity(null, ZonedDateTime.now(), name) + fun TypeEntity.toTypeResponse(): Type.Response { return Type.Response(id ?: throw ResponseStatusException(EXPECTATION_FAILED), name) } diff --git a/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt b/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt index 02169d7..9e7394b 100644 --- a/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt +++ b/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt @@ -1,5 +1,8 @@ package ltd.hlaeja.util +import io.mockk.every +import io.mockk.mockkStatic +import io.mockk.unmockkStatic import java.time.LocalDateTime import java.time.ZoneId import java.time.ZonedDateTime @@ -7,8 +10,11 @@ import java.util.UUID import kotlin.test.Test import ltd.hlaeja.assertj.assertThat import ltd.hlaeja.entity.TypeEntity +import ltd.hlaeja.library.deviceRegistry.Type 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.BeforeEach import org.junit.jupiter.api.Nested 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")) } + @BeforeEach + fun setUp() { + mockkStatic(ZonedDateTime::class) + every { ZonedDateTime.now() } returns timestamp + } + + @AfterEach + fun tearDown() { + unmockkStatic(ZonedDateTime::class) + } + @Nested 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 fun `entity to response successful`() { // given